﻿//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;
var popupStatus = 0;

window.onresize = function() { centerPopup(); }

function HideSelects() {
	if(BrowerSupportsSelectOverlay() == 0) {
		var x = document.getElementsByTagName("select");
		for (i = 0; i < x.length; i++) {
			x[i].style.visibility = "hidden";
		}
	}
}

function ShowSelects() {
	if (BrowerSupportsSelectOverlay() == 0) {
		var x = document.getElementsByTagName("select");
		for (i = 0; i < x.length; i++) {
			x[i].style.visibility = "visible";
		}
	}
}

// Todo : This may need refactoring as it is only for IE6 and below
function BrowerSupportsSelectOverlay() {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
		var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion > 6) {
			return 1;
		}
		else {
			return 0;
		}
	}
	return 1;
}

//loading popup with jQuery magic!
function loadPopup(backgroundSelector, popupSelector, hideSelects) {
	//loads popup only if it is disabled
	if (popupStatus == 0) {
	   centerPopup(backgroundSelector, popupSelector);
	   if (hideSelects === undefined || hideSelects) {
	      HideSelects();
	   }
		$(backgroundSelector).css({"display":"block"});
		$(popupSelector).css({"display":"block"});
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(backgroundSelector, popupSelector) {
	//disables popup only if it is enabled
	if (popupStatus == 1) {
		ShowSelects();
		$(popupSelector).css({"display":"none"})
		popupStatus = 0;
		$(backgroundSelector).css({"display":"none"});
	}
}

//centering popup
function centerPopup(backgroundSelector, popupSelector) {
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(popupSelector).height();
	var popupWidth = $(popupSelector).width();
	//centering
	$(popupSelector).css({
	    "position": "absolute",
		"top": ($(window).height() - popupHeight) / 2 + $(window).scrollTop() + "px",
		"left": ($(window).width() - popupWidth) / 2 + $(window).scrollLeft() + "px"
	});
	//only need force for IE6
	$(backgroundSelector).css({"height":windowHeight,"width":windowWidth});
}
