// JavaScript Document
function PictureSlider(parentDiv,bgWidth,bgHeight,triggerDist,parentObj)
{
  this.parentDiv=parentDiv;
  this.image=new Image();
  var obj=this;
  this.currX=0;
  this.imageName=null;
  this.bgWidth=bgWidth;
  this.bgHeight=bgHeight;
  this.triggerDist=triggerDist;
  this.imgElmt=null;
  this.shadowDiv;
  this.timeoutHnd=null;
  this.parentObj=parentObj;
  this.next=false;
  this.frameDelta;
  this.state=-1;
}
PictureSlider.prototype.setup=function()
{
  var obj = this;
  this.shadowDiv=$(document.createElement('div'));
  this.shadowDiv.css({'position':'absolute','top':'7px','left':'7px','background-color':'#000','opacity':0.3});
  $(this.image).css({'position':'absolute','border':'thin solid #000'});
  this.imgElmt=$(document.createElement('div'));
  this.imgElmt.css({'position':'absolute','top':'0px','left':'-430px'});
  this.imgElmt.click(function(){
    if(obj.parentObj != null && obj.parentObj.click)
      obj.parentObj.click(decodeURI(obj.image.src));
  });
  this.imgElmt.append(this.shadowDiv).append(this.image);
  this.parentDiv.append(this.imgElmt);
  return true;
}
PictureSlider.prototype.setImage=function(img)
{
  this.imageName=img;
  // Replacing image object instead of just switching source
  // because IE does not correctly report the image dimensions
  // when you just switch the source.
  var obj = this;
  tmp=new Image();
  tmp.onload=function(){
      obj.state=2;
  }
  $(tmp).css({'position':'absolute','border':'thin solid #000'});
  $(this.image).replaceWith(tmp)
  this.image=tmp;
  this.state = 0;
}
PictureSlider.prototype.setZIndex=function(zIndex)
{
  this.imgElmt.css('z-index',zIndex);
}
PictureSlider.prototype.getZIndex=function()
{
  return this.imgElmt.css('z-index');
}
PictureSlider.prototype.run=function(delay)
{
  if(this.imageName==null || this.imgElmt == null || this.state > 0)
    return;
  if(delay==null)
    delay=0;
  var obj=this;
  this.state = 1;
  this.timeoutHnd=setTimeout(function(){
    obj.image.src=obj.imageName;
  },delay);
}
PictureSlider.prototype.isReady=function()
{
  return this.state == 0;
}
PictureSlider.prototype.isDone=function()
{
  return this.state == 5;
}
PictureSlider.prototype.triggerNext=function()
{
  var tmp = this.next;
  this.next=false;
  return tmp;
}
PictureSlider.prototype.isVML=function()
{
  return false;
}
PictureSlider.prototype.draw=function(){
  if(this.state > 1 && this.state < 5){
    switch(this.state){
      case 2:
        this.currX=-(this.image.width+1);
        var y=Math.round(Math.random()*5)*((this.bgHeight-this.image.height)/5);
        var newSpeed=(7000+Math.round(Math.random()*10)*1000)*0.03;
        this.frameDelta = Math.round((this.bgWidth-this.currX)/newSpeed);
        this.imgElmt.css({
          'height':this.image.height+7+'px','width':this.image.width+7+'px','top':y+'px','left':this.currX+'px'
        });
        this.shadowDiv.css({'height':this.image.height+'px','width':this.image.width+'px'});
        this.imgElmt.show();
        this.state=3;
        break;
      case 3:
        if(this.currX > this.triggerDist){
          this.state=4;
          this.next=true;
        }
      case 4:
        if(this.currX > this.bgWidth)
          this.state=5;
        this.currX+=this.frameDelta;
        this.imgElmt.css({'left':this.currX+'px'});
        break;
    }
  }
}
