/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Version: 2009-05-20 */

/* requires: js_toolkit.js */
/* 
    set [element] to container div id, 
    set [findClass] to the important child divs' class,
    call [create()]
*/

function divEasyScroller() { };

divEasyScroller.prototype = {

    create: function() {
        var _thisObj = this;
        this._FCreate = function() { _thisObj._create(); };
        this._FStep = function() { _thisObj._step(); };
        this._FNextScroll = function() { _thisObj._nextScroll(); };
        this._create();
    },

    created: false,
    width: null,
    height: null,
    element: null,
    findClass: null,
    wait: 3000,
    slowDownPixels: 15,
    speedDecrease: 0.7,
    minSpeed: 1,
    _divContainer: null,
    _divScroll: null,
    _divsPos: null,
    _startSpeed: 4,
    _delay: 30,
    //_maxHeight: null,

    _verifyProperties: function() {
        if (this.element) {
            if (typeof (this.element) == typeof ('')) {
                var el = $find(this.element);
                if (el) this.element = el;
                else return false;
            }
        }
        else return false;
        return true;
    },

    _create: function() {
        if (this.created) return true;
        if (document.readyState != 'complete') {
            registerEvent(window, 'load', this._FCreate, false);
            return false;
        }
        if (!this._verifyProperties()) return false;

        this.width = $width(this.element);
        this.height = $height(this.element);

        this._divContainer = document.createElement('div');
        this._divContainer.style.position = 'relative';
        this._divContainer.style.left = '0px';
        this._divContainer.style.top = '0px';
        this._divContainer.style.width = this.width + 'px';
        this._divContainer.style.height = this.height + 'px';
        this._divContainer.style.overflow = 'hidden';
        this._divScroll = document.createElement('div');
        this._divScroll.style.position = 'relative';
        this._divScroll.style.left = '0px';
        this._divScroll.style.top = '0px';
        this._divScroll.style.width = this.width + 'px';
        this._divContainer.appendChild(this._divScroll);

        while (this.element.firstChild) {
            this._divScroll.appendChild(this.element.firstChild)
        };
        this.element.appendChild(this._divContainer);

        this._divsPos = new Array();
        for (var i = 0; i < this._divScroll.childNodes.length; i++) {
            if (this._divScroll.childNodes[i].className == this.findClass) {
                this._divsPos.push(this._divScroll.childNodes[i].offsetTop + this._divScroll.childNodes[i].offsetHeight);
            }
        }
        this._divsPos.sort(function(a, b) { return (a < b) ? (-1) : ((a > b) ? 1 : 0); });
        if (this._divsPos.length == 0 || this._divsPos[this._divsPos.length - 1] <= this.height) {
            this._created = true;
            return true;
        }

        this._divScroll.style.position = 'absolute';

        //this._maxHeight = $height(this._divScroll);

        var tmpDiv = document.createElement('div');
        tmpDiv.innerHTML = this._divScroll.innerHTML;
        this._divScroll.appendChild(tmpDiv);

        //************************* Flag as created
        this.created = true;

        this._curTopPos = 0;
        this._curIdx = -1;
        setTimeout(this._FNextScroll, this.wait);

        return true;
    },

    _nextScroll: function() {
        this._curIdx++;
        if (this._curIdx == this._divsPos.length) this._curIdx = 0;
        if (this._divsPos.length == 1) return;
        this._nextTopPos = -this._divsPos[this._curIdx];
        this._curSpeed = this._startSpeed;
        this._curTopPos = (this._curIdx == 0) ? 0 : -this._divsPos[this._curIdx - 1];
        this._divScroll.style.top = this._curTopPos + 'px';
        this._int = setInterval(this._FStep, this._delay);
    },

    _step: function() {
        this._curTopPos -= this._curSpeed;
        if (this._curTopPos < this._nextTopPos) this._curTopPos = this._nextTopPos;
        if (this._nextTopPos - this._curTopPos > -this.slowDownPixels) {
            this._curSpeed -= this.speedDecrease;
            if (this._curSpeed < this.minSpeed) this._curSpeed = this.minSpeed;
        }
        this._divScroll.style.top = this._curTopPos + 'px';

        if (this._curTopPos == this._nextTopPos) {
            clearInterval(this._int);
            setTimeout(this._FNextScroll, this.wait);
        }
    }
}






