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.height=height;
  this.width=width;
  this.imgIndex=0;
  this.paused=false;
  this.triggers=null;
  this.picObjs=null;
  this.intHndl=null;
  this.triggerStack= new Array();
  this.startCalled = false;
  this.preLoader;
  this.clickFunc = null;
  this.ajaxUrl=null;
  this.baseZ = 2;
  if((typeof imgs) == 'string'){
    this.ajaxUrl=imgs;
    this.getImageList();
  }else
    this.images=imgs;
}
HomeScreen.prototype.setClickFunc=function(func)
{
  this.clickFunc = func;
}
HomeScreen.prototype.getImageList=function()
{
  if(this.ajaxUrl != null){
    var obj = this;
    $.getJSON(this.ajaxUrl,function(dataSrc){
      obj.images=dataSrc;
      obj.init();
      if(obj.startCalled)
        obj.start();      
    });
  }
}
HomeScreen.prototype.init=function()
{
  if(this.images == null)
    return true;
  if(this.picObjs == null){
    var tmpPic;
    this.picObjs = new Array();
    for(var i=0;i<6;i++){
      tmpPic=new PictureSlider(this.container,this.width,this.height,30,this);
      if(!tmpPic.setup())
        return false;
      tmpPic.setImage(this.images[this.imgIndex]);
      tmpPic.imgId=this.imgIndex++;
      this.picObjs.push(tmpPic);
    }
  }       
  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.click=function(fileName)
{
  if(this.clickFunc == null)
    return;
  var parts = fileName.split('/');
  this.clickFunc(parts[parts.length-3]+'_'+parts[parts.length-2]+'/'+parts[parts.length-1].split('_')[1]);
}

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()},33);
  }else{
    this.startCalled = true;
  }
}
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 in this.picObjs){
      if(this.picObjs[i].isReady() && this.triggerStack.pop())
        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.getImageList();
        }
        this.picObjs[i].setImage(this.images[tmpInd]);
        this.picObjs[i].imgId=tmpInd;
      }
      this.picObjs[i].draw();
      if(this.picObjs[i].triggerNext())
        this.triggerStack.push(true);
    }
  }
}