/*!
 * Adaptation of the $(document).ready() function from jQuery
 * library for use in simple JavaScript scenarios.
 *
 * ---------------------------------------------------------------------
 * jQuery JavaScript Library v1.4.3
 * http://jquery.com/
 *
 * Copyright (c) 2010 John Resig, http://jquery.com/
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * ----------------------------------------------------------------------
 */

var domReady = (function() {

    var w3c = !!document.addEventListener,
        loaded = false,
        toplevel = false,
        fns = [];

    if (w3c) {
        document.addEventListener("DOMContentLoaded", contentLoaded, true);
        window.addEventListener("load", ready, false);
    }
    else {
        document.attachEvent("onreadystatechange", contentLoaded);
        window.attachEvent("onload", ready);

        try {
            toplevel = window.frameElement === null;
        } catch(e) {}
        if ( document.documentElement.doScroll && toplevel ) {
            scrollCheck();
        }
    }

    function contentLoaded() {
        (w3c)?
            document.removeEventListener("DOMContentLoaded", contentLoaded, true) :
            document.readyState === "complete" &&
            document.detachEvent("onreadystatechange", contentLoaded);
        ready();
    }

    // If IE is used, use the trick by Diego Perini
    // http://javascript.nwbox.com/IEContentLoaded/
    function scrollCheck() {
        if (loaded) {
            return;
        }

        try {
            document.documentElement.doScroll("left");
        }
        catch(e) {
            window.setTimeout(arguments.callee, 15);
            return;
        }
        ready();
    }

    function ready() {
        if (loaded) {
            return;
        }
        loaded = true;

        var len = fns.length,
            i = 0;

        for( ; i < len; i++) {
            fns[i].call(document);
        }
    }

    return function(fn) {
        // if the DOM is already ready,
        // execute the function
        return (loaded)?
            fn.call(document):
            fns.push(fn);
    }
})();

module.exports = domReady;
