function Trigger(){
  this.limit=1;
  this.count=0;
  this.picObj=null;
}
Trigger.prototype.click=function(picObj){
  if(picObj!=null)
    this.picObj=picObj;
  if(this.count>=this.limit){
    if(this.picObj != null) 
      this.picObj.run();
  }else
    this.count++;
}

function HomeScreen(container,imgs,width,height){
  this.container=container;
  this.images=imgs;
  this.height=height;
  this.width=width;
  this.imgIndex=0;
  this.triggerStack= new Array();
  this.waitQueue=new Array();
  this.picObjs=null;
  this.baseZ = 2;
  this.paused=false;
  this.higher;
  this.triggers;
  this.preLoader;
  this.intHndl;
  this.isVML=false;
}
HomeScreen.prototype.init=function()
{
  if(this.picObjs == null){
    var tmpPic, dir = -1;
    this.picObjs = new Array();
    for(var i=0;i<2;i++){
      tmpPic=new PictureDealer(this.container,this.width,this.height,dir,this);
      if(!tmpPic.setup())
        return false;
      tmpPic.setImage(this.images[this.imgIndex]);
      tmpPic.setZIndex(this.baseZ+i);
      tmpPic.imgId=this.imgIndex++;
      tmpPic.wait=false;
      this.picObjs.push(tmpPic);
      this.higher=tmpPic;
      dir = -dir;
    }
    this.isVML = tmpPic.isVML();
  }       
  if(!this.isVML){
    this.triggers = new Array();
    for(var i=0;i<this.images.length;i++)
      this.triggers[i]=new Trigger();
    if(this.preLoader == null)
      this.preLoader = new SequentialPreloader('home',this.images,this)
    else
      this.preLoader.setImages(this.images);
  }
  return true;
}
HomeScreen.prototype.imageLoad=function(loaderId,imgId,image)
{
  this.triggers[imgId].click();
}
HomeScreen.prototype.start=function()
{
  if(this.picObjs != null && this.picObjs.length > 0 && this.intHndl == null){
    this.triggerStack.push(true);
    var obj = this;
    this.intHndl=setInterval(function(){obj.picturePoller()},50);
  }
}
HomeScreen.prototype.pause=function()
{
  this.paused=true;
}
HomeScreen.prototype.resume=function()
{
  this.paused=false;
}
HomeScreen.prototype.picturePoller=function()
{
  if(!this.paused && this.picObjs != null){
    for(var i=0;i<this.picObjs.length;i++){
      if(this.picObjs[i].isReady() && this.triggerStack.pop()){
        if(this.isVML)
          this.picObjs[i].run();
        else
          this.triggers[this.picObjs[i].imgId].click(this.picObjs[i]);
      }
      if(this.picObjs[i].isDone()){
        var tmpInd=this.imgIndex++;
        if(this.imgIndex>=this.images.length)
          this.imgIndex=0;
        this.picObjs[i].setImage(this.images[tmpInd]);
        this.picObjs[i].imgId=tmpInd;
        this.picObjs[i].wait=false;
        this.picObjs[i].setZIndex(this.baseZ+1);
        this.higher.setZIndex(this.baseZ);
        this.higher=this.picObjs[i];
      }
      if(!this.picObjs[i].wait)
        this.picObjs[i].draw();
      if(this.picObjs[i].triggerNext()){
        var tmp = this.waitQueue.shift();
        if(tmp != null)
          tmp.wait = false;
        this.picObjs[i].wait = true;
        this.waitQueue.push(this.picObjs[i]);
        var obj = this;
        this.triggerStack.push(true);
      }
    }
  }
}