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

*/
var imagefader;
function Imagefader(){
	var containerID='contentwrapper';
	var timeout,showTimeout,opacity,ieFilters;
	var startTop=0;
	var showInterval=4200;
	var autoShow=false;
	var imageArray,autoShow;
	var currentPic=[];
	var currentKat=-1;
	var nextPic=[];
	this.start=function(){
		showTimeout=setTimeout('imagefader.showNextPic();',3600);
	}
	this.showNextPic=function(){
		if(showTimeout){
			clearTimeout(showTimeout);
		}
		currentKat++;
		if(currentKat>=imageArray.length){
			currentKat=0;
		}
		nextPic[currentKat]=currentPic[currentKat]+1;
		if(nextPic[currentKat]>=imageArray[currentKat].length){
			nextPic[currentKat]=0;
		}
		opacity=10;
		imageArray[currentKat][currentPic[currentKat]].style.zIndex=40;
		imageArray[currentKat][nextPic[currentKat]].style.zIndex=30;
		if(!ieFilters){
			imageArray[currentKat][currentPic[currentKat]].style.opacity=1;
			imageArray[currentKat][nextPic[currentKat]].style.opacity=0;
			removeClass(imageArray[currentKat][nextPic[currentKat]],'hidden');
			imagefader.fadeOut();
		}else{
			imageArray[currentKat][currentPic[currentKat]].style.filter=imageArray[currentKat][currentPic[currentKat]].style.filter.replace(/ alpha\(opacity=(\d+?)\)/i,'');
			imageArray[currentKat][currentPic[currentKat]].style.filter+=' alpha(opacity=100)';
			imageArray[currentKat][nextPic[currentKat]].style.filter=imageArray[currentKat][nextPic[currentKat]].style.filter.replace(/ alpha\(opacity=(\d+?)\)/i,'');
			imageArray[currentKat][nextPic[currentKat]].style.filter+=' alpha(opacity=0)';
			removeClass(imageArray[currentKat][nextPic[currentKat]],'hidden');
			imagefader.ieFadeOut();
		}
		//imagefader.fadeOut();
		//showTimeout=setTimeout('imagefader.shownextPic[currentKat]();',showInterval);
	}
	this.fadeOut=function(){
		var interval=40;
		if(timeout){
			clearTimeout(timeout);
		}
		if(opacity<=0){
			imagefader.imgSwitch();
		}else{
			opacity-=.5;
			imageArray[currentKat][currentPic[currentKat]].style.opacity=opacity/10;
			imageArray[currentKat][nextPic[currentKat]].style.opacity=1-opacity/10;
			timeout=setTimeout('imagefader.fadeOut();',interval);
		}
	}
	this.ieFadeOut=function(){
		//var interval=1000
		//imageArray[currentKat][currentPic[currentKat]].style.filter='progid:DXImageTransform.Microsoft.Fade(duration='+interval/1000+')';
		//imageArray[currentKat][currentPic[currentKat]].filters[0].Play();
		var interval=40;
		if(timeout){
			clearTimeout(timeout);
		}
		if(opacity<=0){
			imagefader.imgSwitch();
		}else{
			opacity--;
			imageArray[currentKat][currentPic[currentKat]].style.filter=imageArray[currentKat][currentPic[currentKat]].style.filter.replace(/ alpha\(opacity=(\d+?)\)/i,'');
			imageArray[currentKat][currentPic[currentKat]].style.filter+=' alpha(opacity='+opacity*10+')';
			imageArray[currentKat][nextPic[currentKat]].style.filter=imageArray[currentKat][nextPic[currentKat]].style.filter.replace(/ alpha\(opacity=(\d+?)\)/i,'');
			imageArray[currentKat][nextPic[currentKat]].style.filter+=' alpha(opacity='+(100-opacity*10)+')';
			timeout=setTimeout('imagefader.ieFadeOut();',interval);
		}
	}
	this.imgSwitch=function(){
		addClass(imageArray[currentKat][currentPic[currentKat]],'hidden');
		currentPic[currentKat]++;
		if(currentPic[currentKat]>=imageArray[currentKat].length){
			currentPic[currentKat]=0;
		}
		removeClass(imageArray[currentKat][currentPic[currentKat]],'hidden');
		showTimeout=setTimeout('imagefader.showNextPic()',showInterval);
	}
	this.prepare=function(){
		if (!document.getElementsByTagName) return false;
		if (!document.getElementById) return false;
		if (!document.getElementById(containerID)) return false;
		if(document.getElementsByTagName('img').length>0 && document.getElementsByTagName('img')[0].filters){
			ieFilters=true;
		}else{
			ieFilters=false;
		}
		var source;
		imageArray=new Array();
		var ulTags=document.getElementById(containerID).getElementsByTagName('ul');
		var katCount=0;
		for(var i=0; i<ulTags.length; i++){
			if(ulTags[i].className.indexOf('fader')>=0){
				var liTags=ulTags[i].getElementsByTagName('li');
				if(liTags.length>1){
					imageArray[katCount]=[];
					currentPic[katCount]=0;
					nextPic[katCount]=1;
					var width=ulTags[i].offsetWidth+'px';
					var height=ulTags[i].offsetHeight+'px';
					ulTags[i].style.position='relative';
					ulTags[i].style.width=width;
					ulTags[i].style.height=height;
					for (var j=0; j < liTags.length; j++){
						source = liTags[j].getElementsByTagName('img')[0].getAttribute('src');
						imageArray[katCount][j]=new Image();
						imageArray[katCount][j].src=source;
						imageArray[katCount][j]=liTags[j];
						imageArray[katCount][j].style.position='absolute';
						imageArray[katCount][j].style.top='0px';
						imageArray[katCount][j].style.left='0px';
						imageArray[katCount][j].style.width=width;
						imageArray[katCount][j].style.height=height;
	//					if(ieFilters){
	//						imageArray[katCount][j].style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+source+'", sizingMethod="crop")';
	//					}else{
	//						imageArray[katCount][j].style.backgroundImage='url('+source+')';
	//					}
	//					imageArray[katCount][j].getElementsByTagName('img')[0].parentNode.removeChild(imageArray[katCount][j].getElementsByTagName('img')[0]);
						imageArray[katCount][j].loaded=false;
						imageArray[katCount][j].onload=function(){
							this.loaded=true;
						}
						if(j>0){
							addClass(liTags[j],'hidden');
							removeClass(liTags[j],'noscript');
						}
					}
					katCount++;
				}
			//	break;
			}
		}
		if(imageArray.length>=1 && imageArray[0].length>1){
			showInterval=parseInt(showInterval/imageArray.length,10);
			imagefader.start();
		}
		return true;
	}
}
addLoadEvent(function(){
	imagefader=new Imagefader();
	imagefader.prepare();
});
