﻿
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup 
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#foregroundPopup").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup 
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#foregroundPopup").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(pTop){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#foregroundPopup").height();
	var popupWidth = $("#foregroundPopup").width();
	//centering
	$("#foregroundPopup").css({
		"position": "absolute",
//		"top": windowHeight/2-popupHeight/2,
//		"top": windowHeight-popupHeight/1.25,
		"top": pTop-100,
//		"top": pTop-100
		"left": (windowWidth-popupWidth)/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
//		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

	var ignore = 0;

	//LOADING POPUP
	//Click the button event!
	$("#popupButton").click(function(){

	// Smooth scroll to where button is
	var $target = $("#popupButton");
	if ($target.length) {
		var targetOffset = $target.offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 500, function(){centerPopup(targetOffset);loadPopup();});
	}
	
	});
				
	//CLOSING POPUP
	//Click the X event
	$("#popupClose").click(function(){
		disablePopup();
	});
	//Click out event
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Click out event on invisible foreground (used for centering)
	$("#foregroundPopup").click(function(){
		disablePopup();
	});
	//Click out event on visible foreground - stop click event from progressing (otherwise popup will be closed)
	$("#foregroundPopup2").click(function(e){
	    e.stopPropagation();
		window.event.cancelBubble=true;
	});


	//Press Escape event
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	

});
