/* 
 * CopyRight: Guia Campsa
 *   Version: 0.2Beta
 *	 Created: 7 Mar 2008
 *    Modify: 31 Mar 2008
 */

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}


/*
 * Clase que con los valores del desplegable
 */
var GCBuscadorSelectCandidatas = Class.create();
GCBuscadorSelectCandidatas.prototype = {
	initialize: function(name, size, multiple, className, resultado) {
		this.Id;
		this.Nombre;
	}
}


/*
 * Clase Buscador, Generar Select
 */
var GCBuscadorSelect = Class.create();
GCBuscadorSelect.prototype = {
	initialize: function(layer, name, size, multiple, className, resultado) {
		
		//Inicializa la clase, recoge los parametros y establece valores por defecto si es necesario
		this.layer = "";
		this.name = new Date().getTime();
		this.size = 1;
		this.multiple = false;
		this.className = "";
		this.resultado = "";
		
		//Esta clase contendrá el conjunto de objetos de tipo GCBuscadorSelectCandidatas
		this.opciones = new Array();

		//Valores por defecto si es necesario
		try {
			if (name != "") this.name = name;
			if (size > 0) this.size = size;
			if (multiple || !multiple) this.multiple = multiple;
			if (className != "") this.className = className;
			if (resultado.Count >= 0) this.resultado = resultado;
			if (layer != "") this.layer = layer;
		}
		catch (err) {
			//manejar el error
		}
	},
	
/* PUBLICAS	*/
	
	//solo las que deban ser visibles de resultados
	mostrarVisibles: function () {
		
		this._mostrarVisibles();
		this._generaDesplegable();
	},
	
	//Mostrar todos los registros
	mostrarTodos: function () {
		
		this._mostrarTodos();
		this._generaDesplegable();
	},
	
	//obtener el objeto Registro seleccionado
	obtenerCandidata: function () {
		
		var objSelect = document.getElementById(this.name);
		var indice;

		if (objSelect) {
			if (objSelect.type == "text")
				indice=0;
			else
				indice = objSelect.options[objSelect.selectedIndex].value;
			
			if (parseInt(indice) < parseInt(this.resultado.Count)) {
				return this.resultado.Candidata[indice];
			}
		}
		else {
			return "";
		}
	},
	
/* PRIVADAS	*/

	//muestra el primer bloque de nivel de coincidencia, o todas si solo hay uno
	_mostrarVisibles: function () {

		this.opciones = new Array();
		var opcion = null;
		
		for (var i=0; i<this.resultado.Count;i++) {
			if (this.resultado.Coincidencia[0] == this.resultado.Candidata[i].Coincidencia) {
				var opcion = new GCBuscadorSelectCandidatas();
				opcion.Id = i;
				opcion.Nombre = this.resultado.Candidata[i].Nombre;
				
				this.opciones.push(opcion);
			}
		}
	},
	
	//Muestra todas las candidatas
	_mostrarTodos: function () {
		
		this.opciones = new Array();
		var opcion = null;
		for (var i=0; i<this.resultado.Count;i++) {
			var opcion = new GCBuscadorSelectCandidatas();
			opcion.Id = i;
			opcion.Nombre = this.resultado.Candidata[i].Nombre;
			
			this.opciones.push(opcion);
		}	
	},
	
	//general el Desplegable o input si es necesario
	_generaDesplegable: function () {
		if (this.resultado.Count > 1) {
			
			var select = document.createElement('select');
			select.name = this.name;
			select.id = this.name;
			select.size = this.size;
			select.multiple = this.multiple;
			select.className = this.className;
			
			for (var i=0; i<this.opciones.length; i++) {
				
				opt = document.createElement('option');
				opt.value = this.opciones[i].Id;
				opt.innerHTML = this.opciones[i].Nombre;
				
				if (i==0) 
					opt.selected = true;
				
				select.appendChild(opt);
			}
			
			if (this.layer != "") {
				document.getElementById(this.layer).innerHTML = "";
				document.getElementById(this.layer).appendChild(select);
			}
			else
				document.body.appendChild(select);
				

			//necesario para corregir un Bug de IE6+
			if (this.size > 1)
			{
				var id = setTimeout(
					function() { 
						select.size = select.size - 1;
						select.size = select.size + 1; 
						
						clearTimeout(id);
					}, 5);
			}	
		}
		else if (this.resultado.Count == 1) {
		
			var input = document.createElement('input');
			input.name = this.name;
			input.id = this.name;
			input.type= "text";
			input.disabled = "true";
			input.value = this.opciones[0].Nombre;
			input.className = this.className;

			if (this.layer != "") {
				document.getElementById(this.layer).innerHTML = "";
				document.getElementById(this.layer).appendChild(input);
			}
			else
				document.body.appendChild(input);	
		}
		else if (this.resultado.Count =='0') {
		
			var input = document.createElement('input');
			input.name = this.name;
			input.id = this.name;
			input.type= "text";
			input.disabled = "true";
			input.value = "No hay resultados";
			input.className = this.className;

			if (this.layer != "") {
				document.getElementById(this.layer).innerHTML = "";
				document.getElementById(this.layer).appendChild(input);
			}
			else
				document.body.appendChild(input);	
		}	
	}
}


