//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;
var cookieOptions = {
	domain: '.cellumat.be',	
	expiresAt: new Date( 2030, 1, 1 )
};
var cookieOptionsEU = {
	domain: '.cellumat.eu',	
	expiresAt: new Date( 2030, 1, 1 )
};
var cookieName = "cel_country";

$(document).ready(function(){  
	var countryCookie = "";
	// check if cookies are enabled
        //alert($.cookies.get("country"));
        
        // functionality to set cookies when clicking on another country
        setLanguageClick();
        
	if ($.cookies.test()){
		// get cookie with country
		countryCookie = $.cookies.get(cookieName);
	}
	
	if (!(countryCookie) || countryCookie == "" || countryCookie != getCurrentLanguage()){
		initPopup();
	        centerPopup();
	        loadPopup();
	}
}); 

function setLanguageClick(){
	$("#languages").find("li").each(function(index,element){
		$(this).find("a").click(function(e){
			switch(index){
				case 0:
					$.cookies.set(cookieName,"be-nl",cookieOptions);
					$.cookies.set(cookieName,"be-nl",cookieOptionsEU);	
					break;
				case 1:
					$.cookies.set(cookieName,"be-fr",cookieOptions);
					$.cookies.set(cookieName,"be-fr",cookieOptionsEU);
					break;
				case 2:
					$.cookies.set(cookieName,"fr-fr",cookieOptions);
					$.cookies.set(cookieName,"fr-fr",cookieOptionsEU);
					break;
			}
		});
	});
}

function getCurrentLanguage(){
	// return the language selected
	var currentLanguage = $("#languages").find("li").index($(".lang_selected").parent());
	
	if (currentLanguage == -1){
		return -1;
	}
	
	switch(currentLanguage){
		case 0:
			return "be-nl";
		case 1:
			return "be-fr";
		case 2:
			return "fr-fr";
	}
}

function initPopup(){
	//var divPopup = $("<div id='langPopup'><span onclick='country(1)'>Be-NL</span> <span onclick='country(2)'>Be-FR</span> <span onclick='country(3)'>Fr</span></div>");
	var divPopup = $("<div id='langPopup'> \
		<p class='description_BE description'>Ons productengamma is onderhevig aan verschillende wettelijk bepaalde en landgebonden technische specificaties. Daarom vragen wij u hieronder uw land- en taalkeuze te maken.</p> \
		<p class='description_FR description'>Notre gamme de produits étant conforme aux spécificités techniques et légales de chaque pays, nous vous demandons de choisir votre pays de résidence.</p> \
	  	<div class='left'> \
	  		<a href='#' id='lng_be_nl'>Ga naar de website voor Cellumat in <strong>België</strong>.</a> \
	  		<a href='#' id='lng_be_fr'>Visitez ici le site de Cellumat en <strong>Belgique</strong>.</a> \
		  	</div> \
	  	<div class='right'> \
			<a href='#' id='lng_fr_fr'>Visitez ici le site de Cellumat en <strong>France</strong>.</a> \
		</div> \
	    	<br class='clearfloat' /> \
	</div> ");
	$("body").append(divPopup);
	
	$("#lng_be_nl").click(function(e) {
   		e.preventDefault();
		country(1);
	});
	
	$("#lng_be_fr").click(function(e) {
   		e.preventDefault();
		country(2);
	});
	
	$("#lng_fr_fr").click(function(e) {
   		e.preventDefault();
		country(3);
	});
	
	var divBackground = $("<div id='backgroundPopup'></div>");
	$("body").append(divBackground);
}

function country(intCountry){
	var intIndex = -1;
        var strCountry = "";
	disablePopup();
	switch(intCountry){
		case 1:
			if ($.cookies.test()){
                                strCountry = "be-nl";
				$.cookies.set(cookieName,"be-nl",cookieOptions);
				$.cookies.set(cookieName,"be-nl",cookieOptionsEU);
			}
			
			intIndex = 0;
		break;
		case 2:
			if ($.cookies.test()){
                                strCountry = "be-fr";
				$.cookies.set(cookieName,"be-fr",cookieOptions);
				$.cookies.set(cookieName,"be-fr",cookieOptionsEU);
			}
			
			intIndex = 1;
		break;
		case 3:
			if ($.cookies.test()){
                                strCountry = "fr-fr";
				$.cookies.set(cookieName,"fr-fr",cookieOptions);
				$.cookies.set(cookieName,"fr-fr",cookieOptionsEU);
			}

                        intIndex = 2;
		break;
	}
	
	// simulate clicking on another country if not yet selected
	// number of li's correct? or behind the LI is no link --> goto homepage
        if ($("#languages").find("li").length <= intIndex || $("#languages").find("li:eq(" + intIndex + ")").find("a").length == 0){ window.location = strCountry; return false;}
        // not yet selected?
	if ($("#languages").find("li:eq(" + intIndex + ")").find("a").hasClass("lang_selected")){ return false; }
        // change page!
	window.location = $("#languages").find("li:eq(" + intIndex + ")").find("a").attr("href");
}

//loading popup with jQuery magic!  
function loadPopup(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.9"  
		});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#langPopup").fadeIn("slow");  
		popupStatus = 1;  
	}  
} 

 
function disablePopup(){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#backgroundPopup").fadeOut("slow");  
		$("#langPopup").fadeOut("slow");  
		popupStatus = 0;  
	}  
}  

//centering popup  
function centerPopup(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#langPopup").height();  
	var popupWidth = $("#langPopup").width();  
	//centering  
	$("#langPopup").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  

	//only need force for IE6  
	$("#backgroundPopup").css({  
		"height": windowHeight  
	});    
} 
