﻿(function($) {
	//0 means disabled; 1 means enabled;
	var popupStatus = 0;
	var popupWindow = '';
	var popupIETimer = 0;
	
	//loading popup with jQuery magic!
	function loadPopup(target_id) {
		//loads popup only if it is disabled
		if(popupStatus == 0){
			$("#backgroundPopup").css({
				"opacity":  0.7,
				"position": "absolute",
				"height":   $(document).height()+"px",
				"width":    "100%",
				"top":      0,
				"left":     0,
				"z-index":  1
			});
			/*if ($.browser.msie) {
				$("#backgroundPopup").css({ "position": "absolute" });
				$("#backgroundPopup").height($(document).height());
				//alert('boo');
				//$("#backgroundPopup")[0].filter = "alpha(opacity=50)";
			}*/

			$("#backgroundPopup").fadeIn("slow");
			$(target_id).fadeIn("slow");
			popupStatus = 1;
			popupWindow = $(target_id);
		}
		
		
	}
	
	//disabling popup with jQuery magic!
	function disablePopup() {
		
		// Disable the popup only if it is enabled
		if(popupStatus==1) {
			// Disable the IE timer if it's been turned on
			if (popupIETimer) { window.clearTimeout(popupIETimer); }
			
			// Fade out the windows
			$("#backgroundPopup").fadeOut("slow");
			popupWindow.fadeOut("slow");
			popupStatus = 0;
		}
	}
	
	//centering popup
	function centerPopup(target_id){
		//request data for centering
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $(target_id).height();
		var popupWidth = $(target_id).width();
		//centering
		popup = $(target_id);
		popup.css({
			"position": "fixed",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
		
		// MSIE does not support position:fixed, so we work around it
		if ($.browser.msie) {
			popup.css({
				  "position": "absolute",
				  "top": windowHeight/2-popupHeight/2 + $(window).scrollTop()
			});
		}
		
		popupIETimer = window.setInterval(function() {
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			target = windowHeight/2-popupHeight/2 + $(window).scrollTop();
			popup.css({ "left": windowWidth/2 - popupWidth/2 });
			if ($.browser.msie) {
				from = parseInt(popup.css('top'));
				if (Math.abs(target-from) > 1)
					{ popup.css({ "top": (from + (target - from) * 0.4) + "px" }); }
				else
					{ popup.css({ "top": target + "px" }); }
			}
			else {
				popup.css({ "top": (windowHeight/2-popupHeight/2) + "px" });
			}
		}, 25);
		
		// RSC: I messed this up and I forgot what it used to do, oops
		// $("#backgroundPopup").css({ });
		
	}
	
	
	//CONTROLLING EVENTS IN jQuery
	$(document).ready(function(){
		$(document.body).append($('<div id="backgroundPopup">').css({ "display": "none" }));
		
		//LOADING POPUP
		//Click the button event!
		$("a.popup-button").click(function(){
			target_id = $(this).attr('href');
			centerPopup(target_id);
			loadPopup(target_id);
			return false;
		});
		
		//CLOSING POPUP
		//Click the x event!
		$(".popup-close").click(function(){
			disablePopup();
			return false;
		});
		//Click out event!
		$("#backgroundPopup").click(function(){
			disablePopup();
		});
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		});
	
	});
})(jQuery);
