﻿registerNameSpace('KalCode');


KalCode.Directorio = function(){
	this.categorias = KalCode.Data.categorias;
	this.clientes = KalCode.Data.clientes;
	
this.setCategorias = function(){
	var html = '';
	var counter = 0;
	for(i=0; this.categorias.length > i; i++){
		counter += 1;
		if(counter == 1)
			html += '<div class="categoria_grupo">';
		
		html += '<div class="categoria" onclick="directorio.setCategoriaList(\'' +  this.categorias[i].id +'\', this);">' + this.categorias[i].categoria +'</div>';
		
		if(counter == 5){
			html += '</div>';
			counter = 0;
		}
	}
	$('categorias').innerHTML = html;
};
this.setCategoriaList = function(categoria, yo){
	var html = '';
	for(i=0; this.clientes.length > i; i++){
		var el = this.clientes[i];
		if(el.categoria_1 == categoria || el.categoria_2 == categoria){
			
			html += '<table class="empresa"><tr>';
			if(el.logo_url != null && el.logo_url != ''){
				html += '<td class="empresa_logo" ';
				html += 'onclick="window.open(\'http://' + el.web.toLowerCase() +'\', \'_blank\');" ';
				html += 'style="background-image:url(resources/logos/' + el.logo_url +');"';
				html += '></td>';
			
			}
			html += '<td class="empresa_datos"><span  class="empresa_nombre">' + el.empresa + '</span>';
			html += this.nonull('<br>', el.tipo);
			var direccion = '';
			direccion += el.estado + ' ';
			direccion += el.ciudad + ' ';
			direccion += el.municipio + ' ';
			direccion += el.avenida + ' ';
			direccion += el.local + ' ';
			direccion += el.punto_ref + ' ';
			direccion += el.piso;
			
			html += this.nonull('<br>Dirección:', direccion);
			html += this.nonull('<br>Teléfonos: ' , el.fono_1 + this.nonull(', ', el.fono_2) + this.nonull(', ', el.fono_3));
			html += this.nonull('<br>Fax : ', el.fax);
			html += this.nonull('<br>E-Mail: ', el.mail);
			
			if(!el.web.toLowerCase().match('dataconstruccion.com'))
			html += this.nonull('<br>Sitio Web : <a href="http://', el.web.toLowerCase(), '" target="_blank">' + el.web.toLowerCase() + '</a>');
			html += '</td></tr></table>';
		}
		 
		
		
	}
	$('result').innerHTML = html;
	
	var elementos = document.getElementsByTagName("div");
	
	for(f=0; elementos.length > f; f++){
		if(elementos[f].className == 'categoria_selected')
			elementos[f].className = 'categoria';
	}
	yo.className = 'categoria_selected';
	
	$('publi1').style.backgroundImage = 'url(resources/publicidad/' + categoria + '_1.jpg)';
	$('publi2').style.backgroundImage = 'url(resources/publicidad/' + categoria + '_2.jpg)';
	$('publi3').style.backgroundImage = 'url(resources/publicidad/' + categoria + '_3.jpg)';
	$('publi4').style.backgroundImage = 'url(resources/publicidad/' + categoria + '_4.jpg)';
	$('publi5').style.backgroundImage = 'url(resources/publicidad/' + categoria + '_5.jpg)';
	$('publi6').style.backgroundImage = 'url(resources/publicidad/' + categoria + '_6.jpg)';
	
};

this.result_list = new Array();
this.searchList = function(text){
	for(i=0; this.clientes.length > i; i++){
		var el = this.clientes[i];
		var my_string = el.empresa + ' ';
		my_string += el.estado + ' ';
		my_string += el.ciudad + ' ';
		my_string += el.municipio + ' ';
		my_string += el.avenida + ' ';
		my_string += el.local + ' ';
		my_string += el.punto_ref + ' ';
		my_string += el.tipo + ' ';
		my_string = this.normalizeText(my_string);
		
		var found = true;
		for(b=0; text.length > b; b++){
				var key = this.normalizeText(text[b])
				if(!my_string.match(key))
					found = false;		
		}
		if(found)
			this.result_list[this.result_list.length] = el;
	}
};
this.buscar = function(){
	this.result_list = new Array();
	var html = '';
	var value = $('text_box').value;
	
	if(value != ''){
	this.searchList(value.split(" "));
	
	
	for(i=0; this.result_list.length > i; i++){
		var el = this.result_list[i];
		html += '<table class="empresa"><tr>';
		if(el.logo_url != null && el.logo_url != ''){
			html += '<td class="empresa_logo" ';
			html += 'onclick="window.open(\'http://' + el.web.toLowerCase() +'\', \'_blank\');" ';
			html += 'style="background-image:url(resources/logos/' + el.logo_url +');"';
			html += '></td>';
			
		}
		html += '<td class="empresa_datos"><span  class="empresa_nombre">' + el.empresa + '</span>';
		html += this.nonull('<br>', el.tipo);
		var direccion = '';
		direccion += el.estado + ' ';
		direccion += el.ciudad + ' ';
		direccion += el.municipio + ' ';
		direccion += el.avenida + ' ';
		direccion += el.local + ' ';
		direccion += el.punto_ref + ' ';
		direccion += el.piso;
			
		html += this.nonull('<br>Dirección:', direccion);
		html += this.nonull('<br>Teléfonos: ' , el.fono_1 + this.nonull(', ', el.fono_2) + this.nonull(', ', el.fono_3));
		html += this.nonull('<br>Fax : ', el.fax);
		html += this.nonull('<br>E-Mail: ', el.mail);
		if(!el.web.toLowerCase().match('dataconstruccion.com'))
		html += this.nonull('<br>Sitio Web : <a href="http://', el.web.toLowerCase(), '" target="_blank">' + el.web.toLowerCase() + '</a>');
		html += '</td></tr></table>';
	}}
	
	if(this.result_list.length == 0)
		html += '<div class="no_result">No se encontró ningun resultado que contenga <strong>"' + value + '"</strong></div>';
	
	$('result').innerHTML = html;
	
};
this.normalizeText = function(text){
	var new_text = text.toLowerCase().replace('á', 'a').replace('é', 'e').replace('í', 'i').replace('ó', 'o').replace('ú', 'u');
	return new_text;
};



this.nonull = function(pref, text, suf){
	if(text != null && text != '' && text != ' '){
		if(suf)
		return pref + text + suf;
		else
		return pref + text;
	}
	else
		return '';
};
this.setCategorias();

this.onEnter = function(){
	var evtobj=window.event? event : e;
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
	if(unicode == '13')
		directorio.buscar();
		
		
};

}