/**
 * Define the JSX class with the basic utilities.
 */

var JSX = {};
JSX.__constructor=function(){
	this.path='/jsx/';
	this.eventtargets = [[]];
	this.eventcompleted = [];
	this.included=new Array();
	if (typeof window.onload != 'function') {
		window.onload = JSX_onPageLoad;
	} else {
		var old = window.onload;
		window.onload = function() {
			old();
			JSX_onPageLoad();
		}
	}
}

JSX.isDefined = function(a) {
	if (typeof self[a] != "undefined") {
		return true;
	}
	return false;
}

JSX_onPageLoad = function() {
	JSX.onPageLoad();
}
JSX.onPageLoad = function() {
	this.eventcompleted[0] = true;
	for (var i = 0; i < this.eventtargets[0].length; i++) {
		this.eventtargets[0][i].onPageLoad();
	}
}

JSX.setListener = function(t, o) {
	this.eventtargets[t][this.eventtargets[t].length] = o;
	if (this.eventcompleted[t]) {
		if (t == 0) {
			o.onPageLoad();
		}
	}
}

JSX.setPath=function(p){
	this.path=p
}

JSX.useClass=function(a, cb){
	var src = this.path + a.replace(/[\._]/g, '/');
	var ret = a.replace(/[\/\.]/g, '_');
	
	if (this.isDefined(ret)) {
		if (typeof cb == "undefined") {
			return ret;
		}
		cb.call(this, true);
		return ret;
	}

	if (a.indexOf('!') == -1) {
		src += '.class.js';
	}
	else {
		src = src.substring(0, src.indexOf('!')) + a.substr(a.indexOf('!') + 1);
	}
	
	if (this.included[src] == 1) {
		return;
	}
	this.included[src] = 1;
	if (document.body == "undefined" || document.body == null) {
		document.write('<script type="text/javascript" language="JavaScript" src="'+src+'"><\/script>');
	}
	else {
		var h = document.getElementsByTagName('head').item(0);
	    var js = document.createElement('script');
	    js.setAttribute('language', 'javascript');
	    js.setAttribute('type', 'text/javascript');
	    js.setAttribute('src', src);
		if (typeof cb == "undefined") {
		}
		else if (js.addEventListener) {
			js.addEventListener("load", cb, false);
		}
		else {
			js.onreadystatechange = function() {
				if (this.readyState == "complete") cb.call(this);
			}
		}
	    h.appendChild(js);
	}
	
	return ret;
}

JSX.__constructor();
