var ImageScrollList=new Array();
function ImageScroll(container,onscroll){
  var instance = this;  
  var max_width=0;
  var max_height=0;
  

  this.index=ImageScrollList.length;
  ImageScrollList[this.index]=this;
  this.onScroll=onscroll;
  this._container_height=0;
  this._container=container;
  this._no_scroll=0;
  this._shift_x=0;
  this._shift_y=0;
  this._margin=0;  
  this._container.style["position"]="absolute";
  this._container.style["overflow"]="hidden";
  this.items=new Array();
  var itemIndex;
  this.selectedIndex=0;

  
  
  var x=this._container.childNodes;
  for(var i=0; i<x.length; i++){   
    if(x[i].tagName=='IMG'||x[i].tagName=='DIV'){   
      itemIndex=this.items.length;   
      this.items[itemIndex]=x[i];
      if(parseInt(x[i].style.height)>max_height)max_height=parseInt(x[i].style.height);
      if(parseInt(x[i].style.width)>max_width)max_width=parseInt(x[i].style.width);
    }
  }  
  this._container_height=max_height;
  
  var iTop=0; var iLeft=0;
  
 
  
  for(var i=0; i<this.items.length; i++){   
      this.items[i].style["position"]='absolute';
      if(i!=0)this.items[i].style["display"]='none';
      this.items[i].style["top"]=(this._shift_y+iTop+(this._container_height-parseInt(this.items[i].style.height))/2); 
      this.items[i].style["left"]=0;
//      iLeft+=parseInt(this.items[i].style.width)+this._margin;
     
  }  
  this.sliding=false;
}

ImageScroll.prototype.scrollThen=function(left,onScroll){
  this.tmpScroll=onScroll;
  this.scroll(left);
}
ImageScroll.prototype.scroll=function(left){
  if(left){
    var nextIndex=this.selectedIndex-1;
    if(nextIndex<0)nextIndex=this.items.length-1;
    this.scrollTo(nextIndex,"left");
  }else{
    var nextIndex=this.selectedIndex+1;
    if(nextIndex>=this.items.length)nextIndex=0;  
    this.scrollTo(nextIndex,"right");
  }
  
}

ImageScroll.prototype.scrollTo=function(next,direction){
  if(next==this.selectedIndex)return;
  var left=false;
  if(direction=="left")left=true;
  else if(direction=="right")left=false;
  else if(next>this.selectedIndex)left=false;
  else left=true;
  
  this.speed=this._container.offsetWidth/3;
  this._shift_x=0;
  this.nextIndex=next;
  if(!this.sliding){
    this.sliding=true;    
    if(left){
      this.items[this.nextIndex].style["left"]=0-parseInt(this.items[this.nextIndex].style.width);
    }else{
      this.items[this.nextIndex].style["left"]=parseInt(this.items[this.selectedIndex].style.width);
    }
    this.items[this.nextIndex].style["display"]="";
    if(left) this.slide=window.setInterval('ImageScrollList['+this.index+'].swipeLeft()',10);
    else this.slide=window.setInterval('ImageScrollList['+this.index+'].swipe()',10);
  }
}
ImageScroll.prototype.swipe=function(){
  var newLeft1, newLeft2;
  newLeft1=parseInt(this.items[this.selectedIndex].style["left"])-this.speed;
  newLeft2=parseInt(this.items[this.nextIndex].style["left"])-this.speed;
  if(newLeft2<0) newLeft2=0;
  this.items[this.nextIndex].style["left"]=newLeft2;
  this.items[this.selectedIndex].style["left"]=newLeft1;
  if(newLeft2==0){
    window.clearInterval(this.slide);
    this.items[this.selectedIndex].style["display"]="none";
    this.selectedIndex=this.nextIndex;
    this.sliding=false;
    if(this.tmpScroll){ this.tmpScroll(); this.tmpScroll=null;
    }else if(this.onScroll) this.onScroll();
  }
}  

ImageScroll.prototype.swipeLeft=function(){
  var newLeft1, newLeft2;
  newLeft1=parseInt(this.items[this.selectedIndex].style["left"])+this.speed;
  newLeft2=parseInt(this.items[this.nextIndex].style["left"])+this.speed;
  if(newLeft2>0) newLeft2=0;
  this.items[this.nextIndex].style["left"]=newLeft2;
  this.items[this.selectedIndex].style["left"]=newLeft1;
  if(newLeft2==0){
    window.clearInterval(this.slide);
    this.items[this.selectedIndex].style["display"]="none";
    this.selectedIndex=this.nextIndex;
    this.sliding=false;
    if(this.tmpScroll){ this.tmpScroll(); this.tmpScroll=null;
    }else if(this.onScroll) this.onScroll();
  }
}  

