//SETTING UP OUR POPUP
//0 disabled; 1 enabled;
var popupStatus = 0;

//loading popup with!
function loadPopup(image) {
	
	//zwingt products_image raus und popup_images rein!
	var elements = image.split('/');
	elements[elements.length-3]='popup_images';
	var image = elements.join('/');
	
	//loads popup only if it is disabled
	if (popupStatus == 0) {
		$("#backgroundPopup").css( {"opacity" : "0.5"});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupImage").fadeIn("slow");
		$("#img").attr("src",image);
		popupStatus = 1;
	}
}

//disabling popup
function disablePopup() {
	//disables popup only if it is enabled
	if (popupStatus == 1) {
		$("#backgroundPopup").fadeOut("slow");
		$("#popupImage").fadeOut("slow");
		popupStatus = 0;
	}
}

//Positioning
function centerPopup() {
	//request data for centering
	var documentWidth = $(document).width();
	var windowHeight = $(window).height();	
	var windowWidth = $(window).width();
	var documentHeight = $(document).height();
	var popupHeight = $("#popupImage").height();
	var popupWidth = $("#popupImage").width();
	
	var top = windowHeight / 2 - popupHeight/2 + $(window).scrollTop() ;
	var left = documentWidth / 2 - popupWidth / 2 - 60;

	// centering popup
	$("#popupImage").css( {
		"position" : "absolute",
		"top" : top,
		"left" : left
	});

	// only need force for IE6

	$("#backgroundPopup").css( {
		"height" : windowHeight,
		"width" : windowWidth
		
	});
	

	

}





$(document).ready( function() {
	//Click the button event!
		$("#pi_image_container").click( function() {
			//centering with css
				centerPopup();
				// load popup				
				loadPopup($("#pi_image").attr("src"));
			});
		$("#lupe").click( function() {
			//centering with css
				centerPopup();
				// load popup				
				loadPopup($("#pi_image").attr("src"));
			});
	

		// CLOSING POPUP
		// Click the x event!
		$("#popupContactClose").click( function() {
			disablePopup();
		});

		// Click out event!
		$("#backgroundPopup").click( function() {
			disablePopup();
		});

		// Press Escape event!
		$(document).keypress( function(e) {
			if (e.keyCode == 27 || popupStatus == 1) {
				disablePopup();
			}
		});
		
		
	});
