﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/
//@Edit: Alessandro Alessio
//@license: Feel free to use it, but keep this credits please!					
/***************************/


//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var popupId = "";

//loading popup with jQuery magic!
function loadPopup(toLoad){    
	//assign toLoad ad popupId
   popupId = toLoad;
   //loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(popupId).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(toUnload){
	//disables popup only if it is enabled
	if(popupStatus==1){
      if (toUnload=="") { 
   		$("#backgroundPopup").fadeOut("slow");
   		$(toUnload).fadeOut("slow");
   		popupStatus = 0;
      } else {
         $("#backgroundPopup").fadeOut("slow");
   		$(popupId).fadeOut("slow");
   		popupStatus = 0;
      }
	}
}

//centering popup
function centerPopup(poppamelo){
	
   //request data for centering
   var windowHeight = screen.availHeight;
   var windowWidth = screen.availWidth;
   var scrollTopPos = document.body.scrollTop;

	var popupHeight = $(poppamelo).height();
	var popupWidth = $(poppamelo).width();

    //calcolo il top
    top_calculate = ((windowHeight/2)-(popupHeight/2)) + (scrollTopPos/2);

    dbg = 0;
    if (dbg==1) {
        dbg_msg = 'Screen : ' +  windowWidth + 'x' + windowHeight +  '\n';
        dbg_msg = dbg_msg + 'Popup : ' +  popupWidth + "x" + popupHeight +  '\n';
        dbg_msg = dbg_msg + 'Risultato : ' + (((windowHeight/2)-(popupHeight/2))+ (scrollTopPos/2)) +  '\n';
        dbg_msg = dbg_msg + 'Top scroll : ' + (0+scrollTopPos) + ' Down Scroll : ' + (windowHeight+scrollTopPos) +  '\n';
        dbg_msg = dbg_msg + 'Top Calc. : ' + top_calculate;
        alert(dbg_msg);        
    }
	
    //centering
	$(poppamelo).css({
		"position": "absolute",
		"top": 20,
		/*"top": (((windowHeight/2)-(popupHeight/2))+ (scrollTopPos/2)),
		"top": top_calculate,*/
        "left": (windowWidth/2)-(popupWidth/2)-20
	});
	
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	
	/*
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	*/
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
