$(document).ready(function(){

	
	$('.downloadPopup').css('left',($(window).width()/2) - 174)
	$('.downloadPopup').css('top',($(window).height()/2) - 151)
	
	$('#download-popup').click(function(e){
		e.stopPropagation();
		e.preventDefault();

		var cdfault = $(this).find('input[name="campdefault"]').eq(0).val();
		var cdforce = $(this).find('input[name="campforce"]').eq(0).val();

		if($('.topFader').length == 0)
		{
			$('<div class="temp" />').appendTo($('body')).load("http://www.pokerheaven.com/fi/dlpopupcontent.html", function(data){
				$('.topFader').click(closeEvent);
				$('.closeLiveChat').click(closeEvent);

				$('.topFader').prependTo($('body'));

				if(window.scrollY == undefined){
					toffset = document.documentElement.scrollTop;
				}
				else {
					toffset = window.scrollY;
				}

				var newTop = ($(window).height()/2) + toffset - 131;
			
				$('body div.pContent').eq(0).prepend($('.downloadPopup'));
				$('.downloadPopup').css('left',($(window).width()/2) - 174);
				$('.downloadPopup').css('top', newTop);
		
				var arrPageSizes = ___getPageSize();
		
				$('.topFader').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});

				$('.downloadPopup').find('form input[name="campdefault"]').remove();
				$('.downloadPopup').find('form input[name="campforce"]').remove();
				
				if(cdfault != undefined && cdfault != "undefined")
					$('.downloadPopup').find('form').children().append($('<input name="campdefault" type="hidden" value="'+ cdfault +'" />'))
				else if(cdforce != undefined && cdforce  != "undefined")
					$('.downloadPopup').find('form').children().append($('<input name="campforce" type="hidden" value="'+ cdforce +'" />'))

				$('.topFader').show().fadeTo('fast',0.7,function(){
					$('.downloadPopup').fadeIn('fast');
				});


				return false;
			});
		}
		else
		{
			$('.downloadPopup').find('form input[name="campdefault"]').remove();
			$('.downloadPopup').find('form input[name="campforce"]').remove();
		
			if(cdfault != undefined && cdfault != "undefined")
				$('.downloadPopup').find('form').children().append($('<input name="campdefault" type="hidden" value="'+ cdfault +'" />'))
			else if(cdforce != undefined && cdforce  != "undefined")
				$('.downloadPopup').find('form').children().append($('<input name="campforce" type="hidden" value="'+ cdforce +'" />'))

			if(window.scrollY == undefined){
				toffset = document.documentElement.scrollTop;
			}
			else {
				toffset = window.scrollY;
			}

			var newTop = ($(window).height()/2) + toffset - 131;
			
			$('body div.pContent').eq(0).prepend($('.downloadPopup'));
			$('.downloadPopup').css('left',($(window).width()/2) - 174);
			$('.downloadPopup').css('top', newTop);


			$('.topFader').show().fadeTo('fast',0.7,function(){
				$('.downloadPopup').fadeIn('fast');
			});
		}
	});
	
	$('.dForm').submit(function(e){
		e.stopPropagation();
		e.preventDefault();

		var cdfault = $(this).find('input[name="campdefault"]').eq(0).val();
		var cdforce = $(this).find('input[name="campforce"]').eq(0).val();

		if($('.topFader').length == 0)
		{
			$('<div class="temp" />').appendTo($('body')).load("http://www.pokerheaven.com/fi/dlpopupcontent.html", function(data){
				$('.topFader').click(closeEvent);
				$('.closeLiveChat').click(closeEvent);

				$('a.submit').click(function(){
					$(this).parent().parent().submit();
				});
			
				$('.topFader').prependTo($('body'));

				if(window.scrollY == undefined){
					toffset = document.documentElement.scrollTop;
				}
				else {
					toffset = window.scrollY;
				}

				var newTop = ($(window).height()/2) + toffset - 131;
			
				$('body div.pContent').eq(0).prepend($('.downloadPopup'));
				$('.downloadPopup').css('left',($(window).width()/2) - 174);
				$('.downloadPopup').css('top', newTop);
		
				var arrPageSizes = ___getPageSize();
		
				$('.topFader').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});

				$('.downloadPopup').find('form input[name="campdefault"]').remove();
				$('.downloadPopup').find('form input[name="campforce"]').remove();
				
				if(cdfault != undefined && cdfault != "undefined")
					$('.downloadPopup').find('form').children().append($('<input name="campdefault" type="hidden" value="'+ cdfault +'" />'))
				else if(cdforce != undefined && cdforce  != "undefined")
					$('.downloadPopup').find('form').children().append($('<input name="campforce" type="hidden" value="'+ cdforce +'" />'))

				$('.topFader').show().fadeTo('fast',0.7,function(){
					$('.downloadPopup').fadeIn('fast');
				});


				return false;
			});
		}
		else
		{
			$('.downloadPopup').find('form input[name="campdefault"]').remove();
			$('.downloadPopup').find('form input[name="campforce"]').remove();
		
			if(cdfault != undefined && cdfault != "undefined")
				$('.downloadPopup').find('form').children().append($('<input name="campdefault" type="hidden" value="'+ cdfault +'" />'))
			else if(cdforce != undefined && cdforce  != "undefined")
				$('.downloadPopup').find('form').children().append($('<input name="campforce" type="hidden" value="'+ cdforce +'" />'))

			if(window.scrollY == undefined){
				toffset = document.documentElement.scrollTop;
			}
			else {
				toffset = window.scrollY;
			}

			var newTop = ($(window).height()/2) + toffset - 131;
			
			$('body div.pContent').eq(0).prepend($('.downloadPopup'));
			$('.downloadPopup').css('left',($(window).width()/2) - 174);
			$('.downloadPopup').css('top', newTop);


			$('.topFader').show().fadeTo('fast',0.7,function(){
				$('.downloadPopup').fadeIn('fast');
			});
		}
	});
	
	
	// If window was resized, calculate the new overlay dimensions
	$(window).resize(function() {
		// Get page sizes
		var arrPageSizes = ___getPageSize();
		// Style overlay and show it
		$('.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() {
		
		$('.downloadPopup').fadeOut("fast",function(){
			$(this).css('display','none');
			$('.topFader').fadeOut("slow",function(){
				$(this).css('display','none');
			});
		});
	};

