﻿/**
 * Prefix path for modules loaded from a namespace
 */
var prefixPath = "/limao/js/";

/**
 * Path for your modules.json
 */
var modulesIndex = "/limao/js/loader/modules.json";


if( !window.XMLHttpRequest ){
	/**
	 * Defines XMLHttpRequest class when it does not exists
	 * @type Object
	 * @return XMLHttpRequest instance
	 */
	var XMLHttpRequest = function(){
		this._object = null;
		this.onreadystatechange = function(){}
		
		this._getObject = function(){
			if ( navigator.userAgent.indexOf("MSIE") >= 0 ) { 
				var strName="Msxml2.XMLHTTP";
				if ( navigator.appVersion.indexOf("MSIE 5.5") >= 0 ) {
					strName="Microsoft.XMLHTTP";
				} 
				try{ 
					objXmlHttp=new ActiveXObject(strName);
					return objXmlHttp;
				}catch( e ){ 
					alert("Error. Scripting for ActiveX might be disabled") ;
					return null;
				} 
			} 
		}
		
		this._object = this._getObject();
		
		return this._object;
	}
	
}

clearJSCache = function(){
	for( var i = 0; i < window.require.loadedNamespaces.length; i++ ){
		require( window.require.loadedNamespaces[ i ], true );
	}
}

/**
* Function to import a js file based in a namespace (it will be translated to a path)
* @param {String} namespace Namespace for required file
* @type void
*/
require = function( namespace, nocache ){
	if( !nocache && ('|' + window.require.loadedNamespaces.join('|') + '|').indexOf( '|'+namespace+'|' ) >= 0 ) return;
	
	var oXMLHttp = new XMLHttpRequest();
	oXMLHttp.open( "GET", prefixPath + namespace.replace(/([^\\])\./g,'$1/').replace(/\\\./g,'.') + '.js' + ( nocache ? '?wra=' + new Date().toString() + Math.random() : '' ), false );
	oXMLHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	oXMLHttp.send( null );
		
	var scriptTag = document.createElement('script');
	scriptTag.type = "text/javascript";
	scriptTag.text = oXMLHttp.responseText;
	
	var is_gecko = /gecko/i.test(navigator.userAgent);
	var is_ie = /MSIE/i.test(navigator.userAgent);
	
	if( is_gecko ){
		try{
			eval( oXMLHttp.responseText.replace(/function[\s\t]*((\w|$|_)+)[\s\t]*\(/gi,'$1 = function('), window );
		}catch( e ){
			if( window.console ){
				console.log( [ e, namespace ] );
			}
		}
	}
	
	/*
	if( is_ie ){
		window.require.loadedNamespaces.push( namespace );
		document.getElementById('scriptContainer').text	= scriptTag.text;
		return;
	}
	*/
	
	var headTag = document.getElementsByTagName('head')[0];
	headTag.appendChild( scriptTag );
	window.require.loadedNamespaces.push( namespace );
}
window.require.loadedNamespaces = new Array();

/**
* Generic function that will load a js ( based in modules.json ) when you uses a class or funcion that
* is not already defined. It works as a "on-demand js loader"
* @ignore
**/
function _loader( oModule, arrArguments, instance ){
	require( oModule.namespace );
	
	var arrArgStrings = new Array();
	for( var i=0; i<arrArguments.length; i++ ){
		arrArgStrings.push( "arrArguments["+i+"]" );
	}
	
	if( instance ){
		return eval( 'new window[oModule.name](' + arrArgStrings.join(',') + ')' );
	}
	return eval( 'window[oModule.name](' + arrArgStrings.join(',') + ')' );
}

/* Calls the index of modules to pre-configure the "on-demand loading" */
var oXMLHttp = new XMLHttpRequest();
oXMLHttp.open( "GET", modulesIndex, false );
oXMLHttp.send( null );
var objModules = eval( oXMLHttp.responseText );


/* Defines all classes and functions in Module Index as a reference to _loader */
for( var attr in { 'modules' : null, 'functions' : null } ){
	for( var i = 0; i < objModules[attr].length; i++ ){
		var oModule = objModules[attr][i];
		
		var loaderSource = [ 
			"	var oModule = {",
			'		name: "', oModule.name , '",',
			'		namespace: "', oModule.namespace , '"',
			"	};",
			"	return _loader.apply( window, [ oModule, arguments, ( '", attr ,"' == 'modules' ) ] );",
		].join(''); 
		window[ oModule.name ] = new Function( loaderSource );
	}
}