var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0){
		jQuery("#backgroundPopup").css({
			"opacity": "0.7"
		});
		window.scrollTo(0,0);
		jQuery("#backgroundPopup").fadeIn("slow");
		jQuery("#popupContact").fadeIn("slow");                
		jQuery("#popupContact").fadeIn("slow", showVideo);
		
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		hideVideo();
		
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupContact").height();
	var popupWidth = jQuery("#popupContact").width();
	
	jQuery("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	jQuery("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function showVideo() {
	var vc = document.getElementById("flashContent");
	
	if (!vc) {
		var d = document.createElement("div");
		d.setAttribute("id", "flashContent");
		document.getElementById("popupContact").appendChild(d);
	}
	
	if (swfobject.hasFlashPlayerVersion("9.0.0")) {
		var fn = function() {
			var att = {data: "calendar2.swf", width: "800", height: "600"};
			var par = {};
			var id = "flashContent";
			var myObject = swfobject.createSWF(att, par, id);
		};
			
		swfobject.addDomLoadEvent(fn);
	}
}

function hideVideo() {
	var vc = document.getElementById("flashContent");
	
	if (vc) {
		swfobject.removeSWF("flashContent");
	}
}

$(document).ready(function(){
	jQuery("#button").click(function(){
		centerPopup();
		loadPopup();
	});
	
	jQuery("#popupContactClose").click(function(){
		disablePopup();
	});
	
	jQuery("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
//if (checkFirstTime()==true) 
//{
//centerPopup();
//loadPopup();
//}
});


function checkFirstTime()
{
if (getCookie("firstTimeCalendar")=="1") return false; else
{
setCookie("firstTimeCalendar", "1", "Mon, 30-Jan-2012 00:00:00 GMT", "/");
if (getCookie("firstTimeCalendar")!="1") return false;  else 
return true;
}

}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
