/***************************/
//@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;
var suchePopupStatus = 0;
var popupEmpfStatus = 0;
var popupslideshowStatus = 0;
var popupStatusNews = 0;
var popupBigimageStatus = 0;

function loadPopup(background){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.3"
		});
		if (background==true)
		{
			$("#backgroundPopup").fadeIn("def");
		}
		$("#popupJquery").fadeIn("def");
		popupStatus = 1;
	}
}

function loadPopupNews(background){
	//loads popup only if it is disabled
	if(popupStatusNews==0){
		$("#backgroundPopup").css({
			"opacity": "0.3"
		});
		if (background==true)
		{
			$("#backgroundPopup").fadeIn("def");
		}
		$("#popupJquery").fadeIn("def");
		popupStatusNews = 1;
	}
}

function loadWeiterempfehlen(background){
	//loads popup only if it is disabled
	if(popupEmpfStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.4"
		});
		if (background==true) {
			$("#backgroundPopup").fadeIn("def");
		}
		$("#weiterempfehlen").fadeIn("def");
		popupEmpfStatus = 1;
	}
}

function loadSlideshow(background){
	//loads popup only if it is disabled
	if(popupslideshowStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.4"
		});
		if (background==true) {
			$("#backgroundPopup").fadeIn("def");
		}
		$("#slideshow").fadeIn("def");
		popupslideshowStatus = 1;
	}
}

function loadBigimage(background){
	//loads popup only if it is disabled
	if(popupBigimageStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.4"
		});
		if (background==true) {
			$("#backgroundPopup").fadeIn("def");
		}
		$("#bigimage").fadeIn("def");
		popupBigimageStatus = 1;
	}
}

function loadSuchePopup(background){
	//loads popup only if it is disabled
	if(suchePopupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.3"
		});
		if (background==true)
		{
			$("#backgroundPopup").fadeIn("def");
		}
		$("#suchePopup").fadeIn("def");

		suchePopupStatus = 1;
	}
}


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

function disablePopupNews(){
	//disables popup only if it is enabled
	if(popupStatusNews==1){
		$("#backgroundPopup").fadeOut("def");
		$("#popupJquery").fadeOut("def");
		popupStatusNews = 0;
	}
}

function disableSuchePopup(){
	if (suchePopupStatus ==1){
		$("#backgroundPopup").fadeOut("def");
		$("#suchePopup").fadeOut("def");
		suchePopupStatus = 0;
	}
}

function disableSlideshow(){
	if (popupslideshowStatus ==1)
	{
		$("#backgroundPopup").fadeOut("def");
		$("#slideshow").fadeOut("def");
		popupslideshowStatus = 0;
	}
}

function disableBigimage(){
	if (popupBigimageStatus ==1)
	{
		$("#backgroundPopup").fadeOut("def");
		$("#bigimage").fadeOut("def");
		popupBigimageStatus = 0;
	}
}

function disableWeiterempfehlen(){
	if (popupEmpfStatus ==1){
		$("#backgroundPopup").fadeOut("def");
		$("#weiterempfehlen").fadeOut("def");
		popupEmpfStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupJquery").height();
	var popupWidth = $("#popupJquery").width();
	//centering
	$("#popupJquery").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function setPopupPosition(item,popup){
	var pos = item.offset();
	var width = item.width();
	pos.left -= 0;
	pos.top -= 14;
	popup.css({
		"position": "absolute",
		"top": pos.top + "px",
		"left": pos.left + "px"
	});
}

function setPopupPositionNews(item,popup){
	var pos = item.offset();
	var width = item.width();
	pos.left -= 0;
	pos.top -= 14;
	popup.css({
		"position": "absolute",
		"top": pos.top + "px",
		"left": pos.left + "px"
	});
}

function setPopupPositionSuche(item,popup){
	var pos = item.offset();
	var width = item.width();
	pos.left -= 150; /* 150 */
	pos.top -= 12; /* 14 */
	popup.css({
		"position": "absolute",
		"top": pos.top + "px",
		"left": pos.left + "px"
	});
}

function setPopupPositionSlideshow(item,popup){
	var pos = item.offset();
	var width = item.width();
	pos.left -= 0;
	pos.top -= 1;
	popup.css({
		"position": "absolute",
		"top": pos.top + "px",
		"left": pos.left + "px"
	});
}

function setPopupPositionWeiterempfehlen(item,popup){
	var pos = item.offset();
	var width = item.width();
	pos.left += 40;
	pos.top -= 0;
	popup.css({
		"position": "absolute",
		"top": "20px",
		"left": pos.left + "px"
	});
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!

	$("#mitarbeiterliste").click(function(){
		//centering with css
		setPopupPosition($("#mitarbeiterliste"),$("#popupJquery"));
		//load popup
		loadPopup(false);
	});
	
	$("#newsliste").click(function(){
		//centering with css
		setPopupPositionNews($("#newsliste"),$("#popupJquery"));
		//load popup
		loadPopupNews(false);
	});
	
	$("#suche_geschlossen").click(function(){
		//centering with css
		setPopupPositionSuche($("#suche_geschlossen"),$("#suchePopup"));
		//load popup
		loadSuchePopup(false);
	});
	
	$(".slideshow").click(function(){
		//centering with css
		setPopupPositionSlideshow($(".slideshow"),$("#bigimage"));
		//load popup
		loadBigimage(false);
	});
	
	$(".galerie").click(function(){
		//centering with css
		setPopupPositionSlideshow($(".slideshow"),$("#slideshow"));
		//load popup
		loadSlideshow(false);
	});
	
	$(".icon.weiterempfehlen").click(function(){
		//centering with css
		setPopupPositionWeiterempfehlen($(".icon.weiterempfehlen"),$("#weiterempfehlen"));
		loadWeiterempfehlen(true);
	});
				
	//CLOSING POPUP
	$("#mitarbeiterliste_close").click(function(){
		disablePopup();
	});
	
	$("#newsliste_close").click(function(){
		disablePopupNews();
	});
	
	$("#suche_offen").click(function(){
		disableSuchePopup();
	});
	
	$("#weiterempfehlen .close").click(function(){
		disableWeiterempfehlen();
	});
	
	$("#slideshow .close").click(function(){
		disableSlideshow();
	});
	
	$("#bigimage .close").click(function(){
		disableBigimage();
	});
	
	$("#backgroundPopup").click(function(){
		disablePopup();
		disableSuchePopup();
		disableWeiterempfehlen();
		disableSlideshow();
		disablePopupNews();
		disableBigimage();
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
		if(e.keyCode==27 && suchePopupStatus==1){
			disableSuchePopup();
		}
		if(e.keyCode==27 && popupEmpfStatus==1){
			disableWeiterempfehlen();
		}
		if(e.keyCode==27 && popupslideshowStatus==1){
			disableSlideshow();
		}
		if(e.keyCode==27 && popupStatusNews==1){
			disablePopupNews();
		}
		if(e.keyCode==27 && popupBigimageStatus==1){
			disableBigimage();
		}
		
		


		
	});
	


});