///////////////////////////////////
// FOLLOWING CODE NEEDS TO EXECUTED SO THAT CANADIANS CAN SEE A DIRECT LINK TO CANADIAN SITE
///////////////////////////////////

//var urlPrefix = window.parent.document.location.protocol + '//';

// this is the div displayed to the customer if we believe they are from Canada
var clientLocationDiv = '<div id="clModal" style="display: none; z-index: 300; height: 600px; width: 100%; text-align: center;position: absolute; ">' +
                        '<div style="z-index: 300; background: #ff9000; padding: 10px; margin: 200px auto 0px auto; width: 300px; height: 35px; color: #FFFFFF; font: bold 24px Arial; border-left: 2px solid #ff9000; border-right: 2px solid #ff9000;">Shopping from Canada?</div>' +
                        '<div style="z-index: 300; background: #ffffff; padding: 10px; margin: 0px auto; width: 300px; height: 400px; border-right: 2px solid #ff9000; border-bottom: 3px solid #ff9000; border-left: 2px solid #ff9000;">' +
                        '<p style="text-align: left;font: 14px Arial; ">We have a site just for Canadians, with prices in Canadian dollars and no duty or fees. All orders from PartSelect.ca will ship from within Canada.</p>' +
                        '<p style="margin-bottom: 30px; margin-top: 20px;"><a href="http://www.partselect.ca/?source=relocation" style="font: bold 18px Arial;"><img src="/assets/images/canada_flag.png"  border="0" /></a></p>' +
                        '<p><a href="#" OnClick="$(\'#clModal\').fadeOut(100);" style="font: bold 18px Arial;"><img src="/assets/images/us_flag.png" width="250" height="124" border="0" /></a></p>' +
                        '</div>' +                       
                        '</div>';

// execute when document is ready
$(document).ready(function () {    
    clientLocation();
});
  
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// If the user is from Canada, show notification to them about Canadian site... remember their choice.
function clientLocation() 
{
    var clientCountry = readCookie("clientCountry");
    // @@@ testing
    // ... clientCountry = null;
    if(clientCountry == null)
    {
        if (google.loader.ClientLocation) {
            try { clientCountry = google.loader.ClientLocation.address.country_code; }
            catch (err) {
                clientCountry == "US"; 
            }
            if (clientCountry == "CA") {
                $('body').prepend(clientLocationDiv)   
                $('#clModal').fadeIn(100);
                createCookie("clientCountry", "CA", 365);
            }
            else {
                createCookie("clientCountry", "US", 365);
            }            
        }
    } 
}
