//JAX
jax = {
	onload: function() {}
}


//CLASS OBJECT
jax.c = {

//CLASS OBJECT METHODS
	add: function (p,a) {
		if ( a == undefined ) { return false; }
		var params = "";
		var childs = "";
		var ch = "";
		if ( p != "" ) { for ( var i = 0; i <= ( p.length - 1 ); i++ ) { if ( i != 0 ) { ch = ","; } params += ch + '"' + p[i] + '"'; childs += "this." + p[i] + "=" + p[i] + ";\r\n"; } } 
		for ( var i in a )
		{
			if ( typeof a[i] == "string" ) { a[i] = '"' + a[i] + '"'; }
			childs += "this." + i + "=" + a[i] + ";\r\n";
		}
		if ( p == "" ) { ch = "" } else { ch = ","; }
		eval( "var cls = new Function( " + params + ch +" childs  );" );
		return cls;
	},

	
	instance: {
	
		add: function (id,c,args) {
			if ( jax[c][id] != undefined ) { alert("a [" + c + "] with the id [" + id + "] has already been created."); return false; }
			jax[c][id] = new jax.c[c.toUpperCase()](id,args);
			if ( typeof jax[c][id].init == "function" ) { jax[c][id].init(); }
			return jax[c][id];
		},
	
		info: function (el) {
			var att = "";
			for ( var i in el ) { att += "<b>" + i + "</b> = " + el[i] + "<br/>"; }
			jax.dom.o ("container").innerHTML = att;
		}

	}


};



//SYSTEM MODULES CONFIG AND METHODS
jax.module = {
	path: "jax/modules",
	
	add: function (id) { 
		return jax.c.instance.add (id,"module",{}); 
	}
};

jax.timer = {

	add: function (id, args) {
		return jax.c.instance.add (id,"timer",args)
	}
	
}

jax.ajax = {

	add: function (id, args) {
		return jax.c.instance.add (id,"ajax",args)
	}

}

jax.dom = {

	o: function ( id ) {
		if( typeof id == "object" ) { return id; }
		if ( !document.getElementById(id) ) { return false; }
		else { return document.getElementById(id); }
	},
	
	set: function (id,data) {
		
		var el = jax.dom.o(id);

		if ( el == false ) {
			if ( jax.timer["dom_set_"+id] == undefined )
			{  
				jax.timer.add ("dom_set_"+id, {type:1, interval:100}).func = function ()
				{
					jax.dom.set ( id, data );  
				}
				jax.timer["dom_set_"+id].enable();
			}  
		}
		else {
			if ( jax.timer["dom_set_"+id] != undefined ){ jax.timer["dom_set_"+id].disable(); jax.timer["dom_set_"+id] = null; }
			if ( el.value == undefined ) { el.innerHTML = data;  }
			else { el.value = data; }
		}
		
	},

	get: function (id) {
	},

	add: function ( id, type , data, target ) {
		if ( jax.dom.o (target) == false ) { return false; }
		var el = jax.dom.o (target);
		
		if ( type == "a" ) { 
			var el_new = document.createElement(type); 
			var text_new = document.createTextNode(data); 
			el_new.appendChild (text_new);
			el.appendChild (el_new);
		}
		else if ( type == "text" ) { 
			var el_new = document.createTextNode(data); 
			el.appendChild (el_new);
		}
		else { 
			var el_new = document.createElement(type); 
			el_new.setAttribute( "id", id );
			el.appendChild (el_new);
			if ( type != "br" ) jax.dom.set ( id, data ); 
		}
		return el_new;
	},

	del: function (id) {
		if ( jax.dom.o (id) != false )
		{
			var el = jax.dom.o(id).parentNode;
			var el_c = jax.dom.o(id);
			el.removeChild(el_c);
		}
	
	}

}


//
jax.cache = {

	add: function (id,data) {
		if ( !jax.cache[id] )
		{
			id = id.split(".");
            id = id.join("");
			
			jax.cache[id] = [];
			jax.cache[id].data = data;
			return true;
		}
		else { return false; }
	},
	
	get: function (id) {
		id = id.split(".");
        id = id.join("");
		
		if ( jax.cache[id] ) { return jax.cache[id].data; }
		else { return false; }	
	}

	
}



//SYSTEM CLASSES


//MODULE CLASS
jax.c.MODULE = jax.c.add(["id","args"],{
	
	path: jax.module.path,
	ready: "false"
	
});


//TIMER CLASS
jax.c.TIMER = jax.c.add(["id","args"],{
	
	status: false,
	func: false,

	init: function () {
		this.interval = args.interval;
		this.type = args.type;
		this.timer = false;
	},

	info: function () {
		jax.c.instance.info (this);
	},

	enable: function() {
		var t_id = this.id;
		var t_obj = jax.timer[ t_id ];
		if ( t_obj.func != false && t_obj.status != true ) 
		{ 
			if ( this.type == 0 ) {
				t_obj.timer = window.setTimeout ( 'jax.timer["' + t_id + '"].func();jax.timer["' + t_id + '"].status="false";' , t_obj.interval ); t_obj.status = true; 
			}
			if ( this.type == 1 ) {
				t_obj.timer = window.setInterval ( 'jax.timer["' + t_id + '"].func();' , t_obj.interval ); t_obj.status = true; 
			}
		}
		else {} //alert ("no function set for timer: " + j_t_name );
	},
	
	disable: function() {
		var t_id = this.id;
		var t_obj = jax.timer[ t_id ];
		if ( t_obj.func != false && t_obj.status != false && this.type != 0 ) { window.clearTimeout ( t_obj.timer ); t_obj.status = false; }
		else {}  //alert ("no function set for timer: " + j_t_name );
	}

});

//AJAX CLASS
jax.c.AJAX = jax.c.add(["id","args"],{

	init: function () {
		this.url = args.url;
		this.status = false;
	},

	info: function () {
		jax.c.instance.info (this);
	},

	func: "false",
	
	enable: function() {
		
	var cache = jax.cache.get ( this.url );
	if ( cache == false )
	{
		
		if ( this.xmlHttp ) {
		if ( this.status == true ) { this.xmlHttp.abort(); }
		}
		else 
		{
			if (window.XMLHttpRequest)
			{// code for IE7+, Firefox, Chrome, Opera, Safari
				this.xmlHttp=new XMLHttpRequest();
			}
			else
			{// code for IE6, IE5
			this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} 
		}
		
		this.xmlHttp.parent = this;
		this.xmlHttp.onreadystatechange=function()
		{
			
			
			if(this.readyState==4)
			{
    
				if ( this.parent.func != false )
				{
					
					if ( this.status==200 ) {
						this.parent.data = this.responseText;
						jax.cache.add(this.parent.url,this.parent.data);
						this.parent.func();
						this.parent.status = false;
					}
					
					if ( this.status==404 ) {
						this.parent.data = "404 - document not found :(";
						this.parent.func();
						this.parent.status = false;
					}

				}
				else 
				{
				//alert ( this.responseText );
				}

			}
		}
		
		if ( this.url.toString().indexOf('?') ==-1 ) { var ut = "?u=" + jax.date.unixtime(); }
		else  { var ut = "&u=" + jax.date.unixtime(); }
		
		this.xmlHttp.open("GET", this.url + ut , true);
		this.xmlHttp.send(null);
		this.status = true;
	}
	else
	{
		this.data = cache;
		this.func();
	}

}
	
});

jax.date = { 
	
	unixtime: function() { 	
		var foo = new Date; // Generic JS date object
		var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
		return unixtime = parseInt(unixtime_ms / 1000);
	} 
};


//INIT CORE
jax.module.add ("timer");
jax.module.add ("ajax");
jax.module.add ("dom");

//MODULE LOADER AND PARSER



window.onload = function() {
	jax.onload();
}
