/***************************/
//@Author: Steve Ariss - Elevations Marketing
//@website: www.elevaitons.ca
//@email: info@elevations.ca				
/***************************/

$(document).ready(function(){

	//VARIABLES
	var popupStatus = 0;		//0 means disabled; 1 means enabled;
	var popupDiv = $("#popup");	//Styled Container to use as popup
	var screenDim = $("#backgroundPopup");
	var prizesNav = $("#prizeMenu li"); 
	
	//INITIALIZE POPUP
	function popup_init (myDiv){
	
		//set opacity for background Dim
		$(screenDim).css({
			"opacity": "1.0"
		});
		
		//request data for centering
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $(myDiv).height();
		var popupWidth = $(myDiv).width();
		
		//centering
		$(myDiv).css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
		//only need force for IE6
		$(screenDim).css({
			"height": windowHeight
		});
	};
	popup_init(popupDiv);
		
	//OPEN POPUP
	function loadPopup() {
		//loads popup only if it is disabled
		if(popupStatus==0){		
			screenDim.fadeIn("slow");
			popupDiv.fadeIn("fast");	
			popupDiv.empty();		//clear popup contents		
			popupStatus = 1;
		};
	};
	
	//DISABLE POPUP
	function disablePopup(){
		//Remove Events
			$(".XButton").die("click");
			
		//disables popup only if it is enabled
		if(popupStatus==1){
			screenDim.fadeOut("slow");
			popupDiv.fadeOut("fast");
			popupDiv.empty();		
			popupStatus = 0;
		}
	}
	
	//LOADING ANIMATION
	function showLoading(){
		$("#popupLoading")
			.css({visibility:"visible"})  
	    	.css({opacity:"1"})
	    	.css({display:"block"});
	}
	
/////////////////////////////////////////////////////////////////////////////////
////// POPUP CLICK EVENTS ///////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

	//ABOUT US
	$("#aboutUs").live("click", function() {
		loadPopup();
		popupDiv.load("about.php #content", function() {
			//Where to find page descisions
			xClose(); //Binds close button event
			
		});
		return false;
	});
	
	//CONTACT US
	$("#contactUs").live("click", function() {
		loadPopup();
		popupDiv.load("contact.php #content", function() {
			//Where to find page descisions
			xClose(); //Binds close button event
			
		});
		return false;
	});

	//RE-BIND NESTED EVENTS
	//CLOSE ICON ON POPUPS
	function xClose () {
		$(".XButton").live("click", function (event) {
			//alert("close me");
			disablePopup();
		});
	};
		
	//CLOSING POPUP
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		};
	});
});