// JHD Popup
var init_pp = '';
var jht_resize = '';

$(document).ready(function(){ 
	
	//Retreive Base Functions
	function jht_getWinHeight() {
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
			 return window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
			 return document.documentElement.clientHeight;
			}
		}
	}
	
	
	function jht_getWinWidth() {
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
			 return window.innerWidth;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
			 return document.documentElement.clientWidth;
			}
		}
	}
	
	function jht_getBoxHeight(boxid) {
		return document.getElementById(boxid).clientHeight;
	}
	
	function jht_getBoxWidth(boxid) {
		return document.getElementById(boxid).clientWidth;
	}
	
	
	//Window Resize Function
	jht_resize = function () {		
		
		//Fix pp_content			
		var bw = jht_getWinWidth();
		var bh = jht_getWinHeight();
		var rw = jht_getBoxWidth("pp_content");
		var rh = jht_getBoxHeight("pp_content");
		
		var left_space = ((bw - rw)/2);
		var top_space = ((bh - rh)/2);
		
		$('#pp_content').css({
			"left" : left_space+"px",
			"top" : top_space+"px"
		});
		
		
		//Fix Background Size
		$('#pp_background').css({
			"width" : jht_getWinWidth()+"px",
			"height" : jht_getWinHeight()+"px",
			"position" : "fixed",
			"z-index" : "100"
		});	
	}
	
	//Initialize JHDPP
	init_pp = function(type, val, xts, callback) {
		
		//Validate Extra Url Vars
		if (xts == null) {
			xts = '';
		}
		
		//Function to Animate Selection (Ladies and Lads Buttons)
		function init_sel() {
			
			//Set Selection Settings
			$('#pp_content').css({
				"display" : "",
				"position" : "fixed",
				"opacity" : ".0",
				"z-index" : "101",
				"background-color" : "#fff",
				"filter" : "alpha(opacity = 0)",
			});
			
			//Assign Centering Variables//Fix pp_selection			
			var bw = jht_getWinWidth();
			var bh = jht_getWinHeight();
			var rw = jht_getBoxWidth("pp_content");
			var rh = jht_getBoxHeight("pp_content");
			
			var left_space = ((bw - rw)/2);
			var top_space = ((bh - rh)/2);
			
			//Center Selection Box
			$('#pp_content').css({
				"top" : top_space+"px",
				"left" : left_space+"px"
			});
			
			//Show Buttons
			$("#pp_content").animate({
				"opacity" : "1",
				"filter" : "alpha(opacity = 100)"
			}, 600, 'linear', function() {
				if (callback != null) {
					callback();
				}
			});
			
		}
		
		//Set Background Settings
		$('#pp_background').css({
			"display" : "",
			"width" : jht_getWinWidth()+"px",
			"height" : jht_getWinHeight()+"px",
			"position" : "fixed",
			"opacity" : ".0",
			"filter" : "alpha(opacity = 0)",
			"top" : "0px",
			"left" : "0px",
			"z-index" : "100"
		});
		
		//Animate Background Show, and Callback init_sel()
		$("#pp_background").animate({
			"opacity" : ".9",
			"z-index" : "100",
			"filter" : "alpha(opacity = 90)"
		}, 600, 'linear', function() {init_sel();});
		
		$(window).bind('resize', function() {
			jht_resize();
		});
		
		$(function() {
			function imageLoaded() {
				jht_resize();
			}
			$('#pp_content img').livequery(function() {
				if( this.complete ) {
					imageLoaded.call( this );
				} else {
					$(this).one('load', imageLoaded);
				}
			});
		});
		
		//Begin Ajax Retrieval
		retrieve_ajax_content('jhdpp_body', type, val, xts, '', jht_resize);
	}	
	
	//Close JHDPP Window
	close_pp = function() {
		
		function disable_pp() {
			
			//Disable Form
			$('#pp_content').css({
				"display" : "none"
			});
			
			//Disable BG
			$('#pp_background').css({
				"display" : "none"
			});
			
		}
		
		
		//Function to Hide BG, and then Disable PP
		function closebg() {			
			$("#pp_background").animate({
				"opacity" : "0",
				"filter" : "alpha(opacity = 0)"
			}, 600, 'linear', function() {disable_pp();});
		}
		
		//Hide Form and then Hide Overlay BG
		$("#pp_content").animate({
			"opacity" : "0",
			"filter" : "alpha(opacity = 0)"
		}, 600, 'linear', function() {closebg();});
		
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return true;
		}
		return false;
	}
	
	if (!readCookie("jhdppCookie")) {
		//Set Cookie
		var ckyDate = new Date;
		ckyDate.setDate(ckyDate.getDate() + 30);
		document.cookie = 'jhdppCookie=1; expires=' + ckyDate.toGMTString( ) + ';';
		
		//Run JHDPP
		init_pp();
	}
	  
});
