function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if( document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec,steps) {
  if (document.all && navigator.userAgent.indexOf("Opera")==-1) {
    document.images.blendID.style.filter="blendTrans(duration="+millisec/1000+")";
    document.images.blendID.filters.blendTrans.Apply();
    document.images.blendID.src = imagefile;
    document.images.blendID.filters.blendTrans.Play();
  }
  else{
	  var speed = Math.round(millisec / steps);
	  var timer = 0;
  	
	  //set the current image as background
	  document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
  	
	  //make image transparent
	  changeOpac(0, imageid);
  	
	  //make new image
	  document.getElementById(imageid).src = imagefile;

	  //fade in image
	  for(i = 0; i <= steps; i++) {
		  setTimeout("changeOpac(" + i * 100/steps + ",'" + imageid + "')",(i * speed));
	  }
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

Blend = function (images , blendtime, waittime, steps, divid){
  this.images_=new Array();
  this.steps_=steps;
  for (i = 0; i < images.length; i++) {
    this.images_[i] = new Image();
    this.images_[i].src = images[i];
  }
  this.blendTime_=blendtime;
  this.waittime_=waittime;
  this.div_=document.getElementById(divid);
  this.div_.innerHTML="";
  this.imgCtr_=0;
  this.div_.style.backgroundImage="url("+this.images_[this.imgCtr_].src+")";
  this.div_.style.backgroundRepeat="no-repeat";
  this.div_.style.width=this.images_[0].width;
  this.div_.style.height=this.images_[0].height;

  this.img_=document.createElement("img");
  this.img_.id="blendID";
  this.div_.appendChild(this.img_);
  this.img_.src=this.images_[this.imgCtr_].src;
  this.img_.style.border="none";
  this.img_.style.width=this.images_[0].width;
  this.img_.style.height=this.images_[0].height;
  if(!document.all){
    changeOpac(0,this.img_.id);
   }
  var tmp=this;
	setTimeout(function(){ tmp.NextBlend(); },this.waittime_);
}


Blend.prototype.NextBlend=function(){
  this.imgCtr_++;
  if(this.imgCtr_==this.images_.length)
    this.imgCtr_=0;
  blendimage(this.div_.id, this.img_.id, this.images_[this.imgCtr_].src, this.blendTime_,this.steps_);
  var tmp=this;
	setTimeout(function(){ tmp.NextBlend(); },this.waittime_);
}
