// JavaScript Document


//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");
		$("#popupAlert").fadeIn("slow");
		popupStatus = 1;
	}
}


//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupAlert").fadeOut("slow");
		popupStatus = 0;
	}
}


//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupAlert").height();
	var popupWidth = $("#popupAlert").width();

  // Reset the top to zero if the window is really small
  var popupTop = windowHeight/2-popupHeight/2; 
	if(popupTop < 0) popupTop = 0;

//centering
	$("#popupAlert").css({
		"position": "absolute",
		"top": popupTop,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({"height": windowHeight});

}

function topCenterPopup() { 
  //request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var popupWidth = $("#popupAlert").width(); 
	var popupHeight = $("#popupAlert").height();
	var windowHeight = $("#mainPage").height();
	//centering
	$("#popupAlert").css({
		"position": "absolute",
		"top": 0,
		"left": windowWidth/2-popupWidth/2
	});

	// Make the background underlay extra big to fix a bug in 
	// iPhone browsers where it ends up too short if indexed off the popupHeight.
	$("#backgroundPopup").css({"height": windowHeight});
}


// SET UP THE POPUP ALERT ON DOCUMENT LOAD
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#popupAlertTopNavLink").click(function(){
		//centering with css
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
			topCenterPopup(); 
		} else {
			centerPopup(); 
		}
		//load popup
		loadPopup(); 
		return false;
	});
	
	//CLOSING POPUP
	//Click the close window links
	$("a.popupAlertCloseLink").click(function(){
		disablePopup(); 
		return false;
	});
	//Click the evaluate / buy how-to link!
	$("#popupAlertEvaluateLink").click(function(){
		disablePopup(); 
		return true;
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup(); 
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

	//MAKE THE TOP MENU TEXT ALERT FLARE
	/*
	var origFontSize = $("#popupAlertTopNavLink").css("font-size");
	$("#popupAlertTopNavLink").animate({
		fontSize: '36px'}, 3000, function() {
			$("#popupAlertTopNavLink").animate({
					fontSize: origFontSize}, 2000);
	});
	*/

	//var windowWidth = document.documentElement.clientWidth;
	//var windowHeight = document.documentElement.clientHeight;
	//alert(windowWidth + "px / " + windowHeight + "px");


});
