/* Based on OpenLayers.js, Copyright (c) 2005-2008 MetaCarta, Inc. */

/* 
 * @requires prototype.js
 */

(function() {
    var jsfile, agent, docWrite, allScriptTags, host, obj;

    window.amis = {

	 /**
          * Property: _scriptName
          * {String} Relative path of this script.
          */
	_scriptName: "lib/amis.js",

	/**
         * Function: _getScriptLocation
         * Return the path to this script.
         *
         * Returns:
         * {String} Path to this script
         */
	_getScriptLocation: function () {
	    var scriptLocation, scriptName, scripts, src, index, pathLength;

	    scriptLocation = "";
	    scriptName = amis._scriptName;
	    scripts = document.getElementsByTagName('script');
	    for (var i = 0; i < scripts.length; i++) {
		src = scripts[i].getAttribute('src');
		if (src) {
		    index = src.lastIndexOf(scriptName); 
		    // set path length for src up to a query string
		    pathLength = src.lastIndexOf('?');
		    if (pathLength < 0) {
			pathLength = src.length;
		    }
		    // is it found, at the end of the URL?
		    if ((index > -1) && (index + scriptName.length == pathLength)) {
			scriptLocation = src.slice(0, pathLength - scriptName.length);
			break;
		    }
		}
	    }
	    return scriptLocation;
	}
    };

    jsfiles = new Array(
        "amis/Util.js",
	"amis/Type.js",
	"amis/Type/Dataset.js",
        "amis/_search/search.js",
        "amis/explore/explore.js",
        "amis/access/access.js"
    ); // etc.
    jsfiles = new Array("amis/Util.js",
			"amis/Type.js",
			"amis/Type/Dataset.js",
			"amis/_search/search.js",
			"amis/explore/explore.js",
			"amis/access/access.js");
    agent = navigator.userAgent;
    docWrite = (agent.match("MSIE") || agent.match("Safari"));
    host = amis._getScriptLocation() + "lib/";    
    if(docWrite) {
        allScriptTags = new Array(jsfiles.length);
	for (var i = 0; i < jsfiles.length; i++) {
	    allScriptTags[i] = "<script src='" + host + jsfiles[i] + "'></script>"; 
            document.write(allScriptTags.join(""));
	}
    } else {
	obj = function (jsfiles,host) {
	    var timer = setTimeout(function () {
		var s, h;

		for (var i=0; i<jsfiles.length; i++) {
		    s = document.createElement("script");
		    s.src = host + jsfiles[i];
		    h = document.getElementsByTagName("head").length ? 
			document.getElementsByTagName("head")[0] : 
			document.body;
		    h.appendChild(s);
		}
	    }, 1);
	}(jsfiles, host);
    }
})();
