//SETTING UP OUR POPUP
//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").fadeIn("fast", fadeIn_timeout('#popupWindow', 400, "fast"));
		$("#backgroundPopup").css({
			"opacity": "0.550"
		});
		
		$("#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("medium");
		$("#popupWindow").fadeOut("medium");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	
	var scrollDim = getScrollXY();
	var widthPlus =scrollDim[0];
	var heightPlus =scrollDim[1];
	
		
	//alert (scrollDim);	
	$("#backgroundPopup").css({
			"opacity": "0.650"
	});
	
	//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 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 ];
}

