
function Animation(obj) {
  if (this == window) {
    return new Animation(obj);
  } else {
    this.obj = obj;
    this._reset_state();
    this.queue = [];
    this.last_attr = null;
  }
}
Animation.resolution = 20;
Animation.offset = 0;

Animation.prototype._reset_state = function() {
  this.state = {
    attrs: {},
    duration: 500 // de
  }
}

Animation.prototype.stop = function() {
  this._reset_state();
  this.queue = [];
  return this;
}

Animation.prototype._build_container = function() {
  if (this.container_div) {
    this._refresh_container();
    return;
  }

  if (this.obj.firstChild && this.obj.firstChild.__animation_refs) {
    this.container_div = this.obj.firstChild;
    this.container_div.__animation_refs++;
    this._refresh_container();
    return;
  }
  var container = document.createElement('div');
  container.style.padding = '0px';
  container.style.margin = '0px';
  container.style.border = '0px';
  container.__animation_refs = 1;
  var children = this.obj.childNodes;
  while (children.length) {
    container.appendChild(children[0]);
  }
  this.obj.appendChild(container);
  this.obj.style.overflow = 'hidden';
  this.container_div = container;
  this._refresh_container();
}

Animation.prototype._refresh_container = function() {
  this.container_div.style.height = 'auto';
  this.container_div.style.width = 'auto';
  this.container_div.style.height = this.container_div.offsetHeight+'px';
  this.container_div.style.width = this.container_div.offsetWidth+'px';
}

Animation.prototype._destroy_container = function() {
  if (!this.container_div) {
    return;
  }
  if (!--this.container_div.__animation_refs) {
    var children = this.container_div.childNodes;
    while (children.length) {
      this.obj.appendChild(children[0]);
    }
    this.obj.removeChild(this.container_div);
  }
  this.container_div = null;
}

Animation.ATTR_TO = 1;
Animation.ATTR_BY = 2;
Animation.ATTR_FROM = 3;
Animation.prototype._attr = function(attr, value, mode) {

  attr = attr.replace(/-[a-z]/gi, function(l) {
    return l.substring(1).toUpperCase();
  });

  var auto = false;
  switch (attr) {
    case 'background':
      this._attr('backgroundColor', value, mode);
      return this;

    case 'margin':
      value = Animation.parse_group(value);
      this._attr('marginBottom', value[0], mode);
      this._attr('marginLeft', value[1], mode);
      this._attr('marginRight', value[2], mode);
      this._attr('marginTop', value[3], mode);
      return this;

    case 'padding':
      value = Animation.parse_group(value);
      this._attr('paddingBottom', value[0], mode);
      this._attr('paddingLeft', value[1], mode);
      this._attr('paddingRight', value[2], mode);
      this._attr('paddingTop', value[3], mode);
      return this;

    case 'backgroundColor':
    case 'borderColor':
    case 'color':
      value = Animation.parse_color(value);
      break;

    case 'opacity':
      value = parseFloat(value, 10);
      break;

    case 'height':
    case 'width':
      if (value == 'auto') {
        auto = true;
      } else {
        value = parseInt(value, 10);
      }
      break;

    case 'borderWidth':
    case 'lineHeight':
    case 'fontSize':
    case 'marginBottom':
    case 'marginLeft':
    case 'marginRight':
    case 'marginTop':
    case 'paddingBottom':
    case 'paddingLeft':
    case 'paddingRight':
    case 'paddingTop':
    case 'bottom':
    case 'left':
    case 'right':
    case 'top':
    case 'scrollTop':
    case 'scrollLeft':
      value = parseInt(value, 10);
      break;

    default:
      throw new Error(attr+' is not a supported attribute!');
  }

  if (this.state.attrs[attr] === undefined) {
    this.state.attrs[attr] = {};
  }
  if (auto) {
    this.state.attrs[attr].auto = true;
  }
  switch (mode) {
    case Animation.ATTR_FROM:
      this.state.attrs[attr].start = value;
      break;

    case Animation.ATTR_BY:
      this.state.attrs[attr].by = true;

    case Animation.ATTR_TO:
      this.state.attrs[attr].value = value;
      break;
  }
}

Animation.prototype.to = function(attr, value) {
  if (value === undefined) {
    this._attr(this.last_attr, attr, Animation.ATTR_TO);
  } else {
    this._attr(attr, value, Animation.ATTR_TO);
    this.last_attr = attr;
  }
  return this;
}

Animation.prototype.by = function(attr, value) {
  if (value === undefined) {
    this._attr(this.last_attr, attr, Animation.ATTR_BY);
  } else {
    this._attr(attr, value, Animation.ATTR_BY);
    this.last_attr = attr;
  }
  return this;
}

Animation.prototype.from = function(attr, value) {
  if (value === undefined) {
    this._attr(this.last_attr, attr, Animation.ATTR_FROM);
  } else {
    this._attr(attr, value, Animation.ATTR_FROM);
    this.last_attr = attr;
  }
  return this;
}

Animation.prototype.duration = function(duration) {
  this.state.duration = duration ? duration : 0;
  return this;
}

Animation.prototype.checkpoint = function(distance /* = 1.0 */, callback) {
  if (distance === undefined) {
    distance = 1;
  }
  this.state.checkpoint = distance;
  this.state.checkpointcb = callback;
  this.queue.push(this.state);
  this._reset_state();
  return this;
}

Animation.prototype.blind = function() {
  this.state.blind = true;
  return this;
}

Animation.prototype.hide = function() {
  this.state.hide = true;
  return this;
}

Animation.prototype.show = function() {
  this.state.show = true;
  return this;
}

Animation.prototype.ease = function(ease) {
  this.state.ease = ease;
  return this;
}

Animation.prototype.go = function() {
  var time = (new Date()).getTime();
  this.queue.push(this.state);

  for (var i = 0; i < this.queue.length; i++) {
    this.queue[i].start = time - Animation.offset;
    if (this.queue[i].checkpoint) {
      time += this.queue[i].checkpoint * this.queue[i].duration;
    }
  }
  Animation.push(this);
  return this;
}

Animation.prototype._frame = function(time) {
  var done = true;
  var still_needs_container = false;
  var whacky_firefox = false;
  for (var i = 0; i < this.queue.length; i++) {

    var cur = this.queue[i];
    if (cur.start > time) {
      done = false;
      continue;
    } else if (cur.checkpointcb && (cur.checkpoint * cur.duration + cur.start > time)) {
      this._callback(cur.checkpointcb, time - cur.start - cur.checkpoint * cur.duration);
      cur.checkpointcb = null;
    }

    if (cur.started === undefined) {
      if (cur.show) {
        this.obj.style.display = 'block';
      }
      for (var a in cur.attrs) {
        if (cur.attrs[a].start !== undefined) {
          continue;
        }
        switch (a) {
          case 'backgroundColor':
          case 'borderColor':
          case 'color':

            var val = Animation.parse_color(Animation.get_style(this.obj, a == 'borderColor' ? 'borderLeftColor' : a));

            if (cur.attrs[a].by) {
              cur.attrs[a].value[0] = Math.min(255, Math.max(0, cur.attrs[a].value[0] + val[0]));
              cur.attrs[a].value[1] = Math.min(255, Math.max(0, cur.attrs[a].value[1] + val[1]));
              cur.attrs[a].value[2] = Math.min(255, Math.max(0, cur.attrs[a].value[2] + val[2]));
            }
            break;

          case 'opacity':
            var val = ((val = Animation.get_style(this.obj, 'opacity')) && parseFloat(val)) ||
                      ((val = Animation.get_style(this.obj, 'opacity')) && (val = /(\d+(?:\.\d+)?)/.exec(val)) && parseFloat(val.pop()) / 100) ||
                      1;
            if (cur.attrs[a].by) {
              cur.attrs[a].value = Math.min(1, Math.max(0, cur.attrs[a].value + val));
            }
            break;

          case 'height':
          case 'width':
            var val = Animation['get_'+a](this.obj);
            if (cur.attrs[a].by) {
              cur.attrs[a].value += val;
            }
            break;

          case 'scrollLeft':
          case 'scrollTop':
            var val = (this.obj == document.body) ? (document.documentElement[a] || document.body[a]) : this.obj[a];
            if (cur.attrs[a].by) {
              cur.attrs[a].value += val;
            }
            cur['last'+a] = val;
            break;

          default:
            var val = parseInt(Animation.get_style(this.obj, a), 10);
            if (cur.attrs[a].by) {
              cur.attrs[a].value += val;
            }
            break;
        }
        cur.attrs[a].start = val;
      }

      if ((cur.attrs.height && cur.attrs.height.auto) ||
          (cur.attrs.width && cur.attrs.width.auto)) {

        if (/Firefox\/[12]\./.test(navigator.userAgent)) {
          whacky_firefox = true;
        }

        // Set any attributes that affect element size to their final desired values
        this._destroy_container();
        for (var a in {height: 1, width: 1,
                       fontSize: 1,
                       borderLeftWidth: 1, borderRightWidth: 1, borderTopWidth: 1, borderBottomWidth: 1,
                       paddingLeft: 1, paddingRight: 1, paddingTop: 1, paddingBottom: 1}) {
          if (cur.attrs[a]) {
            this.obj.style[a] = cur.attrs[a].value + (typeof cur.attrs[a].value == 'number' ? 'px' : '');
          }
        }

        if (cur.attrs.height && cur.attrs.height.auto) {
          cur.attrs.height.value = Animation.get_height(this.obj);
        }
        if (cur.attrs.width && cur.attrs.width.auto) {
          cur.attrs.width.value = Animation.get_width(this.obj);
        }

      }

      cur.started = true;
      if (cur.blind) {
        this._build_container();
      }
    }

    var p = (time - cur.start) / cur.duration;
    if (p >= 1) {
      p = 1;
      if (cur.hide) {
        this.obj.style.display = 'none';
      }
    } else {
      done = false;
    }
    var pc = cur.ease ? cur.ease(p) : p;

    if (!still_needs_container && p != 1 && cur.blind) {
      still_needs_container = true;
    }

    if (whacky_firefox && this.obj.parentNode) {
      var parentNode = this.obj.parentNode;
      var nextChild = this.obj.nextSibling;
      parentNode.removeChild(this.obj);
    }

    for (var a in cur.attrs) {
      switch (a) {
        case 'backgroundColor':
        case 'borderColor':
        case 'color':
          this.obj.style[a] = 'rgb('+
            Animation.calc_tween(pc, cur.attrs[a].start[0], cur.attrs[a].value[0], true)+','+
            Animation.calc_tween(pc, cur.attrs[a].start[1], cur.attrs[a].value[1], true)+','+
            Animation.calc_tween(pc, cur.attrs[a].start[2], cur.attrs[a].value[2], true)+')';
          break;

        case 'opacity':
          var opacity = Animation.calc_tween(pc, cur.attrs[a].start, cur.attrs[a].value);
          try {
            this.obj.style.opacity = (opacity == 1 ? '' : opacity);
            this.obj.style.filter = (opacity == 1 ? '' : 'alpha(opacity=' + opacity * 100 + ')');
          }
          catch (e) {}
          break;

        case 'height':
        case 'width':
          this.obj.style[a] = pc == 1 && cur.attrs[a].auto ? 'auto' :
                              Animation.calc_tween(pc, cur.attrs[a].start, cur.attrs[a].value, true)+'px';
          break;

        case 'scrollLeft':
        case 'scrollTop':

          var val = (this.obj == document.body) ? (document.documentElement[a] || document.body[a]) : this.obj[a];
          if (cur['last'+a] != val) {
            delete cur.attrs[a];
          } else {
            var diff = Animation.calc_tween(pc, cur.attrs[a].start, cur.attrs[a].value, true) - val;
            if (a == 'scrollLeft') {
              window.scrollBy(diff, 0);
            } else {
              window.scrollBy(0, diff);
            }
            cur['last'+a] = diff + val;
          }
          break;

        default:
          this.obj.style[a] = Animation.calc_tween(pc, cur.attrs[a].start, cur.attrs[a].value, true)+'px';
          break;
      }
    }

    if (p == 1) {
      this.queue.splice(i--, 1);
      this._callback(cur.ondone, time - cur.start - cur.duration);
    }
  }

  if (whacky_firefox) {
    parentNode[nextChild ? 'insertBefore' : 'appendChild'](this.obj, nextChild);
  }

  if (!still_needs_container && this.container_div) {
    this._destroy_container();
  }
  return !done;
}

Animation.prototype.ondone = function(fn) {
  this.state.ondone = fn;
  return this;
}

Animation.prototype._callback = function(callback, offset) {
  if (callback) {
    Animation.offset = offset;
    callback.call(this);
    Animation.offset = 0;
  }
}

Animation.calc_tween = function(p, v1, v2, whole) {
  return (whole ? parseInt : parseFloat)((v2 - v1) * p + v1, 10);
}

Animation.parse_color = function(color) {
  var hex = /^#([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i.exec(color);
  if (hex) {
    return [parseInt(hex[1].length == 1 ? hex[1] + hex[1] : hex[1], 16),
            parseInt(hex[2].length == 1 ? hex[2] + hex[2] : hex[2], 16),
            parseInt(hex[3].length == 1 ? hex[3] + hex[3] : hex[3], 16)];
  } else {
    var rgb = /^rgba? *\(([0-9]+), *([0-9]+), *([0-9]+)(?:, *([0-9]+))?\)$/.exec(color);
    if (rgb) {
      if (rgb[4] === '0') {
        return [255, 255, 255]; // transparent
      } else {
        return [parseInt(rgb[1], 10), parseInt(rgb[2], 10), parseInt(rgb[3], 10)];
      }
    } else if (color == 'transparent') {
      return [255, 255, 255]; // n.
    } else {

      throw 'Named color attributes are not supported.';
    }
  }
}

Animation.parse_group = function(value) {
  var value = trim(value).split(/ +/);
  if (value.length == 4) {
    return value;
  } else if (value.length == 3) {
    return [value[0], value[1], value[2], value[1]];
  } else if (value.length == 2) {
    return [value[0], value[1], value[0], value[1]];
  } else {
    return [value[0], value[0], value[0], value[0]];
  }
}

Animation.get_height = function(obj) {
  var pT = parseInt(Animation.get_style(obj, 'paddingTop'), 10),
      pB = parseInt(Animation.get_style(obj, 'paddingBottom'), 10),
      bT = parseInt(Animation.get_style(obj, 'borderTopWidth'), 10),
      bW = parseInt(Animation.get_style(obj, 'borderBottomWidth'), 10);
  return obj.offsetHeight - (pT ? pT : 0) - (pB ? pB : 0) - (bT ? bT : 0) - (bW ? bW : 0);
}

Animation.get_width = function(obj) {
  var pL = parseInt(Animation.get_style(obj, 'paddingLeft'), 10),
      pR = parseInt(Animation.get_style(obj, 'paddingRight'), 10),
      bL = parseInt(Animation.get_style(obj, 'borderLeftWidth'), 10),
      bR = parseInt(Animation.get_style(obj, 'borderRightWidth'), 10);
  return obj.offsetWidth - (pL ? pL : 0) - (pR ? pR : 0) - (bL ? bL : 0) - (bR ? bR : 0);
}

Animation.get_style = function(obj, prop) {
  var temp;
  return (window.getComputedStyle && window.getComputedStyle(obj, null).getPropertyValue(prop.replace(/[A-Z]/g, function(match) { return '-' + match.toLowerCase() }))) ||
         (document.defaultView && document.defaultView.getComputedStyle && (temp = document.defaultView.getComputedStyle(obj, null)) && temp.getPropertyValue(prop.replace(/[A-Z]/g, function(match) { return '-' + match.toLowerCase() }))) ||
         (obj.currentStyle && obj.currentStyle[prop]) ||
         obj.style[prop];
}

Animation.push = function(instance) {
  if (!Animation.active) {
    Animation.active = [];
  }
  Animation.active.push(instance);
  if (!Animation.timeout) {
    Animation.timeout = setInterval(Animation.animate, Animation.resolution);
  }
}

Animation.animate = function() {
  var done = true;
  var time = (new Date()).getTime();
  for (var i = 0; i < Animation.active.length; i++) {
    if (Animation.active[i]._frame(time)) {
      done = false;
    } else {
      Animation.active.splice(i--, 1); // remove from the list
    }
  }
  if (done) {
    clearInterval(Animation.timeout);
    Animation.timeout = null;
  }
}

Animation.ease = {}
Animation.ease.begin = function(p) {
  return p * p;
}
Animation.ease.end = function(p) {
  p -= 1;
  return -(p * p) + 1;
}
Animation.ease.both = function(p) {
  if (p <= 0.5) {
    return (p * p) * 2;
  } else {
    p -= 1;
    return (p * p) * -2 + 1;
  }
}

