﻿//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var enableCloseExists = document.getElementById("enableClose");
//centering popup
function centerPopup(popupWidth, popupHeight){
//function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var windowPosition = document.documentElement.scrollTop;
	var popupHeight = popupHeight;
	var popupWidth = popupWidth;
	//centering
	$("#popupContact").css({
		"position": "absolute",
//		"top": windowPosition/4-popupHeight/4,
		"top": windowPosition+(windowHeight-popupHeight)/2,
		"left": windowWidth/2-popupWidth/2
	
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});	
	if(popupStatus == 1){
		setTimeout(function(){centerPopup(popupWidth, popupHeight)},100);
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
//loading popup with jQuery magic!
function loadPopup(frmLink, popupWidth, popupHeight, enClose){
	document.getElementById("enableClose").value = enClose;
	if(enClose){
		document.getElementById("closeX").innerHTML = "X";
	}
	else{
		document.getElementById("closeX").innerHTML = "";
	}
	document.getElementById("iframePopup").src = frmLink;
	document.getElementById("iframePopup").style.width = popupWidth + 'px';
	document.getElementById("iframePopup").style.height = popupHeight + 'px';
//	document.getElementById("popupContact").style.width = popupwidth+'px';
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.6"
		});
		$("#backgroundPopup").fadeIn("fast");
		$("#popupContact").fadeIn("fast");
		popupStatus = 1;
	}
	centerPopup(popupWidth, popupHeight);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popupContact").fadeOut("fast");
		popupStatus = 0;
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
//change close X color to
function closeXColor(myColor){
	document.getElementById("popupContactClose").style.color = myColor;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});

	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});

	if(enableCloseExists){
		if(document.getElementById("enableClose").value  == 1) {
			//Click out event!
			$("#backgroundPopup").click(function(){
				disablePopup();
			});
		
			//Press Escape event!
			$(document).keypress(function(e){
				if(e.keyCode==27 && popupStatus==1){
					disablePopup();  }
			});
		}//end if enableclose == 1
	}//end if enableclose exists

});
///////////////////////////////////////////////////////////////////////////////////////////////////////
