
/*******************************************/
/* Copyright Netalfa Kft. - www.netalfa.hu */
/*******************************************/

//
// depends: nawa.js, nawaElement.js
//


function nawaEScroller (id)
{
    var e = nawaElement(id);
    if (e == null)
        return null;
    if (e.scroller == null)
    {
        var s = new NAWAEScroller();
        s.element = e;
        s.id = e.id;
        s.parent = s.element.getParent();
        if (s.parent == null)
            return null;
        s.posTop = s.element.getTop();
        s.posLeft = s.element.getLeft();
        e.scroller = s;
    }
    return e.scroller;
}

function NAWAEScroller ()
{
    this.speedDelay = 100;
    this.speedStep = 5;

    this.element = null;
    this.parent = null;

    this.id = null;

    this.xStopped = true;
    this.xPosition = 0;
    this.xStart = 0;
    this.xEnd = 0;
    this.xMethod = null;
    this.xDirection = -1;
    this.xInterval = null;

    this.posTop = 0;
    this.posLeft = 0;

    this.getElement = getElement;
    function getElement ()
    {
        return this.element;
    }

    this.initx = initx;
    function initx ()
    {
        this.parent.getElement().style.overflow = 'hidden';
        this.parent.getElement().style.position = 'relative';
        return this;
    }

    this.stop = stop;
    function stop ()
    {
        pause();
        this.element.setPosition(this.posTop, this.posLeft);
        this.xStopped = true;
    }

    this.pause = pause;
    function pause ()
    {
        if (this.xInterval == null)
            return;
        window.clearInterval(this.xInterval);
        this.xInterval = null;
    }

    this.start = start;
    function start ()
    {
        this.xStopped = false;
        this.resume();
    }

    this.resume = resume;
    function resume ()
    {
        if (this.xStopped)
            return;
        this.pause();
        this.xInterval = window.setInterval("nawaEScroller('" + this.id + "').scrollTimer()", this.speedDelay);
    }

    this.left = left;
    function left ()
    {
        this.initx();
        this.xPosition = this.element.getLeft();
        this.xStart = this.element.getParent().getWidth();
        this.xEnd = -1 * this.element.getWidth();
        this.xDirection = -1;
        this.xMethod = this.scrollH;
        this.xStopped = false;
        this.resume();
    }

    this.right = right;
    function right ()
    {
        this.initx();
        this.xPosition = this.element.getLeft();
        this.xStart = -1 * this.element.getWidth();
        this.xEnd = this.element.getParent().getWidth();
        this.xDirection = 1;        
        this.xMethod = this.scrollH;
        this.xStopped = false;
        this.resume();
    }

    this.up = up;
    function up ()
    {
        this.initx();
        this.xPosition = this.element.getTop();
        this.xStart = this.element.getParent().getHeight();
        this.xEnd = -1 * this.element.getHeight();
        this.xDirection = -1;        
        this.xMethod = this.scrollV;
        this.xStopped = false;
        this.resume();
    }

    this.down = down;
    function down ()
    {
        this.initx();
        this.xPosition = this.element.getTop();
        this.xStart = -1 * this.element.getHeight();
        this.xEnd = this.element.getParent().getHeight();
        this.xDirection = 1;        
        this.xMethod = this.scrollV;
        this.xStopped = false;
        this.resume();
    }


    this.scrollH = scrollH;
    function scrollH ()
    {
        this.element.setLeft(this.xPosition);
    }

    this.scrollV = scrollV;
    function scrollV ()
    {
        this.element.setTop(this.xPosition);
    }

    this.scrollTimer = scrollTimer;
    function scrollTimer ()
    {
        if (this.xDirection < 0)
        {
            if (this.xPosition < this.xEnd)
                this.xPosition = this.xStart;
        }
        else
        {
            if (this.xPosition > this.xEnd)
                this.xPosition = this.xStart;
        }

        this.xMethod();
        this.xPosition = this.xPosition + this.xDirection * this.speedStep;
    }


}

