﻿
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.650"
		});
		$("#backgroundPopup").fadeIn("fast", fadeIn_timeout('#popupWindow', 400, "fast"));
		//$("#backgroundPopup").fadeIn("slow");
		//$("#popupWindow").fadeIn("slow");
		popupStatus = 1;
	}
}


//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#popupWindow").fadeOut("fast", fadeOut_timeout('#backgroundPopup', 400, "fast"));
		//$("#backgroundPopup").fadeOut("slow");
		//$("#popupWindow").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	
	var scrollDim = getScrollXY();
	var widthPlus =scrollDim[0];
	var heightPlus =scrollDim[1];
	//alert (scrollDim);	
	
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupWindow").height();
	var popupWidth = $("#popupWindow").width();
	

	//centering
	$("#popupWindow").css({
		"position": "absolute",
		"top": (windowHeight/2-popupHeight/2) + heightPlus,
		"left": (windowWidth/2-popupWidth/2) + widthPlus
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

////CONTROLLING EVENTS IN jQuery
//$(document).ready(function(){
//	
//	//LOADING POPUP
//	//Click the button event!
//	$(".popup").click(function(){
//									 
//		//centering with css
//		centerPopup();
//		//load popup
//		loadPopup();
//	});
//				
//	//CLOSING POPUP
//	//Click the x event!
//	$("#popupWindowClose").click(function(){
//		disablePopup();
//	});
//	//Click out event!
//	$("#backgroundPopup").click(function(){
//		disablePopup();
//	});
//	//Press Escape event!
//	$(document).keypress(function(e){
//		if(e.keyCode==27 && popupStatus==1){
//			disablePopup();
//		}
//	});
//
//});	


function initPopup() {
	$(document).ready(function(){
		//LOADING POPUP
		//Click the button event!
		$(".popup").click(function(){
										 
			//centering with css
			centerPopup();
			//load popup
			loadPopup();
		});
					
		//CLOSING POPUP
		//Click the x event!
		$("#popupWindowClose").click(function(){
			disablePopup();
		});
		//Click out event!
		$("#backgroundPopup").click(function(){
			disablePopup();
		});
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		});
	});
}
	
initPopup();

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function popupWin(Pop_w, Pop_h, popTitle, PopSource) {
	//alert('jirka')
	//$(document).ready(function(){
		$("#popupWindow").css({'width' : Pop_w});
		$("#popupWindow").css({'height' : Pop_h});
		
		$("#popupArea").css({
			//"height": Pop_h - (130)
		});
		$("#popupWindow .nadpis").html(popTitle);
		//$("#popupWindow #popupArea").addClass(Pop_class);
		$("#popupWindow #popupArea").html(PopSource);
		
			//centering with css
			centerPopup();
			//load popup
			loadPopup();

	//});
}

function popupAddContent(file){
		//alert(id+' - '+content)
		$.post(file,
	  
		function(html){

		  $("#popupArea").html(html);
		
	  });
}


function popupWinNew(Pop_w, Pop_h, webBlockID, popTitle) {
	
	$(document).ready(function(){
		$("#popupWindow").css({'width' : Pop_w});
		$("#popupWindow").css({'height' : Pop_h});
		$.post('_ajax/_get-web-block.php', {webBlockID: webBlockID},
		function(html){
			$("#popupWindow .nadpis").html(popTitle);
			$("#popupWindow #popupArea").html(html, 100);
		});
		
	});
}



