/*
Junciona - JS TimeLine : Copyright (C) 2001 Luis Navalpotro.
Distributed under the terms of the GNU Library General Public License
Avalaible at http://sourceforge.net/projects/junciona/
questions&contact: lnavalpotro@terra.es
http://ns1.junciona.com
version 0.1. 30-July-2001.
*/

junciona.hasTimeLine = true;

junciona.timeLine = function(id,arrActions,loop){
  objeto = new junciona.scoreChannel(id,arrActions,loop);
  eval(id+"=objeto");
  return objeto;
}

junciona.mgrTimeLine = function(scoreChannel){
  if (scoreChannel != "null"){
   setTimeout(scoreChannel+".next()",0);
  }
}

junciona.scoreChannel = function(id,arrActions,loop){
  this.strObj = this.id = id;
  this.timeLine = arrActions;
  this.currentEv = -1;
  this.hasFinished = false;
  this.loopOn = (loop!=null)?loop:false;
  return this;
}

junciona.scoreChannel.prototype.start = function(startTime){
  (startTime!=null)?setTimeout(this.id+".next()",startTime): this.next();
}

junciona.scoreChannel.prototype.restart = function(startTime){
  this.currentEv = -1;
}

junciona.scoreChannel.prototype.next = function(){
  this.currentEv = this.currentEv+1;
//  alert(this.timeLine[this.currentEv]);
  eval(this.timeLine[this.currentEv]);
}

junciona.scoreChannel.prototype.add = function(ev,pos,add){
  var auxEv = [];
  for (i=0; i<this.timeLine.length; i++){
    if ( pos!=i )auxEv[i] = (this.timeLine[i])?this.timeLine[i]:ev;
    else{
      if (this.timeLine[i]){
        if (!add){
          auxEv[i+1] = this.timeLine[i];
          i++;
        }else auxEv[i] = this.timeLine[i]+ev;
      }else auxEv[i] = ev;
    }
  }
  if (pos!=null){
    auxEv[auxEv.length] = ev;
  }
  this.timeLine = auxEv;
}

junciona.scoreChannel.prototype.setTimeLine = function(arrActions){
//  alert("Before:\n"+this.timeLine);
  this.timeLine = arrActions;
//  alert("After:\n"+this.timeLine);
  this.currentEv = -1;
}