/**
 * 
 */
/**
 * 
 */


//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").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupView").fadeIn("slow");
		popupStatus = 1;
	}
}

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

//centering popup
function centerPopup(height, width){
	//l'altezza e la larghezza puˆ essere passata via parametri
	//altrimenti prende quella settata nel css
	if(height!=undefined && height!=""){
		if(height>700){
			surplusHeight=60;
		}else if(height>450){
			surplusHeight=0;
		}else{
			surplusHeight=0;
		}
		if(height.toLowerCase().indexOf("px")==-1){
			height += "px";
		}
		//alert("Setto l'altezza a "+height);
		$("#popupView").css("height", height);
	}else{
		surplusHeight=150;
	}
	if(width!=undefined && width!=""){
		if(width.toLowerCase().indexOf("px")==-1){
			width += "px";
		}
		//alert("Setto la larghezza a "+width);
		$("#popupView").css("width", width);
	}
	
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupView").height();
	var popupWidth = $("#popupView").width();
	//centering
	$("#popupView").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2+surplusHeight,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//centering popup
function centerPopupGallery(height, width){
	//l'altezza e la larghezza puˆ essere passata via parametri
	//altrimenti prende quella settata nel css
	if(height!=undefined && height!=""){
		if(height>700){
			surplusHeight=60;
		}else if(height>450){
			surplusHeight=0;
		}else{
			surplusHeight=0;
		}
		if(height.toLowerCase().indexOf("px")==-1){
			height += "px";
		}
		//alert("Setto l'altezza a "+height);
		$("#popupView").css("height", height);
	}else{
		surplusHeight=150;
	}
	if(width!=undefined && width!=""){
		if(width.toLowerCase().indexOf("px")==-1){
			width += "px";
		}
		//alert("Setto la larghezza a "+width);
		$("#popupView").css("width", width);
	}
	
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupView").height();
	var popupWidth = $("#popupView").width();
	//centering
	$("#popupView").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2+surplusHeight,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
