var windowHeight=$(window).height()/2;
var windowWidth=$(window).width()/2;

$(document).ready(function(){
	$('body').append("<div id=\"pgemask_container\"><div id=\"pgemask\"></div><div id=\"pop_content\"></div></div>"); 
	ApplyCSS(function(){FirstAlign();});
	
	$(window).bind("load",function(){
		//if(readCookie('lrcookie')!="session"){$('#mypopcontent').PopContent();}
		if($('#pop_content').html()!="")
		{LoadVals();$('#pop_content').CentreElement();}
		
		$('#pgemask').animate({opacity:.8},10);
	});
	
	$(window).resize(function(){
		$('#pop_content').CentreElement();					  
	});
});

//jQuery Functions
$(function(){
	$.fn.PopContent=function(){
		thisName=$(this);
		conHeight=thisName.height();
		conWidth=thisName.width();
		$('#pop_content').css({height:conHeight,width:conWidth}).html($(this).html());
	}
	
	$.fn.CentreElement=function(){
		thisName=$(this);
		thisHeight=thisName.height()/2;
		thisWidth=thisName.width()/2;
		windowHeight=$(window).height()/2;
		windowWidth=$(window).width()/2;
		
		$(this).animate({top:windowHeight-thisHeight,left:windowWidth-thisWidth},{duration:300, queue:false});
	}
	
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	
	$.fn.UnloadMask=function(){
		thisName=$(this);
		$('#pgemask_container').fadeOut(300,function(){
			thisName.empty();$('#pgemask').hide();$('#pop_content').hide();
		});
	}
});

//Javascript Functions
function ApplyCSS(callback)
{
	$('#pgemask_container').css({
		width:'100%',
		height:'100%',
		top:0,
		left:0,
		position:'absolute',
		display:'none',
		zIndex:5
	});
	
	$('#pgemask').css({
		width:'100%',
		height:'100%',
		top:0,
		left:0,
		position:'fixed',
		backgroundColor:'#000000',
		//opacity:0.8,
		display:'none'
	});
	
	$('#pop_content').css({
		width:0,
		height:0,
		top:0,
		left:0,
		position:'absolute',
		display:'none'
	});	
	
	if(callback != undefined){callback();}
}

function FirstAlign()
{
	thisName=$('#pop_content');
	thisHeight=thisName.height()/2;
	thisWidth=thisName.width()/2;
	thisName.css({top:windowHeight-thisHeight,left:windowWidth-thisWidth});
}

function LoadVals()
{
	$('#pgemask_container').show();
	$('#pgemask').customFadeIn(300,function(){
		$('#pop_content').customFadeIn(300);							  
	});
	
	$(document).keyup(function(event){
		if(event.keyCode == 27){$('#pop_content').UnloadMask();createCookie('lrcookie','session',0);}
	});
	
	$('#popclose').click(function(){
		$('#pop_content').UnloadMask();createCookie('lrcookie','session',0);						  
	});
	
	$('#pgemask_container').click(function(){
		$('#pop_content').UnloadMask();createCookie('lrcookie','session',0);								   
	});

}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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 c.substring(nameEQ.length,c.length);
	}
	return null;
}