﻿	(function($) {
	//0 means disabled; 1 means enabled;
	var popupStatus = 0;
	var popupWindow = ''; // TODO: consolidate with popupStatus
	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",
				"background": "#000000",
				"width":    "100%",
				"top":      0,
				"left":     0,
				"z-index":  10001
			});
			/*if (($.browser.msie) && ($.browser.version < 6.5)) {
				$("#backgroundPopup").css({ "position": "absolute" });
				$("#backgroundPopup").height($(document).height());
				//alert('boo');
				//$("#backgroundPopup")[0].filter = "alpha(opacity=50)";
			}*/
		    if (($.browser.msie) && ($.browser.version < 6.5)) {
				$("#backgroundPopup").bgiframe();
			}

			$("#backgroundPopup").fadeIn("slow");
			popupWindow = $(target_id);
			popupWindow.css({
			    "z-index": 10002
			}).fadeIn("slow");
			popupStatus = 1;
			
			// IE only: hide input boxes
			/*if ($.browser.msie) {
				$("select").addClass("overlay-hidden").css({ 'visibility': 'hidden' });
				$(target_id).find(".overlay-hidden").css({ 'visibility': '' });
			}*/
		}
		else {
			//disablePopup();
			popupWindow.hide();
			popupWindow = $(target_id);
			popupWindow.css({ "z-index": 10002 }).show();
		}
		
	}
	
	//disabling popup with jQuery magic!
	function disablePopup() {
		// Disable the popup only if it is enabled
		if(popupStatus != 1) { return; }
		
		// Disable the IE timer if it's been turned on
		if (popupIETimer) { window.clearTimeout(popupIETimer); }
		
		// IE only: hide input boxes

		// Fade out the windows
		$("#backgroundPopup").fadeOut("slow", function() {
			/*if (($.browser.msie) && ($.browser.version < 6.5)) {
				$(".overlay-hidden").each(function() {
					// Incredible IE bug that some <select>s won't resurface unless
					// styled with position: relative
					p = $(this).css('position');
				    $(this).css({ 'visibility': '', 'opacity': 1, 'position': 'relative' });
					if (p == "absolute") { $(this).css({ 'position': 'absolute' }); }
					$(this).removeClass(".overlay-hidden");
				});
			}*/
		});
		
		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(){
		bg = $('<div id="backgroundPopup">').css({ "display": "none" });
		$(document.body).append(bg);
		
		//LOADING POPUP
		//Click the button event!
		$("a.popup-button").click(function(){
			target_id = $(this).attr('href');
			centerPopup(target_id);
			loadPopup(target_id);
			return false;
		});
		
		$("input.popup-button").click(function(){
			target_id = $(this.form).attr('action');
			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);


(function($){
$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.each(function() {
			if ( $('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};

})(jQuery);