document.BASE_URI = "/redesign/public";

/**
 * 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);
}

String.prototype.parseAsXML = window.DOMParser ? function () {
    return new DOMParser().parseFromString(this, "application/xml");
} : window.ActiveXObject ? function () {
    var parser = new ActiveXObject("Microsoft.XMLDOM");
    parser.async = "false";
    parser.loadXML(this);
    return parser;
} : function () {
   var d = document.createElement("div");
   d.innerHTML = this;
   var docFrag = document.createDocumentFragment();
   while (d.firstChild)
       docFrag.appendChild(d.firstChild);
   return docFrag;
}

if (!document.ELEMENT_NODE) {
    document.ELEMENT_NODE = 1;
    document.ATTRIBUTE_NODE = 2;
    document.TEXT_NODE = 3;
    document.CDATA_SECTION_NODE = 4;
    document.ENTITY_REFERENCE_NODE = 5;
    document.ENTITY_NODE = 6;
    document.PROCESSING_INSTRUCTION_NODE = 7
    document.COMMENT_NODE = 8;
    document.DOCUMENT_NODE = 9;
    document.DOCUMENT_TYPE_NODE = 10;
    document.DOCUMENT_FRAGMENT_NODE = 11;
    document.NOTATION_NODE = 12;
}

var importNode = (document.importNode) ? function (node, copy, doc) {
    return (doc || document).importNode(node, copy);
} : function (node, copy, doc) {
    doc = doc || document;
    switch (node.nodeType) {
        case document.ELEMENT_NODE:
            var newNode = doc.createElement(node.nodeName);
            if (node.attributes && node.attributes.length > 0)
                for (var i = 0, il = node.attributes.length; i < il;)
                    newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i++].nodeName));
            if (copy && node.childNodes && node.childNodes.length > 0)
                for (var i = 0, il = node.childNodes.length; i < il;)
                    newNode.appendChild(importNode(node.childNodes[i++], copy, doc));
            return newNode;
            break;
        case document.TEXT_NODE:
        case document.CDATA_SECTION_NODE:
        case document.COMMENT_NODE:
            return doc.createTextNode(node.nodeValue);
            break;
    }
    return false;
};

if (typeof Function.prototype.bind == 'undefined') {
    Function.prototype.bind = function(o) {
        var fn = this;
        var args = Array.prototype.slice.call(arguments, 1);

        return function() {
            fn.apply(o, Array.prototype.slice.call(arguments).concat(args));
        }
    };
}

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;
    }
}

/**
 * getElementsByClassName by Fabian Grutschus a.k.a crash
 *
 * @requires http://moestaverne.com/nopaste/index.php?js_indexOf
 * @param string The className
 * @param object The DOM-Node to search in (optional, when not set, uses document)
 * @return array The elements with the specified class name (always type Array, not a NodeList)
 */
var getElementsByClassName = document.getElementsByClassName ? function (class_name, scope) {
        return [].slice.call((scope || document).getElementsByClassName(class_name));
    } : document.evaluate ? function (class_name, scope) {
        var re = [];
        var scope_doc = scope ? (!scope.ownerDocument ? scope : scope.ownerDocument) : document;
        var xpathResult = scope_doc.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;
    };

function getStyle (obj, style) {
    if (document.defaultView) {
        var realStyle = style.replace(/([a-z])([A-Z])/g, function(dummy, c, c2) { return c + "-" + c2.toLowerCase(); });
        return document.defaultView.getComputedStyle(obj, null).getPropertyValue(realStyle);
    } else {
        var realStyle = style.replace(/-([a-z])/g, function(dummy, c) { return c.toUpperCase(); });
        if (obj.currentStyle) return obj.currentStyle[realStyle];
        else if (document.ids) return obj[realStyle];
        else if (document.all) return obj.style[realStyle];
    }
    return 0;
}

/**
 * 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);
}
