/**
 * $Id: celer.js 836 2008-02-19 22:23:07Z Rick $
 * 
 * Celer Javascript framework
 * 
 * @author Mathijs Kadijk <mkadijk@gmail.com>
 */

// Object declaration
celer = 
{
	// Variables to config Celer
	debug : false,											// Turn debug messages on/off
	base : document.getElementsByTagName('base')[0].href,	// Base URL to use
	modulePath : 'js/celer/',								// Path to use to include modules
	lastUpdate : '200711180255',							// Set the last update of the modules
	
	// Other variables
	loadedModules : ['util', 'date', 'ajax', 'richtext'],
	searchDomFor : [ ],
	domHits : [ ],
	
	/**
	 * Load an Celer module
	 *
	 * @param string moduleName Name of the module to load
	 */
	loadModule : function(moduleName)
	{
    	// Construct script tag
        var js = document.createElement('script');
    	js.language = 'javascript';
    	js.type = 'text/javascript';
    	js.src = celer.base + celer.modulePath + escape(moduleName) + '.js?' + escape(celer.lastUpdate);
    	
    	// Add tag to document
    	var head = document.getElementsByTagName('head').item(0);
    	head.appendChild(js);
	},
	
	/**
	 * This function loads the Celer JS modules for ya'
	 */
	init : function ()
	{
		// Load modules
		for (var i = 0; i < celer.loadedModules.length; i++)
		{
			celer.loadModule(celer.loadedModules[i]);
		}
		
		// Start polling
		setTimeout('celer.checkInitProgress()', 75);
	},
	
	/**
	 * This recursive function gets the child nodes for the given object and searches trough
	 * them using the searchDomFor array
	 * 
	 * @param element the element to start the search in
	 */
	searchChildNodes : function(element)
	{
		var childNodes = element.childNodes;
		for(var i = 0; i < childNodes.length; i++) 
		{
			// Check childs
			celer.searchChildNodes(childNodes[i]);
			
			// Check attributes
			if (childNodes[i].attributes != null)
			{
				for(var j = 0; j < childNodes[i].attributes.length; j++)
				{
					for(var k = 0; k < celer.searchDomFor.length; k++)
					{
						// Is this the attribute we search for?
						if (childNodes[i].attributes[j].name == celer.searchDomFor[k].attribute)
						{
							// Does it have the desired value?
							if (celer.searchDomFor[k].value == null || celer.searchDomFor[k].value == childNodes[i].attributes[j].nodeValue )
							{
								// Evaluate the target function
								var hit = { "target" : celer.searchDomFor[k].target, "id" : childNodes[i].id };
								celer.domHits[celer.domHits.length] = hit;
							}
						}
					}
				}
			}
		}
	},
	
	processHits : function()
	{
		for(var i = 0; i < celer.domHits.length; i++)
		{
			eval(celer.domHits[i].target + "('" + celer.domHits[i].id + "');");
		}
	},
	
	searchFullDom : function()
	{
		if (celer.searchDomFor.length > 0)
		{
			celer.searchChildNodes(document.getElementsByTagName('body')[0]);
			celer.processHits();
		}
	},
	
	addSearchTarget : function(attribute, value, target)
	{
		var object = { "attribute" : attribute, "value" : value, "target" : target };
		celer.searchDomFor[celer.searchDomFor.length] = object;
	},
	
	/**
	 * Check if all modules are loaded and do the afterInit if so
	 */
	checkInitProgress : function()
	{
		var allModulesLoaded = true;

		// Check for each module if it is loaded
		for (var i = 0; i < celer.loadedModules.length && allModulesLoaded; i++)
		{
			var module = eval('celer.' + celer.loadedModules[i]);
			
			if (module == undefined)
				allModulesLoaded = false;
		}
		
		// If everything is loaded, launch the afterInit
		if (allModulesLoaded)
		{
			celer.searchFullDom();
			celer.afterInit();
		}
		else
			setTimeout('celer.checkInitProgress()', 50);
	},
	
	/**
	 * This function is runned after all Celer modules has been loaded
	 */
	afterInit : function ()
	{}
};

window.onload = celer.init;