/* Fader.js 
 * 
 * Fading in an out images in der airtours.de header
 *
 * Copyright (C) 2007 - Lars Seinschedt - Neusta GmbH Bremen <l.seinschedt@neusta.de>
 *
 */

Fader = function(){
	// Poperties
	rotationDivId = "fadingDiv";
	rotationDivWidth = "228";
	rotationDivHeight = "110";
	speed = 2// 1-10 1 == fast /// 10 == slow;
	
	// global used vars.
	images = arguments;
	rotationDiv = document.getElementById(rotationDivId);
	rotationSpeed = speed * 1000
	if(navigator.appName == "Microsoft Internet Explorer"){
		browserTiming = 50;
	}else{
		browserTiming = 0; // was 20
	}
	
	init = function(){
		rotationDiv.style.position = "relative";
		rotationDiv.style.width = rotationDivWidth + "px";
		rotationDiv.style.height = rotationDivHeight + "px";
		z = 100;
		for(var i=0; i<=images.length; i++){
			var div = document.createElement("div");
			div.setAttribute("id", "fadingImage_" + i);
			div.style.height = rotationDivHeight + "px";
			div.style.width = rotationDivWidth + "px";
			div.style.position = "absolute";
			div.style.top = 0;
			div.style.left = 0;
			if(i == images.length){
				background = images[0];	
			}else{
				background = images[i]
			}
			div.style.backgroundImage = 'url(' + background + ')';
			div.style.zIndex = z;
			z--;
			
			rotationDiv.appendChild(div);
		}								
		window.setTimeout("rotate(0)", rotationSpeed);
	}
	
	rotate = function(i){		
		if(i == images.length){
			i = 0;
			for(var j=0; j<images.length; j++){
				alpha(document.getElementById("fadingImage_" + j), 1);
			}
		}
		imageDiv = document.getElementById("fadingImage_" + i);
		window.setTimeout("fade(imageDiv, " + i + ", 1)", rotationSpeed);	
	}
	
	fade = function(elm, i, j){				
		window.setTimeout(function(){
			alpha(elm, (100 - (j * 5)) / 100);
			j++;
			
			if(j < 21){
				fade(elm, i, j);
			}else{
				i++;
				rotate(i);
			}
		}, browserTiming);
	}
	
	alpha = function(elm, value){
		if(navigator.appName == "Microsoft Internet Explorer"){
			elm.style.filter = "Alpha(opacity=" + (value * 100) + ")";
		}else{
			elm.style.MozOpacity = value;
		}
	}
	
	init();	
}

