/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
//to track which popup is being displayed, when none is displayed it will be null
var currentPopup = null;

//loading popup with jQuery magic!
function loadPopup(){
  //loads popup only if it is disabled
	if(popupStatus==0 && currentPopup!=null){
		$("#background-popup").css({
			"opacity": "0.7"
		});
		$("#background-popup").fadeIn("slow");
		currentPopup.fadeIn("slow");
		popupStatus = 1;
	}
}

//enlarge image popup
function prepareEnlarge(imageClicked){
  var largeImgSrc = imageClicked.attr("src").replace("-small", "");
  var largeImgCap = imageClicked.attr("alt");
  $("#popup-enlarge img.large-image-popup").attr("src", largeImgSrc);
  $("#popup-enlarge img.large-image-popup").attr("alt", largeImgCap);
  $("#poup-enlarge span.popup-title").text(largeImgCap);
}

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

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;	
	var popupHeight = currentPopup.height();
	var popupWidth = currentPopup.width();
	var top = windowHeight/2-popupHeight/2;
	//if the windowheight is less than the popup height, cannot have negative number
	if(top < 0){top = 0;}
	var left = windowWidth/2-popupWidth/2;
	//if the windowwidth is less than the popup width, cannot have negative number
	if(left < 0){left = 0;}
	//centering
	currentPopup.css({
		"position": "absolute",
		"top": top,
		"left": left
	});
	//only need force for IE6
	
	$("#background-popup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Front view click
	$(".front-view").click(function(){
		currentPopup = $("#popup-front");
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
  
  //Upper view click
	$(".max-view").click(function(){
		currentPopup = $("#popup-max");
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
  
  //Lower view click
	$(".man-view").click(function(){
		currentPopup = $("#popup-man");
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
  
  //Enlarge image click
  $(".image-enlarge").click(function(){
		currentPopup = $("#popup-enlarge");
		prepareEnlarge($(this));
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("a.popup-close").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#background-popup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});