/* 
 * 图片轮显
 * www.baopet.com().
 */
function MoviePlayer(groupName, initPosLeft, btnCount, has_effect) {
  this.groupName = groupName;
  this.initPosLeft = initPosLeft;
  this.btnCount = btnCount;
  this.has_effect = (has_effect != null ? has_effect : true);

  this.current_id = 1;
  this.is_stop = false;
  this.layerIndex = 0;
  var self = this;
  for(i=1; i<=this.btnCount; i++) {
    /*-----鼠标经过产生效果----*/
    /*
    Event.observe($('player_pet_' + this.groupName + '_' + i), 'mouseover', self.changeSlide.bindAsEventListener(self, i));
    Event.observe($('player_pet_' + this.groupName + '_' + i), 'mouseout', self.startAuto.bindAsEventListener(self));
    */
    /*-----鼠标点击效果----*/
    Event.observe($('player_pet_' + this.groupName + '_' + i), 'click', self.changeSlide.bindAsEventListener(self, i));
  }
  //初始化
  this.init(); 
}

MoviePlayer.prototype.init = function() {
  this.layerIndex++;
  $('u' + this.groupName + '_' + this.current_id).style.zIndex = this.layerIndex;
  
  $('player_pet_' + this.groupName + '_' + this.current_id).className = 'current';
  this.counter();
}

MoviePlayer.prototype.changeSlide = function() {
  this.is_stop = true;
  var data = $A(arguments);
  data.shift();
  this.current_id = data;
  this.doExecuteEffect();
}

MoviePlayer.prototype.startAuto = function() {
  this.is_stop = false;
}

MoviePlayer.prototype.buttonEffect = function() {
  for(i=1; i<=this.btnCount; i++) {
    var tmpButton = $('player_pet_' + this.groupName + '_' + i);
    if(i == this.current_id) {
      tmpButton.className = "current";
    } else {
      tmpButton.className = "";
    }
  } 
}

MoviePlayer.prototype.doExecuteEffect = function() {
  //执行滚动效果
  var curDiv = $('u' + this.groupName + '_' + this.current_id);
  this.layerIndex++;
  curDiv.style.zIndex = this.layerIndex;
  var browser = navigator.appName;
  if(this.has_effect) {
    if(browser == 'Microsoft Internet Explorer') {    
      curDiv.style.posLeft = this.initPosLeft;
      this.moveDiv(this.current_id);
    }
  }
  //更改按钮状态
  this.buttonEffect();

  //循环计数
  this.counter();
}

MoviePlayer.prototype.doact = function() {
  if(!this.is_stop) {
    this.doExecuteEffect();
  }
}

MoviePlayer.prototype.moveDiv = function(moveId) {
  var curDiv = $('u' + this.groupName + '_' + moveId);
  if(curDiv.style.posLeft != 0) {
    curDiv.style.posLeft = curDiv.style.posLeft * 0.8;
    var self = this;
    var oldMoveId = moveId;
    window.setTimeout(function(){ self.moveDiv(oldMoveId); }, 30);
  }
}

MoviePlayer.prototype.counter = function() {
  this.current_id++;
  if(this.current_id > this.btnCount) this.current_id = 1;
}