﻿
var popupStatusCmn = 0;

//loading popup with jQuery magic!
function cmnLoadPopup(btnClassValue){
	//loads popup only if it is disabled
	if(popupStatusCmn==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		
  		var btnIdValue="#popupBlock" + $(btnClassValue).attr('rel');
		$(btnIdValue).fadeIn("slow");
		
		popupStatusCmn = 1;
		
		
		//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(btnIdValue).height();
	var popupWidth = $(btnIdValue).width();
	//centering
	$(btnIdValue).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
	
	// Scroll down to the center of window
	// $('html,body').animate({scrollTop:windowHeight/2 -popupHeight}, 1000);
	
	}
}

//disabling popup with jQuery magic!
function cmnDisablePopup(cmnCloseBtn){
	//disables popup only if it is enabled
	if(popupStatusCmn==1){
		$("#backgroundPopup").fadeOut("slow");
		$(cmnCloseBtn).closest('.popupBlock').fadeOut("slow");

		//$("#popupContact3").fadeOut("slow");
		popupStatusCmn = 0;
	}
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	
	
	//LOADING POPUP
	//Click the button event!
	$(".cmnPopupBtn").click(function(){
		//centering with css
		cmnLoadPopup(this);
		//load popup
 
	});
				
	//CLOSING POPUP
	//Click the x event!
	$(".cmnPopupCloseBtn").click(function(){
		cmnDisablePopup(this);
	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		$("#backgroundPopup").fadeOut("slow");	
		$(".popupBlock").fadeOut("slow");
		popupStatusCmn = 0;
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		$("#backgroundPopup").fadeOut("slow");	
		$(".popupBlock").fadeOut("slow");
		popupStatusCmn = 0;
	});
	
});







