/*
         ___                         _____       __
        /  /\        ________       /    /\     / /\
(-----//  /  \//----/_____/__\\--) /____/  \___/_/ //--------------------------)
      /  /   /      \     | //     \    \  /    /\/    this code was created by
     /__/   /        \    |///-----\\____\/____/  \                       sigea
(---\\  \  /   (-----\\___|/        /    /\    \  /       
      \__\/                        /____/ /\____\/            __       sigea.ch
                                   \____\/  \  /             /\_\--------------)
                                         \___\//-------------\/_/  

*/
var fader;
function Fader(){
	var selectionID='headerimg';
	var placeholderNr=0;
	var placeholder,fadeImage,source,ieFilters;
	var opacity,timeout,showTimeout,imageArray,arraypos;
	var arraypos=2;
	var showInterval=4200;
	this.show=function(){
		source = imageArray[arraypos].src;
		placeholder = document.getElementById(selectionID).getElementsByTagName('div')[placeholderNr];
		placeholderNr=1-1*placeholderNr;
		fadeImage=placeholder.getElementsByTagName('img')[0];
		placeholder.style.backgroundImage='url('+source+')';
		opacity=100;
		if(ieFilters){
			fader.ieFadeOut();
//		}else if(fadeImage.style.opacity){
//			fader.fadeOut();
		}else{
			fader.fadeOut();
			//fader.imgSwitch();
		}
		return true;
	}
	var startShow=function(){
		showTimeout=setTimeout('fader.showNextPic();',3600);
	}
	this.showNextPic=function(){
		if(showTimeout){
			clearTimeout(showTimeout);
		}
		arraypos++;
		if(arraypos>=imageArray.length)
			arraypos=0;
		fader.show();
	}
	this.fadeOut=function(){
		var interval=40;
		if(timeout){
			clearTimeout(timeout);
		}
		if(opacity<=0){
			fader.imgSwitch();
		}else{
			opacity-=4;
			//opacity=Math.round(opacity);
			fadeImage.style.opacity=opacity/100;
			timeout=setTimeout('fader.fadeOut();',interval);
		}
	}
	this.ieFadeOut=function(){
		var interval=1000
		fadeImage.style.filter='progid:DXImageTransform.Microsoft.Fade(duration='+interval/1000+')';
		fadeImage.filters[0].Play();
		setTimeout('fader.imgSwitch();',interval);
	}
	this.imgSwitch=function(){
		fadeImage.src=source;
		if(ieFilters){
			fadeImage.style.filter='alpha(opacity=100)';
		}else if(fadeImage.style.opacity){
			fadeImage.style.opacity=1;
		}
		placeholder.style.backgroundImage='none';
		showTimeout=setTimeout('fader.showNextPic();',showInterval);
	}
	this.prepare=function(){
		if (!document.getElementsByTagName) return false;
		if (!document.getElementById) return false;
		if (!document.getElementById(selectionID)) return false;
		var imgs = document.getElementById(selectionID).getElementsByTagName('img');
		var j=0;
		var div=document.createElement('div');
		div.style.width=imgs[0].width+'px';
		div.style.height=imgs[0].height+'px';
		div.style.cssFloat='left';
		div.style.styleFloat='left';
		div.appendChild(imgs[0].cloneNode(true));
		document.getElementById(selectionID).replaceChild(div,imgs[0]);
		div=document.createElement('div');
		div.style.width=imgs[2].width+'px';
		div.style.height=imgs[2].height+'px';
		div.style.cssFloat='left';
		div.style.styleFloat='left';
		div.appendChild(imgs[2].cloneNode(true));
		document.getElementById(selectionID).replaceChild(div,imgs[2]);
		imageArray=new Array();
		for ( var i=0; i < imgs.length; i++){
			if(i!=1){
				imageArray[j]=new Image();
				imageArray[j].src=imgs[i].getAttribute('src');
				j++;
			}
		}
		if(imgs[0].filters)
			ieFilters=true;
		else
			ieFilters=false;
		if(imageArray.length>2)
			startShow();
		return true;
	}
}
function prepareFader(){
	fader=new Fader();
	fader.prepare();
}
addLoadEvent(prepareFader);

function addLoadEvent(func){
	if(window.addEventListener){
		window.addEventListener('load',func,false);
	}else if(window.attachEvent){
		window.attachEvent('onload',func);
	}else{
		var oldonload = window.onload;
		if (typeof window.onload != 'function'){
			window.onload = func;
		}else{
			window.onload = function(){
				oldonload();
				func();
			}
		}
	}
}
