rotator =
{
  delay:15,
  currentItem:0,
  timeStart:0,
  percent:0,
  stopped:true,
  slider:{},
  img:{},
  data:{},
  image_path:'',
  index_start:0,
  index_limit:8,
  
  start:function(){
    this.currentItem=0;
    this.timeStart=(new Date()).getTime();
    this.slider=$('#rotator .slider');
    this.sliderFill=$('#rotator .slider .slider-fill');
    this.img=$('#rotator .image');
    this.rotator_body=$('#rotator');
    this.image_path=this.data.image_path 
    
    if (this.data.items.length>0) {
      this.stopped=false;
      this.img.attr({'src':rotator.image_path+rotator.data.items[0].img});
      $('.link',rotator.rotator_body).attr({href:rotator.data.items[0].link});
      $('.title',rotator.rotator_body).html(rotator.data.items[0].title);
      $('.text',rotator.rotator_body).html(rotator.data.items[0].text);
      this.updateRotator();
    }
  },
  
  stop:function(){
    rotator.stopped=true;
    rotator.sliderFill.css({'width':'1px'});
    rotator.percent=0;
  },
  
  nextImage:function(){
    rotator.stop();
    rotator.selectNewImage(rotator.currentItem+1);
    rotator.restart();
  },

  prevImage:function(){
    rotator.stop();
    rotator.selectNewImage(rotator.currentItem-1);
    rotator.restart();
  },
  
  restart:function(){
    if(!rotator.stopped)
      return;
    rotator.stopped=false;
    rotator.updateRotator();
  },
  
  updateRotator:function(){
    if(rotator.stopped)
      return;
    rotator.diff=(new Date()).getTime()-rotator.timeStart;
    rotator.percent=Math.round(0.1*rotator.diff/rotator.data.items[rotator.currentItem].time,0);
    if(rotator.percent<100){
      rotator.sliderFill.css({'width':Math.round((rotator.percent/100)*rotator.slider.width())+'px'});
      window.status=rotator.percent;
    } else {
      rotator.percent=0;
      rotator.sliderFill.css({'width':'1px'});
      rotator.selectNewImage(rotator.currentItem+1);
    }
    setTimeout(rotator.updateRotator,rotator.delay);
  },
  
  selectNewImage:function(n){
    if(n==rotator.currentItem)
      return;
    if(n>=rotator.data.items.length)
      n=0;
    if(n<0)
      n=rotator.data.items.length-1
    rotator.currentItem=n;
    rotator.img.attr('src',rotator.image_path+escape(rotator.data.items[n].img));
    $('.link',rotator.rotator_body).attr({href:rotator.data.items[n].link});
    $('.title',rotator.rotator_body).html(rotator.data.items[n].title);
    $('.text',rotator.rotator_body).html(rotator.data.items[n].text);
    rotator.timeStart=(new Date()).getTime();
  }
};
