/**
 * 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 (typeof Function.prototype.bind == 'undefined') Function.prototype.bind = function(object) {
    var method = this;
    var args = [].slice.call(arguments, 1);

    return function () {
        method.apply(object, args.concat([].slice.apply(arguments)));
    };
}

/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/
function xhr(method, url, data, cb, apply_para) {
    method = method.toLowerCase();
    var req;
    req = new XMLHttpRequest();
    req.open(method, url + (data && method == 'get' ? '?' + data : ''), true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    if (method == 'post') {
        req.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
        req.setRequestHeader("Content-Length", data.length);
    }
    req.onreadystatechange = function() {
        if (req.readyState == 4 && req.status == 200) {
            if (cb) {
                cb.apply(null, [req].concat(apply_para));
            }
        }
    }
    req.send(data);
}

/**
 * function loadScript
 * 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.6
 * @url      http://design-noir.de/webdev/JS/loadScript/
 */

function loadScript(url, callback) {
	var f = arguments.callee;
	if (!("queue" in f))
		f.queue = {};
	var queue =  f.queue;
	if (url in queue) { // script is already in the document
		if (callback) {
			if (queue[url]) // still loading
				queue[url].push(callback);
			else // loaded
				callback();
		}
		return;
	}
	queue[url] = callback ? [callback] : [];
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.onload = script.onreadystatechange = function() {
		if (script.readyState && script.readyState != "loaded" && script.readyState != "complete")
			return;
		script.onreadystatechange = script.onload = null;
		while (queue[url].length)
			queue[url].shift()();
		queue[url] = null;
	};
	script.src = url;
	document.getElementsByTagName("head")[0].appendChild(script);
}
