
var MyFader=function(){
	
	this.options=function(){
		this.currentPhoto=1;
		this.secondPhoto=2;
		this.fadeStep=20;
		this.pauseTimeout=3000; // pause in between transitions
		this.currentOpacity = new Array();
	}
	
	this.prepare=function(){
		var self=this;
		$('fadeStopButton').observe('click',function(event){
			self.stop();
		}); 
		$('fadeStartButton').observe('click',function(event){
			self.init();
		}); 
		
	}
	
	
	this.init=function(){
		this.options();
	
		// STOP IE FROM FLICKERING THE BACKGROUND IMAGE ON OPACITY CHANGE  :]
		// DAMN YOU INTERNET EXPLORER, DAMN YOU........
		try {
			document.execCommand('BackgroundImageCache', false, true);
		} catch(e) {}
	
		this.changed=false;	
		this.firstRun=true;
		this.fContainer=$('fader');
		this.fInner=$('innerFader');
		this.fTitle=$('title');
		this.doFading=true;
		this.images=[];
		this.imgTitles=[];
		
		this.images = this.fInner.getElementsByTagName('img');
		
		for(i=0;i<this.images.length;i++){
			this.currentOpacity[i+1]=0;
			this.imgTitles[i+1]=this.images[i].alt;
			this.images[i].style.display='none';
		}
		this.currentOpacity[this.currentPhoto]=99;

		this.fInner.style.background='transparent url('+this.images[this.currentPhoto-1].src+') no-repeat';
		this.fContainer.style.background='transparent url('+this.images[this.secondPhoto-1].src+') no-repeat';
		this.fTitle.style.display='inline';

		// START FADING :)
		this.doFade();	
		
		
	}
	
	this.doFade=function(){
		if(this.doFading){	
		 
			if(this.changed){
				clearTimeout(time);
			}
			
			var self=this;
			
			var  timeoutFunc=function(){ self.doFade(); }
			if(this.firstRun){
				time=setTimeout (timeoutFunc, this.pauseTimeout );
				this.firstRun=false;
			}else{
				time=setTimeout (timeoutFunc, 150 );
			}	

			this.currentOpacity[this.currentPhoto]-= this.fadeStep;
			this.currentOpacity[this.secondPhoto] += this.fadeStep;	
			this.fTitle.update(this.imgTitles[this.currentPhoto]);
	
			if(this.currentOpacity[this.secondPhoto]/100 >= 0.4) {
				this.fTitle.update('');
			}		
		
			if(this.currentOpacity[this.secondPhoto]/100>=0.98) {
				this.currentPhoto = this.secondPhoto;
				this.fTitle.update(this.imgTitles[this.currentPhoto]);
		
				clearTimeout(time);
				time=setTimeout ( timeoutFunc,this.pauseTimeout);
				this.secondPhoto++;
				this.fInner.style.background='transparent url('+this.images[this.currentPhoto-1].src+') no-repeat';	
				if(this.secondPhoto==(this.images.length+1)){this.secondPhoto=1}
				this.fContainer.style.background='transparent url('+this.images[this.secondPhoto-1].src+') no-repeat';
				this.fContainer.style.background='transparent url('+this.images[this.secondPhoto-1].src+') no-repeat';
				this.changed=true;
			}

			if(document.all) {
				this.fInner.style.filter = "alpha(opacity=" + this.currentOpacity[this.currentPhoto] + ")";
				this.fContainer.style.filter = "alpha(opacity=" + this.currentOpacity[this.secondPhoto] + ")";
			} else {
				this.fInner.style.MozOpacity =this. currentOpacity[this.currentPhoto]/100;
				this.fContainer.style.MozOpacity =this.currentOpacity[this.secondPhoto]/100;
			}
		}
	}
	
	this.stop=function(){
		this.doFading=false;	
	}
}