/*
*
* Copyright (c) 2006 Millstream Web Software http://www.millstream.com.au
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* * 
*/

/*
* Class: Slideshow
* Written by: Andrew Tetlaw http://tetlaw.id.au
* Displays a cross-fading slideshow
* Option:default
* 	uri : [], // an array of image URIs
*		interval : 5, // seconds for each slide to be visible
*		duration : 1, // duration of fade efefct
*		fps : 50, // frames per second of slide effect; shouldn't need to adjust this
*		crossfade : true // if you don't want a cross-fade set this to false and it'll do a fade out - fade in , instead.
*/


	var fadeimages=new Array()
	
	// festlegen des Bilder-Array
	var preloadlist = new Array();
	var arraysize; 
	var halt; 
	
	// eigentliche Preload-Funktion
	function preload_img(arr)
	{
		// Übernahme der Bilderadressen
		//var img_list = preload_img.arguments;
		var img_list = arr;
		arraysize = img_list.length;
		
		// Bilder werden vom Browser aufgerufen
		for(var i=1; i < img_list.length; i++) {
			preloadlist[i] = new Image;
			preloadlist[i].src = img_list[i];
		}
		// die Kontrollfunktion wird aufgerufen
		preload_check(img_list[0]);
	}

	function preload_check(obj)
	{
		for(var i=1; i < preloadlist.length; i++) {
			if(preloadlist[i].complete == true) {
				preloadlist.splice(i, 1);
			}
		}
		if(preloadlist.length == 1) {
			//location.href="/de/{/literal}{$season}{literal}/slideshow";
			obj.initialize2();
		} else {
			setTimeout(function(){preload_check(obj)}, 100);
		}		
	}

	
	function hideLoader()
	{
		if(halt)
			return;
		var obj = document.getElementById("load_txt");
		var obj2 = document.getElementById("load_img");
		var step = 7;
		var start = obj.style.MozOpacity*1;
		var start2 = obj.style.filter;
		if(start=="" || start==0)
		{
			start = 1;
		}
		if(start2==undefined)
		{
			start = ((start*100)-step)/100;			
		}
		else
		{
			if(start2=="")
			{
				start2 = 100;
			}
			else
			{
				var pos = start2.indexOf("=")+1;
				var x = start2.substr(pos,3);
				start2 = start2.substr(pos,2);
				
			}
			start2 = start2-step;
		}
		
		obj.style.MozOpacity  = start;
		obj2.style.MozOpacity = start;
		
		
		if(start2 != undefined && start2 != "NaN")
		{		
			obj.style.filter  = "Alpha(opacity="+start2+")";
			obj2.style.filter = "Alpha(opacity="+start2+")";
		}
	
		if(start <= 0 || start2 <=1 || isNaN(start2))
		{
			if(isNaN(start2))
			{
				if(start<=0 || isNaN(start))
				{
					obj.style.MozOpacity = 0;
					obj2.style.MozOpacity = 0;
					obj.style.filter  = "Alpha(opacity=0)";
					obj2.style.filter = "Alpha(opacity=0)";
					window.clearTimeout(an);
					document.getElementById("load_div").style.display = "none";
					halt = true;
					return;
				}
			}
			else
			{
					obj.style.MozOpacity = 0;
					obj2.style.MozOpacity = 0;
					obj.style.filter  = "Alpha(opacity=0)";
					obj2.style.filter = "Alpha(opacity=0)";
					window.clearTimeout(an);
					document.getElementById("load_div").style.display = "none";
					halt = true;
					return;				
			}
		}

		var an = window.setTimeout("hideLoader()",200);
	}



var Slideshow = Class.create();

Slideshow.prototype = {
	initialize : function(elm, options) {
		this.elm = $(elm);
		this.counter = 0;
		this.loaded = false;
		this.options = Object.extend({
			uri : [],
			interval : 7,
			duration : 1,
			fps : 60,
			crossfade : true
		}, options || {})
		this.options.uri = $A(this.options.uri);
		pre_imgs[0] = this;
		preload_img(pre_imgs);
	},
	initialize2 : function(){
		if(FastInit) {
			//FastInit.addOnLoad(this.start.bind(this)); //<---- WTF?! W!T!F! 
			FastInit.addOnLoad(this.start());
		} else {
			Event.observe(window, 'load', this.start.bind(this));
		}
		
	},
	start: function() {
		//console.log('start... ' + this.options.uri.length + ' uris defined');
		var img = document.createElement('img');
		img.src = this.options.uri[0];
		//img.id = "the_ultimate_image";
    this.cycle();

		new PeriodicalExecuter(this.cycle.bind(this), this.options.interval); 
	},
	cycle: function() {
		var elm = this.elm;
		var opt = this.options;
		if(this.counter++ == opt.uri.length) {
			location.href=basepath+"de/"+theseason+"/2/aktuell";
			this.counter = 1
			this.loaded = true;
		}
		count = this.counter;
		var next = document.createElement('img');
		next.src = this.options.uri[count-1];
		//next.id="the_ultimate_image";
		if(!this.loaded) {
			var preload = document.createElement('img');
			preload.src = this.options.uri[count]; 
		}
		//console.log(next.src);
		if(this.options.crossfade) {
			var currentSlide;
			if(elm.firstChild) {
				currentSlide = elm.firstChild;
			} else {
				currentSlide = document.createElement('div');
				elm.appendChild(currentSlide);
			}
			var nextSlide = $(document.createElement('div'));
			nextSlide.className = 'slide-image';
			var bild = document.createElement("img");
			bild.src = next.src;
			nextSlide.appendChild(bild);
			//nextSlide.style.backgroundImage = 'url('+next.src+')';
			nextSlide.setOpacity(0);
			elm.appendChild(nextSlide);
			new Effect.Parallel([new Effect.Fade(currentSlide,{sync:true}),
				new Effect.Appear(nextSlide,{sync:true})],
				{duration: opt.duration, fps: opt.fps, afterFinish : function() {Element.remove(currentSlide)}}
			);
		} else {
			new Effect.Fade(elm, { 
				duration: opt.duration, 
				fps: opt.fps, 
				afterFinish: function() {
					elm.style.backgroundImage = 'url('+next.src+')';
					new Effect.Appear(elm, {
							duration: opt.duration,
							fps: opt.fps,
							queue:'end'
					});
				} 
			});
		}
		
		hideLoader();
	}
};

