// --- (c) 2007, script code by www.doran.be

var doranjo = {
	'libsloaded': [] /* --- Here we place all the dynamically loaded libraries */
	,'version': {
		'major': 0
		,'minor': 1
		,'build': 1
		,'toString': function() { return(this.major + '.' + this.minor + '.' + this.build); }
		}
	,'root': ''
	};
	

// --- Get the root path for doranjo
if (document && document.getElementsByTagName) {
	arrTemp = document.getElementsByTagName('script');
	for(var i=0; i<arrTemp.length; i++) {
		if (arrTemp[i].getAttribute('src')) {
			strTemp = arrTemp[i].getAttribute('src');
			if (strTemp.match('doranjo.js')) {
				doranjo.root = strTemp.substring(0,strTemp.indexOf('doranjo.js'));
				}
			}
		}
	}


/* --- Routines so that we can dynamically add libraries that we need in our web application */
doranjo.lib = {}
doranjo.lib.isloaded = function(lib) {
	for(var i=0; i<doranjo.libsloaded.length; i++) {
		if (doranjo.libsloaded[i] == lib) { return true; }
		}
	return false;
	}
doranjo.lib.add = function() {
	if (document.getElementById && document.createElement) {
		for (i=0; i<arguments.length; i++) {
			var file = arguments[i];
			if(!doranjo.lib.isloaded(file)) {
				// --- Ok, the file has not been loaded yet
				objTemp = document.createElement('script');
				objTemp.setAttribute('type','text/javascript');
				objTemp.setAttribute('src',doranjo.root + 'doranjo/' + file + '.js?v=' + doranjo.version.toString());
				document.getElementsByTagName('head')[0].appendChild(objTemp);
				doranjo.libsloaded[doranjo.libsloaded.length] = file;
				}
			else {
				doranjo.debug.add({
					'type': 'info'
					,'lib': 'lib'
					,'func': 'add'
					,'text': escape(file) + ' is already loaded'
					});
				}
			}
		}
	}


/* --- Routines for debugging purposes */
doranjo.debug = {
	'messages': [] /* --- Here we place all the debug messages */
	,'add': function(msg) {
		/* msg -> {'type':info|error,'lib':libraryname,'func':thefunction name inside the library,'text':thetext} */
		this.messages[this.messages.length] = msg;
		}
	,'getHtml': function(whichtype) {
		if (!whichtype) { whichtype = 'info,error'; }
		var h = '';
		for (i=0; i<this.messages.length; i++) {
			var l = '';
			l += '<tr valign="top">';
			l += '<td>' + this.messages[i].type + '</td>';
			l += '<td>' + this.messages[i].lib + '</td>';
			l += '<td>' + this.messages[i].func + '</td>';
			l += '<td>' + this.messages[i].text + '</td>';
			l += '</tr>';
			h = l + h;
			}
		h = '<table id="doranjo_debug"><thead><tr><th>Type</th><th>Library</th><th>Function</th><th>Text</th></tr></thead><tbody>' + h + '</tbody></table>';
		return h;
		}
	}
