//Clase que representa un conjunto de variables clave/valor
function Params(){
    //Private: alamacena los conjuntos clave/valor
    this._params = Array();
    //Public: añade un nuevo conjunto clave/valor
    this.add = function(name,value){
        this._params.push({name: name, value: value});
    }
    //Public: obtiene el total de conjuntos a�adidos
    this.getCount = function(){
        return this._params.length;
    }
    //Public: obtiene un conjunto especifico
    this.getItem = function(index){
        return index < this.getCount()? this._params[index] : null;
    }
}
//Clase que representa una cola de envios de peticiones AJAX al servidor
function AjaxQueue()
{
    //Public: indica si actualmente la cola est� procesando alguna peticion
    this.isWatting = false;
    //Private: array que almacena la cola de peticiones
    this._cola = Array();
    
    //Public: pone en cola la peticion enviada
    //Parametros
    // command : es el identificador del comando a ejecutar
    // params  : un objeto de tipo Params que contiene los parametros a enviar
    // callback: funcion a ejecutar cuando llega la respuesta del servido
    this.callServer = function(controller, action,params,callback, errorCallback){
        var callString = "c=" + controller;
        callString += "&a=" + action;
        //Crea la cadena de parametros a enviar
		if(params != null)
		{
			var i;
	        for(i=0;i<params.getCount();i++)
	        {
	            callString += "&" +  params.getItem(i).name + "=" + params.getItem(i).value;
	        }
		}
        //Pone en cola
        this._cola.push({post: callString, callback: callback, errorCallback: errorCallback});
        //Ejecuta inmediatamente si no hay nada pendiente
        this._doCall();
    }
    //Private: ejecuta la cola actual. Cuando llega la respuesta de una peticion, automaticamente pasa a 
    //la siguiente si existe
    this._doCall = function(){
        if(this._cola.length > 0 && this.isWatting == false)
        {
            this.isWatting = true;
            var me = this;
            $.ajax({
					url: "index.php",
					error: function() {
						if(me._cola[0].errorCallback)
							me._cola[0].errorCallback
		                //Quita el elemento de la cola
		                me._cola.splice(0,1);
		                me.isWatting = false;
		                //Pasa a la siguiente peticion si existe
		                me._doCall();
					},
					type: "POST",
					success: function(response){
					//Llega respuesta del servidor
	                if(me._cola[0].callback)
	                    me._cola[0].callback(response);
	                //Quita el elemento de la cola
	                me._cola.splice(0,1);
	                me.isWatting = false;
	                //Pasa a la siguiente peticion si existe
	                me._doCall();
				},
				data: this._cola[0].post
			});
        }
    }
}
function Template(text, values)
{
	this.text = text;
	this.values = values;
	this.get = function()
	{
		var text = this.text;
		var ptr = /{{(\w+)}}/;
		var match = text.match(ptr);
		if(match)
			do
			{
				text = text.replace(ptr,this.values[match[1]]);
				match = text.match(ptr);
			}while(match != null);
		return text;
	};
}
/**
 * Añade el metodo trim a las cadenas de texto
 */
String.prototype.trim = function()  
{
	return this.replace(/(\s+)/,"").replace(/(\s+)$/,"");  
}
var queue = new AjaxQueue();
function loadSection(html){
	$("#center").html(html);
}
function getController(){
	return section.substring(1).split("/")[0];
}
$().ready(function(){
	$("#login").click(function(){
		var loginDlg = new LoginDialog();
		loginDlg.open();
	});
	$("#lang").click(function(){
		var status = $("#lang ul").css("display");
		$("#lang ul").css({display: (status == "none"? "block" : "")});
	});
	if(window.getSection){
		window.setInterval(getSection, 300);
	}
});

