$(document).ready(function() {
	/**
		Events
	**/
	
	$("#link-skills").click(function(e) {
		e.preventDefault();
		$("#skills").toggle("slow");
	});
	$("#link-referenzen").click(function(e) {
		e.preventDefault();
		$("#referenzen").toggle("slow");
	});
	
	$(".cv a").click(function(e) {
		e.preventDefault();
		$("#skills").slideUp("slow");
	});
	$(".ref a").click(function(e) {
		e.preventDefault();
		$("#referenzen").slideUp("slow");
	});
	$(".impr a").click(function(e) {
		e.preventDefault();
		$(".impressums-feld").fadeOut("slow");
	});
	$(".bild a").click(function(e) {
		e.preventDefault();
		$("#bild").fadeOut("slow");
	});
	
	$("#link-impressum").click(function(e) {
		e.preventDefault();
		$("#bild").fadeOut("fast");
		$(".impressums-feld").fadeIn("slow");
	});
	$("#link-bild").click(function(e) {
		e.preventDefault();
		$(".impressums-feld").fadeOut("fast");
		$("#bild").fadeIn("slow");
	});
	
	
	
	/**
		Seitenaufruf auf schwarzem Hintergrund
	**/
	
	var popupStatus = 0;
	function loadPopup(id) {
		if(popupStatus==0) {
			$("#hintergrundlayer").css({
				"opacity": "0.7"
			});
			$("#hintergrundlayer").fadeIn("fast");
			$(id).fadeIn("slow");
			popupStatus = 1;
		}
	}
	
	function disablePopup(id) {
		if(popupStatus==1) {
			$("#hintergrundlayer").fadeOut("fast");
			$(id).fadeOut("slow");
			popupStatus = 0;
		}
	}
	
	//Popup zentrieren - Fensterberechnung
	function centerPopup(id) {
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $(id).height();
		var popupWidth = $(id).width();
		
		$(id).css({
			"position": "absolute",
			"top": 40,
			"left": 120
		});
		//Fuer IE6
		$("#hintergrundlayer").css({
			"height": windowHeight
		});
	}
	
	$("#auftragsarbeit").click(function(e) {
		e.preventDefault();  
		centerPopup("#text-auftragsarbeit");
		loadPopup("#text-auftragsarbeit");
	});
	$("#subunternehmer").click(function(e) {
		e.preventDefault();  
		centerPopup("#text-subunternehmer");
		loadPopup("#text-subunternehmer");
	});
	$("#projektarbeit").click(function(e) {
		e.preventDefault();  
		centerPopup("#text-projektarbeit");
		loadPopup("#text-projektarbeit");
	});
	$("#freiemitarbeit").click(function(e) {
		e.preventDefault();  
		centerPopup("#text-freiemitarbeit");
		loadPopup("#text-freiemitarbeit");
	});
	$("#festanstellung").click(function(e) {
		e.preventDefault();  
		centerPopup("#text-festanstellung");
		loadPopup("#text-festanstellung");
	});

	
	//Layer-Seite schliessen - bei Klick auf "Schließen"
	$(".layer-schliessen").click(function(e){
		e.preventDefault(); 
		disablePopup(".layerseite");
	});
	//Layer-Seite schliessen - bei Klick auf Hintergrund
	$("#hintergrundlayer").click(function(e){
		e.preventDefault(); 
		disablePopup(".layerseite");
	});


	
	/**
		Fluessiges Scrollen zu einem Anker
	**/
	
	$('a[href*=#]').click(function() {
	   var duration = 1000;
	   var easing = 'swing';
	   
	   var newHash = this.hash;
	   var target = $(this.hash).offset().top;
	   var oldLocation = window.location.href.replace(window.location.hash, '');
	   var newLocation = this;

	   // make sure it's the same location      
	   if(oldLocation+newHash==newLocation)
	   {
		  // animate to target and set the hash to the window.location after the animation
		  $('html:not(:animated),body:not(:animated)').animate({ scrollTop: target }, duration, easing, function() {

			 // add new hash to the browser location
			 window.location.href=newLocation;
		  });

		  // cancel default click action
		  return false;
	   }
	});
	
	
	/**
		Info-Fenster bei Mousehover
	**/
	$(function(){
		$("[title]").mbTooltip({ 	// also $([domElement]).mbTooltip  >>  in this case only children element are involved
			opacity : .97,       	//opacity
			wait:400,           	//before show
			cssClass:"default",  	// default = default
			timePerWord:70,      	//time to show in milliseconds per word
			hasArrow:false,			// if you whant a little arrow on the corner
			hasShadow:true,
			imgPath:"images/",
			ancor:"mouse", 			//"parent"  you can ancor the tooltip to the mouse position or at the bottom of the element
			shadowColor:"black",	//the color of the shadow
			mb_fade:200 			//the time to fade-in
		});
	});
});
