var popupStatus = 0;

function loadPopup(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({"opacity": "0.7"});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#popupContact").fadeIn("slow");  
		popupStatus = 1;  
	}  
}  

function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	//request data for centering
	//var windowWidth = $(window).width();
	var windowWidth = myWidth;
	var windowHeight = myHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
	"position": "absolute",
	"top": (windowHeight/2-popupHeight/2) + $(window).scrollTop(),
	"left": (windowWidth/2-popupWidth/2)
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
	"height": $(document).height()
	});
}

function displayPopup() {
	var randomnumber=Math.floor(Math.random()*3);
	if ($.cookie('popNewsDay') == 1 || $.cookie('popNewsMonth') == 1 || $.cookie('newsSignedUp') == 1 || randomnumber > 0) {
	// do nothing
	} else  {
		centerPopup();
		loadPopup();
		$(window).scroll(function () { 
			centerPopup();
		});
		date = new Date();
        date.setTime(date.getTime() + (20 * 60 * 1000));
		$.cookie('popNewsDay', '1', { expires: date, path: '/' });
	}
}

$(document).ready(function () {
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$("#popupLater").click(function(){
		$.cookie('popNewsMonth', '1', { expires: 30, path: '/' });
		disablePopup();
	});
	var t = window.setTimeout(displayPopup,15000);
});