/************ POP CODE *************/
/***************************/
/**@Author: Adrian "yEnS" Mato Gondelle
/**@website: www.yensdesign.com
/**@email: yensamg@gmail.com
/**@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		jQuery("#backgroundPopup").css({
			"opacity": "0.7"
		});
		jQuery("#backgroundPopup").fadeIn("slow");
		jQuery("#popupCountry").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#popupCountry").fadeOut("slow");
		popupStatus = 0;
	}
}

/**
 * Set cookie so we don't prompt again, then close popup.
 * @author 		Alex Fuckert
 * @copyright	Exorbyte GmbH
 */
function stayHere(){
	jQuery.cookie('SetLoc', 'true', { expires: 365, path: '/'});
	disablePopup();
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupCountry").height();
	var popupWidth = jQuery("#popupCountry").width();
	//centering
	jQuery("#popupCountry").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	jQuery("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	jQuery("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	jQuery("#popupCountryClose").click(function(){
		stayHere();
		//disablePopup();
	});
	//Click out event!
	jQuery("#backgroundPopup").click(function(){
		stayHere();
		//disablePopup();
	});
	//Press Escape event!
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			stayHere();
			//disablePopup();
		}
	});

});



/**
 *  Prompt the user to change to the suggest country's site. If yes, set the target site cookie "SetLoc" so he won't be asked there - 
 *  this is done by appending the URL parameter "sec_loc=true" which is checked for on the homepage of all shops.
 *  If no, set the cookie on the current site so he won't be asked again.
 */
function promptCountryChange(countryCode, url, managementURL){
	document.getElementById("goToCountry").innerHTML = countryCode;
	document.getElementById("goToCountryLink").setAttribute("title", "Go to '"+countryCode+"' website");
	document.getElementById("goToCountryLink").setAttribute("href", url+"?set_loc=true");
	document.getElementById("goToCountryImage").setAttribute("src", managementURL+"/img/"+countryCode.toLowerCase()+"_large.jpg");
	document.getElementById("goToCountryImage").setAttribute("alt", "Go to '"+countryCode+"' website");
	centerPopup();
	loadPopup();
}

/**
 *  Initializes the country selector. Gets the country-url mappings from the management server (via shopURL and managementURL).
 *  Renders the links with flags to the shops that are returned to the element specified by countrySelectorID.
 *  Note that for each country which is to be rendered, we try to source the image from content.exorbyte.com/images/<cc>.png 
 *  where <cc> is the the country code.
 *  Optionally checks the country matching the current visitor's IP (via countryURL).
 *  If the countries don't match and the cookie doesn't exist and the visitor's country exists as a shop then ask the user if he wants to visit that page.
 *  If he does, we set the cookie on the target site. If he doesn't we set the cookie on the current site so the he isn't asked again. 
 *  Asking currently only happens on the homepage (set by checkCountry argument).
 *
 *  @param shopURL				The URL of the shop page that forwards to the shop mappings on the admin server.
 *  @param countryURL			The URL of the shop page that forwards to the IP geo detection service.
 *  @param managementURL		The URL of the management server.
 *  @param checkCountry			TRUE to check the visitor's country by geo detecting his IP address.
 *  @param currentCountry		The the country code of this shop.
 *  @param countrySelectorID	The ID of the element into which the country links are to be rendered.
 *  @param foundCountryID		The ID of the element into which to display the found country (used in popup)
 */
function init(ipCountry, currentCountry, managementURL, checkCountry, countrySelectorID){

	gManagementURL = managementURL;
	var popupEl = document.getElementById("popupCountry");
	if (popupEl!=null) popupEl.style.display = "";

	// Get the shop country mappings
	var shops = {"de":"http:\/\/commerce.exorbyte.de","us":"http:\/\/commerce.exorbyte.com","uk":"http:\/\/commerce.exorbyte.co.uk"};
			
			var countrySelector = document.getElementById(countrySelectorID);
			if (countrySelector!=null){
				// Render the country flags
				for (var i in shops){
					var country = shops[i].name;
					var linkEl = document.createElement('a');
					linkEl.setAttribute('href', shops[i]+'?set_loc=true');
					linkEl.setAttribute('title', i);
					var imgEl = document.createElement('img');
					imgEl.setAttribute('src', managementURL+'/img/'+i+'.png');
					imgEl.setAttribute('alt', i);
					linkEl.appendChild(imgEl);
					if (i==currentCountry) linkEl.setAttribute('class', 'current_country');
					countrySelector.appendChild(linkEl);
				}
			}

			// Optionally check visitor's location against current site
			if (checkCountry){
						countryLower = ipCountry.toLowerCase();

						// Prompt user to change country if cookie is not set AND user's country is not current shop country AND user's country exists in mapping
						if (jQuery.cookie('SetLoc')==null
							&& countryLower!=currentCountry.toLowerCase()
							&& shops[countryLower])
						{
							promptCountryChange(ipCountry, shops[countryLower], managementURL);
						}
					}

			}


