/*****************************************************************************
 ***                                                                       ***
 *** DOMutils V1.0                                                         ***
 ***                                                                       ***
 ***                              Stefan Haglund, 5D Systemkonsult AB 2011 ***
 ****************************************************************************/

    var bDOMFirstEntry=true;


    function containsDOM (container, containee)
    {
      var isParent = false;

      do {
        if((isParent = container == containee)) break;
        containee = containee.parentNode;
      } while (containee != null);

      return isParent;
    }

    function resetEventControl()
    {
        bDOMFirstEntry=true;
    }


    function checkMouseEnter(element, evt)
    {
        if(bDOMFirstEntry) {
            bDOMFirstEntry=false; return(true);
        }
        return(false);
    }

    function checkMouseLeave(element, evt)
    {
        if(element.contains && evt.toElement) {
            if(!element.contains(evt.toElement)) {
                bDOMFirstEntry=true; return(true);
            }
        } else {
            if(evt.relatedTarget) {
                if(!containsDOM(element, evt.relatedTarget)) {
                    bDOMFirstEntry=true; return(true);
                }
            }
        }
        return(false);
    }


    function getClosestAncestor(oElement,sTagName)
    {
        while(oElement&&oElement.tagName!=sTagName) {
            oElement=oElement.parentNode;
        }
        return oElement;
    }

    function getClosestDescendant(oElement,sTagName)
    {
        var i;
        var oChildNode,oDescendantNode;

        if(oElement&&oElement.hasChildNodes) {
            for(i=0;i<oElement.childNodes.length;i++) {
                oChildNode=oElement.childNodes[i];
                if(oChildNode.nodeName==sTagName) { return(oChildNode); }
            }
            for(i=0;i<oElement.childNodes.length;i++) {
                oChildNode=oElement.childNodes[i];
                oDescendantNode=getClosestDescendant(oChildNode,sTagName)
                if(oDescendantNode) return(oDescendantNode);
            }
        }
        return(null);
    }


    function getElementPositionLeft(oElement)
    {
        var iLeft=0;

        while(oElement) {
            iLeft+=oElement.offsetLeft;
            oElement=oElement.offsetParent;
        }
        return iLeft;
    }

    function getElementPositionTop(oElement)
    {
        var iTop=0;

        while(oElement) {
            iTop+=oElement.offsetTop;
            oElement=oElement.offsetParent;
        }
        return iTop;
    }

/****************************************************************************/

