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

var nawaElements = Array();

function nawaGetElement (id)
{
    return document.getElementById(id);
}


function nawaElementCreate (e)
{
    if (e == null)
        return null;
    var id = null;
    if (typeof(e) == "string")
    {
        id = e;
        if (id == '')
            return null;
        e = nawaGetElement(id);
        if (e == null)
            return null;
    }
    var ne = new NAWAElement();

    if (e == null && id == null)
        return null;
    if (id != null)
    {
        ne._id = id;
        nawaElements[id] = ne;
    }
    else
    {
        ne._element = e;
    }
    return ne;
}

function nawaElement (id)
{
    if (id == null || id == '')
        return null;
    var e = nawaElements[id];    
    if (e == null)
    {
        e = nawaElementCreate(id);
        if (e != null)
            nawaElements[id] = e;
    }
    return e;
}

function NAWAElement ()
{
    this._id = null;
    this._element = null;
    this._parent = null;

    this.shower = null;
    this.scroller = null;
    this.mover = null;
    this.iframebg = null;

    this.getDOMElement = getDOMElement;
    function getDOMElement ()
    {
        if (this._id == null)
            return this._element;
        else
            return nawaGetElement(this.getDOMId());
    }

    this.getDOMId = getDOMId;
    function getDOMId ()
    {
        return this._id;
    }

    this.getParent = getParent;
    function getParent ()
    {
        if (this._parent != null)
            return this._parent;

        var e = this.getDOMElement();
        if (e == null)
            return null;
        var parent = e.parentNode;
        if (parent == null)
            return null;

        if (parent.id != null && parent.id != '')
        {
            var p = nawaElement(parent.id);
            if (p == null)
            {
                alert('NAWAElement.getParent(): inconsistency! (1)');
                return null;
            }
            this._parent = p;
            return this._parent;
        }

        var ne = nawaElementCreate(e);//new NAWAElement();
        if (ne == null)
        {
            alert('NAWAElement.getParent(): inconsistency! (2)');
            return null;
        }
        this._parent = ne;
        return this._parent;
    }

    this.setWidth = setWidth;
    function setWidth (width)
    {
        this.setStyle('width', width);
        if (this.iframebg != null)
            this.iframebg.setWidth(width);
    }

    this.setHeight = setHeight;
    function setHeight (height)
    {
        this.setStyle('height', height);
        if (this.iframebg != null)
            this.iframebg.setHeight(height);
    }

    this.getWidth = getWidth;
    function getWidth ()
    {
        return this.getDOMElement().offsetWidth;
    }
    
    this.getHeight = getHeight;
    function getHeight ()
    {
        return this.getDOMElement().offsetHeight;
    }

    this.getTop = getTop;
    function getTop ()
    {
        return this.getDOMElement().offsetTop;
    }

    this.getLeft = getLeft;
    function getLeft ()
    {
        return this.getDOMElement().offsetLeft;
    }

    this.setPosition = setPosition;
    function setPosition (top, left)
    {
        this.setTop(top);   
        this.setLeft(left);
        if (this.iframebg != null)
            this.iframebg.setPosition(top, left);
    }

    this.setTop = setTop;
    function setTop (top)
    {
        this.setStyle('top', top);
        if (this.iframebg != null)
            this.iframebg.setTop(top);
    }

    this.setTopOffset = setTopOffset;
    function setTopOffset (offset)
    {
        this.setTop(this.getTop() + offset);
        if (this.iframebg != null)
            this.iframebg.setTopOffset(offset);
    }

    this.setLeft = setLeft;
    function setLeft (left)
    {
        this.setStyle('left', left);
        if (this.iframebg != null)
            this.iframebg.setLeft(left);
    }

    this.setLeftOffset = setLeftOffset;
    function setLeftOffset (offset)
    {
        this.setLeft(this.getLeft() + offset);
        if (this.iframebg != null)
            this.iframebg.setLeftOffset(offset);
    }

    this.setRight = setRight;
    function setRight (right)
    {
        this.setLeft(right - this.getWidth());
        if (this.iframebg != null)
            this.iframebg.setRight(right);
    }

    this.setBottom = setBottom;
    function setBottom (bottom)
    {
        this.setTop(bottom - this.getHeight());
        if (this.iframebg != null)
            this.iframebg.setBottom(bottom);
    }

    this.setFromRight = setFromRight;
    function setFromRight (right)
    {
        this.setLeft(nawaGetAreaWidth() - this.getWidth() - right);
        if (this.iframebg != null)
            this.iframebg.setFromRight(right);
    }

    this.setFromBottom = setFromBottom;
    function setFromBottom (bottom)
    {
        this.setTop(nawaGetAreaHeight() - this.getHeight() - bottom);
        if (this.iframebg != null)
            this.iframebg.setFromBottom(bottom);
    }

    this.setStyle = setStyle;
    function setStyle (style, value)
    {
        this.getDOMElement().style[style] = value;
    }

    this.getStyle = getStyle;
    function getStyle (style)
    {
        return this.getDOMElement().style[style];
    }

    this.setClip = setClip;
    function setClip (clip)
    {
        if (clip == null)
        {
            if (nawaBrowser.isGecko())
                this.setStyle('clip', null);
            else
                this.setStyle('clip', "rect(0px," + this.getWidth() + " px," + this.getHeight() + "px,0px)");
        }
        else
        {
            this.setStyle('clip', clip);
        }
        if (this.iframebg != null)
            this.iframebg.setClip(clip);
    }

    this.setClipRect = setClipRect;
    function setClipRect (top, right, bottom, left)
    {
        this.setStyle('clip', "rect(" + top + "px," + right + "px," + bottom + "px," + left + "px)");
        if (this.iframebg != null)
            this.iframebg.setClipRect (top, right, bottom, left);
    }

    this.isInArea = isInArea;
    function isInArea ()
    {
        var top = this.getTop();
        var left = this.getLeft();
        if ((top > nawaGetAreaHeight()) || (left > nawaGetAreaWidth()) || (left + this.getWidth() < 0) || (top + this.getHeight() < 0))
            return false;
        return true;
    }

    this.show = show;
    function show ()
    {
        this.setStyle('visibility', "visible");
        if (this.iframebg != null)
            this.iframebg.show ();
    }

    this.hide = hide;
    function hide ()
    {
        this.setStyle('visibility', "hidden");
        if (this.iframebg != null)
            this.iframebg.hide ();
    }

    this.hideOut = hideOut;
    function hideOut ()
    {
        this.hide();
        /* We move the element to the left to remove the scrollbars in
         * the case the move have grown the area.
         * This case even if the window is resized, the scrollbars won't
         * be shown because of the hidden element.
         */
        this.setPosition(0, (-1 * this.getWidth()));
    }



    this.setPositionCenter = setPositionCenter;
    function setPositionCenter ()
    {
        this.setTop((nawaGetAreaHeight() - this.getHeight()) / 2);
        this.setLeft((nawaGetAreaWidth() - this.getWidth()) / 2);
        if (this.iframebg != null)
            this.iframebg.setPositionCenter ();
    }

    this.setPositionHCenter = setPositionHCenter;
    function setPositionHCenter ()
    {
        this.setLeft((nawaGetAreaWidth() - this.getWidth()) / 2);
        if (this.iframebg != null)
            this.iframebg.setPositionHCenter ();
    }

    this.setPositionVCenter = setPositionVCenter;
    function setPositionVCenter ()
    {
        this.setTop((nawaGetAreaHeight() - this.getHeight()) / 2);
        if (this.iframebg != null)
            this.iframebg.setPositionVCenter ();
    }


    this.iframebgAlign = iframebgAlign;
    function iframebgAlign ()
    {
        if (this.iframebg == null)
            return;
        this.iframebg.setWidth(this.getWidth());
        this.iframebg.setHeight(this.getHeight());
        this.iframebg.setTop(this.getTop());
        this.iframebg.setLeft(this.getLeft());
        this.iframebg.setStyle('zIndex', this.getStyle('zIndex') - 1);
    }

    this.iframebgInit = iframebgInit;
    function iframebgInit (iframeId)
    {
        if (!nawaBrowser.isIE())
            return null;
        if (this.iframebg != null)
            return this.iframebg;
        var bgId = iframeId;
        if (bgId == null)
        {
            var domId = this.getDOMId();
            if (domId == null)
                return null;
            bgId = domId + '__iframebg';
            var iframeE = nawaGetElement(bgId);
            if (iframeE == null)
            {
                //alert('no iframe element, creating...');
                if (this.getParent().getDOMElement() == null)
                    return null;
                var domParent = this.getParent().getDOMElement();
                var html = domParent.innerHTML;
                var iframeHTML = '<iframe id="' + bgId + '" scrolling="no" frameborder="0" style="position: absolute; top: 0px; left: 0px; visibility: hidden;"></iframe>';
                domParent.innerHTML = iframeHTML + html;
                alert(html);
                this.iframebg = nawaElement(bgId);
                this.iframebgAlign();
                //alert('w: ' + this.getWidth() + ', h: ' + this.getHeight() + ', t: ' + this.getTop() + ', l: ' + this.getLeft());
                //alert('w: ' + this.iframebg.getWidth() + ', h: ' + this.iframebg.getHeight() + ', t: ' + this.iframebg.getTop() + ', l: ' + this.iframebg.getLeft());
                return this.iframebg;
            }
            else
            {
                this.iframebg = nawaElement(bgId);        
                this.iframebgAlign();
                return this.iframebg;
            }
        }
        else
        {
            this.iframebg = nawaElement(bgId);
            this.iframebgAlign();
            return this.iframebg;
        }
    }
}

