// JavaScript Document
/*
* Scriptor
*
* Dynamic javascript and stylesheet loader.
*
* Version 1.5b
*
*/

/*
* Defining some variables 
*
* scriptor_config: An array of defined script objects tha can be dynamically loaded
*    Properties:
*      objectId: The id of the object. You will use this id to load the object
*      description: The name of the object
*      path: Relative path to the script to load
*      stylesheets: A list of stylesheets. You can define an alternative stylesheet by 
*        requesting it with its name property. The first element should be called 
*        default and will be loaded by default
*
* scrtor_registers: Read only, internal array of loaded script objects
*
*/
var scriptor_config = Array();
var scriptor_registers = Array();

// dataView
scriptor_config[scriptor_config.length] = 
	{ objectId : 'dataView',
	 description: 'dataView 2.0b', 
	 path: 'dataView/dataView.js',
	 stylesheets: [ 
			{ name: 'default', path: 'scriptor/dataView/default.css' } ],
	 
	 langs: [
			{ name: 'es', path: 'dataView/lang/es.js'},
			{ name: 'en', path: 'dataView/lang/en.js'} ]
	};

// tabView
scriptor_config[scriptor_config.length] = 
	{ objectId : 'tabView',
	 description: 'tabView 1.5.1b', 
	 path: 'tabView/tabView.js',
	 stylesheets: [ 
			{ name: 'default', path: 'scriptor/tabView/default.css' } ] 
	};

// calendarView
scriptor_config[scriptor_config.length] = 
	{ objectId : 'calendarView',
	 description: 'calentabView 1.0.1b', 
	 path: 'calendarView/calendarView.js',
	 stylesheets: [ 
			{ name: 'default', path: 'scriptor/calendarView/default.css' } ],
	 
	 langs: [
			{ name: 'es', path: 'calendarView/lang/es.js'},
			{ name: 'en', path: 'calendarView/lang/en.js'} ]
	
	};
	
// galleryView
scriptor_config[scriptor_config.length] = 
	{ objectId : 'galleryView',
	 description: 'galleryView 1.1b', 
	 path: 'galleryView/galleryView.js',
	 stylesheets: [ 
			{ name: 'default', path: 'scriptor/galleryView/default.css' } ]
	
	};

// treeView
scriptor_config[scriptor_config.length] = 
	{ objectId : 'treeView',
	 description: 'treeView 2.0b', 
	 path: 'treeView/treeView.js',
	 stylesheets: [ 
			{ name: 'default', path: 'scriptor/treeView/default.css' } ]
	
	};

// httpRequest
scriptor_config[scriptor_config.length] =
	 { objectId : 'httpRequest', 
	 description: 'httpRequest 1.0b',
	 path: 'httpRequest/httpRequest.js',
	 stylesheets: [
			{ name: 'default', path: 'scriptor/httpRequest/default.css' } ],
	 
	 langs: [
			{ name: 'es', path: 'httpRequest/lang/es.js'},
			{ name: 'en', path: 'httpRequest/lang/en.js'} ]
	 };

/*
* 
*/
function scriptor_load(objectId, styleId, langId) {
	if (typeof(objectId) != 'string') {
		alert('Error: objectId debe ser de tipo string.');
		return false;
	}
	
	var found = false;
	for (var n=0; n < scriptor_config.length; n++) {
		if (scriptor_config[n].objectId == objectId) {
			
			found = true;
			
			if (!scriptor_loaded(objectId)) {
				// load script
				var http_request = false;
		
				if (window.XMLHttpRequest) {
					http_request = new XMLHttpRequest();
				} else if (window.ActiveXObject) {
					try {
						http_request = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
						try {
							http_request = new ActiveXObject("Microsoft.XMLHTTP");
						} catch(e) {}
					}
				}
				
				if (!http_request) {
					alert( 'Failed to create httpRequest.' );
					return false;
				}
				
				if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/plain');
				}
					
				http_request.open( 'POST', 'scriptor/xml_getScriptStream.php', false );
				http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				http_request.send( 'script=' + encodeURIComponent(scriptor_config[n].path) );
				
				if (http_request.status == 200) {
					eval(http_request.responseText);
				}
				else {
					alert( 'Http_error: ' + http_request.status);
					return false;
				}
				
				if (typeof(langId == 'string') && scriptor_config[n].langs) {
					for (var a = 0; a < scriptor_config[n].langs.length; a++) {
						if (scriptor_config[n].langs[a].name == langId) {
							var http_request = false;
		
							if (window.XMLHttpRequest) {
								http_request = new XMLHttpRequest();
							} else if (window.ActiveXObject) {
								try {
									http_request = new ActiveXObject("Msxml2.XMLHTTP");
								} catch (e) {
									try {
										http_request = new ActiveXObject("Microsoft.XMLHTTP");
									} catch(e) {}
								}
							}
							
							if (!http_request) {
								alert( 'Failed to create httpRequest.' );
								return false;
							}
							
							if (http_request.overrideMimeType) {
								http_request.overrideMimeType('text/plain');
							}
								
							http_request.open( 'POST', 'scriptor/xml_getScriptStream.php', false );
							http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
							http_request.send( 'script=' + encodeURIComponent(scriptor_config[n].langs[a].path) );
							
							if (http_request.status == 200) {
								eval(http_request.responseText);
							}
							else {
								alert( 'Http_error: ' + http_request.status);
								return false;
							}
							break;
						}
					}
				}
				
				// load style
				var styleNdx = 0;
				if (styleId && typeof(styleId) == 'string') {
					for (var a = 0; a < scriptor_config[n].stylesheets.length; a++) {
						if (scriptor_config[n].stylesheets[a].name == styleId) {
							styleNdx = a;
							break;
						}
					}
				}
				
				var tmpStyle = document.createElement('link');
				tmpStyle.setAttribute('rel', 'stylesheet');
				tmpStyle.setAttribute('type', 'text/css');
				tmpStyle.setAttribute('href', scriptor_config[n].stylesheets[styleNdx].path);
				document.getElementsByTagName("head").item(0).appendChild(tmpStyle);
				
				scriptor_registers[scriptor_registers.length] = objectId;
			}
			
			break;
		}
	}
	
	if (!found) {
		alert('Scriptor Error: El objeto no fue encontrado o no está correctamente definido (' + objectId + ').');
		return false;
	}
	
	return true;
}

function scriptor_loaded(objectId) {
	found = false;
	
	for (var n=0; n < scriptor_registers.length; n++) {
		if (scriptor_registers[n] == objectId) {
			found = true;
			break;
		}
	}
	
	return found;
}
