/*********************************************************
 * Author: Marko Roeper <marko.roeper@googlemail.de>     *
 * All Rights Reserved                                   *
 *********************************************************/

// ... kein $ wegen Parallelbetrieb zu Prototype
jQuery(document).ready(function()
{
	// Silbentrennung starten
	Hyphenator.run();

});

jQuery(window).load(function()
{
	// Keine History Einträge => kein JS Zurück Link 
	if(history.length<2) { jQuery('a.jsback').remove(); }

	var orig_search_width = parseInt(jQuery('#search_text').css('width'))/2;

	/* Umformatierungen, falls JS zur Verfuegung steht */
	jQuery('#search_text').css('width', orig_search_width+'px');
	jQuery('#search form').css('padding-bottom', '3px');
	
	// Suchfeld vergroessern bei Focus
	jQuery('#search_text').focus(function()
	{
		jQuery(this).css('width', orig_search_width*2+'px');
	}).blur(function()
	{
		jQuery(this).css('width', orig_search_width+'px');
	});
	
	// Teaser Overlay
	if(jQuery('#teaser_overlay').children().length>0)
	{
		if(jQuery('#teaser ul').children().length>0) 
		{
			jQuery('#teaser_overlay').animate({ 
	    	    left: '0px'
			}, 1000 );
	
			jQuery('#landing_teaser').mouseenter(function()
			{
				jQuery('#teaser_overlay').animate({ 
		    	    left: '-610px'
				}, 300 );
			}).mouseleave(function()
			{
				jQuery('#teaser_overlay').animate({ 
		    	    left: '0px'
				}, 1000 );
			});
		}
		else 
		{
			jQuery('#teaser_overlay').css({ 
	    	    left: '0px'
			});
		}
	}

	// Timeline
	if(jQuery('#timeline').children().length>0)
	{
		var timeLineWidth = 0;
		var timeLineDistance = 300;
		var timeLineDuration = 700;
		jQuery('#timeline li').each(function()
		{
			timeLineWidth += parseInt(jQuery(this).width());
		});
		var minLeft = parseInt(jQuery('#timeline ul').css('left'));
		var currentLeft = minLeft;
		var nextLeft = 0;
		var maxLeft = (timeLineWidth-820)*(-1);

		jQuery('#timeline').css('overflow-x','hidden');
		jQuery('#timeline ul').css('margin-left','30px').css('width',timeLineWidth+'px');
		jQuery('#tl_back').css('display','block').click(function(e)
		{
			e.preventDefault();

			currentLeft = parseInt(jQuery('#timeline ul').css('left'));
			nextLeft = currentLeft + timeLineDistance;
			if(nextLeft > minLeft) nextLeft = minLeft;

			jQuery('#timeline ul').animate(
			{
				left: nextLeft+'px'
			}, timeLineDuration);
		});
		jQuery('#tl_forward').css('display','block').click(function(e)
		{
			e.preventDefault();
			
			currentLeft = parseInt(jQuery('#timeline ul').css('left'));
			nextLeft = currentLeft - timeLineDistance;
			if(nextLeft < maxLeft) nextLeft = maxLeft;

			jQuery('#timeline ul').animate(
			{
				left: nextLeft+'px'
			}, timeLineDuration);
		});
	}
	
	// Galerien
	var galleryIndex = 0;
	var galleryHeight = 0;
	var galleryCurrentLeft = new Array();
	var galleryWidth = new Array();
	var galleryPhotosWidth = new Array();
	var galleryDuration = 400;
	var galleryMinLeft = 0;
	var galleryMaxLeft = new Array();
	var galleryLength = new Array();
	var galleryHeadline = '';

	jQuery('.gallery').each(function()
	{
		// Jede Gallerie erhält eine eindeutige ID
		jQuery(this).attr('id', 'gal_'+galleryIndex).css({'padding-left' : 0});
		
		// Überschrift umplatzieren
/* 		jQuery('#gal_'+galleryIndex+' :header').insertAfter(jQuery('#gal_'+galleryIndex+' .photos')).css({'padding' : '5px 30px 0 30px'}); */
		
		// Überschrift entfernen
		jQuery('#gal_'+galleryIndex+' :header').remove();

		// Berechnungen (Breite, Höhe, Anzahl, Laufweite)
		galleryPhotosWidth[galleryIndex] = parseInt(jQuery('#gal_'+galleryIndex+' .photos').width());
		galleryLength[galleryIndex] = parseInt(jQuery('#gal_'+galleryIndex+' li').length);
		jQuery('#gal_'+galleryIndex+' .photos').before('<a class="gal_back" href="#">&laquo; zurück</a>').after('<a class="gal_forward" href="#">weiter &raquo;</a>').css({'overflow-x' : 'hidden'});
		galleryWidth[galleryIndex] = galleryLength[galleryIndex] * galleryPhotosWidth[galleryIndex];
		galleryHeight = parseInt(jQuery('#gal_'+galleryIndex+' .photos').height());
		galleryMaxLeft[galleryIndex] = (galleryWidth[galleryIndex]-galleryPhotosWidth[galleryIndex])*(-1);

		// Umformatierungen (Breite, Höhe, Positionierung)
		jQuery('#gal_'+galleryIndex+' .photos').css({'height' : (galleryHeight+100)+'px'});
		jQuery('#gal_'+galleryIndex+' ul').css({'width' : galleryWidth[galleryIndex]+'px', 'position' : 'absolute', 'top' : 0, 'left' : 0});

		// Blätterpfeile und -aktionen definieren
		if(galleryLength[galleryIndex]>1)
		{
			jQuery('#gal_'+galleryIndex+' .gal_back').attr('id','back_'+galleryIndex).css({'height' : galleryHeight+'px', 'visibility' : 'visible'}).click(function(e)
			{
				e.preventDefault();
				var galleryIndex = parseInt(jQuery(this).attr('id').replace(/back_/,''));
				galleryCurrentLeft[galleryIndex] = parseInt(jQuery('#gal_'+galleryIndex+' ul').css('left'));
				nextLeft = galleryCurrentLeft[galleryIndex] + galleryPhotosWidth[galleryIndex];
				if(nextLeft > 0) nextLeft = 0;
				jQuery('#gal_'+galleryIndex+' ul').animate({	left: nextLeft+'px'	}, galleryDuration);
			});
			jQuery('#gal_'+galleryIndex+' .gal_forward').attr('id','forward_'+galleryIndex).css({'height' : galleryHeight+'px', 'visibility' : 'visible'}).click(function(e)
			{
				e.preventDefault();
				var galleryIndex = parseInt(jQuery(this).attr('id').replace(/forward_/,''));
				galleryCurrentLeft[galleryIndex] = parseInt(jQuery('#gal_'+galleryIndex+' ul').css('left'));
				nextLeft = galleryCurrentLeft[galleryIndex] - galleryPhotosWidth[galleryIndex];
				if(nextLeft < galleryMaxLeft[galleryIndex]) nextLeft = galleryMaxLeft[galleryIndex];
				jQuery('#gal_'+galleryIndex+' ul').animate({ left: nextLeft+'px' }, galleryDuration);
			});
		}
		galleryIndex++;
	});
	

});
