// Sequencer emulation 2.0
// Copyright © 1997-2006 Butz Yung. All rights reserved.
// Do NOT copy or modify any part of the script without permission.
// All comments and notice must be left as is.
// Homepage: http://www.animetheme.com

var seq_items = []

var Seq = new Seq_object()

function Seq_object() {
  this.item = Seq_item
}

function Seq_item(str) {
  if (!seq_items[str])
    seq_items[str] = new Seq_core(str)
  return seq_items[str]
}

function Seq_core(str) {
  this.name = str
  this.count = 0
  this.At = Seq_at
  this.Play = Seq_play
  this.Stop = Seq_stop
  this.Pause = Seq_pause
  this.exec = Seq_exec
}

function Seq_at(init_delay, func, max_count, interval) {
  this.initialized = true
  this.init_delay = init_delay * 1000
  this.func = func + ((/\)$/.test(func)) ? "" : "()")
  this.max_count = max_count
  this.interval = interval * 1000
}

function Seq_play() {
  if (!this.initialized || !self.loaded)
    return

  this.paused = false

  if (this.count) {
    if (this.timerID)
      return

    seq_items[this.name].exec()
  }
  else {
    if (this.timerID)
      clearTimeout(this.timerID)

    this.timerID = setTimeout('seq_items["' + this.name + '"].exec()', this.init_delay)
  }
}

function Seq_exec() {
  if (this.paused)
    return
  if (this.count++ == this.max_count) {
    this.Stop()
    return
  }

  this.timerID = setTimeout('seq_items["' + this.name + '"].exec()', this.interval)

  eval(this.func)
}

function Seq_stop() {
  this.Pause()
  this.count = 0
  if (Seq.onstop)
    Seq.onstop(this.name)
}

function Seq_pause() {
  this.paused = true

  if (!this.timerID)
    return

  clearTimeout(this.timerID)
  this.timerID = null
}

