var veil=$('#veil'),overlay=$('#overlay'),glossar=$('#glossar'),teamHeight,restoreTeam,personDetail=false,closeteam=false,cleanmap=false,gooseNavVis=1,gooseNavDelay;
var init = function(){




/* ### HELPER FUNCTIONS ### */
	var getCID = function(){
		return window.location.hash.split('/').shift();
	}





/* ### AKKORDEON ANIMATION ### */

	$('div.akkordeon').wrap('<div class="akkordeon-wrap">');
	$('div.akkordeon p:first-child').each(function(i){
		var n = $(this);
		n.addClass('akkordeon-tit');
		n.parent().before(n);
	});

	$('p.akkordeon-tit').click(function(e){
		e.preventDefault;

		$(this).next().slideToggle(function(){
			$(this).parent().toggleClass('open');
		});
	});

	$('div.csc-default div.csc-header').click(function(e){
		e.preventDefault;

		var cur = $(this);
		if(personDetail && cur.next().attr('id') == 'teamoverview' ){
			restoreTeam();
		}else{
			if(cur.parent().hasClass('open') || '#' + cur.parent().attr('id') == getCID() ){
				cur.next().slideToggle(function(){			
					$(this).parent().toggleClass('open');
				})
			}
			window.location.hash = cur.parent().attr('id') +'/'+ cur.text();

		}
	});

	var toggleCont = function(cur){
		if(personDetail){ closeteam=true; restoreTeam(); }
		if( overlay.is(':visible') ){ hideOverlay() };

		cur = cur.children('div.csc-textpic').first();
		cur.children('div.rechte-spalte').fadeToggle(400, function(){
			if ( $.browser.msie ){
				this.style.removeAttribute('filter');
			}
		});
		cur.slideToggle(function(){			
			$(this).parent().toggleClass('open');
		});

		$('div.csc-textpic:visible').not(':animated').children('div.rechte-spalte').fadeToggle(400, function(){
			if ( $.browser.msie ){
				this.style.removeAttribute('filter');
			}
		});
		$('div.csc-textpic:visible').not(':animated').slideUp(function(e){
			$(this).parent().toggleClass('open');
		});
	}





/* ### GOOSE SPECIAL ### */

	if($('#pid25').length > 0){
		$("#main").slides({
			preload: true,
			generateNextPrev: true,
			container: '#cont',
			animationStart: function(current){
				$('.csc-textpic-text').animate({
					opacity:0
				},100);
				if(gooseNavVis==2){
					$('#maincol.goosenavvis').removeClass('goosenavvis');
					//window.clearTimeout( gooseNavDelay );
				}else if(gooseNavVis==1){
					window.clearTimeout( gooseNavDelay );
				}
				goosenavvis = 3;
			},
			animationComplete: function(current){
				$('.csc-textpic-text').animate({
					opacity:1
				},{
					duration: 800,
					complete: function(){
						if ( $.browser.msie ){
							this.style.removeAttribute('filter');
						}
					}

				});
			} /*,
			slidesLoaded: function() {
				$('.csc-textpic-text').animate({
					opacity:0
				},200);
			} */
		});
		$('#maincol').append('<a href="/home/" class="close">&nbsp;</a>');
		$('a.next, a.prev').text(' ');

		gooseNavDelay = window.setTimeout(function(){
			$('#maincol').addClass('goosenavvis');
			gooseNavVis=2;
		},3000);
	}

	



/* ### GLOSSAR OVERLAY ### */

	var showOverlay = function(e){
		e.preventDefault();
		var anode = $(this);
		anode.blur();
		glossar.text(' ');

		veil.show().css({'opacity':0}).fadeTo(600, 0.4);
		//overlay.fadeIn();
		glossar.load( '/?id=15&type=3000&cid='+ anode.attr('href').split('#').pop().substr(1) +'&r='+ Math.round( Math.random()*100000) , false, function(){ overlay.slideDown(); }  );
		
	}

	var hideOverlay = function(e){
		veil.fadeOut();
		overlay.slideUp();
	}

	$('a.internal-link-overlay').click(showOverlay);
	$('#closeoverlay').click(hideOverlay);
	veil.click(hideOverlay);
	veil.css({'opacity':0});





/* ### TEAM AJAX ### */

	if($('#c15').length > 0){

		$('#c15 div.csc-textpic').attr('id', 'teamoverview');
		$('#c15 div.csc-textpic').after('<div id="person"><div id="teamback">zurück zur Übersicht</div><div id="cv"/></div>');

		restoreTeam = function(){
			personDetail=false;
			$('#teamoverview').animate({left: 0});
			$('#person').animate({left:520},{
				complete:function(e){
					$('#c15').animate({height: teamHeight },{complete:function(){ 
						$('#c15').removeAttr('style');
						if(closeteam){
							closeteam=false;
							$('#teamoverview').slideToggle(function(){			
								$(this).parent().toggleClass('open');
							});
						}
					}});
				}
			 });
		}
		$('#teamback').click(restoreTeam);
		$('#c15').click(function(e){
			e.preventDefault();
			var tg = $(e.target);

			var href = false;
			if(tg.attr('href')){
				href = tg.attr('href');
			}else if(tg.parent().attr('href')){
				href=tg.parent().attr('href');
			}

			if(href){
				personDetail=true;
				teamHeight = $('#c15').height();

				$('#teamoverview').animate({left: '-=520'});
				$('#cv').text(' ');
				$('#cv').load(href +'?type=2000', false, function(e){
					$('#person').animate({left:0},{complete:function(){ $('#c15').animate({height: $('#person').outerHeight() + 60 }); }});
				});
			}
		});
	}





/* ### ROUNDABOUT ANLAGEPROZESS ### */

	if($('#roundabout').length > 0){

		$('#roundabout div.text-cont').append('<div class="next" />');

		$('#roundabout').roundabout({
			childSelector: 'div.csc-default',
			minOpacity: 1,
			minScale: 0.1,
			startingChild: 0,
			btnNext: '#roundabout div.next',
			reflect: true
		});
		


	}





/* ### URL & HASH HANDLING ### */

	if($('#pid13').length == 0 && $('#pid25').length == 0){ /* exclude roundabout and goose */
		var hash = window.location.hash;
		var opencont = $('div.autoopen');
		if(hash != ''){ 
			autoopen = true;
			hash = getCID();
			opencont = $(hash);
		}
		toggleCont( opencont );
	
		$('#maincol').hide().fadeIn(400, function(){
			if ( $.browser.msie ){
				this.style.removeAttribute('filter');
			}
		});


	}


	if($('#c93').length > 0 && getCID() != '#c93' ){
		$('#c93 div.csc-textpic').addClass('map');
		cleanmap = window.setTimeout(function(){
			$('#c93 div.csc-textpic').removeClass('map');
			$('#c93').removeClass('open');
			clearTimeout(cleanmap);
		}, 1000);
	}
	

	var onURLchange = function(e) {		
		var hash = window.location.hash;
		if(hash != ''){ 
			hash = getCID();
			toggleCont( $(hash) );
		}else{
			toggleCont( $('div.autoopen') );
		}
	}

	$(window).bind( 'hashchange', onURLchange);
	
	

	$('#mainnav, #metanav').click(function() {
		$('div.csc-textpic:visible').slideUp(function(e){
			$(this).parent().toggleClass('open');
		});
		$('#maincol').fadeOut();
	});

	
}
$(document).ready(init); 


