jQuery(document).ready( function($) { 

	/*
	footer = $('#footer').html();
	$('#footer').detach();
	$( '<div id="#innerfooter">' + footer + '</div>' ).appendTo( '#main .page' );
	*/


	$('#header #topnav span a').wrapInner('<span />');
	/* $('#footer').css('top', $('#main').height() + 'px'); */
	
	/* js tabs */	
	$('ul.section').addClass('jsOn');
	$('ul.section.jsOn').before('<div class="imgTitle"></div><div class="imgTeaser"></div><div class="imgTeaserSubImg"></div>');
	$('div.imgTitle, div.imgTeaser').hide();
	$('ul.section.jsOn').after('<div class="textTeaser"></div>');
	
	theFirstText = $('#c55').html();
	theFirstText2 = $('#c259').html();
	theWord = 'weiterlesen';
	if ( theFirstText2 ) { theFirstText = theFirstText2; theWord = 'read more'; }
	$('div.textTeaser').html( theFirstText );
	$('#c55').detach();
	if ( theFirstText2 ) { $('#c259').detach(); }
	
	$('ul.section.jsOn li div.title a span').after('<b></b>');
	$('ul.section.jsOn li div.title').bind('click',function() {
		if ( $(this).hasClass('active') ) { return false; }
		selfClass = $(this).parents('li').attr('class');
		selfSubImg = $(this).parents('li').find('span.subImg');
		Iurl = $(this).find('a').attr('href');
		$(this).parents('ul').find('div.title').each( function() { 
			$(this).removeClass('active');
			$(this).removeClass('loading');
			} );
		$(this).addClass('loading');
		THIS = this;
		$.ajax( {
			url: Iurl,
			type: "GET",
			data: { type: 666 },
			dataType: "html",
			success: function(response) {
				$(THIS).addClass('active');
				$(THIS).removeClass('loading');
				theText = theLongText = $(response).filter('#main').find('#col3_content div:first').html();
				isMoreText = $(response).filter('#main').find('#col3_content div:first div.csc-default:nth-child(2)').html();
				
				theShortText = $('#main li.' + selfClass + ' div.text').html();
				theText = '<div class="shortText csc-default"><p>' + theShortText + '</p></div><div class="longText">' + theLongText + '</div>';
				//alert( theShortText );
				h1 = $(response).filter('#main').find('h1#pagetitle').html();
				h1Inner = $(response).filter('#main').find('h1#pagetitle span').html();
				//alert(h1);
				h2 = $(response).filter('#main').find('h2#subtitle').html();
				if (h2) theText = h2 + theText;
				theTeaser = false;
				theImages = $(response).filter('#main').find('#col3_content div.csc-textpic-imagewrap:first').html();
				if ( theImages ) theTeaser = theImages;
				if ( !theTeaser && !theImages ) {
					$('div.imgTeaser ').slideUp();
					$('div.imgTitle').hide();
					} 				
				$('div.imgTeaser').fadeOut('',function() {					
					if ( theTeaser && !$('div.imgTeaser').html() ) {
						$('div.imgTeaser').slideDown();		
						
						}
					$('div.imgTeaser').html( theTeaser );	
					/*
					$('div.imgTeaserSubImg').css('background-image', $(selfSubImg).css('background-image') );	
					$('div.imgTeaserSubImg').css('background-position', $(selfSubImg).css('background-position') );
					$('div.imgTeaserSubImg').css('height', $(selfSubImg).css('height') );
					$('div.imgTeaserSubImg').css('width', $(selfSubImg).css('width') );
					*/					
					$('div.imgTeaser div, div.imgTeaser dl, div.imgTeaser li').css('width','auto');
					$('div.imgTeaser a img').unwrap();
					//$('div.imgTeaser a').bind('click',function() {return false; } );
					if ( theTeaser ) {
						$('div.imgTeaser').fadeIn( );
						$('div.imgTitle').html( h1Inner );
						$('div.imgTitle').show();
						}
					} );
				$('div.textTeaser').fadeOut('',function() { 
					$('div.textTeaser').html( theText );
					if ( isMoreText && !$('div.textTeaser div.shortText a.crop').html() ) {
						$('div.textTeaser .shortText p').append(' <a class="crop" href="#">' + theWord + '</a>');
					}					
					
					$('div.textTeaser div.longText').hide();
					$('div.textTeaser div.shortText a.crop').bind('click', function() {
						$('div.textTeaser div.shortText').hide();
						$('div.textTeaser div.longText').show();
						return false;
						});
					if ( $('div.textTeaser a.more.toPage').html() ) {
						$('div.textTeaser a.more.toPage').show();
						$('div.textTeaser a.crop').hide();
						$('div.textTeaser a.more.toPage').addClass('crop');
					}		
					if ( theImages ) $('div.textTeaser div.csc-textpic-imagewrap:first').remove();
					$('div.textTeaser').fadeIn( );
					} );	
				},
			error: function(error) {
				$(THIS).removeClass('active');
				$(THIS).removeClass('loading');
				}	
			} );
		return false;
		});
	$('ul.section.jsOn li').each( function() {
				isShort = $(this).find('a').hasClass('shortcut');
				if ( isShort ) {
					$(this).find('div.title').unbind();
				}
		} );	
	//$('ul.section li div.title:first').trigger('click');		
	
	
	/* js menu */	
	var tHeight = ( $('#nav .hlist ul li:first a').height() * 2 + 20 ) + 'px';
	$('#nav .hlist').removeClass('out');
	$('#nav').css('height',tHeight);	
	var oHeight = $('#nav').height() + 'px';
	var teaserHeight = $('#teaser').height() + 'px';
	var oHeightPlus = $('#nav').height() - ( $('#nav .hlist ul li:first a').height() * 2 + 20 );
	var maxTeaserHeight = $('#teaser').height();
	var newHeight = 0;
	
		//set the height if teaser is bigger
		if ( oHeightPlus < maxTeaserHeight ) {
			oHeight = maxTeaserHeight + ( $('#nav .hlist ul li:first a').height() * 2 + 20 );
			newHeight = oHeight;
			$('#nav .hlist').css('height',oHeight + 'px');
		}
		
		//set the height of all li of the max
		maxHeight = 0;
		$('#nav .hlist ul li').each( function() { 
			mHeight = $(this).height(); 
			if (mHeight > maxHeight) maxHeight = mHeight;
			});
		if ( newHeight ) maxHeight = newHeight;	
		$('#nav .hlist ul li').css('height',maxHeight + 'px');	
		$('#nav .hlist ul li ul li').css('height','auto');	//reset childs		
		
	$('#nav .hlist ul').bind('mouseenter', function() {	
		$('#nav .hlist').addClass('out');
		$('#media').cycle('pause');
		$('#nav').stop(true); $('#teaser').stop(true);
		$('#nav').animate({'height':oHeight} );
		$('#teaser').animate({'height':'0px'} );
	} );
	$('#nav').bind('mouseleave', function() {
			$('#nav .hlist').removeClass('out');	
			$('#media').cycle('resume');
			$('#nav').stop(true); $('#teaser').stop(true);
			$('#teaser').animate({'height':teaserHeight} );			
			$('#nav').animate({'height':tHeight} );	
	} );	
		
	
$('#media').css('height',$('#media img:first').height() + 'px');	
$('#media.loadByAjax').after('<div id="mNav"></div>');
	var mode = 'scrollUp';
	if ( $('#media').hasClass('loadByAjax') ) {		
			$.ajax({
				type: 'GET',
				url: window.location.href,
				data: 'type=71629',
				dataType: 'xml',
				success: function(xml) {
					iA = $(xml).find('media>img').length;
					iN = 0;
					//alert(iA);
					$(xml).find('media>img').each(
						function() {
							$('<img />')
								.attr('src',    $(this).attr('src'   ))
								.attr('width',  $(this).attr('width' ))
								.attr('height', $(this).attr('height'))
								.attr('alt',    $(this).attr('alt'   ))
								.attr('title',  $(this).attr('title' ))
								.load(function(){
									iN ++;							
									// static: new images will be appended to the bottom of the list
									$('#media').append($(this));
									// random: new images will be added randomly to the list 
									// $('#media>img:nth-child(' + Math.ceil(Math.random() * $('#media>img').length) + ')').after($(this));							
									if (iN == iA) $('#media').cycle(
										{
											fx:     'fade',
											timeout: 7000,
											speed:   2500,
											pager:  '#mNav',
											random:  0      // does not work with with $.cycle.lite.min.js
															// but we don't care, because the preloading would mix up and brake the slideshow anyways
										}
									);
								});
						}
					);			
				}
			});
	
	};
	
	if ( $('#media').hasClass('scrollHoriz') ) { 
		$('#teaser div.teaser').addClass('scroll');
		mWidth = 0;
		var iA = $('#media.scrollHoriz img').length;
		var i = 0;
		$('#media.scrollHoriz img').each( function() {
			mWidth += $(this).width();
			if (i<=iA) $(this).clone().appendTo($('#media.scrollHoriz'));
			i++;
			if ( iA == i) { 				
				$('#media.scrollHoriz').css('width', (mWidth*2) + 'px');	
				//scrollLeft();				
				}
			} );
	};	
	var scrolling = false;
	function scrollLeft(  ) {
		if ( !scrolling ) return false;
		firstImgWidth = $('#media.scrollHoriz img:first').width();
		moveLeft = '-=' + firstImgWidth + 'px';
		$('#media.scrollHoriz').animate({'left':moveLeft},'slow', function() {
			$('#media.scrollHoriz img:last').after(	$('#media.scrollHoriz img:first') );
			$('#media.scrollHoriz').css('left','0px');
			if (scrolling) scrollLeft( scrolling );
			}).delay(2000);
	}
	$('.teaser #media.scrollHoriz').bind('mouseenter',function() { scrolling = true; scrollLeft( ); });
	$('.teaser #media.scrollHoriz').bind('mouseleave',function() { scrolling = false; });	
	
});

