$(document).ready(function(){
	//$('.layout').hide(); //does not work in safari on snow leopard!
	$('.layout').fadeIn('slow');
	fixMsie();
	//replace anchors to prevent jumping to anchor
	$('div.section').each(function(index,section){
		section.setAttribute('id',section.getAttribute('id')+'-passive');
	});
	//remove subheadings
	$('h2').each(function(index,heading){
		heading.parentNode.removeChild(heading);
	});
	//redirect local anchors
	/*$('a').filter(function(index){return $(this).attr('href').substr(0,1) == '#' && $(this).attr('href').indexOf('articles-') != 1;}).click(function(){
		showSection('#'+this.getAttribute('href').split('#')[1]);//work around msie
	});*/
	$('a[href]').click(function(){
		clickThru('#'+this.getAttribute('href').split('#')[1]);//work around msie
	});
	//show current or default section
	if(window.location.hash){
		clickThru(window.location.hash);
	}	else	{
		clickThru('#aktuell');
		//clickThru('#wir');
	}
	//prevent wobbling navigation
	$('div.head li a').each(function(index,anchor){
		$(anchor).css({display:'block',width:Math.floor($(this).width() * 1.1) + 'px',float:'left',textAlign:'center'});
	});

	window.setTimeout("diashow();",3000);
});
function clickThru(hash){
	if(hash.indexOf('http://') == 0){
		return true;
	}
	if(hash.indexOf('articles') != 1){
		showSection(hash);
	}	else	{
		hash = $(hash).parent('div.section').attr('id');
		showSection('#'+hash.replace(/-passive/,''));
	}
}

function showSection(hash){
	//hover navigation
	$('div.head li a').removeClass('hover');
	$("div.head li a[href='"+hash+"']").addClass('hover');
	//hide sections and show current
	$('div.section').hide();
	$(hash+'-passive').show();
	$(hash+'-passive .teaser *').hide();
	$(hash+'-passive .text *').hide();
	$(hash+'-passive .teaser *').fadeIn('fast');
	$(hash+'-passive .text *').fadeIn('slow');
	//remove classes from body and set new background-color
	if(navigator.userAgent.indexOf('MSIE 6') == -1){
		//won't work in msie6, because it doesn't support setting the background color of a transparent png
		$('body').attr('class','default');
		$('body').addClass($(hash+'-passive').attr('class').replace('section',''));
	}
	if(hash == '#wo'){
		$('#wo-passive div.teaser').html('<iframe style="border:1px solid #000;margin-left:21px;margin-top:42px;" width="249" height="249" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.de/maps?f=q&amp;source=s_q&amp;hl=de&amp;geocode=&amp;q=noonberlin&amp;sll=51.151786,10.415039&amp;sspn=13.966081,39.331055&amp;ie=UTF8&amp;hq=noonberlin&amp;hnear=&amp;cid=10086381801473737130&amp;ll=52.502195,13.413877&amp;spn=0.013741,0.010729&amp;z=15&amp;output=embed"></iframe><br /><small><a href="http://maps.google.de/maps?f=q&amp;source=embed&amp;hl=de&amp;geocode=&amp;q=noonberlin&amp;sll=51.151786,10.415039&amp;sspn=13.966081,39.331055&amp;ie=UTF8&amp;hq=noonberlin&amp;hnear=&amp;cid=10086381801473737130&amp;ll=52.502195,13.413877&amp;spn=0.013741,0.010729&amp;z=15" style="font-size:10px;font-family:Helvetica,Arial,sans-serif;margin-left:21px;color:#000;text-align:left">Größere Kartenansicht</a></small>');
	} else {
		$('#wo-passive div.teaser').html('');
	}
	
	if(window.location.hash.indexOf('articles-') == 1){
		window.setTimeout("scrollToArticle();",1000);
	}
}
function scrollToArticle(){
	var hash = $(''+window.location.hash).parent('div.section').attr('id');
	var top = $('#'+hash).offset().top;
	$('#'+hash).scrollTop(top);
}
function fixMsie(){
	if(navigator.userAgent.indexOf('MSIE') == -1) {
		$('body').css({overflowY:'scroll'});
	}
	fixMsie6();
}
function fixMsie6(){if(navigator.userAgent.indexOf('MSIE 6') != -1){
	$('img.transparent').each(function(index,image){
		var src = $(image).attr('src');
		$(image).attr('src','index.layout/transparent.png');
		$(image).css({filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='image')"})
	});
}}
function diashow(){
	var height = parseInt($('.diashow img').first().css('height'));
	var width = parseInt($('.diashow img').first().css('width'));
	var src = $('.diashow img').first().attr('src');
	var args = src.split('-')[1].split('.')[0];
	var cols = parseInt(args.split('x')[0]);
	var rows = parseInt(args.split('x')[1]);
	var left = parseInt($('.diashow img').first().css('marginLeft'));
	var top = parseInt($('.diashow img').first().css('marginTop'));
	var deltaLeft = Math.floor(width/cols);
	var deltaTop = Math.floor(height/rows);
	var newLeft = left - deltaLeft;
	var newTop = top;
	if(newLeft <= -width){
		newLeft = 0;
		newTop = top - deltaTop;
	}
	if(newTop <= -height){
		newTop = 0;
		newLeft = 0;
	}
	$('.diashow img').fadeOut('slow',function(){
		$('.diashow img').first().css({marginLeft:newLeft+'px',marginTop:newTop+'px'});
		$('.diashow img').fadeIn('slow',function(){
			window.setTimeout("diashow();",5000);
		});
	});
	
	
	
	
}

