﻿//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var popHtml = "";
var popTextId = "";

//loading popup with jQuery magic!
function loadPopup(divid) {
	//loads popup only if it is disabled
	if (popupStatus == 0) {
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		// ie6 bug: select shows over other elements.
		// this fixes by hiding all selects before the overlays appear
		// in ie6 only
		jQuery.each(jQuery.browser, function (i, val) {
			if (i == "msie" && jQuery.browser.version.substr(0, 1) == "6")
				$("select").css("visibility", "hidden");
		});
		// end fix.
		$("#backgroundPopup").fadeIn("slow");
		$("#" + divid).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup() {
	//disables popup only if it is enabled
	if (popupStatus == 1) {
		// ie6 bug: select shows over other elements.
		// this fixes by showing all selects before the overlays disappear
		// in ie6 only.
		jQuery.each(jQuery.browser, function (i, val) {
			if (i == "msie" && jQuery.browser.version.substr(0, 1) == "6")
				$("select").css("visibility", "visible");
		});
		// end fix.
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		$("#popupEmail").fadeOut("slow");
		$("#popupHcp").fadeOut("slow");
		$("#popupCaregiver").fadeOut("slow");
		popupStatus = 0;
		setTimeout(replaceHtml, 500);
	}
}

function replaceHtml() {
	$("#" + popTextId).html(popHtml);
}

//centering popup
function centerPopup(divid) {
	//request data for centering
	window.scrollTo(0, 0);
	var windowWidth = clientDimensions("x");
	var windowHeight = clientDimensions("y");
	var popupHeight = $("#" + divid).height();
	var popupWidth = $("#" + divid).width();
	//centering
	$("#" + divid).css({
		"position": "absolute",
		"top": 300,
		"left": windowWidth / 2 - popupWidth / 2
	});
	//only need force for IE6

	$("#backgroundPopup").css({
		"height": windowHeight,
		"width": windowWidth
	});
}

function clientDimensions(axis) {
	var myWidth = 0, myHeight = 0;
	if (window.innerHeight && window.scrollMaxY) {
		myHeight = window.innerHeight + window.scrollMaxY;
		myWidth = window.innerWidth + window.scrollMaxX;
		//alert("innerHeight");
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		myHeight = document.body.scrollHeight;
		myWidth = document.body.scrollWidth;
		//alert("scrollHeight");
	} else {
		myHeight = document.documentElement.clientHeight;
		myWidth = document.documentElement.clientWidth;
		//alert("else");
	}

	if (axis == "x")
		return myWidth;
	else if (axis == "y")
		return myHeight;
	else
		return 0;
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function doPopup(url, type) {
	if (type == "email") {
		popTextId = "popupEmail";
		centerPopup("popupEmail");
		loadPopup("popupEmail");
	} else if (type == "exit") {
		popTextId = "popupText";
		$("#popupText").css("display", "block");
		$("#popupTextActelion").css("display", "none");
		popHtml = $("#popupText").html();
		while ($("#popupText").html().indexOf("sitename") != -1) {
			$("#popupText").html($("#popupText").html().replace("sitename", url));
		}
		centerPopup("popupContact");
		$("li").css("z-index", "0");
		$("ul").css("z-index", "0");
		loadPopup("popupContact");
	} else if (type == "hcp") {
		popTextId = "popupHcpText";
		popHtml = $("#popupHcpText").html();
		$("#popupHcpText").html($("#popupHcpText").html().replace("exitingUrl", url));
		centerPopup("popupHcp");
		loadPopup("popupHcp");
	} else if (type == "caregivers") {
		popTextId = "popupCaregiverText";
		centerPopup("popupCaregiver");
		loadPopup("popupCaregiver");
	}
}

$(document).ready(function () {
	//CONTROLLING EVENTS IN jQuery
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function () {
		disablePopup();
	});
	$("#popupEmailClose").click(function () {
		disablePopup();
	});
	$("#popupHcpClose").click(function () {
		disablePopup();
	});
	//Click out event!
	//	$("#backgroundPopup").click(function() {
	//		disablePopup();
	//	});
	//Press Escape event!
	//	$(document).keypress(function(e) {
	//		if (e.keyCode == 27 && popupStatus == 1) {
	//			disablePopup();
	//		}
	//	});
});
