/***************************/
//@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(popup, bg){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$(bg).css({
			"opacity": "0.7"
		});
		$(bg).fadeIn("slow");
		$(popup).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(popup, bg){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$(bg).fadeOut("slow");
		$(popup).fadeOut("slow");
		popupStatus = 0;
	}
	
	//pour ie6 seulement, désactive le drop down
	if(window.XMLHttpRequest == null)
	{
	
		$("#ddTopics").css({
			"display": "block"
		});
	}
}

//centering popup
function centerPopup(popup, bg){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	
	var popupHeight = $(popup).height();
	var popupWidth = $(popup).width();
	
	//centering
	/*$(popup).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});*/
	
	$(popup).css({
		"position": "absolute",
		"top": 50,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$(bg).css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	var popups = {
			popup : [
			{ button : "#button", popup : "#popupSafety", close : "#popupSafetyClose", bg : "#backgroundPopup" }
		]
			
	};
	
	for(var i=0; i<popups.popup.length; i++)
	{
		
		var button = popups.popup[i].button;
		var popup = popups.popup[i].popup;
		var bg = popups.popup[i].bg;
		var close = popups.popup[i].close;
		
		//LOADING POPUP
		//Click the button event!
		$(button).click(function(){
			
			//pour ie6 seulement, désactive le drop down
			if(window.XMLHttpRequest == null)
			{
			//alert('ie6');
				$("#ddTopics").css({
					"display": "none"
				});
			}
			
			//centering with css
			centerPopup(popup, bg);
			//load popup
			loadPopup(popup, bg);
		});
					
		//CLOSING POPUP
		//Click the x event!
		$(close).click(function(){
			disablePopup(popup, bg);
			showLanding();
		});
		//Click out event!
		$(bg).click(function(){
			disablePopup(popup, bg);
		});
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup(popup, bg);
			}
		});
	}

});