/*
* This notice must be untouched at all times.

============= META ==================
@name : jaxWidgets_RuntimeEngine.js  
@version : 0.10 
@copyright (c) 2005 Sarat Pediredla. All rights reserved.
@last-modified: 14/06/2005
@url : http://sarat.xcelens.co.uk/jaxWidgets
@latest-version-url : http://sarat.xcelens.co.uk/jaxWidgets/latest/
======================================

============== DESCRIPTION =============
The Runtime Engine to register and process widgets
=========================================

============= FEATURES ==================
- Dynamically loads and parses jaxWidgets
- i18n support for strings
============================================

============= FUTURE PLANS ==================

==============================================

LICENSE: LGPL

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

For more details on the GNU Lesser General Public License,
see http://www.gnu.org/copyleft/lesser.html
*/
// Define namespace
if (typeof jaxWidgets != "object") { var jaxWidgets = {}; }

// Register namespace and object
jaxWidgets.RuntimeEngine = function()
{
	var registeredWidgets = new Array();
	var totalWidgets = -1;
	var loadedWidgets = new Array();
	var currentWidgets = new Array();
	
	this.registerWidget = function(tag, handler)
	{
		var newWidget = new Array(tag, handler);
		registeredWidgets[++totalWidgets] = newWidget;
	};
	
	this.loadEngine = function()
	{
		// Load library component
		_Library = new jaxWidgets.Library();				
		for(var i=0; i <= totalWidgets; i++)
		{
			currentWidgets = document.getElementsByTagName("jax:"+registeredWidgets[i][0]);
			for(var j =0; j < currentWidgets.length; j++)
			{
				registeredWidgets[i][1].load(currentWidgets[j]);
			}
			
			_Library.cleanup(currentWidgets);
		}		
	};
	
	/*
	Debug function for testing purposes only 
	You NEED to create a div with id=debug in your page for this to work
	*/
	this.writeDebug = function(string)
	{
		document.getElementById("debug").innerHTML += string + "<br>";
	};
	
	/*
	Error Log function. Can be disabled through setErrors(false)
	*/
	this.writeError = function(string)
	{
		if(document.getElementById("jaxErrorLogDiv"))
		{
			document.getElementById("jaxErrorLogDiv").innerHTML += string + "<br>";
		}
		else
		{
			var logDiv = document.createElement("div");
			logDiv.setAttribute("id","jaxErrorLogDiv");
			logDiv.style.color = "red";
			logDiv.style.fontSize = "0.9em";
			logDiv.style.fontWeight = "bold";
			var bodyTag = document.getElementsByTagName("body")[0];
			bodyTag.insertBefore(logDiv,bodyTag.firstChild);
			logDiv.innerHTML = "jax Error : ";
			logDiv.innerHTML += string + "<br>";
		}
	};
			
}

// Register event handlers
// Use quirksmode idea for flexible registration by copying existing events
// onKeyUp
/******* Define GLOBAL Constants *********/
var _Engine;              // The jax Runtime Engine
var _Library;             // The jax Runtime library

_Engine = new jaxWidgets.RuntimeEngine();
var oldEventCode = (window.onload) ? elm.onload : function () {};
window.onload = function () {oldEventCode(); _Engine.loadEngine();};
