$(function() {
	function spalten(){
		$('#details h3,#details a:has(img)').wrapAll('<div class="addLeft" />');
		$('#details strong:nth(0),#details span:nth(0),#details strong:nth(1),#details span:nth(1),#details strong:nth(2),#details span:nth(2)').wrapAll('<div class="addMiddle" />');
		$('#details strong:not(:nth(0),:nth(1),:nth(2)),#details span:not(:nth(0),:nth(1),:nth(2))').wrapAll('<div class="addRight" />');
		$('#details .addLeft').css('height',$('#details').height());
		$('#details .addMiddle').css('height',$('#details').height());
		var imgLink = $('#details img').attr('src');
	}
	function fancybox() {
		//$('div.ngg-galleryoverview a').has('img').fancybox();
		//$('.fancybox').fancybox();
	}
	function setHash(){
		if($(location).attr('hash')) {
			var hash = location.hash.split('#')[1];
			$('a[href]').each(function(){
				var href = $(this).attr('href').split('/').reverse()[1];
				if(href == hash){
					$(this).click();
				}
			});
		} else {
			$('#contentList>a:first').click();
		}
	}
	function openMenu() {
		var href = location.href.split('/')[3];
		if(href == 'referenzen'){
			$('#menu>li.page-item-7>a').next('ul:hidden').show();
			$('#menu>li.page-item-7>a').prev().text('-');
			$('#menu>li.page-item-7>a').parent().addClass('current_page_parent');
		}
		if(href == 'ueber-uns'){
			$('#menu>li.page-item-52>a').next('ul:hidden').show();
			$('#menu>li.page-item-52>a').prev().text('-');
			$('#menu>li.page-item-52>a').parent().addClass('current_page_parent');
		}
	}
	function menu() {
		$('#menu>li.page-item-52>a,#menu>li.page-item-7>a').before('<span>+</span>');
		$('#menu>li.page-item-52>a').click(function(){
			$('#menu>li.page-item-7>ul:visible').hide('blind',500);
			$('#menu>li.page-item-7:has(ul:visible)').find('span').text('+');
			$(this).next('ul:hidden').show('blind',500);
			$(this).prev().text('-');
			$('li.current_page_parent').removeClass('current_page_parent');
			$(this).parent().addClass('current_page_parent');
			return false;
		});
		$('#menu>li.page-item-7>a').click(function(){
			$('#menu>li.page-item-52>ul:visible').hide('blind',500).prev().text('+');
			$('#menu>li.page-item-52:has(ul:visible)').find('span').text('+');
			$(this).next('ul:hidden').show('blind',500);
			$(this).prev().text('-');
			$('li.current_page_parent').removeClass('current_page_parent');
			$(this).parent().addClass('current_page_parent');
			return false;
		});
	} 
	function contentLoader() {
		$('#contentList>a:not(.noajax)').click(function(){
			$('#container').empty().removeClass().addClass('loading');
			var hash = $(this).attr('href').split('/').reverse()[1];
			location.hash = hash;
			var href = $(this).attr('href');
			$('#contentList>a').removeClass('current_page_item');
			$(this).addClass('current_page_item');
			$.post(href,{'ajax':true},function(result){
				var text = $('p:first', result).attr('id','infoText');
				var pictures = $('img', result);
				var details = $(result).filter('#details');
				$('#container').removeClass().addClass('loaded').empty();
				$('#infoText').remove();
				$('#container').append(pictures);
				$('#container').after(text);
				$('#container').cycle({random:true});
				$('#details').remove();
				$('#container').after(details);
				$('#details').show('fade',600);
				//spalten();
				fancybox();
			},'html');
			return false;
		});
	}
	function startbild() {
		var imageFragment = $('img','#container');
		$('#container').html(imageFragment).cycle();
	}
	function groupbyyear() {
		$currentYear = (new Date).getFullYear();
		var year = [];
		$('.archivnews[rel]').each(function(){
			year.push($(this).attr('rel'));
		});
		$('.archivnews[rel=2009]').wrapAll('<div class="year" id="year_2009" />').parent().before('<strong>' + $('.archivnews[rel=2009]').attr('rel') + '</strong>');
		$('.archivnews[rel=2010]').wrapAll('<div class="year" id="year_2010" />').parent().before('<strong>' + $('.archivnews[rel=2010]').attr('rel') + '</strong>');
		$('.archivnews[rel=2011]').wrapAll('<div class="year" id="year_2011" />').parent().before('<strong>' + $('.archivnews[rel=2011]').attr('rel') + '</strong>');
		$('.archivnews[rel=2012]').wrapAll('<div class="year" id="year_2012" />').parent().before('<strong>' + $('.archivnews[rel=2012]').attr('rel') + '</strong>');
		$('.archive').accordion({
			header:'strong'
		});
	}
	function activate() {
		menu();
		contentLoader();
		setHash();
		startbild();
		openMenu();
		//spalten();
		groupbyyear();
		fancybox();
	}
	activate();
});
