//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;
// loading popup with jQuery magic!
function loadPopup(overflow) {
	// loads popup only if it is disabled
	if (overflow == "scroll") {
		$("#popup").css({
			"overflow" : "scroll"
		});
	} else {
		$("#popup").css({
			"overflow" : "hidden"
		});
	}

	if (popupStatus == 0) {
		$("#mask").css({
			"opacity" : "0.7"
		});
		$("#mask").fadeIn("slow");
		$("#popup").fadeIn("slow");
		popupStatus = 1;
	}
}

// disabling popup with jQuery magic!
function disablePopup(callBack) {
	// disables popup only if it is enabled
	if (popupStatus == 1) {
		$("#mask").fadeOut("slow");
		$("#popup").fadeOut("slow", callBack);
		popupStatus = 0;
	}
}

// centering popup
function centerPopup() {
	// request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();
	// centering
	$("#popup").css({
		"position" : "absolute",
		"top" : windowHeight / 2 - popupHeight / 2,
		"left" : windowWidth / 2 - popupWidth / 2
	});
	// only need force for IE6

	$("#mask").css({
		"height" : windowHeight
	});

}

function initPopup() {

	$("#mask").click(function() {
		disablePopup();
	});
	// Click out event!
	$("#mask").click(function() {
		disablePopup();
	});
	$("#close").click(function() {
		disablePopup();
	});
	// Press Escape event!
	$(document).keypress(function(e) {
		if (e.keyCode == 27 && popupStatus == 1) {
			disablePopup();
		}
	});

}

function displayPopup() {
	this.centerPopup();
	this.loadPopup();

}

function resizePopup(height, width, animate, duration) {

	if (animate) {
		if (duration == null)
			duration = 1500;
		if (height != null && width != null) {
			$("#popup").animate({
				"height" : height,
				"width" : width
			}, duration);
		} else {

			if (height != null) {
				$("#popup").animate({
					"height" : height
				}, duration);
			}
			if (width != null) {
				$("#popup").animate({
					"width" : width
				}, duration);

			}
		}

	} else {
		if (height != null) {
			$("#popup").css({
				"height" : height
			});
		}

		if (width != null) {
			$("#popup").css({
				"width" : width
			});

		}

	}
	this.centerPopup();

}

function getSimplePopupFromDiv(divId, height, width, overflow) {
	$("#popupContent").html($("#" + divId).html());
	resizePopup(height, width, false);
	loadPopup(overflow);
}

function getAjaxActionPopup(actionName, typeform, data, height, width, debug) {
	if (debug) {
		alert("[data]:action=" + actionName + (data != "" ? "&" + data : "")
				+ "\n[type]:" + typeform);
	}
	$.ajax({
		type : typeform,
		url : "main.php",
		data : "action=" + actionName + (data != "" ? "&" + data : ""),
		success : function(data) {
			$("#popupContent").html(data);
			resizePopup(height, width, false);
			loadPopup();
		}
	});

}

