/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
//Modify by Michaël ROCHE (Hack cookie + lauch popup)
/***************************/

//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){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupNewsletter").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupNewsletter").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupNewsletter").height();
	var popupWidth = $("#popupNewsletter").width();
	//centering
	$("#popupNewsletter").css({
		//"position": "absolute",
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//affiche popup
function aff_popup_unique(fichier,winName,features){
	if(ScanCookie("dejapopup_infos")==0){
		window.open(fichier,winName,features);
		CreationCookie("dejapopup_infos","oui",false)
	}
}		

//scan cookie
function ScanCookie(variable){
	var cook = document.cookie;
	variable += "=";
	var place = cook.indexOf(variable,0);
	if (place <= -1) {
		return("0");
	}
	else {
		var end = cook.indexOf(";",place)
		if (end <= -1) {
			return(unescape(cook.substring(place+variable.length,cook.length)));
		}
		else {
			return(unescape(cook.substring(place+variable.length,end)));
		}
	}
}

//create cookie
function CreationCookie(nom,valeur,permanent){
	if(permanent){
		var dateExp = new Date(2020,11,11);
		dateExp = dateExp.toGMTString();
		var ifpermanent = '; expires=' + dateExp + ';';
	}
	else {
		ifpermanent = '';
	}
	document.cookie = nom + '=' + escape(valeur) + ifpermanent;
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	if(ScanCookie("dejapopup_infos")==0){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		CreationCookie("dejapopup_infos","oui",false)
	}
				
	//CLOSING POPUP
	//Click the x event!
	$("#submitNewsletter").click(function(){
		disablePopup();
	});
	$("#popupNewsletterClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
