var _sliders = new Array();

function HorizontalSlider(element, selected, itemWidth, increment, interval) {
    this._element = element;
    this._selected = selected;
    this._itemWidth = itemWidth;
    this._increment = increment;
    this._interval = interval;
    this._current = selected;
    this._intervalId = -1;
    this._index = _sliders.length;
    this._children = new Array();
    for (var i = 0; i < element.childNodes.length; i++) {
        var node = element.childNodes[i];
        if (node.nodeType == 1) {
            var links = node.getElementsByTagName("a");
            this._children.push(node);
            node.style.position = "absolute";
        }
    }
    _sliders[this._index] = this;
    this._normalize();
}

function _normalize() {
    var delta = this._current - this._selected;
    this._current %= this._children.length;
    if (this._current < 0) {
        this._current += this._children.length;
    }
    this._selected = this._current - delta;
    for (var i = 0; i < this._children.length; i++) {
        var childStyle = this._children[i].style;
        if (Math.abs(i - this._current) 
                < Math.abs(i + this._children.length - this._current)) {
            this._children[i].style.left = 
                (this._itemWidth * (i - this._current)).toString() + "px";
        }
        else {
            this._children[i].style.left = (this._itemWidth 
                * (i + this._children.length - this._current)).toString() + "px";
        }
    }
}

function _schedule() {
    this._intervalId = window.setTimeout(
        "_sliderCallback(" + this._index.toString() + ")", this._interval);
}

function _advance() {
    var diff = Math.round((this._current - this._selected) * this._itemWidth);
    this._intervalId = -1;
    if (diff != 0) {
        if (this._current > this._selected) {
            this._current = Math.max(
                this._current - this._increment / this._itemWidth,
                this._selected);
        }
        else {
            this._current = Math.min(
                this._current + this._increment / this._itemWidth, 
                this._selected);
        }
        if (Math.abs(this._current - this._selected) > 0.1 * this._increment / this._itemWidth) {
            this._schedule();
        }
        else {
            this._selected = Math.round(this._selected);
            this._current = this._selected;
        }
    }
    this._normalize();
}

function _slideLeft() {
    this._selected--;
    if (this._intervalId == -1) {
        this._schedule();
    }
}

function _slideRight() {
    this._selected++;
    if (this._intervalId == -1) {
        this._schedule();
    }
}

function _selected() {
    var index = Math.round(this._selected % this._children.length);
    if (index < 0) {
        index += this._children.length;
    }
    return this._children[index];
}

function _sliderCallback(index) {
    _sliders[index]._advance();
}

HorizontalSlider.prototype._normalize = _normalize;
HorizontalSlider.prototype._schedule = _schedule;
HorizontalSlider.prototype._advance = _advance;
HorizontalSlider.prototype.slideLeft = _slideLeft;
HorizontalSlider.prototype.slideRight = _slideRight;
HorizontalSlider.prototype.selected = _selected;

