/**
 * Provide a base object and the JSX global.
 * @author Uppercase Development Inc
 * @since December 2004
 */

function com_gorad_lang_Object(arg){
	if(this.constructor!=arguments.callee) {
		return new com_gorad_lang_Object(arg);
    }

    this.__properties = {};
    for (var i in arg) {
        this[i] = arg[i];
    }
}

com_gorad_lang_Object.prototype.get = function(k, i){
	if (typeof i == 'undefined') {
		return this.__properties[k];
	}
	return this.__properties[k][i];
}

com_gorad_lang_Object.prototype.getClone = function(){
	return this.clone();
}

// Creates this.parent with current function definitions.
com_gorad_lang_Object.prototype.extendClass = function(p){
    this.parent = (typeof p == 'object') ? p.clone() : this.cloneFunctions();
}

com_gorad_lang_Object.prototype.pop = function(k){
	if(typeof this.__properties[k] != "object") {
		this.__properties[k] = new Array();
		return null;
	}
	var v = this.__properties[k][this.__properties[k].length];

	return v;
}

com_gorad_lang_Object.prototype.push = function(k,v){
	if(typeof this.__properties[k] != "object") {
		this.__properties[k] = new Array();
	}
	this.__properties[k][this.__properties[k].length] = v;
}

// com_gorad_lang_Object.prototype.search.call([0,1,2,3], 2);
com_gorad_lang_Object.prototype.search = function(v){
    if (typeof this.__properties == 'object') {
        var p = this.__properties;
    } else {
        var p = this;
    }
    for (var k in p) {
        try {
            if (v == p[k])
                return k;
        } catch (e) {

        }
    }
    return null;
}

com_gorad_lang_Object.prototype.set = function(k,v){
    if (typeof k == 'object') {
        for (var i in k) {
            this.__properties[i] = k[i];
        }
    } else {
    	this.__properties[k] = v;
    }
    return this;
}

//Object_prototype_clone = function(depth) {
//    if (isNaN(depth)) {
//        depth = 2;
//    } else if (depth == 0) {
//        return this;
//    }
//
//	var o = {};
//	for (var i in this) {
//	    try {
//    	    if (typeof this[i] == 'object' && this[i].length && typeof this[i].clone == 'function') {
//    	        o[i] = this[i].clone(depth-1);
//    	        continue;
//	        } else {
//                o[i] = this[i];
//	        }
//	    } catch (e) {
//	    }
//	}
//
//	return o;
//}


//Object.prototype.cloneFunctions = function() {
//	var o = {};
//	for (var i in this)
//	    if (typeof this[i] == 'function')
//	        o[i] = this[i];
//}



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

var JSX = {};
JSX.__constructor=function(){
	this.path='/jsx/';
	this.eventtargets = [[]];
	this.eventcompleted = [];
	this.included=new Array();
    this.context = com_gorad_lang_Object();
    this.util = {};
	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;
}

/**
 * Preferred alternative to the Firebug console
 */
JSX.console = {};

JSX.console.log = function(message){
    if (typeof console != 'undefined' && typeof console.log != 'undefined') {
        // call console.log with all passed arguments
        var console = console.log;
        console.apply(this, arguments);
    }
}


JSX.console.trace = function(){
    if (typeof console.trace != 'undefined') {
        console.trace();
    }
}

JSX.__constructor();
