if (typeof(sonar) != "object" || sonar == null) sonar = {};
//==============================================================================
// @library
//      sonar.element
// @functions
//      getElementsByClassName(oid, cls)
//      getChildElements(oid)
//      getChildElementById(oid, id)
//      getChildElementsByName(oid, name)
//      getChildElementsByTagName(oid, tag)
//      getChildElementsByClassName(oid, cls)
//      getPositionX(oid)
//      getPositionY(oid)
//      getCurrentIndex(oid)
//      movePreviousIndex(oid, args)
//      moveNextIndex(oid, args)
//==============================================================================
sonar.element =
{
    /***************************************************************************
     * @function
     *      getElementsByClassName(oid, cls)
     **************************************************************************/
    getElementsByClassName : function(oid, cls)
    {
        oid = typeof(oid) == "object" && oid != null && (oid.nodeType == 1 || oid.nodeType == 9) ? oid : document.getElementById(oid);
        cls = typeof(cls) == "string" && cls.length > 0 ? cls.split(" ") : null;

        if (oid && cls)
        {
            var list      = [];
            var children  = oid.getElementsByTagName("*");
            var attribute = null;
            var test      = false;

            for (var i = 0; i < children.length; i++)
            {
                if (children[i].nodeType == 1)
                {
                    attribute = children[i].className ? children[i].className : children[i].getAttribute("class");

                    if (typeof(attribute) == "string" && attribute.length > 0)
                    {
                        for (var x = 0; x < cls.length; x++)
                        {
                            test = new RegExp("(^|\\s)" + cls[x] + "(\\s|$)").test(attribute);

                            if (!test) break;
                        }

                        if (test) list.push(children[i]);
                    }
                }
            }

            return list;
        }
    },



    /***************************************************************************
     * @function
     *      getChildElements(oid)
     **************************************************************************/
    getChildElements : function(oid)
    {
        oid = typeof(oid) == "object" && oid != null && (oid.nodeType == 1 || oid.nodeType == 9) ? oid : document.getElementById(oid);

        if (oid)
        {
            var list      = [];
            var children  = oid.childNodes;
            var attribute = null;

            for (var i = 0; i < children.length; i++)
            {
                if (children[i].nodeType == 1) list.push(children[i]);
            }

            return list;
        }
    },



    /***************************************************************************
     * @function
     *      getChildElementById(oid, id)
     **************************************************************************/
    getChildElementById : function(oid, id)
    {
        oid = typeof(oid) == "object" && oid != null && (oid.nodeType == 1 || oid.nodeType == 9) ? oid : document.getElementById(oid);
        id  = typeof(id) == "string" && id.length > 0 ? id : null;

        if (oid && id)
        {
            var children  = oid.childNodes;
            var attribute = null;

            for (var i = 0; i < children.length; i++)
            {
                if (children[i].nodeType == 1)
                {
                    attribute = children[i].id ? children[i].id : children[i].getAttribute("id");

                    if (attribute == id) return children[i];
                }
            }
        }
    },



    /***************************************************************************
     * @function
     *      getChildElementsByName(oid, name)
     **************************************************************************/
    getChildElementsByName : function(oid, name)
    {
        oid  = typeof(oid) == "object" && oid != null && (oid.nodeType == 1 || oid.nodeType == 9) ? oid : document.getElementById(oid);
        name = typeof(name) == "string" && name.length > 0 ? name : null;

        if (oid && name)
        {
            var list      = [];
            var children  = oid.childNodes;
            var attribute = null;

            for (var i = 0; i < children.length; i++)
            {
                if (children[i].nodeType == 1)
                {
                    attribute = children[i].name ? children[i].name : children[i].getAttribute("name");

                    if (attribute == name) list.push(children[i]);
                }
            }

            return list;
        }
    },



    /***************************************************************************
     * @function
     *      getChildElementsByTagName(oid, tag)
     **************************************************************************/
    getChildElementsByTagName : function(oid, tag)
    {
        oid = typeof(oid) == "object" && oid != null && (oid.nodeType == 1 || oid.nodeType == 9) ? oid : document.getElementById(oid);
        tag = typeof(tag) == "string" && tag.length > 0 ? tag.toLowerCase() : null;

        if (oid && tag)
        {
            var list      = [];
            var children  = oid.childNodes;
            var attribute = null;

            for (var i = 0; i < children.length; i++)
            {
                if (children[i].nodeType == 1)
                {
                    attribute = children[i].tagName.toLowerCase();

                    if (attribute == tag) list.push(children[i]);
                }
            }

            return list;
        }
    },



    /***************************************************************************
     * @function
     *      getChildElementsByClassName(oid, cls)
     **************************************************************************/
     getChildElementsByClassName : function(oid, cls)
     {
        oid = typeof(oid) == "object" && oid != null && (oid.nodeType == 1 || oid.nodeType == 9) ? oid : document.getElementById(oid);
        cls = typeof(cls) == "string" && cls.length > 0 ? cls.split(" ") : null;

        if (oid && cls)
        {
            var list      = [];
            var children  = oid.childNodes;
            var attribute = null;
            var test      = false;

            for (var i = 0; i < children.length; i++)
            {
                if (children[i].nodeType == 1)
                {
                    attribute = children[i].className ? children[i].className : children[i].getAttribute("class");

                    if (typeof(attribute) == "string" && attribute.length > 0)
                    {
                        for (var x = 0; x < cls.length; x++)
                        {
                            test = new RegExp("(^|\\s)" + cls[x] + "(\\s|$)");

                            if (!test) break;
                        }

                        if (test) list.push(children[i]);
                    }
                }
            }

            return list;
        }
     },



    /***************************************************************************
     * @function
     *      getPositionX(oid)
     **************************************************************************/
    getPositionX : function(oid)
    {
        oid = typeof(oid) == "object" && oid != null && oid.nodeType == 1 ? oid : document.getElementById(oid);

        if (oid)
        {
            var x = 0;

            while (oid && oid != document.body)
            {
                x   += oid.offsetLeft;
                oid  = oid.offsetParent;
            }

            return x;
        }
    },



    /***************************************************************************
     * @function
     *      getPositionY(oid)
     **************************************************************************/
    getPositionY : function(oid)
    {
        oid = typeof(oid) == "object" && oid != null && oid.nodeType == 1 ? oid : document.getElementById(oid);

        if (oid)
        {
            var y = 0;

            while (oid && oid != document.body)
            {
                y   += oid.offsetTop;
                oid  = oid.offsetParent;
            }

            return y;
        }
    },



    /***************************************************************************
     * @function
     *      getCurrentIndex(oid)
     * @used
     *      sonar.element.getChildElements(oid)
     **************************************************************************/
    getCurrentIndex : function(oid)
    {
        oid = typeof(oid) == "object" && oid != null && oid.nodeType == 1 ? oid : document.getElementById(oid);

        if (oid && oid.parentNode)
        {
            var sibling = sonar.element.getChildElements(oid.parentNode);

            for (var i = 0; i < sibling.length; i++)
            {
                if (sibling[i] == oid) return i;
            }
        }
    },



    /***************************************************************************
     * @function
     *      movePreviousIndex(oid, args)
     * @arguments
     *      minIndex ==> (0)
     *      maxIndex ==> (length-1)
     *      doRotate ==> true | (false)
     * @used
     *      sonar.element.getChildElements(oid)
     *      sonar.element.getCurrentIndex(oid)
     **************************************************************************/
    movePreviousIndex : function(oid, args)
    {
        oid  = typeof(oid) == "object" && oid != null && oid.nodeType == 1 ? oid : document.getElementById(oid);
        args = typeof(args) == "object" && args != null ? args : {};

        if (oid && oid.parentNode)
        {
            var parents = oid.parentNode;
            var sibling = sonar.element.getChildElements(parents);
            var index   = sonar.element.getCurrentIndex(oid);

            args.minIndex = /^[0-9]+$/.test(args.minIndex) ? parseInt(args.minIndex) : 0;
            args.maxIndex = /^[0-9]+$/.test(args.maxIndex) ? parseInt(args.maxIndex) : sibling.length - 1;
            args.doRotate = /^true$/i.test(args.doRotate);

            if (args.minIndex >= sibling.length) args.minIndex = sibling.length - 1;
            if (args.maxIndex >= sibling.length) args.maxIndex = sibling.length - 1;

            if (args.minIndex < args.maxIndex)
            {
                if (index - 1 >= args.minIndex)
                {
                    parents.insertBefore(oid, sibling[index - 1]);
                }
                else if (args.doRotate)
                {
                    if (args.maxIndex + 1 < sibling.length)
                    {
                        parents.insertBefore(oid, sibling[args.maxIndex + 1]);
                    }
                    else
                    {
                        parents.appendChild(oid);
                    }
                }
            }
        }
    },



    /***************************************************************************
     * @function
     *      moveNextIndex(oid, args)
     * @arguments
     *      minIndex ==> (0)
     *      maxIndex ==> (length-1)
     *      doRotate ==> true | (false)
     * @used
     *      sonar.element.getChildElements(oid)
     *      sonar.element.getCurrentIndex(oid)
     **************************************************************************/
    moveNextIndex : function(oid, args)
    {
        oid  = typeof(oid) == "object" && oid != null && oid.nodeType == 1 ? oid : document.getElementById(oid);
        args = typeof(args) == "object" && args != null ? args : {};

        if (oid && oid.parentNode)
        {
            var parents = oid.parentNode;
            var sibling = sonar.element.getChildElements(parents);
            var index   = sonar.element.getCurrentIndex(oid);

            args.minIndex = /^[0-9]+$/.test(args.minIndex) ? parseInt(args.minIndex) : 0;
            args.maxIndex = /^[0-9]+$/.test(args.maxIndex) ? parseInt(args.maxIndex) : sibling.length - 1;
            args.doRotate = /^true$/i.test(args.doRotate);

            if (args.minIndex >= sibling.length) args.minIndex = sibling.length - 1;
            if (args.maxIndex >= sibling.length) args.maxIndex = sibling.length - 1;

            if (args.minIndex < args.maxIndex)
            {
                if (index + 1 <= args.maxIndex)
                {
                    if (index + 2 < sibling.length)
                    {
                        parents.insertBefore(oid, sibling[index + 2]);
                    }
                    else
                    {
                        parents.appendChild(oid);
                    }
                }
                else if (args.doRotate)
                {
                    parents.insertBefore(oid, sibling[args.minIndex]);
                }
            }
        }
    }
}