/*****************************************************************************/
/*                                2010 Shio2e                                */
/*                     JAVASCRIPT BUSCADOR :: CATEGORIAS                    */
/*****************************************************************************/

buscador.categorias = {
	// VARIABLES CONFIGURACION
	classCategoria : 'seleccionado',

	// VARIABLES INTERNAS
	categorias : new Array(),

	// FUNCIONES
	crear : function (categorias){
		categorias.each(function(categoria, posicion) {
			this.add(categoria);
		}.bind(this));
	},

	add : function (categoria){
		this.categorias.push({ id: categoria.id, nombre: categoria.nombre });
	},

	click : function (elemento, id_categoria){
		if (elemento.className == this.classCategoria) {
			this.deseleccionar(elemento, id_categoria);
		} else {
			this.seleccionar(elemento, id_categoria);
		}
		buscador.regenerarComercios();
		if (buscador.seccionActiva == 'mapa') {
			buscador.regenerarMarcadores();
		} else if (buscador.seccionActiva == 'buscador') {
			buscador.buscadorOfertas.consulta();
		} else {
			buscador.abrir('mapa');
		}
	},

	seleccionar : function (elemento, id_categoria){
		// LINEA PARA SELECCION DE UNA UNICA CATEGORIA
		this.resetear();

		elemento.className = this.classCategoria;
		buscador.filtroCategorias.push(id_categoria);
		buscador.filtroCategorias = buscador.filtroCategorias.uniq();
		this.actualizarMarcadas();
	},

	deseleccionar : function (elemento, id_categoria){
		elemento.className = '';
		buscador.filtroCategorias = buscador.filtroCategorias.without(id_categoria);
		this.actualizarMarcadas();
	},

	actualizarMarcadas : function (){
		var texto = '';
		if (buscador.filtroCategorias.length == 0 || buscador.filtroCategorias.length == this.categorias.length) {
			texto = 'todas';
		} else {
			texto = buscador.filtroCategorias.length;
		}
		$('num_cat_selected').update(texto);
	},

	nombreXIdCategoria : function (id_categoria){
		for (i=0; i<this.categorias.length; i++)
			if (this.categorias[i].id == id_categoria)
				return this.categorias[i].nombre;
		return '';
	},

	nombreXIdCategorias : function (categorias){
		var texto = '';

		categorias.each(function(categoria, posicion) {
			texto += (texto == '' ? '' : ', ')+this.nombreXIdCategoria(categoria);
		}.bind(this));

		return texto;
	},

	resetear : function (){
		var prefijo = 'cat.';
		while (buscador.filtroCategorias.length > 0)
			if ($(prefijo+buscador.filtroCategorias[0]))
				this.deseleccionar($(prefijo+buscador.filtroCategorias[0]), buscador.filtroCategorias[0]);
	}
}
