// -------------------------------------------------------------------
// Drop-in Slideshow Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Last updated: March 9th, 2007
// -------------------------------------------------------------------

var _dropinslideshowcount=0
var _dropinslidesarray = new Array()

function dropinslideshow(imgarray, w, h, maxDelay, minDelay, idx){
	this.count=_dropinslideshowcount
	this.divID="_dropslide"+(++_dropinslideshowcount) //Generate unique ID for this slideshow instance (automated)
	this.imgID="_img"+this.divID
	this.w = parseInt(w)
	this.h = parseInt(h)
	this.createcontainer(this.w, this.h)

	this.minDelay = (parseInt(minDelay) < 1) ? 1 : parseInt(minDelay)
	this.maxDelay = (parseInt(maxDelay) <= this.minDelay) ? this.minDelay + 1 : parseInt(maxDelay)
	this.delaytime= this.rand(this.maxDelay, this.minDelay, -1)

	this.imgarray=imgarray
	var preloadimages=[]
	for (var i=0; i<imgarray.length; i++){
		preloadimages[i]=new Image()
		preloadimages[i].src=imgarray[i][0]
	}

	this.activecanvasindex=0          //Current "active" canvas- Two canvas DIVs in total
	this.activecanvasindex=0          //Current "active" canvas- Two canvas DIVs in total
	this.activeimgID=this.imgID+"_"+this.activecanvasindex
	this.inactiveimgID=this.imgID+"_"+(this.activecanvasindex+1)

	this.animatestartposY=this.h*(-1) //Starting "top" position of an image before it drops in
	this.slidedegreeY=5               //Slide degree (> is faster)
	this.slidedelay=50                //Delay between slide animation (< is faster)
	if (this.w>this.h) {
	  this.slidedegreeX=parseInt((this.w/this.h)*this.slidedegreeY)
	} else {
	  this.slidedegreeX=parseInt((this.h/this.w)*this.slidedegreeY)
	}

	if (!idx) {
	  this.curimageindex=this.rand(this.imgarray.length, 0, -1)
	  this.randomize = true
	} else {
	  this.curimageindex=parseInt(idx)
	  this.randomize = false
	}
	_dropinslidesarray[this.count]=this.curimageindex

	this.zindex=100
	this.zindexhold=this.zindex+1
	this.zindexstep=Math.round(this.h*this.slidedegreeY)

	this.isMouseover=0
	this.init()
}


dropinslideshow.prototype.createcontainer=function(w, h){
  document.write('<div id="'+this.divID+'" style="position:relative; text-align:left; width:'+w+'px; height:'+h+'px; overflow:hidden">')
	document.write('<div style="position:absolute; width:100%; height:'+h+'px; top:0;"></div>')
	document.write('<div style="position:absolute; width:'+w+'px; height:'+h+'px; top:-'+(h-5)+'px;"></div>')
	document.write('</div>')
	this.slideshowref=document.getElementById(this.divID)
	this.canvases=[]
	this.canvases[0]=this.slideshowref.childNodes[0]
	this.canvases[1]=this.slideshowref.childNodes[1]
}

dropinslideshow.prototype.populatecanvas=function(canvas, imageindex){
	var imageHTML='<img id="'+this.activeimgID+'" src="'+this.imgarray[imageindex][0]+'" border="0" alt="Hold" title="Hold" />'
	if (this.imgarray[imageindex][1]!="")
		imageHTML='<a href="'+this.imgarray[imageindex][1]+'" target="'+this.imgarray[imageindex][2]+'">'+imageHTML+'</a>'
	canvas.innerHTML=imageHTML
}

dropinslideshow.prototype.animateslide=function(){
	var ready=true
	if (this.zindexhold > this.zindex) {
	  this.zindex++
	  ready=false
	  var opstep=(this.zindexhold-this.zindex)/this.zindexstep
    //if (this.count==0) alert(opstep)
    changeOpac((opstep-((1-opstep)))*100, this.inactiveimgID)
    if (opstep < 55/100 && this.inactivecanvas.style.zIndex>this.activecanvas.style.zIndex) {
      this.inactivecanvas.style.zIndex=this.activecanvas.style.zIndex-1
    }
    changeOpac((1-(opstep*40/100))*100, this.activeimgID)

    this.zindex=this.zindex+opstep
	  if (this.zindex>this.zindexhold) this.zindex=this.zindexhold
	} else if(this.zindexhold == this.zindex) {
	  this.activecanvas.style.zIndex=this.zindex
    changeOpac(100, this.activeimgID)
    changeOpac(100, this.inactiveimgID)
	}
	
	if (this.curimageposY<0){ //if image hasn't fully dropped in yet
		this.curimageposY=this.curimageposY+this.slidedegreeY
		if (this.curimageposY>=0) {
		  this.curimageposY=0
		  this.curimageposX=0
		  this.activecanvas.style.left=this.curimageposX+"px"
		}
		this.activecanvas.style.top=this.curimageposY+"px"
		ready=false
	} else if (this.curimageposY>0){
		this.curimageposY=this.curimageposY-this.slidedegreeY
		if (this.curimageposY<=0) {
		  this.curimageposY=0
		  this.curimageposX=0
		  this.activecanvas.style.left=this.curimageposX+"px"
		}
		this.activecanvas.style.top=this.curimageposY+"px"
	  ready=false
	}
	if (this.curimageposX<0){ //if image hasn't fully dropped in yet
		this.curimageposX=this.curimageposX+this.slidedegreeX
		if (this.curimageposX>=0) {
		  this.curimageposX=0
		  this.curimageposY=0
		  this.activecanvas.style.top=this.curimageposY+"px"
		}
		this.activecanvas.style.left=this.curimageposX+"px"
		ready=false
	} else if (this.curimageposX>0){
		this.curimageposX=this.curimageposX-this.slidedegreeX
		if (this.curimageposX<=0) {
		  this.curimageposX=0
		  this.curimageposY=0
		  this.activecanvas.style.top=this.curimageposY+"px"
		}
		this.activecanvas.style.left=this.curimageposX+"px"
	  ready=false
	}
	
	if (ready) {
		clearInterval(this.animatetimer)
		//this.activecanvas.style.top=0
		//this.activecanvas.style.left=0
		this.setupnextslide()
		var slideshow=this
		this.delaytime=this.rand(this.maxDelay, this.minDelay, -1) * 1000
		setTimeout(function(){slideshow.rotateslide()}, this.delaytime)
	}
}

dropinslideshow.prototype.setupnextslide=function(){
	this.inactiveimgID=this.imgID+"_"+this.activecanvasindex
	this.inactivecanvas=this.canvases[this.activecanvasindex]
	this.activecanvasindex=(this.activecanvasindex==0)? 1 : 0
	this.activeimgID=this.imgID+"_"+this.activecanvasindex
	this.activecanvas=this.canvases[this.activecanvasindex]
	this.animatestartposY = (this.rand(3,0,-1)-1)*this.h
	this.animatestartposX = (this.rand(3,0,-1)-1)*this.w
	this.activecanvas.style.top=this.animatestartposY+"px"
	this.activecanvas.style.left=this.animatestartposX+"px"
	this.curimageposY=this.animatestartposY
	this.curimageposX=this.animatestartposX
	if (this.curimageposY==0 && this.curimageposX==0) {
	  this.zindex=this.zindexhold-this.zindexstep
	}
  this.activecanvas.style.zIndex=(++this.zindex)
	if (this.randomize) {
	  this.curimageindex=this.rand(this.imgarray.length, 0, this.curimageindex)
  	_dropinslidesarray[this.count]=this.curimageindex
  } else {
	  this.curimageindex=(this.curimageindex<this.imgarray.length-1)? this.curimageindex+1 : 0
	}
	this.populatecanvas(this.activecanvas, this.curimageindex)
}

dropinslideshow.prototype.rotateslide=function(){
	var slideshow=this
	if (this.isMouseover) {
		setTimeout(function(){slideshow.rotateslide()}, 50)
	} else {
		this.animatetimer=setInterval(function(){slideshow.animateslide()}, this.slidedelay)
	}
}

dropinslideshow.prototype.rand=function(maxNum, minNum, currentIdx){
	//maxNum = parseInt(maxNum)
	//minNum = parseInt(minNum)
	if (maxNum <= minNum) minNum = maxNum - 1
	
	var n = Math.floor(Math.random() * (maxNum - minNum)) + minNum
  n = (n==currentIdx)? ((currentIdx==maxNum-1)? minNum : currentIdx+1) : n

	return n
}

dropinslideshow.prototype.init=function(){
	var slideshow=this
	this.populatecanvas(this.canvases[this.activecanvasindex], this.curimageindex)
	this.setupnextslide()
	this.slideshowref.onmouseover=function(){slideshow.isMouseover=1}
	this.slideshowref.onmouseout=function(){slideshow.isMouseover=0}
  this.delaytime=this.rand(this.maxDelay, this.minDelay, -1) * 1000

	setTimeout(function(){slideshow.rotateslide()}, this.delaytime)
}

//blend/fade

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	if (opacity>100) {
	  opacity=100
  } else if (opacity<0) {
    opacity=0
  }
	
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
