
//0 means disabled; 1 means enabled;
var articlePopupStatus = 0;

function articlePopupInit()
{
	//LOADING POPUP  
	//Click the button event!  
	$(".addWitness").click(function(){  
		//centering with css  
		articlePopupCenter();  
		//load popup  
		articlePopupLoad();  
	});  

	//
	//CLOSING POPUP
	// Press ESCAPE, Click on X (right-top) or Click Out from the popup to close the popup!
	//  
	//Click the x event!  
	$("#articlePopupClose").click(function(){  
		articlePopupDisable();  
	});  
	//Click out event!  
	$("#backgroundPopup").click(function(){  
		articlePopupDisable();  
	});  
	//Press Escape event!  
	$(document).keypress(function(e){  
		if(e.keyCode==27 & articlePopupStatus==1){  
			articlePopupDisable();  
		}
	});
}

// loading popup with jQuery magic!
function articlePopupLoad() {
	// loads popup only if it is disabled
	if (articlePopupStatus == 0) {
		$("#backgroundPopup").css( {
			"opacity" : "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#articlePopup").fadeIn("slow");
		articlePopupStatus = 1;
	}
}

// disabling popup with jQuery magic!
function articlePopupDisable() {
	// disables popup only if it is enabled
	if (articlePopupStatus == 1)
	{
		$("#backgroundPopup").fadeOut("slow");
		$("#articlePopup").fadeOut("slow",function(){
			// cleanup possible errors
			$("#articlePopup ul.tpfWidgetError").remove();
			$("#articlePopup .formHasErrors").remove();
			$("#articlePopup .tpfWidgetError").removeClass('tpfWidgetError');
		});
		articlePopupStatus = 0;

	}
}

// centering popup
function articlePopupCenter() {
	// request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#articlePopup").height();
	var popupWidth = $("#articlePopup").width();
	// centering
	$("#articlePopup").css( {
		"position" : "absolute",
		"top" : windowHeight / 2 - popupHeight / 2,
		"left" : windowWidth / 2 - popupWidth / 2
	});
	// only need force for IE6

	$("#backgroundPopup").css( {
		"height" : windowHeight
	});

}

$(document).ready(function(){

	articlePopupInit();

});

