$(function(){
		
		$('#menu a').click(function(e){

			e = $(e.target);
			id = e.attr('href'); 
			if(id == window.active)return;
			
			$('#menu .active').removeClass('active');	
			e.addClass('active');
			
			if(!$('.iphone').length){
		
					
				off = e.offset();
				offWrap = $('#wrapper').offset();
				ox  = Math.round(off.left - offWrap.left + e.width()/2 - 6);
				oy = 42;
				$('#menu').animate({backgroundPosition:'('+(ox)+'px '+oy+'px)'},600,'easeOutExpo');
				
		
			}

			
			scrollPos = $(window).scrollTop();
			scrollPosLeft = $(window).scrollLeft();
					
			self.location.hash = id;
			prevID = window.active;
			window.active = id;
			
			$(window).scrollTop(scrollPos);
			$(window).scrollLeft(scrollPosLeft);
			
			pre = $('#content > li:not('+window.active+'):visible');
			
			if(pre.length && prevID != '#map'){
				$('#content > li:visible > *').stop().animate({opacity:0,left:'50px'},300,'easeInOutExpo'); //,
				$('#content ul').stop().animate({opacity:1},200,null,showActiveContent);
				
			} else {
				showActiveContent();
			}

			return false;
		});
		
		if(self.location.hash.length != 0){			
			$('#menu a[href="'+self.location.hash+'"]').trigger('click');
		} else {
			if($.browser.msie && $.browser.version < 7)home();
		}
		
		$('h1').click(home).css({cursor:'pointer'});
		
		window.setInterval(checkHash, 50);
})


function showActiveContent(){
	// console.log('showActiveContent');
	
	if(window.active == '#tours') { showForm(); } else { hideForm(); }
	if(window.active  == '#map') { showMap(); } else { hideMap(); }
	if(window.active  == '#kontakt') { showContact() } else { hideContact() };
	
	if(window.active != '#map')$(window.active +' > *').css({opacity:0,background:'#FFFFFF',left:'-50px',position:'relative'}); //left:'-100px',position:'relative'
	$('.open').removeClass('open');
	$(window.active).addClass('open').show();
	// console.log($(window.active).height());
	// $('#content').stop().animate({height:$(window.active).height()+'px'},300,'easeInOutSine');
	if(window.active != '#map')$(window.active +' > *').stop().animate({opacity:1,left:0},500,'easeOutExpo'); //,left:0
	
	
	pageTracker._trackPageview(window.active);
	$('#content > li:not(.open)').hide();
}

function home(){
	$('#menu a').eq(0).trigger('click');
}


function checkHash(){
	if(window.active != self.location.hash)
	{
		$('#menu a[href="'+self.location.hash+'"]').trigger('click');
	}
}

function showForm(){
	$('#tourform').html('<iframe height="640" allowTransparency="true" frameborder="0" scrolling="no" style="width:100%;border:none"  src="http://nicheberlin.wufoo.com/embed/z7x4m1/"><a href="http://nicheberlin.wufoo.com/forms/z7x4m1/" title="I might want to take a Tour ..">Request a Tour</a></iframe>');
}

function hideForm(){
	$('#tourform').html('');
}


function showContact(){
	$('#contactform').html('<iframe height="540" allowTransparency="true" frameborder="0" scrolling="no" style="width:100%;border:none"  src="http://nicheberlin.wufoo.com/embed/m7x4z5/"><a href="http://nicheberlin.wufoo.com/forms/m7x4z5/" title="Contact us">Fill out my Wufoo form!</a></iframe>');
}

function hideContact(){
	$('#contactform').html('');
}

function showMap(){
	$('#gmap').html('<iframe width="450" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.de/maps/ms?t=p&amp;hl=en&amp;ie=UTF8&amp;msa=0&amp;msid=109180327459991600620.000459af821a81fc5acca&amp;ll=52.515385,13.41362&amp;spn=0.208929,0.30899&amp;z=11&amp;output=embed"></iframe>');
}

function hideMap(){
	$('#gmap iframe').attr('src','about:blank');
}

