var MODULE_CONTAINER = 'site';
var MODULE_SCRIPT = 'xLoadModule.php'

String.prototype.repeat = function(n)
{
	var s = '';
	while(n-- > 0)
		s += this;
	return 	s;
}

var Module = new Class({
	
	options: {
		onComplete : Class.empty,
		onSuccess : Class.empty,
		onError : Class.empty,
		params : {}
	},
	
	_files : '',
	
	initialize : function(name)
	{
		this.name = name;
		this.contents = $(MODULE_CONTAINER);
		this.addEvent('onComplete', this.onComplete);
		this.addEvent('onError', this.onError);
		this.addEvent('onSuccess', this.onSuccess);
	},
	
	load: function(options)
	{
		this.setOptions(options);
		var module = this;
		
		var p = this.options.params;
		var qs = '&params=';
		for(var i in p)
			qs += i + ':' + escape(p[i]) + ';';
		
		this.ajax = new Ajax(MODULE_SCRIPT + '?module=' + this.name + qs, 
			$merge( options, 
			{
				//	update : $(MODULE_CONTAINER),
				onSuccess : function()
				{
					var xml = new XML(this.response.xml, 'root');
					if(!xml || xml.error)
					{
						alert('XML INVALIDE!\n________________________________\n' + this.response.text)
						return;
					}

					module.response = xml;
					module.fireEvent('onComplete');						
				}	
			}));
		this.ajax.request();
	},
	
	onError : function(list_err)
	{
		var c = '';
		for(var i = 0; i < list_err.length; i++)
			c += ' - ' + list_err[i] + '<br />'; 

		c = (i <= 1 ? 'Une erreur est survenue ' : 'Des erreurs sont survenues ') + ' dans le module <b>' + this.name  + '</b> : <br />' + c
		c += '<div id="mb_submit"><input type="button" id="mb_btn_submit" onclick="modal_box.close();" value="Ok" /></div>';
		modal_box.display(c, {title : 'Erreur', width: '500', height: 350});
		$('mb_btn_submit').focus();
	},
	
	onComplete : function()
	{
		//vérification des erreurs
		var erreurs = this.response.getNode('erreurs');
		if(erreurs)
			this.fireEvent('onError', [this.response.getValues('erreur',erreurs)]);
		else
			this.fireEvent('onSuccess');
	
	},
	
	onSuccess : function()
	{
		//chargement des JS
//		var js = this.response.getValues('javascript');
//		this.jsToLoad = js.length;
//		for(var i = 0; i < js.length; i++)
//			this.loadFile('javascript', XMLGetValue(js[i]));

		//chargement des CSS
		var css = this.response.getValues('css');
		for(var i = 0; i < css.length; i++)
			this.loadFile('css', css[i]);
	
		
		//affichage du content
		var update = this.options.update ? this.options.update : this.contents;
		update.setHTML(this.response.getValue('contents'))

		//chargement des modules additionnels
		var m = this.response.getNodes('module');
		for (var i = 0; i < m.length; i++)
		{
			var name = this.response.getValue(m[i], 'name');
			var container = $(this.response.getValue(m[i], 'container'));
			var onload = this.response.getValue(m[i], 'onload')
			new Module(name).load({update: container});
		}
		
		if(ol = this.response.getValue('onload'))
		{
			eval(ol);
			//this.moduleOnLoad = XMLGetValue(root, 'onload');
			//this.tryOnLoad(0);
		}
	},
	
//	tryOnLoad : function(nb)
//	{
//		if(nb > 10) // 28
//		{
//			str = '<i>Les fichiers suivants n\'ont pu êtres chargés :</i><br />';
//			Module.jsToLoad.each(function(f)
//			{
//				str += ' - ' + f + '<br />';
//			});
//				
//			modal_box.display(str, {title : 'Timeout', width: '250', height: 100});
//			return;
//		}
//
//		if(Module.jsToLoad.length == 0)
//		{
//			modal_box.close();
//			eval(this.moduleOnLoad);	
//		}
//		else
//		{
//			modal_box.display('Chargement des scripts <br />' + '_'.repeat(nb), {title : 'Veuillez patienter' + '.'.repeat(nb%4), width: '250', height: 100});
//			this.tryOnLoad.delay(500, this, ++nb);
//		}
//	},
	
	loadFile : function(type, file)
	{

		if(this._files.indexOf(file) == -1)
		{
			if(type == 'javascript')
				Module.jsToLoad.push(file);
			this._files += file + ';';
			new Asset[type](file + '?bogus=' + Math.floor(new Date().getTime() / 1000));
		}
	}

	
});

Module.jsToLoad = [];
Module.jsLoaded = function(js)
{
	Module.jsToLoad.remove(js);
}

Module.implement(new Options);
Module.implement(new Events);

