var rowers = [];

var rower_effect = {
  conf: {from: 0, to: 150},
  Pre: function(){
    this.el.style.top = -this.from+"px";
  },
  Run: function(){
    this.el.style.top = -(this.from + this.ratio*(this.to-this.from))+"px";
  },
  Post: function(){
    this.el.style.top = -this.to+"px";
  }
}

rower = function(id){
  this.id = id;
  this.start = 0;
  this.amount = 0;
  this.el = document.getElementById("rower"+id);
  this.el.childClean();
  this.inel = this.el.childNodes[1];
  this.inel.childClean();
  this.amount = this.inel.childNodes.length;
  this.rows = Engine.rows();
  this.lock = false;
  
  var myself = this;
  this.r = document.getElementById("rower"+id+"_r");
  this.r.firstChild.onclick = function(e){
    myself.click(1);
    return false;
  }
  this.l = document.getElementById("rower"+id+"_l");
  this.l.firstChild.onclick = function(e){
    myself.click(-1);
    return false;
  }
  
  this.Adapt(false);
}

rower.prototype.Adapt = function(resize){
  if(resize && Engine.rows() != this.rows){
    this.start = 0;
    this.inel.style.top = 0;
    this.rows = Engine.rows();
  }else if(resize) return;
  for(var i=0; i<this.inel.childNodes.length; i++){
    this.RemoveHalf(this.inel.childNodes[i]);
  }
  for(var i=this.rows-2; i<this.inel.childNodes.length; i+=this.rows-1){
    this.AddHalf(this.inel.childNodes[i]);
  }
  if((this.rows-1) < this.inel.childNodes.length){
    this.r.style.display = "";
  }else{
    this.r.style.display = "none";
  }
  Engine.Hrefy();
}
rower.prototype.AddHalf = function(el){
  if(el == this.inel.lastChild){
    this.inel.appendChild(el.cloneNode(true));
  }else{
    this.inel.insertBefore(el.cloneNode(true), el.nextSibling);
  }
  var e = document.createElement("div");
  e.className = "mask half";
  el.appendChild(e);
  e = document.createElement("a");
  e.className = "ico";
  e.innerHTML = '>';
  e.href = "#";
  e.engined = true;
  var myself = this;
  e.onclick = function(){
    myself.click(1);
    return false;
  }
  el.appendChild(e);
  el.halved = true;
}
rower.prototype.RemoveHalf = function(el){
  if(!el.halved) return;
  el.removeChild(el.lastChild);
  el.removeChild(el.lastChild);
  el.halved = false;
}

rower.prototype.click = function(side){
  if(this.lock) return;
  this.lock = true;
  var to = this.start+side;
  var myself = this;
  RunEffect(this.inel, rower_effect, {from: 150*this.start, to: 150*to, callback: function(){
    myself.lock = false;
  }});
  this.start = to;
  if(this.start){
    this.l.style.display = "";
  }else{
    this.l.style.display = "none";
  }
  if((this.rows-1)*(this.start+1) < this.inel.childNodes.length){
    this.r.style.display = "";
  }else{
    this.r.style.display = "none";
  }
}

function InitRowers(){
  rowers = [];
  var i=1;
  while(document.getElementById("rower"+i)){
    rowers.push(new rower(i++));
  }
  if(rowers.length){
    window.onresize = function(){
      for(var i=0; i<rowers.length; i++){
        rowers[i].Adapt(true);
      }
    }
  }
}
AddLoad(function(){ InitRowers(); });
