/*
 * 	hostenv_browser.js
 */
hostenv = {
	globals: this,
	browser: {},
	loadUriMethod: "GET"
};

var djConfig = {
	isDebug: true,
	debugContainerId: "debugContainer"
};

(function(){
	var dua = navigator.userAgent;
	var dav = navigator.appVersion;
	var browser = hostenv.browser;

	browser.opera = dua.indexOf("Opera") >= 0;
	browser.khtml = (dav.indexOf("Konqueror") >= 0)||(dav.indexOf("Safari") >= 0);
	browser.safari = dav.indexOf("Safari") >= 0;
	var geckoPos = dua.indexOf("Gecko");
	browser.mozilla = browser.moz = (geckoPos >= 0)&&(!browser.khtml);
	if (browser.mozilla) {
		// gecko version is YYYYMMDD
		browser.geckoVersion = dua.substring(geckoPos + 6, geckoPos + 14);
	}
	browser.ie = (document.all)&&(!browser.opera) || false;
	browser.ie50 = browser.ie && dav.indexOf("MSIE 5.0")>=0;
	browser.ie55 = browser.ie && dav.indexOf("MSIE 5.5")>=0;
	browser.ie60 = browser.ie && dav.indexOf("MSIE 6.0")>=0;
	browser.ie70 = browser.ie && dav.indexOf("MSIE 7.0")>=0;

	if(browser.mozilla) {
//		hostenv.loadUriMethod = "POST";
	}
})();


hostenv.getXmlhttpObject = function(){
	this.XMLHTTP_PROGIDS = this.XMLHTTP_PROGIDS || ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];

    var http = null;
	var last_e = null;
	try{ http = new XMLHttpRequest(); }catch(e){}
    if(!http){
		for(var i=0; i<3; ++i){
			var progid = this.XMLHTTP_PROGIDS[i];
			try{
				http = new ActiveXObject(progid);
			}catch(e){
				last_e = e;
			}

			if(http){
				this.XMLHTTP_PROGIDS = [progid];  // so faster next time
				break;
			}
		}
	}

	if(!http){
		jess.raise(last_e);
	}

	return http;
}

hostenv.getText = hostenv.loadUri = function(uri, async_cb, fail_ok) {
    var req = hostenv.getXmlhttpObject();
    if (async_cb) {
        req.onreadystatechange = function () {
        	if (4 == req.readyState && req.status) {
        		if (req.status == 200) {
        			async_cb(req.responseText);
        		}
        	}
        };
    }
    req.open(this.loadUriMethod, uri, async_cb ? true : false);
    if(typeof req.overrideMimeType === "function") {
	    req.overrideMimeType("text/plain");
    }
    try {
        req.send(null);
    } catch (e) {
        if (fail_ok && !async_cb) {
            return null;
        } else {
            throw e;
        }
    }
    if (async_cb) {
        return null;
    }
    return req.responseText;
}

hostenv.loadScript = function(name, evalCb) {
	try {
		var text = hostenv.loadUri("src/" + name);
		if(typeof evalCb !== "function" || evalCb() !== false) {
			/* 	This callback is introduced to workaound feature which was introduced in FF>3.5 (other browser don't seem to this (yet;-));

				The feature manifests itself in the following scenario:
				
					load X (async, typically form definitions)
						callback: 
							if(!modules.contains("A")) {		//this is basically what jess.require does
							
								hostenv.loadScript("src/A.js") 	//this is a blocked operation 
																//however in FF>3,5 callbacks
																//of other requests will be called
																//so that A can already be defined by 
																//another callback
							}
				
					load Y (async, typically form definitions)
						callback: 
							if(!modules.contains("A")) {		//this is basically what jess.require does
							
								hostenv.loadScript("src/A.js") 	//this is a blocked operation 
																//however in FF>3,5 callbacks
																//of other requests will be called
																//so that A can already be defined by 
																//another callback
							}
				
			*/
			eval(text);
		}
	} catch(e) {
		throw Error(String.format("Failed to load script '%s'; %s", name, e.message));
	}
	return text.length;
}

hostenv.provide = function(name) {
	var path = name.split(".");
	var obj = hostenv.globals;
	for(var i = 0, l = path.length; i < l; ++i) {
		if(typeof(obj[path[i]]) == "undefined") {
			obj = obj[path[i]] = {};
		} else {
			obj = obj[path[i]];
		}
	}
	return obj;
}

