/*
* @	WST - Web Standards Tools v 1.1
* @ Diego de Andrade Pereira - contact@wstframework.org
* @ SNT Brasília, Brasil - 06/05/2008 18:12
*/

(function(){
	wst.extend(wst, {
		/* ------------------------------------------------------------------------------------------------
														MODULES
		------------------------------------------------------------------------------------------------ */
		modules : {
			// By default the dynamic load is made with Ajax. In this case the wst.Ajax module
			// already must to be loaded. Otherwise, the load is made with the "standard" mode, with
			// a script element. The better choice is Ajax.
			load : function(module, ajaxLoad)
			{
				var wstFnID = 'wst.modules.load';
				if(wst.ok(this[module]) && !this[module].loaded)
				{
					ajaxLoad = ajaxLoad || true;
					var logDomEnabled = wst.log.dom.enabled;
					wst.log.dom.enabled = false;
					if(wst.ok(wst.load) && ajaxLoad)
					{
						var moduleContent = wst.load(wst.core.baseDir + this[module].src);
						var status = wst.ajax.stats.list.status.slice(-1)[0];
						if(status == 200)
						{
							this[module].loaded = true;
							eval(moduleContent);
						}
						else
						{
							var err = '';
							switch(status)
							{
								case 404:
									err = '404: ' + module + wst.messages.functions['modulesLoad404'];
									break;
								case 500:
									err = '500: ' + wst.messages.functions['modulesLoad500'] + module;
									break;
								default:
									err = status + ': ' + wst.messages.functions['modulesLoadDefault'] + module;
									break;
							}
							wst.error(err);
						}
						wst.log.dom.enabled = logDomEnabled;
					}
					else
					{
						var wstFnCaller = arguments.callee.caller,
							wstArgsCaller = wstFnCaller.arguments,
							wstThisCaller = wstArgsCaller.callee.caller;
						var scriptEvt = wst.browser.ie ? 'readystatechange' : 'load';
						
						var script = wst.cE('<script src="' + wst.core.baseDir + this[module].src + '" type="text/javascript"></script>');
						wst.listen(script, scriptEvt, function(){
							if(scriptEvt == 'readystatechange' ? this.readyState == 'complete' : true)
							{
								wst.modules[module].loaded = true;
								wstFnCaller.apply(wstThisCaller, wstArgsCaller);
							}
						});
						
						wst.get('head').append(script);
						wst.log.dom.enabled = logDomEnabled;
						return false;
					}
				}
			},
			// Must to be called before the page load event
			loadAll : function()
			{
				var wstFnID = 'wst.modules.loadAll';
				var module, modules = this;
				wst.loop(modules, function(module){
					if(this.autoLoad && !this.loaded)
					{
						wst.include(wst.core.baseDir + this.src);
						wst.modules[module].loaded = true;
					}
				});
			},
			// relative paths to wst.baseDir
			// path = wst.baseDir + module.src
			lang : {
				autoLoad : true,
				src : 'lang/wst.lang.pt-BR.js',
				loaded : false
			},
			ajax : {
				autoLoad : true,
				src : 'modules/wst.Ajax.js',
				loaded : false
			},
			// BUG: the date module must to be pre-loaded, otherwise the wst.get method will create an 
			// infinite loop with the log method
			date : {
				autoLoad : true,
				src : 'modules/wst.date.js',
				loaded : false
			},
			mediaPlayer : {
				autoLoad : true,
				src : 'modules/wst.mediaPlayer.js',
				loaded : false
			},
			HTMLEntityNames : {
				autoLoad : false,
				src : 'entities/wst.html.entity-names.js',
				loaded : false
			},
			HTMLEntityNumbers : {
				autoLoad : false,
				src : 'entities/wst.html.entity-numbers.js',
				loaded : false
			},
			HTMLEntities : {
				autoLoad : false,
				src : 'entities/wst.html.entities.js',
				loaded : false
			},
			HTMLChars : {
				autoLoad : false,
				src : 'entities/wst.html.chars.js',
				loaded : false
			},
			formValidator : {
				autoLoad : false,
				src : 'modules/wst.formValidator.js',
				loaded : false
			},
			opacity : {
				autoLoad : false,
				src : 'modules/wst.opacity.js',
				loaded : false
			},
			flash : {
				autoLoad : false,
				src : 'modules/wst.flash.js',
				loaded : false
			}
		}
	});
	
	wst.modules.loadAll();
})();