/**
 * function whenDOMReady
 * Copyright (C) 2006-2007 Dao Gottwald
 *
 * 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
 *
 * Contact information:
 *   Dao Gottwald  <dao at design-noir.de>
 *
 * @version  1.3
 * @url      http://design-noir.de/webdev/JS/whenDOMReady/
 */

function whenDOMReady(fn) {
	var f = arguments.callee;
	if ("listeners" in f) { // already initialized
		if (f.listeners) // still loading
			f.listeners.push(fn);
		else // DOM is ready
			fn();
		return;
	}
	f.listeners = [fn];
	f.callback = function() {
		removeEvent(window, "load", f.callback);
		if (document.removeEventListener)
			document.removeEventListener("DOMContentLoaded", f.callback, false);
		if (f.listeners) {
			while (f.listeners.length)
				f.listeners.shift()();
			f.listeners = null;
		}
	};
	if (document.addEventListener)
		document.addEventListener("DOMContentLoaded", f.callback, false);
	/*@cc_on @if (@_win32) else
		document.write("<script defer src=\"//:\""+
		               " onreadystatechange=\"if (this.readyState == 'complete')"+
		               " whenDOMReady.callback();\"><\/script>");
	@end @*/
	addEvent(window, "load", f.callback);
}

if(!Function.prototype.bind) Function.prototype.bind = function() {
	var __method = this, args = Array.prototype.slice.call(arguments), object = args.shift();
	return function() {
		return __method.apply(object, args.concat(args.slice.call(arguments)));
	}
};

/**
* getElementsByClassName by Fabian Grutschus a.k.a crash
*
* @param string The className
* @param object The DOM-Node to search in (optional, when not set, uses document)
* @return nodelist The elements with the specified class name
*/
var getElementsByClassName =
    document.getElementsByClassName ? function (class_name, scope) {
        return (scope || document).getElementsByClassName(class_name);
    } :
    document.evaluate ? function (class_name, scope) {
        var re = [];
        var xpathResult = document.evaluate(".//*[contains(concat(' ', @class, ' '), ' " + class_name + " ')]", scope || document, null, 0, null);
        var ele;
        while ((ele = xpathResult.iterateNext()))
            re.push(ele)
        return re;
    } :
    function (class_name, scope) {
        var re = [];
        var elements = (scope || document).getElementsByTagName('*');
        for (var i = 0, ele; ele = elements[i]; i++) {
            if (ele.className && ele.className.split(' ').indexOf(class_name) >= 0) {
                re.push(ele);
            }
        }
        return re;
    };

if (Array.indexOf === undefined) {
    Array.prototype.indexOf = function (val) {
        for (var i = 0, ele; ele = this[i]; i++) {
            if (ele === val) {
                return i;
            }
        }
        return -1;
    }
}
if (Array.push === undefined) {
    Array.prototype.push = function (arr) {
        this[this.length] = arr;
    }
}