//---------------------------------------------------------------------
// ianime.js - Animation module for iPhone and iPod touch
//   by Satoshi Nakajima (copyright 2007)
//   version 0.22 (Nov. 26th, 2007)
//
// [Usage]
// 1. Free to use for either commercial or non-commercial
// 2. Feedback is welcome at
//   http://satoshi.blogs.com/uie/2007/11/ianime_js.html
// 3. I recommand to use this file as-is. If you stil want to modify, please
//   Keep this section as-is
//   Change the name by appending your identity (e.g., "ianime010Joe.js")
//   Clearly indicate that it's modified from the original version.
//---------------------------------------------------------------------
var iAnime = function() {
    this.items = new Array();
};

iAnime.effects = new Array();

iAnime.drawElement = function(e,r,x,y) {
    e.style.left = x + "px";
    e.style.top = y + "px";
};

iAnime.prototype = {
    citems: 0, timer: 0, frames: 0,
    paused: false,
    // Default parameters (can be overridden)
    delay: 33,
    // Having default no-op callbacks eliminates "if" statements
    onStart: function() {},
    onComplete: function() {}
};

iAnime.noop = function() {};

iAnime.prototype.add = function(param)
{
    if (!param.duration) param.duration=1;
    if (param.id) {
        param.element = document.getElementById(param.id);
    }
    if (param.element) {
        param.sx = param.element.offsetLeft;
        param.sy = param.element.offsetTop;
    }
    if (!param.x) param.x = param.sx;
    if (!param.y) param.y = param.sy;
    param.dx = param.x - param.sx;
    param.dy = param.y - param.sy;   
    param.st = this.paused ? this.pTime : (1*(new Date));
    if (param.effect && iAnime.effects[param.effect]) {
        param.drawElement = iAnime.effects[param.effect];
        if (param.drawElement.init) {
            param.drawElement.init(param);
        }
    } else {
        param.drawElement = param.element ? iAnime.drawElement : iAnime.noop;
    }
    this.items[this.citems++] = param;
    param.drawElement(param.element, 0, param.sx, param.sy);
    if (this.paused==false) {
        if (this.timer==0) {
            this.frames=0;
            var self = this;
            this.timer = setInterval(function() {self.tick();}, this.delay);
            this.onStart();
        }
    }
    return param;
};

iAnime.prototype.addSequence = function(seq)
{
    if (seq.length==0) return;
    var item = seq.shift();
    if (item[0]) {
        // Nested sequence
        this.addSequence(item);
        this.addSequence(seq);
    } else {
        var self = this;
        var prev = item.onComplete;
        item.onComplete = function() { 
            if (prev) prev(item);
            self.addSequence(seq);
        } 
        this.add(item);
    }
}

iAnime.prototype.pause = function(pause)
{
  if (pause) {
    if (!this.paused) {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = 0;
      }
      this.pTime = 1*(new Date);
    }
  } else {
    if (this.paused && this.citems>0) {
      var duration = 1*(new Date) - this.pTime;
      for (var i=this.citems-1; i>=0; i--) {
        this.items[i].st += duration;
      }
      this.frames = 0;
      var self = this;
      this.timer = setInterval(function() {self.tick();}, this.delay);
      this.onStart();
    }
  }
  this.paused = pause;      
}

iAnime.prototype.tick = function()
{
    this.frames++;
    var now = 1*(new Date);
    for (var i = this.citems - 1 ; i >= 0 ; i--) {
        var item = this.items[i];
        var r = (now - item.st) / item.duration;
        if (r < 1) {
            if (item.drawElement.ratio) {
                r = item.drawElement.ratio(r);
            }
            item.drawElement(item.element, r,
                item.sx+item.dx*r, item.sy+item.dy*r);
        } else {
            item.drawElement(item.element, 1,
                item.sx+item.dx, item.sy+item.dy);
            this.items[i] = this.items[--this.citems];
            this.items[this.citems] = null;
            if (item.onComplete) {
                item.onComplete(item);
            }
            if (item.remove == true) {
                document.body.removeChild(item.element);
            }
        }
    }
    // Stop the timer if there is no animation
    if (this.citems==0) {
        clearInterval(this.timer);
        this.timer = 0;
        this.onComplete(this.frames);
    }
};
