if (!window.pb) {
  window.pb = {};
}


pb.animate = { }

pb.animate.fadein = Class.create({
  initialize: function(div, steps, delay, current) {
    this.div = div;
    this.steps = steps == undefined ? 10 : steps;
    this.delay = delay == undefined ? 30 : delay;
    this.current = current == undefined ? 0 : current;
    this.timerId = 0;

    if (Prototype.Browser.IE) {
      Element.select(this.div, '*').each((function(d) {
        if (Element.getStyle(d, 'position') == 'relative') {
          if (!this.divs) this.divs = [];
          this.divs[this.divs.length] = d;
        }
      }).bind(this));
    }

    this.initEvents();

  },

  initEvents: function() {
    this.onStep     = function(animation) { };
    this.onStop     = function(animation) { };
    this.onStart    = function(animation) { };
    this.onFinished = function(animation) { };
  },

  start: function() {
    this.step();
    this.onStart();
  },

  stop: function() {
    clearTimeout(this.timerId);
    this.timerId = 0;
    this.onStop();
  },

  step: function() {
    var oSize = 1 / this.steps;

    var o = this.current + oSize;
    if (o > 1) o = 1;
    Element.setOpacity(this.div, o);
    if (this.divs) {
      this.divs.each(function(d) { Element.setOpacity(d, o); });
    }
    this.current = o;
    if (o < 1) this.timerId = setTimeout((function() { this.step(); }).bind(this), this.delay);
    else this.onFinished(this);

    this.onStep(this);
  }
});

pb.animate.fadeout = Class.create({
  initialize: function(div, steps, delay, current) {
    this.div = div;
    this.steps = steps == undefined ? 10 : steps;
    this.delay = delay == undefined ? 30 : delay;
    this.current = current == undefined ? 1 : current;
    this.timerId = 0;

    if (Prototype.Browser.IE) {
      Element.select(this.div, '*').each((function(d) {
        if (Element.getStyle(d, 'position') == 'relative') {
          if (!this.divs) this.divs = [];
          this.divs[this.divs.length] = d;
        }
      }).bind(this));
    }

    this.initEvents();
  },

  initEvents: function() {
    this.onStep     = function(animation) { };
    this.onStop     = function(animation) { };
    this.onStart    = function(animation) { };
    this.onFinished = function(animation) { };
  },

  start: function() {
    this.step();
    this.onStart();
  },

  stop: function() {
    clearTimeout(this.timerId);
    this.timerId = 0;
    this.onStop();
  },

  step: function() {
    var oSize = 1 / this.steps;

    var o = this.current - oSize;
    if (o < 0) o = 0;
    Element.setOpacity(this.div, o);
    if (this.divs) {
      this.divs.each(function(d) { Element.setOpacity(d, o); });
    }
    this.current = o;
    this.onStep(this);
    if (o > 0) this.timerId = setTimeout((function() { this.step(); }).bind(this), this.delay);
    else this.onFinished(this);

  }
});

pb.animate.size = Class.create({
  initialize: function(div, steps, delay, toWidth, toHeight) {
    this.div = div;
    this.steps = steps == undefined ? 10 : steps;
    this.currentStep = 0;
    this.delay = delay == undefined ? 30 : delay;
    this.dim = Element.getDimensions(div);
    this.stepWidth = toWidth === 0 || !!toWidth ? (toWidth - this.dim.width) / steps : false;
    this.stepHeight = toHeight === 0 || !!toHeight ? (toHeight - this.dim.height) / steps : false;
    this.timerId = 0;
    this.toWidth = toWidth;
    this.toHeight = toHeight;

    if (this.stepWidth < 0 || this.stepHeight < 0) {
      Element.setStyle(div, { overflow: 'hidden' });
    }

    this.initEvents();
  },

  initEvents: function() {
    this.onStep     = function(animation) { };
    this.onStop     = function(animation) { };
    this.onStart    = function(animation) { };
    this.onFinished = function(animation) { };
  },

  start: function() {
    this.step();
    this.onStart(this);
  },

  stop: function() {
    clearTimeout(this.timerId);
    this.timerId = 0;
    this.onStop(this);
  },

  step: function() {
    if (this.stepWidth !== false) {
      this.dim.width = Math.round(this.dim.width + this.stepWidth);
      if (this.currentStep+1 >= this.steps && this.dim.width != this.toWidth) this.dim.width = this.toWidth;
      if (this.dim.width < 0) this.dim.width = 0;
    }

    if (this.stepHeight !== false) {
      this.dim.height = Math.round(this.dim.height + this.stepHeight);
      if (this.currentStep+1 >= this.steps && this.dim.height != this.toHeight) this.dim.height = this.toHeight;
      if (this.dim.height < 0) this.dim.height = 0;
    }
    Element.setStyle(this.div, { width: this.dim.width + 'px', height: this.dim.height + 'px' });

    this.currentStep++;
    if (this.currentStep >= this.steps) this.onFinished(this);
    else this.timerId = setTimeout((function() { this.step(this); }).bind(this), this.delay);
  }

});

