/*
FILE:		fx.js
VERSION:	0.0.1
AUTHOR:		Andrew Real
COMPILED:	2011-04-19
COMMENTS:

	- Must be loaded with ui.js, ui.js, and ajax.js
	- Must be inserted within HEAD element

*/
system = {

	addLoadEvent : function(f) {

		system.addEvent(window,"load",f);	// legacy

		},

	addClickEvent : function(e,f) {

		system.addEvent(e,"click",f);		// legacy

		},

	addEvent : function(e,ev,f) {

		ev_C = 'on'+ev;
		oldFunc = e[ev_C];
		if (typeof (e[ev_C] != 'function')) {

			e[ev_C] = f;

			}

		else {

			e[ev_C] = function() {

				oldFunc();
				f();

				}

			}

		},

	importCSSFile : function(s) {

		x = document.createElement("link");
		x.href = s.href;
		x.type = 'text/css';
		x.rel = 'stylesheet';
		x.media = s.media;
		document.getElementsByTagName("head")[0].appendChild(x);

		},

	importJSFile : function(s) {

		x = document.createElement("script");
		x.type = "text/javascript";
		x.src = s.src;
		document.getElementsByTagName("head")[0].appendChild(x);

		},

	getElementByHTML : function(html) {

		e = document.getElementsByTagName("*");
		foundMatch = false;
		for (i=0;i<e.length;i++) {

			if (e[i].innerHTML == html) {

				foundMatch = true;
				foundElement = e[i];

				}

			}

		if (foundMatch) return foundElement;
		else return null;

		},

	console : {

		val : new Array(),
		now : function() {

			return new Date();

			},

		log : function(x,a) {

			if (a == undefined || a == null) a = "system";
			system.console.val.push(a+"||[["+x+"]] - "+system.console.now().getTime());

			},

		display : function() {

			system.console.log("console displayed");
			if (document.getElementById("TORD_console")) {

				document.getElementById("TORD_console").parentNode.removeChild(document.getElementById("TORD_console"));

				}

			x = document.createElement("DIV");
			x.style.position = "fixed";
			x.style.bottom = "0px";
			x.style.width = "100%";
			x.style.height = "300px";
			x.style.overflowX = "hidden";
			x.style.overflowY = "scroll";
			x.style.textAlign = "left";
			x.style.backgroundColor = "red";
			x.style.zIndex = "100005";
			x.setAttribute("id","TORD_console");
			html = '';
			for (i=0;i<system.console.val.length;i++) {

				// format: agent||[[message]] - stamp //
				j = system.console.val[i];
				a = j.split("||");
				s = a[1].search(/]]/);
				k = a[1].substring(2,s);
				t = a[1].substring(s+5);
				html+= a[0]+": <span class=\"bold\">"+k+"<\/span> - "+t+"<br>";

				}

			x.innerHTML = html;
			document.body.appendChild(x);

			}	

		}

	}

system.addEvent(window,"error",function(e) {

	system.console.log(e,"error_handle");

	});
