/* animator 1.0, copyright(c) 2010 inventive designs <www.inventivedesigns.cz>.
   ! - requires JQuery > 1.4.2 min
*/
function animator(jq,options) {
  var t = this;
  t.jq = jq;
  for (var i in options) t[i] = options[i];
  t.value = t.jq.css(t.property);
  t.valueEnd = t.value;
  t.valueOrig = t.value;
  if (t.property.search(/left|right|top|bottom/)!=-1 && t.jq.css('position')=='static') {
    t.css('position',t.position);
  }
  if (t.slider) {
    if (!t.valueMin) t.valueMin = t.step-t.jq.innnerWidth();
    if (!t.valueMax) t.valueMax = t.value;
  }
  if (!t.dstMinmov) t.dstMinmov = t.speedMax-1;
}
animator.prototype = {
  property:'left',
  ease:true,
  interval:25,
  speed:0,
  speedMin:0,
  speedMax:20,
  a:1,
  step:0,
  position:'relative',
  slider:false,
  update:function(t) {
    t.speed += t.a*t.easeSign;
    if (t.speed<t.speedMin) {
      t.speed = t.speedMin;
      t.easeSign = 0;
    } else if (t.speed>t.speedMax) {
      t.speed = t.speedMax;
      t.easeSign = 0;
    }
    t.value += t.speed*t.sign;
    var valueEndAbs = t.valueEnd*t.sign;
    var valueAbs = t.value*t.sign;
    if (valueAbs<valueEndAbs) {
      if (valueAbs>=valueEndAbs-t.seqSum(t.speedMin,t.speed-t.a,t.a)-t.dstMinmov && t.easeSign!=-1 && t.speed!=t.speedMin) {
        t.easeSign = -1;
      }
    } else {
      t.value = t.valueEnd;
      t.speed = 0;
      window.clearInterval(t.intUpdate);
      if (t.finished) t.finished();
    }
    t.updateProp(t);
  },
  updateProp:function(t) {
    t.jq.css(t.property,t.value);
  },
  start:function() {
    var t = this;
    if (this.ease) {
      this.easeSign = 1;
      this.speed = this.speedMin;
    } else {
      this.easeSign = 0;
      this.speed = this.speedMax;
    }
    window.clearInterval(this.intUpdate);
    this.intUpdate = window.setInterval(function(){t.update(t)},this.interval);
  },
  next:function() {
    if (this.slider) {
      this.sign = -1;
      this.valueEnd -= this.step;
      if (this.valueEnd<this.valueMin) {
        this.valueEnd = this.valueMin;
      }
      this.start();
    }
  },
  prev:function() {
    if (this.slider) {
      this.sign = 1;
      this.valueEnd += this.step;
      if (this.valueEnd>this.valueMax) {
        this.valueEnd = this.valueMax;
      }
      this.start();
    }
  },
  animate:function(value) {
    if (value<this.value) this.sign = -1;
    else this.sign = 1;
    this.valueEnd = value;
    this.start();
  },
  animateBack:function() {
    if (this.valueOrig<this.value) this.sign = -1;
    else this.sign = 1;
    this.valueEnd = this.valueOrig;
    this.start();
  },
  set:function(value,resetOrig) {
    this.valueEnd = value;
    if (resetOrig!==false) this.valueOrig = value;
    this.value = value;
    this.updateProp(this);
  },
  seqSum:function(a1,an,d) {
    var n = 1+(an-a1)/d;
    return (a1+an)*n/2;
  }
}

