
$(document).ready(function() {
	
	
	$("div[id=content]").css('padding-top', ($("div[id=navi]").height() + 44));
	
	$(".shortdescription").fadeTo(0,0.01);

	var mediaover = false;

/////////////////////////// N A V I ////// C L I C K //////////////////////////////////////////////

	$("div[class=navileiste] a").click(function(i){
		var referrer = $(this).attr("href").substring(1,$(this).attr("href").length);
		//referrer = referrer.replace(' ', '');
		$("div[class=project]").each(function(i){
			
			einklappen(this);
			var header = $('h3', this).text();
			header = header.replace(/\s/gi, '');
			
			if(header == referrer){
				$('body').scrollTo(216*i+10, 0, { axis: 'y', duration:400});
				ausklappen(this);
			}
			
		});
		
	});
	
/////////////////////////// P R O J E K T ///// H O V E R  //////////////////////////////////////////////	
	$(".project").hover(function(){
		if($(this).position().top - $('body').scrollTop() > 100 && $(this).position().top - $(window).scrollTop() > 100) $('.shortdescription', this).fadeTo(100, 1);
		},
		function(){
			if($(this).attr('id') != 'active') $('.shortdescription', this).fadeTo(100, 0.01);
		}
	);
			
/////////////////////////// P R O J E C T ////// C L I C K //////////////////////////////////////////////
	$(".object").mouseover(function(){
		mediaover = true;
	});
	$(".object").mouseout(function(){
		mediaover = false;	
	});
	$(".longdescription").mouseover(function(){
		mediaover = true;
	});
	$(".longdescription").mouseout(function(){
		mediaover = false;	
	});
	$(".project").click(function(){
		if($(this).attr('id') != 'active') ausklappen(this);
		else if(!mediaover) einklappen(this);
		var anchor = $('h3',this).text();
		anchor = anchor.replace(/\s/gi, '');
		window.location.hash = anchor;
		//$('body').scrollTo($(this).position().top - 130, 0, {axis: 'y', duration:400});
		$('body').scrollTo($(this).position().top - ($("div[id=navi]").height() + 33), 0, {axis: 'y', duration:400});
	});
	
	function ausklappen(handler){
		
		$(handler).attr('id', 'active');
		$('.shortdescription', handler).fadeTo(50, 1);
		$('.longdescription', handler).fadeTo(50, 1);
		var imageheight2 = 0;
		
		$('.secondimages > .imageblock', handler).each(function(i){
			
			var imageheight1 = 0;
			$('img', this).each(function(j){
				imageheight1 += $(this).attr("height");
			});
			if(imageheight1 > imageheight2) imageheight2 = imageheight1;
		});
		
		$('.longdescription', handler).animate({
			height:imageheight2
		});
		$('.secondimages', handler).show();
		$('.secondimages', handler).animate({
			height: imageheight2	
		});
	}
	function einklappen(handler){
		//alert("einklappen:"+ $('.shortdescription', handler).text());
		$(handler).removeAttr('id');
		$('.shortdescription', handler).fadeTo(50, 0.01);
		$('.longdescription', handler).fadeTo(50, 0.01);
		$('.longdescription', handler).animate({
			height:'10px'
		});
		
		$('.secondimages', handler).animate({
			height: '0px'	
		});
	}
	

/////////////////////////// H A S H //////////////////////////////////////////////	
	
	if(window.location.hash != ''){
		var referrer = window.location.hash.substring(1,window.location.hash.length);
		
		$("div[class=project]").each(function(i){
			var hash = $('h3', this).text();
			hash = hash.replace(/\s/gi, '');
			if(hash == referrer){
				var objekt = this;
				$('body').scrollTo(216*i+10, 0, { axis: 'y', duration:400, onAfter:function(){
					//alert("fertisch");
					ausklappen(objekt);	
					}});
			}
		});	
	}	
	
	
	
});



