/**
 * jQuery.Preload
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 * Date: 3/12/2008
 *
 * @projectDescription Multifunctional preloader
 * @author Ariel Flesler
 * @version 1.0.7
 *
 * @id jQuery.preload
 * @param {String, jQuery, Array< String, <a>, <link>, <img> >} original Collection of sources to preload
 * @param {Object} settings Hash of settings.
 *
 * @id jQuery.fn.preload
 * @param {Object} settings Hash of settings.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * @example Link Mode:
 *	$.preload( '#images a' );
 *
 * @example Rollover Mode:
 *	$.preload( '#images img', {
 *		find:/\.(gif|jpg)/,
 *		replace:'_over.$1'
 *	});
 *
 * @example Src Mode:
 *	$.preload( [ 'red', 'blue', 'yellow' ], {
 *		base:'images/colors/',
 *		ext:'.jpg'
 *	});
 *
 * @example Placeholder Mode:
 *	$.preload( '#images img', {
 *		placeholder:'placeholder.jpg',
 *		notFound:'notfound.jpg'
 *	});
 *
 * @example Placeholder+Rollover Mode(High res):
 *	$.preload( '#images img', {
 *		placeholder:true,
 *		find:/\.(gif|jpg)/,
 *		replace:'_high.$1'
 *	});
 */
;(function( $ ){

	var $preload = $.preload = function( original, settings ){
		if( original.split )//selector
			original = $(original);

		settings = $.extend( {}, $preload.defaults, settings );
		var sources = $.map( original, function( source ){
			if( !source )
				return;//skip
			if( source.split )//URL Mode
				return settings.base + source + settings.ext;
			var url = source.src || source.href;//save the original source
			if( typeof settings.placeholder == 'string' && source.src )//Placeholder Mode, if it's an image, set it.
				source.src = settings.placeholder;
			if( url && settings.find )//Rollover mode
				url = url.replace( settings.find, settings.replace );
			return url || null;//skip if empty string
		});

		var data = {
			loaded:0,//how many were loaded successfully
			failed:0,//how many urls failed
			next:0,//which one's the next image to load (index)
			done:0,//how many urls were tried
			//found:false,//whether the last one was successful
			total:sources.length//how many images are being preloaded overall
		};

		if( !data.total )//nothing to preload
			return finish();

		var imgs = '<img/>',//ensure one
			thres = settings.threshold;//save a copy

		while( --thres > 0 )//it could be oddly negative
			imgs += '<img/>';
		imgs = $(imgs).load(handler).error(handler).bind('abort',handler).each(fetch);

		function handler( e ){
			data.found = e.type == 'load';
			data.image = this.src;

			var orig = data.original = original[this.index];
			data[data.found?'loaded':'failed']++;
			data.done++;
			if( settings.placeholder && orig.src )//special case when on placeholder mode
				orig.src = data.found ? data.image : settings.notFound || orig.src;
			if( settings.onComplete )
				settings.onComplete( data );
			if( data.done < data.total )//let's continue
				fetch( 0, this );
			else{//we are finished
				if( imgs.unbind )//sometimes IE gets here before finishing line 84
					imgs.unbind('load').unbind('error').unbind('abort');//cleanup
				imgs = null;
				finish();
			}
		};
		function fetch( i, img, retry ){
			if( $.browser.msie && data.next && data.next % $preload.gap == 0 && !retry ){//IE problem, can't preload more than 15
				setTimeout(function(){ fetch( i, img, true ); }, 0);
				return false;
			}
			if( data.next == data.total ) return false;//no more to fetch
			img.index = data.next;//save it, we'll need it.
			img.src = sources[data.next++];
			if( settings.onRequest ){
				data.image = img.src;
				data.original = original[data.next-1];
				settings.onRequest( data );
			}
		};
		function finish(){
			if( settings.onFinish )
				settings.onFinish( data );
		};
	};

	// each time we load this amount and it's IE, we must rest for a while, make it lower if you get stack overflow.
	$preload.gap = 14;

	$preload.defaults = {
		threshold:2,//how many images to load simultaneously
		base:'',//URL mode: a base url can be specified, it is prepended to all string urls
		ext:'',//URL mode:same as base, but it's appended after the original url.
		replace:''//Rollover mode: replacement (can be left empty)
		/*
		find:null,//Rollover mode: a string or regex for the replacement
		notFound:''//Placeholder Mode: Optional url of an image to use when the original wasn't found
		placeholder:'',//Placeholder Mode: url of an image to set while loading
		onRequest:function( data ){ ... },//callback called every time a new url is requested
		onComplete:function( data ){ ... },//callback called every time a response is received(successful or not)
		onFinish:function( data ){ ... }//callback called after all the images were loaded(or failed)
		*/
	};

	$.fn.preload = function( settings ){
		$preload( this, settings );
		return this;
	};

})( jQuery );













/**
 * Begin Action Heroteaser
 * hinzugefuegt am 20.04.2009 TB hmmh
 */

var start_rotation = 0;
var startclick_interval = 7000;
var rotation_X = 0;
var rotations_interval = 5000;
var rotation_aktiv;
var new_start;
var last_rotations_id = 0;
var rotations_elemente = 0;

//function image_preloader(element){
//	var preload_array = $(element).findobject('.teaser-image');
//	$(element).find('.teaser-preload').css('display','block');
//	var prZ = 0;
//	var preload_val_array = new Array();
//	$.each( preload_array, function(i,imageurl){
//		preload_val_array[prZ] = $(preload_array[prZ]).relarray().url;
//		prZ = prZ + 1;
//	});
//
//	$.preload( preload_val_array, {
//		base:'',
//		ext:'',
//		onFinish:function(){
//			$('.teaser-preload').fadeOut(1000);
//		}
//	});
//}

function image_preloader(element){
	 var preload_array = $(element).findobject('.teaser-image');
	 $(element).find('.teaser-preload').css('display','block');
	 var prZ = 0;
	 var preload_val_array = new Array();
	 var first_image = '';
	 $.each( preload_array, function(i,imageurl){
	     if(prZ < 1){
	          first_image = $(preload_array[prZ]).relarray();
	        }
	  preload_val_array[prZ] = $(preload_array[prZ]).relarray().url;
	  prZ = prZ + 1;
	 });
	 $.preload( preload_val_array, {
	  base:'',
	  ext:'',
	  onFinish:function(){
	     $('#heroteaser-image').append('<a href="' + first_image.link + '" class="big-image image-selected"><img class="big-image image-selected" style="left: 0px; height: 285px; width: 970px;" src="' + first_image.url + '" alt=""/></a>');
	           $('.teaser-preload').fadeOut(1000);
	  }
	 });
}

function hero_rotation(image_url,link_url){
	var animatetime = 250;
	$('img.to-remove').remove();
	$('a.to-remove').remove();

  	$('img.big-image').addClass('to-remove').removeClass('image-selected');
  	$('a.big-image').addClass('to-remove').removeClass('image-selected');

  	$('#heroteaser-image').append('<a href="' + link_url + '" class="big-image image-selected"><img class="big-image image-selected" style="left: 1170px; height: 285px; width: 970px;" src="' + image_url + '" alt=""/></a>');

	$('img.to-remove').stop(true,true)
  		.animate({left: "-970", width: "0", height: "285"},1400)
  		;
  	/*$('img.image-selected').stop()
  		.animate({left: "-60", width: "1040", height: "285"},400)
  		.animate({left: "55"},animatetime)
  		.animate({left: "-20", width: "1000", height: "285"},animatetime)
  		.animate({left: "15"},animatetime)
  		.animate({left: "-5", width: "980", height: "285"},animatetime)
  		.animate({left: "2"},animatetime)
  		.animate({left: "0", width:"970", height: "285"},animatetime)
  		;*/
  	$('img.image-selected').stop().animate({left: "0", width: "970", height: "285"},400);
}

function hero_thumb_rotation(){

}

function hero_auto_rotation(set_rotation_X){
   	var rotations_array = $('#heroteaser-image').findobject('.teaser-image');
   	rotations_elemente = rotations_array.length;

	$('#heroteaser-image').find('.teaser-image').removeClass('aktiv-teaser-image');
   	$('#heroteaser-image').find('.teaser-image').removeClass('aktiv-teaser-bg');
   	$(rotations_array[rotation_X]).addClass('aktiv-teaser-bg');


	if(start_rotation > 0)
		var rotations_active_image_url = $(rotations_array[rotation_X]).relarray();

   		//hero_rotation($(rotations_array[rotation_X]).attr('rel'));
   		if(rotations_active_image_url)
   			hero_rotation(rotations_active_image_url.url,rotations_active_image_url.link);


	rotation_X = parseInt(rotation_X) + parseInt(1);

	if(rotation_X >= rotations_elemente)
		rotation_X = 0;

	rotation_aktiv = setTimeout("hero_auto_rotation()",rotations_interval);

	start_rotation = 1;
}

$(document).ready(function(){
  if($('#heroteaser-image div.heroteaser-content a.teaser-image').exists()){
  	 image_preloader('#heroteaser-image');
	 /**
	 * Setzt die Rotation der Bilder in Bewegung
	 */
	 var rotations_active_image_url;
	 hero_auto_rotation();

	 /*
	 * Setzt die Teaser-Thumbs ueber das Hauptbild, ermittelt die Gesamtbreiter aller Thumbs,
	 * setzt diese in die Mitte des Hauptbilds und macht sie dann sichtbar
	 */
	 $('#heroteaser-image').find('div.heroteaser-content').css('visibility','hidden');
	 var heroteaser_elemente = $('#heroteaser-image').findobject('.teaser-image');
	 var heroteaser_elemente_width = 134;
	 var heroteaser_content_width = heroteaser_elemente_width * heroteaser_elemente.length ;
	 $('#heroteaser-image div.heroteaser-content').css('width',heroteaser_content_width);
	 $('#heroteaser-image div.heroteaser-content').css('height',$('#heroteaser-image').find('a.teaser-image').height() + 6);
	 $('#heroteaser-image').find('div.heroteaser-content').css('visibility','visible');

	 /**
	 * Wenn Teaser-Thumbs vorhanden sind, koennen diese zur Rotation des Hauptbilds per Mouseklick genutzt werden.
	 */
	  $('#heroteaser-image').find('.teaser-image').click(function(){

	  		$('#heroteaser-image').find('.teaser-image').removeClass('aktiv-teaser-bg');
	  		$('#heroteaser-image').find('.teaser-image').removeClass('aktiv-teaser-image');
			//$(this).effect('bounce');
			$(this).addClass('aktiv-teaser-image');
			var active_image_url = $(this).relarray();
			hero_rotation(active_image_url.url,active_image_url.link);

		   	window.clearTimeout(rotation_aktiv);
		   	window.clearTimeout(new_start);

			if($('#heroteaser-image div.heroteaser-content a.aktiv-teaser-image').exists()){
				rotation_X = parseInt($(this).attr('rev')) + parseInt(1);
		   	 	new_start = window.setTimeout(function(){ hero_auto_rotation(rotation_X);}, startclick_interval);
				if(rotation_X >= rotations_elemente){rotation_X = 0;}
		   	 }

	  });

  }
});
 /**
 * End Action Heroteaser
 */

function ht_init() {
	$('#heroteaser').css('position', 'relative');
	$('#heroteaser').css('height', '305px');
	$('#heroteaser').css('width', '980px');
	$('#heroteaser').html('<img id="ht_big_1" src="" alt="" \/><img id="ht_big_2" src="" alt="" \/>');
	$('#heroteaser').append('<img id="ht_small_1_1" src="" alt="" \/><img id="ht_small_1_2" src="" alt="" \/>');
	$('#heroteaser').append('<img id="ht_small_2_1" src="" alt="" \/><img id="ht_small_2_2" src="" alt="" \/>');
	$('#ht_big_1, #ht_big_2, #ht_small_1_1, #ht_small_1_2, #ht_small_2_1, #ht_small_2_2').css('overflow', 'hidden');
	$('#ht_big_1, #ht_big_2, #ht_small_1_1, #ht_small_1_2, #ht_small_2_1, #ht_small_2_2').css('position', 'absolute');
	$('#ht_big_1, #ht_big_2, #ht_small_1_1, #ht_small_1_2, #ht_small_2_1, #ht_small_2_2').css('z-index', '1');
	$('#ht_big_1, #ht_big_2').css('bottom', '11px');
	$('#ht_big_1, #ht_big_2').css('left', '13px');
	$('#ht_big_1, #ht_big_2').css('height', '240px');
	$('#ht_big_1, #ht_big_2').css('width', '749px');
	$('#ht_big_1').css('z-index', '2');
	$('#ht_big_2').css('z-index', '3');
	$('#ht_small_1_1, #ht_small_1_2').css('right', '19px');
	$('#ht_small_1_1, #ht_small_1_2').css('bottom', '77px');
	$('#ht_small_2_1, #ht_small_2_2').css('right', '15px');
	$('#ht_small_2_1, #ht_small_2_2').css('bottom', '16px');
	$('#ht_small_1_1, #ht_small_1_2, #ht_small_2_1, #ht_small_2_2').css('height', '54px');
	$('#ht_small_1_1, #ht_small_1_2, #ht_small_2_1, #ht_small_2_2').css('width', '163px');
	$('#heroteaser').append('<img id="ht_overlay" src="' + overlay_src + '" alt="" \/>');
	$('#ht_overlay').css('position', 'absolute');
	$('#ht_overlay').css('top', '0');
	$('#ht_overlay').css('left', '0');
	$('#ht_overlay').css('height', $('#heroteaser').height());
	$('#ht_overlay').css('width', $('#heroteaser').width());
	$('#ht_overlay').css('z-index', '5');

	$('#ht_big_1').attr('src', ht_image[1]);
	$('#ht_big_2').attr('src', ht_image[0]);
	$('#ht_small_1_1').attr('src', ht_image[2]);
	$('#ht_small_1_2').attr('src', ht_image[1]);
	$('#ht_small_2_1').attr('src', ht_image[0]);
	$('#ht_small_2_2').attr('src', ht_image[2]);

	fade_diff = fade_interval - fade_time;
	shown_image = 1;
	shown_image_1 = 2;
	shown_image_2 = 0;
	active_image = 2;
	window.setTimeout("ht_run_1();", fade_interval);
}

function ht_load() {
	$('#heroteaser').html('<img id="ht_overlay" src="' + overlay_src + '" alt="" style="visibility:hidden;" \/>');
	for (var i = 0; i <= ht_image.length; i++) {
		$('#heroteaser').append('<img id="ht_image_' + i + '" src="' + ht_image[i] + '" alt="" style="display:none;" \/>');
	}
	$('#ht_image_2').load(function() {
		ht_init();
	});
}

function ht_run_1() {
	if (active_image == 2) {
		$('#ht_big_2, #ht_small_1_2, #ht_small_2_2').fadeOut(fade_time);
	}
	else {
		$('#ht_big_2, #ht_small_1_2, #ht_small_2_2').fadeIn(fade_time);
	}
	window.setTimeout("ht_run_2();", fade_time);
}
function ht_run_2() {
	$('#ht_big_' + active_image).attr('src', ht_image[shown_image]);
	$('#ht_small_1_' + active_image).attr('src', ht_image[shown_image_1]);
	$('#ht_small_2_' + active_image).attr('src', ht_image[shown_image_2]);
	shown_image++;
	shown_image_1++;
	shown_image_2++;
	if (shown_image >= ht_image.length) {
		shown_image = 0;
	}
	if (shown_image_1 >= ht_image.length) {
		shown_image_1 = 0;
	}
	if (shown_image_2 >= ht_image.length) {
		shown_image_2 = 0;
	}
	if (active_image == 2) active_image = 1;
	else active_image = 2;
	window.setTimeout("ht_run_1();", fade_diff);
}