// metodo mancante in IE
if (!Array.prototype.indexOf)
  Array.prototype.indexOf = function(elt, from)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };

function isElement(o){
    return (
        typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
        typeof o === "object" && o.nodeType === 1 && typeof o.nodeName==="string"
    );
}
function xhrObj() {
    if (window.ActiveXObject) {
        var i, t = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
        for (i=0; i<t.length; i++)
            try { return new ActiveXObject(t[i]); } catch(e){}
    }
    if(window.XMLHttpRequest) return new XMLHttpRequest();
    if(window.createRequest) return window.createRequest();
    return null;
} // xhrObj

function load(url, options) {
    if (!options) options = {};
    if (options.nocache) url += "&"+Math.random();
    var x = xhrObj();
    x.open('GET', url, options.async || false);
    x.send(null);
    return (x.readyState != 4 || x.status < 200 || x.status > 200) ? false : x.responseText;
} // load

function globalEval (code) {
    if (typeof code != "string")
        return false;
    code = (code || "").replace( /^\s+|\s+$/g, "" );
    if (!code) return;
    
	var head = document.getElementsByTagName("head")[0] || document.documentElement,
		script = document.createElement("script");

	script.type = "text/javascript";
	script.text = code;
	// Use insertBefore instead of appendChild  to circumvent an IE6 bug.
	// This arises when a base node is used (#2709).
	return head.insertBefore( script, head.firstChild )
	   && head.removeChild( script );
} // globalEval

function include(urls, options) {
    if (!options) options = {};
    if (!include.already) include.already = {}; // init
    
    if (typeof urls == 'string') urls = [urls]; // support for single url: convert to array
    
    // downward loop, because we remove some elements run-time
    var i = urls.length;    
    while (i--) {
        urls[i] = (options.pre || '')+urls[i]+(options.post || '');
        if (include.already[urls[i]])
            urls.splice(i,1); // already loaded, skip it
    }

    //for (var i=0; i < urls.length; i++) load(urls[i], {async:true}); // preload
    var ret = true;
    for (var i=0; i < urls.length; i++) {
        var res = load(urls[i], options);
        if (res === false) { 
            ret = false;
            continue;
        }
        include.already[urls[i]] = 1;
        globalEval(res);
    }                     
    return ret;
} // include

function includeLib(libs) {
    if (!include.already) include.already = {};
    if (typeof libs == 'string') libs = libs.split(':');
    // downward loop, because we remove some elements run-time
    var i = libs.length;    
    while (i--)
        // empty or already loaded, skip it
        if (!libs[i].length || include.already[libs[i]])
            libs.splice(i,1); 
    if (!libs.length) return;
    include('?getjs='+libs.join(':'));
} // includeLib
