/*------------------------------------------------------------------
	ImageFader.js v.1.0 2007/04/14 Daniel Schneider, d.schneider@hd7b.de
	this file is part of the xml-xsl-php framework (xxp-cms)
	copyright (c) 2007 HD7B-MEDIEN www.hd7b.de
  ----------------------------------------------------------------*/

ImageFader=function(imgAreaId, arrImages, imagePath, fadetime, delay, randomFlag){	
  this.imgArea=document.getElementById(imgAreaId);
  if(this.imgArea){       
    this.firstElementFaded=false;   
    this.arrImages=arrImages;
    this.imagePath=imagePath;
    this.fadetime=fadetime;
    this.delay=delay;
    this.currentIndex=-1;
    if(randomFlag==1) this.arrImages.shuffle();
    //objCollector
    this.objCollector=document.createElement("div");
    this.objCollector.setAttribute('id','collector');
    this.objCollector.style.display='block';
    this.objCollector.style.position='relative';
    this.objCollector.style.left='0px';
    this.objCollector.style.top='0px';  
		this.imgArea.appendChild(this.objCollector);   
    //objSwap1
		this.objSwap1=document.createElement("img");
		this.objSwap1.setAttribute('id','swap1');
		this.objSwap1.style.visibility='hidden';
    this.objSwap1.style.position='absolute';
    this.objSwap1.style.left='0px';
    this.objSwap1.style.top='0px';
    this.objSwap1.style.opacity=0;
    this.objSwap1.style.filter="alpha(opacity=0)";
		this.objCollector.appendChild(this.objSwap1);
    //objSwap2
		this.objSwap2=document.createElement("img");
		this.objSwap2.setAttribute('id','swap2');
		this.objSwap2.style.visibility='hidden';
    this.objSwap2.style.position='absolute';
    this.objSwap2.style.left='0px';
    this.objSwap2.style.top='0px';
    this.objSwap2.style.opacity=0;
    this.objSwap2.style.filter="alpha(opacity=0)";
		this.objCollector.appendChild(this.objSwap2);
  }
  else{
    return;
  }
  this.fadeTo=this.objSwap1;
  
	myFader=this;
	this._triggerLoad();
}
ImageFader.prototype._triggerLoad = function(){
	if(this.currentIndex+1 < this.arrImages.length) this.currentIndex++;
	else this.currentIndex=0;
	myFader=this;
  var tmpLoad=new Image();
  this.preLoad=tmpLoad;
  tmpLoad.onload=function() { myFader._startFade(); }
  tmpLoad.src=this.imagePath + this.arrImages[this.currentIndex];
	this._setDebug("load Started");
}
ImageFader.prototype._startFade = function(){
	this._setDebug("load Complete");
  this.fadeTo.style.opacity=0;
  this.fadeTo.style.filter="alpha(opacity=0)";
  this.fadeTo.src=this.preLoad.src;
  this.fadeTo.style.visibility="visible";
  this.nInt=40;
	this.nTime=0;
	myFader=this;
	if(this.fadeInterval) clearInterval(this.fadeInterval);
	this.fadeInterval=setInterval(function() { myFader._fade() }, this.nInt);
}
ImageFader.prototype._fade = function(){
	this.nTime+=this.nInt;
	var ieop=Math.round(this._easeInOut(this.nTime, 0, 1, this.fadetime) * 100);
	var op=ieop/100;
	this.fadeTo.style.opacity=op;
	this.fadeTo.style.filter="alpha(opacity="+ieop+")";
	if(this.firstElementFaded){
  	this.fadeFrom.style.opacity=1-op;
  	this.fadeFrom.style.filter="alpha(opacity="+(100-ieop)+")";
  }
	this._setDebug("fadeToId:" + this.fadeTo.id + " alpha:" + this.fadeTo.style.opacity + " time:" +this.nTime + " src:" + this.fadeTo.src);	
	if(this.nTime==this.fadetime){	
  
    var tmpFade=this.fadeTo;
    if(!this.firstElementFaded){
      this.firstElementFaded=true;
      this.fadeTo=this.objSwap2;
    }
    else{
      this.fadeFrom.style.visibility="hidden";
      this.fadeTo=this.fadeFrom;    
    }    
    this.fadeFrom=tmpFade;
    clearInterval(this.fadeInterval);
    myFader=this;
    setTimeout(function() { myFader._triggerLoad() },  this.delay);
	}
}
ImageFader.prototype._easeInOut=function(t,b,c,d){
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}
ImageFader.prototype._setDebug = function(debugInfo){
  if(this.debugMode) this.objDebug.innerHTML="<b>debug</b>: " + debugInfo;
}
Array.prototype.shuffle = function(){
  var tmp, rand;
  for(var i=0; i < this.length; i++){
    rand=Math.floor(Math.random()*this.length);
    tmp=this[i]; 
    this[i]=this[rand];
    this[rand]=tmp;
  }
}

