// Path control emulation
// Copyright © 1997-2001 Butz Yung / Anime Theme. 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

// Browser and platform detect (v7.0)

var ie = (navigator.appName.indexOf("Internet Explorer") != -1)
var ie4 = !!document.all
var w3c_dom = !!self.HTMLElement
var nc4 = !!(!w3c_dom && window.captureEvents)
var ie5, ie55, ie6, ie6_only, ie7, ie_win, ie_other
if (ie && ie4) {
  if (navigator.appVersion.indexOf("MSIE 7") != -1) {
    ie5 = true
    ie55 = true
    ie6 = true
    ie7 = true
  }
  else if (navigator.appVersion.indexOf("MSIE 6") != -1) {
    ie5 = true
    ie55 = true
    ie6 = true
    ie6_only = true
  }
  else if (navigator.appVersion.indexOf("MSIE 5") != -1) {
    ie5 = true
    ie55 = (navigator.appVersion.indexOf("MSIE 5.5") != -1)
  }
  if (navigator.appVersion.indexOf("Win") != -1)
    ie_win = true
  else
    ie_other = true
}

if (w3c_dom) {
  if (!ie4) {
    document.writeln('<script language="JavaScript" src="dom.js"></sc'+'ript>')
    ie4 = true
  }
  ie_other = true
}

var dhtml = (ie4 || nc4)

// End


var Ovals = new Array()
var KeyFrames = new Array()

var Paths = new Array()

function Path(target, func) {
  Paths[Paths.length] = this
  this.Absolute = false
  this.Target = target
  this.PlayState = 0
  this.TimerInterval = 10
  this.Repeat = 1
  this.Bounce = 0
  this.Duration = 1
  if (func)
    this.onstop = (func.indexOf("(") == -1) ? eval(func) : new Function(func)

  this.resetPath = resetPath
  this.Play = Play
  this.Stop = Stop
  this.Pause = Pause
  this.Seek = Seek
  this.PolyLine = PolyLine
  this.KeyFrame = KeyFrame
  this.Oval = Oval
}

function resetPath() {
  this.target = eval(this.Target)
  this.max = Math.round(this.Duration * this.TimerInterval)
  if (this.max == 0)
    this.max = 1
}

function Play() {
  if (this.PlayState == 1)
    return
  if (this.PlayState == 0) {
    this.reversed = false
    this.count = -1
    this.repeat = this.Repeat
  }
  this.PlayState = 1
  playPath(this)
}

function Stop() {
  if (this.PlayState == 0)
    return
  this.PlayState = 0
  if (this.onstop)
    this.onstop()
}

function Pause() {
  if (this.PlayState == 0)
    return
  this.PlayState = 2
}

function Seek(time) {
  if (this.PlayState == 0) {
    this.PlayState = 2
    this.repeat = this.Repeat
  }
  this.count = time * this.TimerInterval - 1
}

function PolyLine(pts, xy) {
  if (pts == 2) {
    this.resetPath()
    this.x_org = xy[0]
    this.y_org = xy[1]
    this.xy = new Array((xy[2] - xy[0]) / this.max, (xy[3] - xy[1]) / this.max)
    return
  }
  var time = new Array()
  var d = this.Duration / (pts - 1)
  for (var i = 0; i < pts - 1; i++)
    time[time.length] = d
  this.KeyFrame(pts, xy, time, true)
}

function KeyFrame(pts, xy, time, no_record, relative) {
  var x_start, y_start
  if (relative) {
    x_start = 0
    y_start = 0
  }
  else {
    x_start = xy[0]
    y_start = xy[1]
    relative = new Array(x_start, y_start)
  }
  var x_last = xy[xy.length - 2] - x_start
  var y_last = xy[xy.length - 1] - y_start
  var index = pts + "," + x_last + "," + y_last
  var k = KeyFrames[index]
  if (!no_record && k)
    this.xy = k
  else {
    var path = new Array()
    for (var i = 0; i < pts - 1; i++) {
      var t = Math.round(time[i] * this.TimerInterval)
      var x = xy[2 * i]
      var y = xy[2 * i + 1]
      var x_mod = (xy[2 * i + 2] - x) / t
      var y_mod = (xy[2 * i + 3] - y) / t
      for (var p = 0; p < t; p++) {
        path[path.length] = x + x_mod * p - x_start
        path[path.length] = y + y_mod * p - y_start
      }
    }
    path[path.length] = x_last
    path[path.length] = y_last
    this.xy = path
    if (!no_record)
      KeyFrames[index] = path
  }
  this.Duration = (this.xy.length / 2) / this.TimerInterval
  this.resetPath()
  this.x_org = relative[0]
  this.y_org = relative[1]
}

function Oval(start_x, start_y, width, height) {
  this.resetPath()
  this.x_org = start_x
  this.y_org = start_y
  var index = width + "," + height + "," + this.Duration
  var o = Ovals[index]
  if (o) {
    this.xy = o
    return
  }

  width /= 2
  height /= 2
  var PI = Math.PI
  var circle = new Array()
  for (var i = 0; i < this.max; i++) {
    var rad = (i+1) / this.max * 2 * PI
    circle[2 * i] = -(Math.round(Math.cos(rad) * width) - width)
    circle[2 * i + 1] = -Math.round(Math.sin(rad) * height)
  }

  this.xy = circle
  Ovals[index] = circle
}

function runPath() {
  for (var i = 0; i < Paths.length; i++) {
    var p = Paths[i]
    playPath(p)
  }
}

function playPath(p) {
  if ((p.PlayState == 0) || (p.PlayState == 2))
    return

  var onstop = false
  var xy = p.xy
  if (++p.count == p.max) {
    if (--p.repeat == 0) {
      p.PlayState = 0
      if (p.onstop)
        onstop = true
      p.count--
    }
    else {
      p.count = 0
      if (p.Bounce)
        p.reversed = !p.reversed
    }
  }

  var x, y
  if (p.reversed) {
    if (xy.length == 2) {
      x = -xy[0] * (p.count+1)
      y = -xy[1] * (p.count+1)
    }
    else {
      x = xy[2 * (p.max - p.count) - 2]
      y = xy[2 * (p.max - p.count) - 1]
    }
  }
  else {
    if (xy.length == 2) {
      x = xy[0] * (p.count+1)
      y = xy[1] * (p.count+1)
    }
    else {
      x = xy[2 * p.count]
      y = xy[2 * p.count + 1]
    }
  }
  if (!p.Absolute) {
    x += p.x_org
    y += p.y_org
  }
  if (ie4) {
    p.target.style.posLeft = x
    p.target.style.posTop = y
  }
  else {
    p.target.left = x
    p.target.top = y
  }
  if (onstop)
    p.onstop()
}
