/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Version: 2009-06-10 */

function divScroller() { };

divScroller.prototype = {

    create: function() {
        var _thisObj = this;
        this._thisObj = this;
        this._FScrollNext = function() { return _thisObj._scrollNext(); };
        this._create();
    },

    RIGHT: 0,
    LEFT: 1,
    _created: null,
    element: null,
    width: null,
    findClass: null,
    divSpacing: 10,
    maxScrollSpeed: 10,
    scrollInterval: 10,
    _int: null,
    direction: this.RIGHT,
    _divContainer: null,
    _divScroll: null,

    _create: function() {
        if (this.direction != this.RIGHT && this.direction != this.LEFT) this.direction == this.RIGHT;
        if (this._created == true) return true;
        if (typeof (this.element) == typeof ('')) {
            var el = $find(this.element);
            if (!el) return false;
            this.element = el;
        }
        this._created = true;

        this._findChildrenWidth = function(pel) {
            var totalWidth = 0;
            var count = pel.childNodes.length;
            for (var i = 0; i < count; i++) {
                var el = pel.childNodes[i];
                try {
                    if (el.className == this.findClass) {
                        var width = $width(el);
                        if (width && !isNaN(width)) totalWidth += width + this.divSpacing;
                    }
                } catch (E) { }
                totalWidth += this._findChildrenWidth(el);
            }
            return totalWidth;
        }
        this.totalWidth = this._findChildrenWidth(this.element);
        this.totalWidth2 = this.totalWidth * 2;

        if (this.width == null) this.width = $width(this.element.parentNode);
        if (this.height == null) this.height = $height(this.element.parentNode);

        this.curPos = 0;

        var _thisObj = this._thisObj;

        this._divContainer = document.createElement('div');
        this._divContainer.style.position = 'relative';
        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 = 'absolute';
        this._divScroll.style.top = '0px';
        this._divScroll.style.width = this.totalWidth2 + 'px';
        this._divContainer.appendChild(this._divScroll);
        if (this.direction == this.LEFT) {
		        this._divScroll.style.right = '0px';
		        this._divScroll.style.left = 'auto';
        }
        else {
		        this._divScroll.style.left = '0px';
		        this._divScroll.style.right = 'auto';
        }

	      var tmpDiv = document.createElement('div');
        tmpDiv.style.width = this.totalWidth + 'px';
	      tmpDiv.style.position = 'absolute';
	      tmpDiv.style.left = '0px';
	      tmpDiv.style.top = '0px';

        while (this.element.firstChild) {
            tmpDiv.appendChild(this.element.firstChild)
        };
        this._divScroll.appendChild(tmpDiv)
        this.element.appendChild(this._divContainer);
        
        if (this.totalWidth2 > this.width) {
	        tmpDiv = document.createElement('div');
	        tmpDiv.innerHTML = this._divScroll.innerHTML;
        	tmpDiv.style.width = this.totalWidth + 'px';
	        tmpDiv.style.position = 'absolute';
	        tmpDiv.style.left = this.totalWidth + 'px';
	        tmpDiv.style.top = '0px';
	        this._divScroll.appendChild(tmpDiv);
	      }
    },
    scrollStart: function(speedStart) {
        if (speedStart == 0) return this.scrollStop();
        if (this.totalWidth2 <= this.width) return;

        this._scrollBack = speedStart < 0;
        speedStart = Math.abs(speedStart);
        if (speedStart > this.maxScrollSpeed) speedStart = this.maxScrollSpeed;

        this._scrollSpeed = speedStart;
        this._scrollSpeedInc = (this.maxScrollSpeed - speedStart) / 30;

        if (!this._SCROLLING) {
            this._SCROLLING = true;
            this._int = setInterval(this._FScrollNext, this.scrollInterval);
        }
    },
    _scrollNext: function(evt) {
        if (this._SCROLLING == false) return;

        if (this._scrollBack) {
            this.curPos -= this._scrollSpeed;
            
            if (this.curPos <= -this.totalWidth) this.curPos = this.totalWidth+this.curPos ;
        }
        else {
            this.curPos += this._scrollSpeed;
            if (this.curPos >= 0) this.curPos -= this.totalWidth;
        }
        if (this.direction == this.LEFT)
            this._divScroll.style.right = parseInt(this.curPos) + 'px';
        else this._divScroll.style.left = parseInt(this.curPos) + 'px';

        this._scrollSpeed += this._scrollSpeedInc;

        if (this._scrollSpeed > this.maxScrollSpeed) this._scrollSpeed = this.maxScrollSpeed;
    },
    scrollStop: function() {
        this._SCROLLING = false;
        if (this._int) {
            clearInterval(this._int);
            this._int = null;
        }
    }
};
