$(document).ready(function() {
		
	//Links extern öffnen
	$('a.external').attr({target : '_blank'});

	//Navigation 1. Ebene Hover und IE6 Drop Down
	$('#pageHead > ul > li > ul').show().hide();
	
	$('#pageHead > ul > li').hover(function() {
		$(this).find('> a').addClass('active');
		$(this).find('> ul').stop(true, true).slideDown(300);
	},
	function(){
		$(this).find('> a').removeClass('active');
		$(this).find('> ul').slideUp(450);
	}
	);
	
	//Startseite: Verlängerung der Verteilertexte
	$('#pageBody.welcome #container .companiesContainer .company a span.zoom').hover(function(){
		if(!$(this).attr('spanHeight')) {
			$(this).css('height', 'auto')
			$(this).attr('spanHeight', $(this).height()+'px');
		}
		$(this).css('height', '7.2em');
		$(this).stop().animate({height: $(this).attr('spanHeight')}, 250);
	}, 
	function (){
    	$(this).stop().animate({height: '7.2em'}, 400);
	});
	
	//Background-Image Slider
	if($('#pageBody').hasClass('welcome')) {
		$('#pageBody').wrap('<div id="imageFader" style="background: url(../pics/bg_headerImage_welcome5.jpg) right top no-repeat; height: '+parseInt($('#pageBody').outerHeight() + 24) +'px;" />'); //erstellt das tragende Divs für die Imagecontainer der Startseite
	} else {
		$('#pageBody').wrap('<div id="imageFader" style="background: url(../pics/bg_headerImage_sub4.jpg) right top no-repeat; height: '+parseInt($('#pageBody').outerHeight() + 24) +'px;" />'); //erstellt das tragende Divs für die Imagecontainer der Subseite
	};
	
	$('#pageBody').css({'background': 'none', 'position': 'absolute', 'z-index': '3'}); //legt den echten pageBody über die Imagecontainer und entfernt das background-image
		
		//hier Imagecontainer eintragen
		var imageContainerWelcome = [
				'/pics/bg_headerImage_welcome.jpg',
				'/pics/bg_headerImage_welcome2.jpg',
				'/pics/bg_headerImage_welcome3.jpg',
				'/pics/bg_headerImage_welcome5.jpg' /* letztes Bild auch im CSS dem Div imageFader als Background geben */
		]
		
		var imageContainerSub = [
				'/pics/bg_headerImage_sub.jpg',
				'/pics/bg_headerImage_sub2.jpg',
				'/pics/bg_headerImage_sub3.jpg',
				'/pics/bg_headerImage_sub4.jpg'	/* letztes Bild auch im CSS dem Div imageFader als Background geben */
		]
		
		//fragt ab ob Sub- oder Startseite
		var slider = ($('#pageBody').hasClass('welcome')) ? imageContainerWelcome : imageContainerSub; // variable = (bedingung) ? ausgabe1 : ausgabe2;		
		
		//erstellt die Imagecontainer
		var html = '';
		
		for (var i = 0; i < slider.length; i++) {
			html += '<div class="imageContainer" style="background: url('+slider[i]+') no-repeat right top;" />'
		}
		
		$('#pageBody').before(html);
		
		//sucht den ersten imageContainer und macht ihn sichtbar
		$('.imageContainer:first').show();
		
		imageFader();
		
		//History Flash einbindung		
		if(jQuery.browser.flash == true) {
			$('#history').hide();
			$('#history').before('<a id="openFlash">' + openSentence + '</a>');
			$('#imageFader').css('height', parseInt($('#pageBody').outerHeight() + 24));
		}
		
		var outerHeight = $(document).height();
		
		$('a#openFlash').live('click', function() {
			$('#wrapper').before('<div id="overlay"></div>');	
			$('#wrapper').before('<div id="flashBox"><a id="close" alt="' + close + '" title="' + close + '"><span>' + close + '</span></a><object type="application/x-shockwave-flash" width="980" height="500" data="/swfs/' + flashName + '"><param name="movie" value="/swfs/' + flashName + '" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="base" value="/swfs/" /></object></div>');
			$('#overlay').css('height', outerHeight).hide().fadeTo('slow', 0.6);
		});	
		
		$('#overlay, a#close').live('click', function() {
			$('#flashBox').remove();
			$('#overlay').fadeTo('normal', 0, function(){
				$(this).remove();	   
			});
		});
	
});

//sucht den nächsten versteckten Container und fadet ihn ein
function imageFader() {
	window.setTimeout(function() {
		
		var element = $('#imageFader').find('.imageContainer:hidden').first();
		
		if(element.length==0) {
			$('.imageContainer:not(:last)').hide();
			element = $('.imageContainer:first');
			$('.imageContainer:last').css('z-index', '1');
			var last = 1;
		} else {
			element.prev().css('z-index', '1');
		}
		
		element.css('z-index', '2').fadeIn(2500, function() {
			if(last == 1)  $('.imageContainer:last').hide();
		});		
				
		imageFader();
		
	},8000);
}
