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

//
// depends: nawa.js, nawaElement.js
//
/*
Notes:
NAWAEScroller.eWidth and NAWAEScroller.eHeight are needed as a workaround
for firefox and opera: if the position is not absolute and the size of the
scrollable div is bigger than the container div, the size of the container div
is returned (it can be seen very good by setting the css border of the scrollable
div).
The workaround: setting the position to absolute in initialization for taking
the size, then restore the position to relative.
*/

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.getDOMId();
        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.eWidth = 0;
	this.eHeight = 0;
	
    this.posTop = 0;
    this.posLeft = 0;

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

    this.initx = initx;
    function initx ()
    {
    	// workaround for bug in firefox and opera
    	var tmpPosition = this.element.getDOMElement().style.position;
        this.element.getDOMElement().style.position = 'absolute';
        this.eWidth = this.element.getWidth();
        this.eHeight = this.element.getHeight();
        this.element.getDOMElement().style.position = tmpPosition;
        
        this.parent.getDOMElement().style.overflow = 'hidden';
//        this.parent.getDOMElement().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.eWidth;
        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.eWidth;
        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.eHeight;
        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.eHeight;
        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 ()
    {
        this.element.getDOMElement().style.visibility = 'visible';
    	
        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;
    }


}

