﻿/***************************/
//@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){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupHomepage").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupHomepage").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup both vertically and horizontally
function centerPopup(){
	//request data for centering
	// original version
//	var windowWidth = document.documentElement.innerWidth;
//	var windowHeight = document.documentElement.innerHeight;

	// taken from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	var windowWidth = 0, windowHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	//alert ("windowWidth = " + windowWidth + " -  windowHeight = " + windowHeight )

	// NB: impostando l'altezza del DIV popupHmePage ad "auto" CHROME e SAFARI restituiscono un valore errato per popupHeight
	// stranamente, questo valore fa sì che la finestra di popup sia centrata nella pagina anzichè nella finestra... BOH!!!
	var popupHeight = $("#popupHomepage").height();
	var popupWidth = $("#popupHomepage").width();
//	alert ("popupWidth = " + popupWidth + " -  popupHeight = " + popupHeight )
	
	//centering
	$("#popupHomepage").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//centering popup horizontally
function centerPopupH(top){
	// la finestra compare a top pixel dal margine superiore della finestra
	// taken from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	var windowWidth = 0, windowHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	var popupWidth = $("#popupHomepage").width();
	
	//centering
	$("#popupHomepage").css({
		"position": "absolute",
		"top": top,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

	// la finestra compare a top pixel dal margine superiore della finestra
	// il valore di default è 0 => finestra centrata in verticale (NB non funziona bene con Chrome e Safari)
	var top = typeof(popupHomepageTop) == 'number' ? popupHomepageTop : 0;
	// se è definita la variabile nonMostrareSubitoPopupHomepage non viene eseguita questa parte di codice (mi serve per il preview in area amministrativa)
	var nonMostrarePopupHomepage = typeof(nonMostrareSubitoPopupHomepage) == 'undefined' ? false : true;
	//SHOW POPUP IF COOKIE IS NOT SET
	if (($.cookies.get("popupHomepage")!="foobar") && (! nonMostrarePopupHomepage)) {
		//centering with css
		if (top == 0) {
			// centro la finestra sia in verticale che in orizzontale
			centerPopup();
		} else {
			// centro la finestra orizzontalmente e la metto a top pixel dal bordo superiore della pagina
			centerPopupH(top);
		}
		//load popup
		loadPopup();
		// set cookie
		$.cookies.set("popupHomepage", "foobar", { hoursToLive: 0 });
	}

	//CLOSING POPUP
	//Click the x event!
	$("#popupHomepageClose").click(function(){
		disablePopup();
	});
	//Click the link event!
	$("#popupHomepageCloseLink").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

