jQuery(document).ready(function(){

	
	jQuery('.downloadPopup').css('left',(jQuery(window).width()/2) - 174)
	jQuery('.downloadPopup').css('top',(jQuery(window).height()/2) - 151)
	
	jQuery('#download-popup').click(function(e){
		e.stopPropagation();
		e.preventDefault();

		var cdfault = jQuery(this).find('input[name="campdefault"]').eq(0).val();
		var cdforce = jQuery(this).find('input[name="campforce"]').eq(0).val();

		if(jQuery('.topFader').length == 0)
		{
			jQuery('<div class="temp" />').appendTo(jQuery('body')).load("http://www.pokerheaven.com/dlpopupcontent.html", function(data){
				jQuery('.topFader').click(closeEvent);
				jQuery('.closeLiveChat').click(closeEvent);

				jQuery('.topFader').prependTo(jQuery('body'));

				if(window.scrollY == undefined){
					toffset = document.documentElement.scrollTop;
				}
				else {
					toffset = window.scrollY;
				}

				var newTop = (jQuery(window).height()/2) + toffset - 131;
			
				jQuery('body div.pContent').eq(0).prepend(jQuery('.downloadPopup'));
				jQuery('.downloadPopup').css('left',(jQuery(window).width()/2) - 174);
				jQuery('.downloadPopup').css('top', newTop);
		
				var arrPageSizes = ___getPageSize();
		
				jQuery('.topFader').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});

				jQuery('.downloadPopup').find('form input[name="campdefault"]').remove();
				jQuery('.downloadPopup').find('form input[name="campforce"]').remove();
				
				if(cdfault != undefined && cdfault != "undefined")
					jQuery('.downloadPopup').find('form').children().append(jQuery('<input name="campdefault" type="hidden" value="'+ cdfault +'" />'))
				else if(cdforce != undefined && cdforce  != "undefined")
					jQuery('.downloadPopup').find('form').children().append(jQuery('<input name="campforce" type="hidden" value="'+ cdforce +'" />'))

				jQuery('.topFader').show().fadeTo('fast',0.7,function(){
					jQuery('.downloadPopup').fadeIn('fast');
				});


				return false;
			});
		}
		else
		{
			jQuery('.downloadPopup').find('form input[name="campdefault"]').remove();
			jQuery('.downloadPopup').find('form input[name="campforce"]').remove();
		
			if(cdfault != undefined && cdfault != "undefined")
				jQuery('.downloadPopup').find('form').children().append(jQuery('<input name="campdefault" type="hidden" value="'+ cdfault +'" />'))
			else if(cdforce != undefined && cdforce  != "undefined")
				jQuery('.downloadPopup').find('form').children().append(jQuery('<input name="campforce" type="hidden" value="'+ cdforce +'" />'))

			if(window.scrollY == undefined){
				toffset = document.documentElement.scrollTop;
			}
			else {
				toffset = window.scrollY;
			}

			var newTop = (jQuery(window).height()/2) + toffset - 131;
			
			jQuery('body div.pContent').eq(0).prepend(jQuery('.downloadPopup'));
			jQuery('.downloadPopup').css('left',(jQuery(window).width()/2) - 174);
			jQuery('.downloadPopup').css('top', newTop);


			jQuery('.topFader').show().fadeTo('fast',0.7,function(){
				jQuery('.downloadPopup').fadeIn('fast');
			});
		}
	});
	
	jQuery('.dForm').submit(function(e){
		e.stopPropagation();
		e.preventDefault();

		var cdfault = jQuery(this).find('input[name="campdefault"]').eq(0).val();
		var cdforce = jQuery(this).find('input[name="campforce"]').eq(0).val();

		if(jQuery('.topFader').length == 0)
		{
			jQuery('<div class="temp" />').appendTo(jQuery('body')).load("http://www.pokerheaven.com/dlpopupcontent.html", function(data){
				jQuery('.topFader').click(closeEvent);
				jQuery('.closeLiveChat').click(closeEvent);

				jQuery('a.submit').click(function(){
					jQuery(this).parent().parent().submit();
				});
			
				jQuery('.topFader').prependTo(jQuery('body'));

				if(window.scrollY == undefined){
					toffset = document.documentElement.scrollTop;
				}
				else {
					toffset = window.scrollY;
				}

				var newTop = (jQuery(window).height()/2) + toffset - 131;
			
				jQuery('body div.pContent').eq(0).prepend(jQuery('.downloadPopup'));
				jQuery('.downloadPopup').css('left',(jQuery(window).width()/2) - 174);
				jQuery('.downloadPopup').css('top', newTop);
		
				var arrPageSizes = ___getPageSize();
		
				jQuery('.topFader').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});

				jQuery('.downloadPopup').find('form input[name="campdefault"]').remove();
				jQuery('.downloadPopup').find('form input[name="campforce"]').remove();
				
				if(cdfault != undefined && cdfault != "undefined")
					jQuery('.downloadPopup').find('form').children().append(jQuery('<input name="campdefault" type="hidden" value="'+ cdfault +'" />'))
				else if(cdforce != undefined && cdforce  != "undefined")
					jQuery('.downloadPopup').find('form').children().append(jQuery('<input name="campforce" type="hidden" value="'+ cdforce +'" />'))

				jQuery('.topFader').show().fadeTo('fast',0.7,function(){
					jQuery('.downloadPopup').fadeIn('fast');
				});


				return false;
			});
		}
		else
		{
			jQuery('.downloadPopup').find('form input[name="campdefault"]').remove();
			jQuery('.downloadPopup').find('form input[name="campforce"]').remove();
		
			if(cdfault != undefined && cdfault != "undefined")
				jQuery('.downloadPopup').find('form').children().append(jQuery('<input name="campdefault" type="hidden" value="'+ cdfault +'" />'))
			else if(cdforce != undefined && cdforce  != "undefined")
				jQuery('.downloadPopup').find('form').children().append(jQuery('<input name="campforce" type="hidden" value="'+ cdforce +'" />'))

			if(window.scrollY == undefined){
				toffset = document.documentElement.scrollTop;
			}
			else {
				toffset = window.scrollY;
			}

			var newTop = (jQuery(window).height()/2) + toffset - 131;
			
			jQuery('body div.pContent').eq(0).prepend(jQuery('.downloadPopup'));
			jQuery('.downloadPopup').css('left',(jQuery(window).width()/2) - 174);
			jQuery('.downloadPopup').css('top', newTop);


			jQuery('.topFader').show().fadeTo('fast',0.7,function(){
				jQuery('.downloadPopup').fadeIn('fast');
			});
		}
	});
	
	
	// If window was resized, calculate the new overlay dimensions
	jQuery(window).resize(function() {
		// Get page sizes
		var arrPageSizes = ___getPageSize();
		// Style overlay and show it
		jQuery('.topFader').css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});
	});
	

	
	
	function ___getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};
});


	
function closeEvent() {
		
		jQuery('.downloadPopup').fadeOut("fast",function(){
			jQuery(this).css('display','none');
			jQuery('.topFader').fadeOut("slow",function(){
				jQuery(this).css('display','none');
			});
		});
	};


