﻿var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

function gimmePopup() {
	centerPopup();
	loadPopup();
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#popupContactClose2").click(function(){
		disablePopup();
	});
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

}

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);
}

function checkPopup() {
	var mftVar;
	mftVar = getCookie("mft_sub");
	
	if (mftVar != 'done') {
		document.cookie="mft_sub=done; expires=Mon, 01-Jan-2015 00:00:00 GMT; path=/; domain=myfitnesstunes.com";
		gimmePopup();
	}
}
