var site_url     = 'http://perriconemd.com/?site=US';
var site_url_uk     = 'http://perriconemd.co.uk/?site=UK';
var site_url_de     = 'http://perriconemd.de/?site=DE';



Event.observe(window, 'load', function() {
	siteGeoIPInit();
});

function siteGeoIPInit(){
     //checkForDefaultSite();
     loadChooseCountryLnkJS();
     //checkGeoIPForSite();
}

function checkForDefaultSite(){
     var site_param;
     try{
         site_param = gup('site');
     }catch(err){}
     if(site_param != null && site_param != ""){
         setGeoIPCookie(site_param);
     }
}

function loadChooseCountryLnkJS(){
	$$(".changeCountryLink").each(function(element){
		element.href = "javascript:void(0)";
		Event.observe(element, 'click', function(event) {
			chooseSiteOverlay();
			_gaq.push(['_setAccount', googleTrackingID]);
			_gaq.push(['_trackPageview', '/changeCountryOverlay']);
			Event.stop(event);
		});
	});
}
/*function loadChooseCountryLnkJS(){
     // $$(".changeCountryLink").each(function(element){
     jQuery(document).find(".changeCountryLink").each(function(index){

         this.href = "javascript:void(0)";
         //Event.observe(element, 'click', function(event) {
         jQuery(this).bind('click', function(event) {
             chooseSiteOverlay();
             _gaq.push(['_setAccount', googleTrackingID]);
             _gaq.push(['_trackPageview', '/changeCountryOverlay']);
         //    Event.stop(event);
         });
     });
}*/

function checkGeoIPForSite(){
     var countryCode = geoip_country_code();
     var siteGeoIPCookie = loadCookie("site_geoip");

     if(siteGeoIPCookie == null || siteGeoIPCookie == "") {
         try{
         if(siteGeoIPArr.indexOf(countryCode) < 0 ){
             chooseSiteOverlay();
         }}catch(err){}

     }
     else {
         if(siteGeoIP != siteGeoIPCookie){
             redirectToSite(siteGeoIPCookie);
         }
     }
}

function chooseSiteOverlay(){
     openQuickView(choose_popup,515,415,"false",Effect.Appear,Effect.Fade);
}
/*function chooseSiteOverlay(){
     try{
         //jQuery(document).custom_popup('','',choose_popup,'iframe', 450,260,1);
		 openQuickView(choose_popup,405,240,false,Effect.Appear,Effect.Fade);
     }catch(err){alert(err)};
}*/


function setCookieAndRedirect(site){
     setGeoIPCookie(site);
     redirectToSite(site)
}

function setGeoIPCookie(site){
     makeCookie("site_geoip",site, 365*100);
}

function redirectToSite(site){
     var redirect_to = site_url;

     if(site == 'UK'){
         redirect_to = site_url_uk;
     }
     else if(site == 'DE'){
         redirect_to = site_url_de;
     }

     document.location = redirect_to;
}

function loadCookie(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) {
			//alert("cookie " + c.substring(nameEQ.length,c.length));
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}



checkForDefaultSite();
checkGeoIPForSite();
