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

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

function nawaEShower (id)
{
    var e = nawaElement(id);
    if (e == null)
        return null;
    if (e.shower == null)
    {
        var s = new NAWAEShower();
        s.element = e;
        s._id = e.getDOMId();
        e.shower = s;
    }
    return e.shower;
}



function NAWAEShower ()
{
    this.speedDelay = 1;
    this.speedStep = 10;

    this.element = null;
    this._id = null;

    this.postTask = null;

    this.xState = new NAWAShowerState();

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

    this.executePostTask = executePostTask;
    function executePostTask ()
    {
        if (this.postTask == null)
            return;
        this.postTask = this.postTask.execute();
    }


    this.stop = stop;
    function stop ()
    {
        pause();
        this.xStopped = true;
    }

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

    this.resume = resume;
    function resume ()
    {
        if (this.xStopped)
            return;
        this.pause();
        this.xInterval = window.setInterval("nawaEShower('" + this._id + "').showTimer()", this.xState.delay);
    }


    this._showDone = _showDone;
    function _showDone ()
    {
        this.pause();
        this.executePostTask();
    }

    this._getStepCount = _getStepCount;
    function _getStepCount (start, end)
    {
        return Math.abs(end - start) / this.speedStep;
    }

    this._getStepSize = _getStepSize;
    function _getStepSize (start, end, cnt)
    {
        return Math.ceil(Math.abs(end - start) / cnt);
    }

    this._showStart = _showStart;
    function _showStart (method)
    {
        // setting speed;
        var maxSteps = Math.max(this._getStepCount(this.xState.stateTop, this.xState.endTop), this._getStepCount(this.xState.stateRight, this.xState.endRight));
        maxSteps = Math.max(maxSteps, this._getStepCount(this.xState.stateBottom, this.xState.endBottom));
        maxSteps = Math.max(maxSteps, this._getStepCount(this.xState.stateLeft, this.xState.endLeft));

        this.xState.stepTop = this._getStepSize(this.xState.stateTop, this.xState.endTop, maxSteps);
        this.xState.stepRight = this._getStepSize(this.xState.stateRight, this.xState.endRight, maxSteps);
        this.xState.stepBottom = this._getStepSize(this.xState.stateBottom, this.xState.endBottom, maxSteps);
        this.xState.stepLeft = this._getStepSize(this.xState.stateLeft, this.xState.endLeft, maxSteps);

        if (method == this._reclipInc)
        {
            this.element.hide();
            this.element.setClipRect(this.xState.stateTop, this.xState.stateRight, this.xState.stateBottom, this.xState.stateLeft);
            this.element.show();
        }

        this.xMethod = method;
        this.xStopped = false;
        this.resume();
    }


    this.showCenter = showCenter;
    function showCenter ()
    {
        this.xState.init(this);

        this.xState.stepRight = this.speedStepH / 2;
        this.xState.stepLeft = this.speedStepH / 2;

        this.xState.stateTop = this.element.getHeight() / 2;
        this.xState.stateBottom = this.element.getHeight() / 2;
        this.xState.stateLeft = this.element.getWidth() / 2;
        this.xState.stateRight = this.element.getWidth() / 2;

        this._showStart(this._reclipInc);
    }
    

    this.showVCenter = showVCenter;
    function showVCenter ()
    {
        this.xState.init(this);

        this.xState.stateTop = this.element.getHeight() / 2;
        this.xState.stateBottom = this.element.getHeight() / 2;

        this._showStart(this._reclipInc);
    }
    
    this.showVUp = showVUp;
    function showVUp ()
    {
        this.xState.init(this);

        this.xState.stateTop = this.element.getHeight();

        this._showStart(this._reclipInc);
    }

    this.showVDown = showVDown;
    function showVDown ()
    {
        this.xState.init(this);

        this.xState.stateBottom = 0;

        this._showStart(this._reclipInc);
    }

    this.showHLeft = showHLeft;
    function showHLeft ()
    {
        this.xState.init(this);

        this.xState.stateLeft = this.element.getWidth();

        this._showStart(this._reclipInc);
    }

    this.showHRight = showHRight;
    function showHRight ()
    {
        this.xState.init(this);

        this.xState.stateRight = 0;

        this._showStart(this._reclipInc);
    }


    this.showHCenter = showHCenter;
    function showHCenter ()
    {
        this.xState.init(this);

        this.xState.stateLeft = this.element.getWidth() / 2;
        this.xState.stateRight = this.element.getWidth() / 2;

        this._showStart(this._reclipInc);

    }


    this.hideCenter = hideCenter;
    function hideCenter ()
    {
        this.xState.init(this);

        this.xState.endLeft = this.element.getWidth() / 2;
        this.xState.endRight = this.element.getWidth() / 2;
        this.xState.endTop = this.element.getHeight() / 2;
        this.xState.endBottom = this.element.getHeight() / 2;

        this._showStart(this._reclipDec);
    }


    this.hideVCenter = hideVCenter;
    function hideVCenter ()
    {
        this.xState.init(this);

        this.xState.endTop = this.element.getHeight() / 2;
        this.xState.endBottom = this.element.getHeight() / 2;

        this._showStart(this._reclipDec);
    }

    this.hideVCenterExcept = hideVCenterExcept;
    function hideVCenterExcept (keep)
    {
        this.xState.init(this);

        this.xState.endTop = (this.element.getHeight() - keep) / 2;
        this.xState.endBottom = (this.element.getHeight() + keep) / 2;

        this._showStart(this._reclipDec);
    }

    this.hideVUp = hideVUp;
    function hideVUp ()
    {
        this.xState.init(this);

        this.xState.endBottom = 0;

        this._showStart(this._reclipDec);
    }

    this.hideVUpExcept = hideVUpExcept;
    function hideVUpExcept (keep)
    {
        this.xState.init(this);

        this.xState.endBottom = keep;

        this._showStart(this._reclipDec);
    }

    this.hideVDown = hideVDown;
    function hideVDown ()
    {
        this.xState.init(this);

        this.xState.endTop = this.element.getHeight();

        this._showStart(this._reclipDec);
    }

    this.hideVDownExcept = hideVDownExcept;
    function hideVDownExcept (keep)
    {
        this.xState.init(this);

        this.xState.endTop = this.element.getHeight() - keep;

        this._showStart(this._reclipDec);
    }

    this.hideHCenter = hideHCenter;
    function hideHCenter ()
    {
        this.xState.init(this);

        this.xState.endLeft = this.element.getWidth() / 2;
        this.xState.endRight = this.element.getWidth() / 2;

        this._showStart(this._reclipDec);
    }


    this.hideHCenterExcept = hideHCenterExcept;
    function hideHCenterExcept (keep)
    {
        this.xState.init(this);

        this.xState.endLeft = (this.element.getWidth() - keep) / 2;
        this.xState.endRight = (this.element.getWidth() + keep) / 2;

        this._showStart(this._reclipDec);
    }

    this.hideHRight = hideHRight;
    function hideHRight ()
    {
        this.xState.init(this);

        this.xState.endLeft = this.element.getWidth();

        this._showStart(this._reclipDec);
    }

    this.hideHRightExcept = hideHRightExcept;
    function hideHRightExcept (keep)
    {
        this.xState.init(this);

        this.xState.endLeft = this.element.getWidth() - keep;

        this._showStart(this._reclipDec);
    }

    this.hideHLeft = hideHLeft;
    function hideHLeft ()
    {
        this.xState.init(this);

        this.xState.endRight = 0;

        this._showStart(this._reclipDec);
    }

    this.hideHLeftExcept = hideHLeftExcept;
    function hideHLeftExcept (keep)
    {
        this.xState.init(this);

        this.xState.endRight = keep;

        this._showStart(this._reclipDec);
    }










    this._reclipInc = _reclipInc;
    function _reclipInc ()
    {
        if (this.xState.stateLeft < this.xState.endLeft || 
            this.xState.stateRight > this.xState.endRight ||
            this.xState.stateTop < this.xState.endTop ||
            this.xState.stateBottom > this.xState.endBottom)
        {
            if (this.xState.endTop == 0 &&
                this.xState.endLeft == 0 &&
                this.xState.endRight == this.element.getWidth() &&
                this.xState.endBottom == this.element.getHeight())
            {
                this.element.setClip(null);
                if (this.xState.bgiframe != null)
                    this.xState.bgiframe.setClip(null);
            }
            else
            {
                this.element.setClipRect(this.xState.endTop, this.xState.endRight, this.xState.endBottom, this.xState.endLeft);
                if (this.xState.bgiframe != null)
                    this.xState.bgiframe.setClipRect(this.xState.endTop, this.xState.endRight, this.xState.endBottom, this.xState.endLeft);
            }
            this._showDone();
        }
        else
        {
            this.element.setClipRect(this.xState.stateTop, this.xState.stateRight, this.xState.stateBottom, this.xState.stateLeft);
            if (this.xState.bgiframe != null)
                this.xState.bgiframe.setClipRect(this.xState.stateTop, this.xState.stateRight, this.xState.stateBottom, this.xState.stateLeft);

            this.xState.stateTop = this.xState.stateTop - this.xState.stepTop;
            this.xState.stateRight = this.xState.stateRight + this.xState.stepRight;
            this.xState.stateBottom = this.xState.stateBottom + this.xState.stepBottom;
            this.xState.stateLeft = this.xState.stateLeft - this.xState.stepLeft;
        }
    }


    this._reclipDec = _reclipDec;
    function _reclipDec ()
    {
        if (this.xState.stateLeft > this.xState.endLeft || 
            this.xState.stateRight < this.xState.endRight ||
            this.xState.stateTop > this.xState.endTop ||
            this.xState.stateBottom < this.xState.endBottom)
        {
            if (this.xState.endTop == this.element.getHeight() ||
                this.xState.endLeft == this.element.getWidth() ||
                this.xState.endRight == 0 ||
                this.xState.endBottom == 0)
            {
                this.element.hide();
                if (this.xState.bgiframe != null)
                    this.xState.bgiframe.hide();
            }
            else
            {
                this.element.setClipRect(this.xState.endTop, this.xState.endRight, this.xState.endBottom, this.xState.endLeft);
                if (this.xState.bgiframe != null)
                    this.xState.bgiframe.setClipRect(this.xState.endTop, this.xState.endRight, this.xState.endBottom, this.xState.endLeft);
            }
            this._showDone();
        }
        else
        {
            this.element.setClipRect(this.xState.stateTop, this.xState.stateRight, this.xState.stateBottom, this.xState.stateLeft);
            if (this.xState.bgiframe != null)
                this.xState.bgiframe.setClipRect(this.xState.stateTop, this.xState.stateRight, this.xState.stateBottom, this.xState.stateLeft);

            this.xState.stateTop = this.xState.stateTop + this.xState.stepTop;
            this.xState.stateRight = this.xState.stateRight - this.xState.stepRight;
            this.xState.stateBottom = this.xState.stateBottom - this.xState.stepBottom;
            this.xState.stateLeft = this.xState.stateLeft + this.xState.stepLeft;
        }
    }




    this.showTimer = showTimer;
    function showTimer ()
    {
        if (this.xMethod == null)
            return;
        this.xMethod();
    }

}

function NAWAShowerState ()
{
    this.delay = 1;

    this.stepTop = 0;
    this.stepBottom = 0;
    this.stepRight = 0;
    this.stepLeft = 0;

    this.stateTop = 0;
    this.stateBottom = 0;
    this.stateRight = 0;
    this.stateLeft = 0;

    this.endTop = 0;
    this.endBottom = 0;
    this.endRight = 0;
    this.endLeft = 0;

    this.init = init;

    function init (shower)
    {
        this.delay = shower.speedDelay;

        var e = shower.getElement();

        this.stepTop = 0;
        this.stepBottom = 0;
        this.stepRight = 0;
        this.stepLeft = 0;

        this.stateTop = 0;
        this.stateBottom = e.getHeight();
        this.stateRight = e.getWidth();
        this.stateLeft = 0;

        this.endTop = 0;
        this.endBottom = e.getHeight();
        this.endRight = e.getWidth();
        this.endLeft = 0;

//        shower.element.iframebgInit(null);//shower.element.getDOMId() + '__iframebg');
//        shower.element.iframebgAlign();
    }

}




