"use strict";
/* getElementsByClassName -- a cross-browser implementation of HTML5s document.getElementByClassName()
 *
 * Copyright (C) 2007-2009  Fabian Grutschus (f.grutschus at lubyte.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * USAGE: var elements = getElementsByClassName("test"); // finds all elements which contains "test" into the class attribute
 *        var elements = getElementsByClassName("test", document.getElementById("myform")); // finds all elements inside the element given by the second parameter
 *        var elements = getElementsByClassName("test myclassname", document.body); // finds elements that contains the classNames "test" and "myclassname" inside the document body
 * NOTE: getElementsByClassName() always returns a array, never a NodeList!
 * ALSO: getElementsByClassName() behaves differently in Quirks mode (case-intensive), like the standard function
 *
 * @version 1.1
 * @param   String       class names to search for (space separated list)
 * @param   HTMLElement  scope to search in (optional)
 * @returns Array        list of elements
 * @url     http://git.lubyte.de/?p=javascript-q.git;a=blob;f=snippets/getElementsByClassName.js
 */

var getElementsByClassName = document.getElementsByClassName ? function (className, scope) {
        return Array.prototype.slice.call((scope || document).getElementsByClassName(className));
    } : document.evaluate ? function (className, scope) {
        scope = scope || document;
        var re = [], xpathResult, ele,
            scopeDocument    = !scope.ownerDocument ? scope : scope.ownerDocument,
            searchClassNames = getElementsByClassName.quirksCheck(className.trim(), scope).split(" "),
            searchClassName  = searchClassNames[0],
            searchClassNameLength = searchClassNames.length;

        if (scopeDocument.compatMode === "BackCompat") {
            xpathResult = scopeDocument.evaluate(".//*[contains(concat(' ', translate(@class, 'ABCDEFGHIJKLMNOPQRSTUVWYXZ', 'abcdefghijklmnopqrstuvwyxz'), ' '), ' " + searchClassName + " ')]", scope, null, 0, null);
        } else {
            xpathResult = scopeDocument.evaluate(".//*[contains(concat(' ', @class, ' '), ' " + searchClassName + " ')]", scope, null, 0, null);
        }

        if (searchClassNameLength > 1) {
            var eleClassNames, classNameLength, i;
            while ((ele = xpathResult.iterateNext())) {
                classNameLength = 1;
                eleClassNames = getElementsByClassName.quirksCheck(ele.className, scope).split(" ");
                check: for (i = 1; searchClassName = searchClassNames[i]; i++) {
                    if (eleClassNames.indexOf(searchClassName) > -1) {
                        classNameLength++;
                        if (classNameLength === searchClassNameLength) {
                            re.push(ele);
                            break check;
                        }
                    }
                }
            }
        } else {
            while ((ele = xpathResult.iterateNext())) {
                re.push(ele);
            }
        }

        return re;
    } : function (className, scope) {
        scope = scope || document;
        var re = [], ele, eleClassNames, i = 0,
            elements = scope.getElementsByTagName("*"),
            searchClassNames = getElementsByClassName.quirksCheck(className.trim(), scope).split(" "),
            searchClassName  = searchClassNames[0],
            searchClassNameLength = searchClassNames.length;

        if (searchClassNameLength > 1) {
            var classNameLength, j;
            for (; ele = elements[i]; i++) {
                eleClassNames = getElementsByClassName.quirksCheck(ele.className, scope).split(" ");
                classNameLength = 0;
                check: for (j = 0; searchClassName = searchClassNames[j]; j++) {
                    if (eleClassNames.indexOf(searchClassName) > -1) {
                        classNameLength++;
                        if (classNameLength === searchClassNameLength) {
                            re.push(ele);
                            break check;
                        }
                    }
                }
            }
        } else {
            for (; ele = elements[i]; i++) {
                eleClassNames = getElementsByClassName.quirksCheck(ele.className, scope).split(" ");
                if (eleClassNames.indexOf(searchClassName) > -1) {
                    re.push(ele);
                }
            }
        }

        return re;
    };
getElementsByClassName.quirksCheck = function (className, scope) {
    return (!scope.ownerDocument ? scope : scope.ownerDocument).compatMode === "BackCompat" ? className.toLowerCase() : className;
};

if (typeof Array.prototype.indexOf === "undefined") {
    Array.prototype.indexOf = function (val) {
        for (var i = 0, len = this.length, ele; i < len ; i++) {
            ele = this[i];
            if (ele === val) {
                return i;
            }
        }
        return -1;
    };
}
if (typeof String.prototype.trim === "undefined") {
    String.prototype.trim = function () {
        var str = this.replace(/^\s\s*/, ''), ws = /\s/, i = str.length;
        while (ws.test(str.charAt(--i))) {}
        return str.slice(0, i + 1);
    };
}

