/* =============================================================================
* iScroll.js
*
* Class for creating generic HTML P-based dynamic repeating list scrollers
*
* Developed by Jake Kronika <jkronika@imagescape.com>
* For Imaginary Landscape, LLC <www.imagescape.com>
* Copyright (c) 2006
*
* Last updated 2006.04.10
*
* Reuse or modification without permission is prohibited.
* --------------------------------------------------------------------------- */

// browser sniffing
var is_ie = (is_ie ? is_ie : navigator.userAgent.match(/msie/i));

function iScroller(div, items, spacing, speed, hold) {
  this.container = div;
  this.display_items = (items ? items : 3);
  this.margin = (spacing ? spacing : 5); // px
  this.speed = (isFinite(parseInt(speed)) ? parseInt(speed) : 65); // ms
  this.hold = ((isFinite(parseInt(hold)) && parseInt(hold) > 0)
    ? parseInt(hold) * 1000
    : 0);
  this.nodes = [];
  this.timer = null;
  this.height = 0;

  this.init = function() {
    if (typeof (this.container) != 'object') {
      if (typeof (this.container) == 'string'
          && document.getElementById) {
        this.container = document.getElementById(this.container);
      } else {
        return null;
      }
    }

    if (typeof this.container != 'object'
        || !this.container
        || typeof this.container.style != 'object'
        || !this.container.style) {
      return null;
    }

    this.nodes = this.container.getElementsByTagName('p');

    if (this.display_items < this.nodes.length) {
      this.container.style.position = 'relative';
      this.container.style.overflow = 'hidden';

      this.setHeight();
      this.scroll();
    }
  };

  this.scroll = function() {
    clearTimeout(this.timer);

    for (var i = 0; i < this.nodes.length; i++) {
      var newTop = ((parseInt(this.nodes[i].style.top) || 0) - 1) + 'px';

      this.nodes[i].style.position = 'relative';

      this.nodes[i].style.top = newTop;
    }

    var firstTop = parseInt(this.nodes[0].style.top) || 0;

    var firstHeight = this.nodes[0].offsetHeight + this.margin;
    var shift = (0 - firstTop) + (is_ie && this.nodes.length <= 2 ? 2 : 0);

    if (shift >= firstHeight) {
      for (var i = 0; i < this.nodes.length; i++) {
        this.nodes[i].style.top = '0';
      }

      this.container.appendChild(this.nodes[0]);

      if (this.hold > 0) {
        this.pause();
        var _self = this;
        setTimeout(function() {
          _self.scroll();
        }, this.hold);

        return;
      }
    }

    var _self = this;
    this.timer = setTimeout(function() {
      _self.scroll();
    }, this.speed);
  };

  this.setHeight = function() {
    if (!this.nodes) {
      return null;
    }

    var h_ar = [];
    for (var i = 0; i < this.nodes.length; i++) {
      var h_val = parseInt(this.nodes[i].offsetHeight);

      for (var j = 0; j < h_ar.length; j++) {
        h_ar[j] = Math.min(h_ar[j], h_val);
        h_val = Math.max(h_ar[j], h_val);
      }

      h_ar[h_ar.length] = h_val;
    }

    var h = 0;
    for (var i = 0; i < this.display_items; i++) {
      h += parseInt(h_ar[i]);
    }

    if (h < parseInt(h_ar[h_ar.length - 1])) {
      this.height = parseInt(h_ar[h_ar.length - 1]);
    } else {
      this.height = h;
    }

    if (!this.height) {
      return null;
    }

    this.height += (2 * this.margin);

    this.container.style.height = (this.height + this.margin) + 'px';

    var self = this;
    this.container.onmouseover = function() { self.pause(); }
    this.container.onmouseout = function() {
      self.timer = setTimeout(function() { self.scroll(); }, 10);
    }
  };

  this.pause = function() {
    clearTimeout(this.timer);
  };

  this.onload = window.onload;
  var _self = this;
  window.onload = function() {
    _self.init();

    if (_self.onload && typeof (_self.onload) == 'function') {
      _self.onload();
    }
  };

  return this;
}

/* EXAMPLES to set up one or more scrollers on a page

  function call scheme:
    new iScroller(id[, items[, spacing[, speed[, hold]]]])
  parameters (in order for function calls):
    id       the string ID of the DIV to scroll ** REQUIRED
    items    number of items to display at any given time (default: 3)
    spacing  margin above/below each item (default: 5)
    speed    number of ms between vertical movements (default: 65)
    hold     number of seconds to pause on each item (default: 0)

  new iScroller('scroller1');
  new iScroller('scroller2', 5);
  new iScroller('scroller3', 1, 3);
  new iScroller('scroller4', 2, 6, 85);
  new iScroller('scroller5', 3, 8, 100, 1);
*/

