$(document).ready(function () {
	
	// copy content
	$('#bereich_ausgabe').appendTo('#bereich_zaehler');
	
	// content equal height
	$("#haupt_content, #meta_content, #navi_content, #haupt_und_meta_content").equalHeights();
	$("#footer_navi_sp2, #footer_navi_sp3, #footer_navi_sp4").equalHeights();
	$(".kontakt_row_links, .kontakt_row_rechts").equalHeights();
	



	// Such-Optionen
	$('.suche_radio_wrap_hide').hide();

	$('span#selector').hoverIntent(
 		function () {
			$(".suche_radio_wrap_hide").stop(true,true).slideToggle(250, 'easeOutQuad', function () {});
		}, 
		function () {
			$(".suche_radio_wrap_hide").stop(true,true).slideToggle(250, 'easeOInQuad', function () {});
		}
	);
	
	$('input:radio[name=channel_id[]]').change(function(val){
		var theval=$("input:radio[name=channel_id[]]:checked").parent().text();
		$('.suche_radio.show').next('p').empty().text(theval);
		$(".suche_radio_wrap_hide").stop(true,true).slideToggle(250, 'easeOutQuad', function () {});
	});
	
	// hide news & events on left side 
	if ($('div#haupt_content').length > 0) {
		if ($('h2#termine').length == 0) { $('#left_termine').hide();}
		if ($('h2#news').length == 0)	{ $('#left_news').hide(); }
	}
	
	// hide Projekte & Partnerorg on startpage
	if ($('body#id_0_0').length > 0) {
		$('span.icon_id_28, span.icon_id_29').parent('li').hide();
		// hide the rest 
		$('span.icon_id_25, span.icon_id_44, span.icon_id_54, span.icon_id_55').parent('li').hide();
	}

	// fancyBox
	$(".fancybox").fancybox({
		openEffect	: 'none',
		closeEffect	: 'none'
	});
	
	// hover for  Stifung & Agentur
	$('ul#hilfs_navi_oben li.hilfs_li_1 a').hover(function() {
		$('a.farb_navi_parent_1').addClass('activated');
	}, function() {
		$('a.farb_navi_parent_1').removeClass('activated');
	});
	
	$('ul#hilfs_navi_oben li.hilfs_li_2 a').hover(function() {
		$('a.farb_navi_parent_2').addClass('activated');
	}, function() {
		$('a.farb_navi_parent_2').removeClass('activated');
	});
	
	
	// list style for <ul>
	$('#haupt_content ul li, #haupt_und_meta_content ul li').prepend('<span class="list_style"></span>');
	$('ul.projekte ul li').prepend('<span class="list_style"></span>');
	
	// scroller
	$('.scroll-pane').jScrollPane();
	
	$('li.nav_active').siblings().each(function() {
		$(this).stop(true,true).animate({
			opacity: 0.5
			},1000,'easeOutExpo',function() {
			// Animation complete.
		}).hover(function(){
			$(this).stop(true,true).animate({
			opacity: 1
			},1000,'easeOutExpo',function() {
			// Animation complete.
		})}, function() {
			$(this).stop(true,true).animate({
			opacity: 0.5
			},1000,'easeOutExpo',function() {
			// Animation complete.
			});
		});
	});
	
	// show/hide MP3 player
	$('.show_player').click(function(fx) {
		fx.preventDefault();
		var foo = this.id;
		// alert(this.id);
		var is = foo.match(/show_player_id_(\d+)/);
		if (is[1]) {
			// alert(is[1]);
			var addHeight = $('#jp_container_' + is[1]).outerHeight();
			// alert(addHeight);
			
			if ($(this).hasClass('ausgefahren')) {
				// alert('einfahren');
				$('#navi_content, #haupt_und_meta_content').animate({
                    'height' : '-=' + addHeight
        		}, 'fast', 'easeOutExpo');
				
				$('#jp_container_' + is[1] + ' .jp-pause').click();
				$('#jp_container_' + is[1]).removeClass('playing');
				$(this).text('im Browser abspielen');
			}
			else {
				// alert('ausfahren');
				$('#navi_content, #haupt_und_meta_content').animate({
                    'height' : '+=' + addHeight
        		}, 'fast', 'easeOutExpo');
				
				// first stop all running players
				$('.show_player.ausgefahren').click();
				
				$('#jp_container_' + is[1] + ' .jp-play').click();
				$('#jp_container_' + is[1] + ' .jp-play').jPlayer("play");
				$('#jp_container_' + is[1]).addClass('playing');
				$(this).text('Abspielen stoppen');
			}
			
			$(this).toggleClass('ausgefahren');
			$('#jp_container_' + is[1]).slideToggle();
			
		}
	});
	
	
	// show projekt_lang
	$('a.projekt_lang_a').click(function(fx) {
		fx.preventDefault();
		var addHeight = $(this).next('span.projekt_lang').outerHeight();
		// alert(addHeight);
		$('#navi_content, #haupt_und_meta_content').animate({
                    'height' : '+=' + addHeight
        }, 'fast', 'easeOutExpo');
		$(this).toggleClass('ausgefahren').next('span.projekt_lang').slideToggle();
		$(this).hide();
	});
	
	// twitter 
	$('#click_twitter').click(function(fx) {
		fx.preventDefault();
		$('div#content_twitter').slideToggle();
	});
	
	
	// animate (hide & show) bereich click
	
	$('.bereich_a_wahl').click(function(fx) {
		fx.preventDefault();
		var foo = this.id;
		var is = foo.match(/bereich_a_(.*)/);
		if (is[1]) {
			// alert(is[1]);
			var addHeight = 0;
			var substHeight = 0;

			$('.tww_jahr.tww_jahr_' + is[1] + ", .content_row_ma." + is[1]).each(function(idx, para) {
				addHeight += $('.tww_jahr_' + is[1] + ':eq(' + idx + ')').outerHeight();
				addHeight += $('.content_row_ma.' + is[1] + ':eq(' + idx + ')').outerHeight();
			});
			
			$('.content_row_hr.' + is[1]).each(function(idx, para) {
				addHeight += $('.content_row_hr.' + is[1] + ':eq(' + idx + ')').outerHeight();
			});		
			
			// alert(addHeight);
			
			if ($(this).hasClass('ausgefahren')) {
				// alert('einfahren');
				$('#navi_content, #haupt_und_meta_content').animate({
                    'height' : '-=' + addHeight
        		}, 'fast', 'easeInQuad');
			}
			else {
				// alert('ausfahren');
				// first close all open...
				if ($('.tww_jahr').length > 0) {
					$('a.bereich_a_wahl.ausgefahren').click();
				}
				else {

					// $('a.bereich_a_wahl.ausgefahren').click();
					
					$('.content_row_hr:visible').each(function(idx, para) {
						substHeight += $('.content_row_hr:visible:eq(' + idx + ')').outerHeight();
					});
					$('.content_row_ma:visible').each(function(idx, para) {
						substHeight += $('.content_row_ma:visible:eq(' + idx + ')').outerHeight();
					});
			
					addHeight -= substHeight;
					
					$('.content_row_hr:visible, .content_row_ma:visible').hide();
				}
				// stop all playing...
				// $('.jp_container.playing .jp-stop').();
				
				$('#navi_content, #haupt_und_meta_content').animate({
                	'height' : '+=' + addHeight
        		}, 'fast', 'easeOutQuad');
				
			}

			// alert("add " + addHeight + " subst " + substHeight);

			$(this).toggleClass('ausgefahren');
			$('.tww_jahr.tww_jahr_' + is[1]).slideToggle();
			$('.content_row.' + is[1]).slideToggle();
		}
		
	});
	
	$('#bereich_a_2012').click();
	
	// new window 
	$('a[rel=external]').attr('target','_blank'); 

	// fix a:active outline bug in IE6+7
	// http://haslayout.net/css-tuts/Removing-Dotted-Border-on-Clicked-Links
	if($.browser.msie) {
		$('a').click(function() {
			$(this).blur();
		});
	}
	
});

