// JavaScript Document
function PictureDealer(parentDiv,bgWidth,bgHeight,direction,parentObj)
{
  this.parentDiv=parentDiv;
  this.image=new Image;
  var obj=this;
  this.image.onload=function(){
      obj.state = 2;
  }
  this.imageName=null;
  this.bgWidth=bgWidth;
  this.bgHeight=bgHeight;
  this.direction=direction;
  this.dim;
  this.imgElmt=null;
  this.ctx=null;
  this.canvas=null;
  this.hnd=null;
  this.timeoutHnd=null;
  this.parentObj=parentObj;
  this.state=0;
  this.next=false;
  this.opacity=100;
  this.steps=10;
  this.seq=0;
  for(var i=1;i<=this.steps;i++)
    this.seq+=1/i;
}
PictureDealer.prototype.isReady=function()
{
  return this.state == 0;
}
PictureDealer.prototype.reset=function()
{
  if(this.imgElmt==null)
    return;
  this.y=100+this.bgHeight;
  this.angle=0;
  this.speedX=Math.round((this.bgWidth/5)/this.seq);
  this.rAngle=(Math.random()*this.direction)*Math.PI/90;
  this.counter=1;
  this.opacity=100;
  if(this.canvas==null){
    this.picW=this.image.width;
    this.picH=this.image.height;
    this.dim=0;
    this.x=(this.bgWidth-this.picW)/2;
    this.speed=Math.round((this.y-(this.bgHeight-this.picH)/2)/this.seq);
    this.imgElmt.setStyles({
      'height':this.picH+'px','width':this.picW+'px','top':this.y+'px','left':this.x+'px'
    });
  }else{
    this.picW=this.image.width+20;
    this.picH=this.image.height+20;
    this.dim=Math.sqrt(Math.pow(this.picH,2)+Math.pow(this.picW,2));
    this.x=(this.bgWidth-this.dim)/2;
    this.speed=Math.round((this.y-(this.bgHeight-this.dim)/2)/this.seq);
    this.canvas.setProperties({'height':this.dim+2,'width':this.dim+2});
    this.imgElmt.setStyles({
      'height':this.dim+2+'px','width':this.dim+2+'px','top':this.y+'px','left':this.x+'px'
    });
  }
}
PictureDealer.prototype.isVML=function()
{
    return (this.canvas==null);
}
PictureDealer.prototype.setup=function()
{
  if(this.canvas!=null)
    return false;
  var obj=this;
  this.canvas=$(document.createElement('canvas'));
  if(this.canvas.getContext){
    this.ctx=this.canvas.getContext('2d');
    this.imgElmt=$(document.createElement('div'));
    this.imgElmt.setStyle('position','absolute');
    this.imgElmt.grab(this.canvas);
  }else{
    this.canvas=null;
    var ieVer=parseInt(navigator.appVersion.charAt(navigator.appVersion.indexOf("MSIE")+5));
    if (navigator.userAgent.indexOf("MSIE") < 0 || ieVer < 5)
      return false;
    this.imgElmt=$(document.createElement('v:image'));
    this.imgElmt.setProperties({
      'strokecolor':'white',
      'stroked':'true',
      'strokeweight':'10px'
    });
    this.imgElmt.setStyle('position','absolute');
 //drop shadow (too slow in IE7) 
//    var shadow=$(document.createElement('v:shadow'));
//    shadow.setProperties({'on':'true','opacity':'0.5'});
//    this.imgElmt.grab(shadow);
  }
  this.parentDiv.grab(this.imgElmt);
  return true;
}
PictureDealer.prototype.setImage=function(img)
{
  this.imageName=img;
  this.state = 0;
}
PictureDealer.prototype.setZIndex=function(zIndex)
{
  this.imgElmt.setStyle('z-index',zIndex);
}
PictureDealer.prototype.getZIndex=function()
{
  return this.imgElmt.getStyle('z-index');
}
PictureDealer.prototype.drawImg=function()
{
  if(this.ctx != null){
    this.ctx.save();
    this.ctx.translate(this.dim/2,this.dim/2);
    this.ctx.save();
    this.ctx.translate(1,2);
    this.ctx.rotate(this.angle);
    this.ctx.fillStyle='rgba(0,0,0,0.5)';
    this.ctx.fillRect (-this.picW/2,-this.picH/2,this.picW,this.picH);
    this.ctx.restore();
    this.ctx.rotate(this.angle);
    this.ctx.fillStyle='#ffffff';
    this.ctx.fillRect (-this.picW/2,-this.picH/2,this.picW,this.picH);
    this.ctx.drawImage(this.image,-this.picW/2+10,-this.picH/2+10);
    this.ctx.restore();
  }else{
    this.imgElmt.setStyle('rotation',(this.angle*180)/Math.PI);
  }
}
PictureDealer.prototype.run=function(delay)
{
  if(this.imageName==null || this.hnd!=null || this.imgElmt == null || this.state > 0)
    return;
  if(delay==null)
    delay=0;
  this.state=1;
  var obj=this;
  this.timeoutHnd=setTimeout(function(){
    var ieDelay = 0;
    if(obj.canvas==null){
      obj.imgElmt.setProperty('src',obj.imageName);
      ieDelay = 400;
    }
    setTimeout(function(){obj.image.src=obj.imageName},ieDelay);
  },delay);
}
PictureDealer.prototype.isDone=function()
{
  return this.state == 6;
}
PictureDealer.prototype.triggerNext=function()
{
  var tmp = this.next;
  this.next=false;
  return tmp;
}
PictureDealer.prototype.draw=function()
{
  if(this.state > 1 && this.state < 6){
    switch(this.state){
      case 2:  
        this.reset();
        this.drawImg();
        this.imgElmt.fade('show');
        this.state=3;
        break;
      case 3:
        if(this.ctx!=null)
          this.ctx.clearRect(0,0,this.bgWidth,this.bgHeight);
        this.y-=this.speed/this.counter;
        this.x+=this.direction*this.speedX/this.counter;
        this.angle+=this.rAngle;
        this.imgElmt.setStyles({'top':this.y+'px','left':this.x+'px'});
        this.drawImg();
        this.counter++;
        if(this.counter>this.steps){
          this.state = 4;
          var obj=this;
          setTimeout(function(){obj.state=5;},2500);
          this.next = true;
        }
        break;
      case 4:
        break;
      case 5:
        if(this.opacity <= 0)
          this.state=6;
        else{
          this.opacity-=15;
          this.imgElmt.setStyle('opacity',this.opacity/100);
        }
        break;
    }
  }
}