window.onload = function(e){
	mostraFlash("menu", "/media/swf/menu.swf?secao=lojas", "100%", 340);
	setRadio('estado','SP');
	montaCidades('SP');
	//buscaLojas();
	setTimeout('buscaLojas()',2000);
	
}


function montaCidades(estado,d){
	//d (true/false) para selecionar o valor defaulf
	
	$('ufEstadoEscolhido').innerHTML = estado;
	var xml = new xmlConnection();
	if (xml.create()){
		//xml.setURL('/lupo/mostrarCidadesLojas/');
		xml.setURL('/mostrarCidadesLojas/');
		xml.setMethod('GET');
		xml.add('nocache',nocache());
		xml.add('estado',estado);			
	}
	xml.onComplete = function() {
		this.setChildName('cidade');	
		var numItens = this.getCountItens();		
		var html = '';
		
		if(numItens==1){
			html += '<li><input type="checkbox" checked="checked" name="cidade[]" id="c'+this.getAttByName('id',0)+'" value="'+this.getDataByName(0)+'" onclick="buscaLojas();"  /><span class="corEstados" style="cursor:pointer;" onclick="setCheckbox(\'c'+this.getAttByName('id',j)+'\');buscaLojas();">'+this.getDataByName(0)+'</span></li>';
		}else{
			for (var j=0; j<numItens; j++) {
				if(d!=true){
					html += '<li><input type="checkbox" name="cidade[]" id="c'+this.getAttByName('id',j)+'" value="'+this.getDataByName(j)+'" onclick="buscaLojas();"  /><span class="corEstados" style="cursor:pointer;" onclick="setCheckbox(\'c'+this.getAttByName('id',j)+'\');buscaLojas();">'+this.getDataByName(j)+'</span></li>';
				}else{
					//default pra primeira vez
					if(this.getDataByName(j)=="São Paulo"){
						html += '<li><input type="checkbox" checked name="cidade[]" id="c'+this.getAttByName('id',j)+'" value="'+this.getDataByName(j)+'" onclick="buscaLojas();"  /><span class="corEstados" style="cursor:pointer;" onclick="setCheckbox(\'c'+this.getAttByName('id',j)+'\');buscaLojas();">'+this.getDataByName(j)+'</span></li>';
					}else{
						html += '<li><input type="checkbox" name="cidade[]" id="c'+this.getAttByName('id',j)+'" value="'+this.getDataByName(j)+'" onclick="buscaLojas();"  /><span class="corEstados" style="cursor:pointer;" onclick="setCheckbox(\'c'+this.getAttByName('id',j)+'\');buscaLojas();">'+this.getDataByName(j)+'</span></li>';
					}
				}			
			}
		}
		$('ulCidades').innerHTML = html;		
		setTimeout('buscaLojas(1)',100);
		$('qtdeCidades').value = numItens;
	}
	xml.execute();	
}

function buscaLojas(pag){
	
	pag = pag || 1;
	
	
	var xml = new xmlConnection();
	if (xml.create()){
		//xml.setURL('/lupo/mostrarLojas/');
		xml.setURL('/mostrarLojas/');
		xml.setMethod('GET');
		xml.add('nocache',nocache());
		xml.add('estado',radio('estado'));
		xml.add('pag',pag);
		
		var cidades = [];
		for(i=0;i<$('qtdeCidades').value*1;i++){
			if($('c'+i).checked){
				cidades.push($('c'+i).value)
			}
		}	
		
		xml.add('cidade',cidades.join(","));
		
	}
	xml.onComplete = function() {
		this.setChildName('loja');
		var numItens = this.getCountItens();
		

		var cidade = "";
		var html = "";
		
		
		for (var j=0; j<8; j++) {
			if (j < numItens){
				var totalRegistros = this.getAttByName('qtde',j);
				var totalPags = this.getAttByName('qtdePags',j);
				
				if(this.getAttByName('cidade',j)!=cidade){
					html += '<div style="position:relative;height:50px;">';
					html += '	<div id="bgVazio"></div>';
					html += '	<div id="segundaCidadeEscolhida">'+this.getAttByName('cidade',j)+' - '+this.getAttByName('estado',j)+'</div>';
					html += '</div>';
					cidade = this.getAttByName('cidade',j);
				}
				
				html += '<div id="resultadoLojas">';
				if(this.getAttByName('localizacao',j)!="" && this.getAttByName('localizacao',j)!="None" && this.getAttByName('localizacao',j)!=null){
					html += '	'+this.getAttByName('localizacao',j);
				}
				html += '</div>';
				html += '<div id="resultadoBuscaLocal">';
				if(this.getAttByName('endereco',j)!="" && this.getAttByName('endereco',j)!="None" && this.getAttByName('endereco',j)!=null){
					html += '	'+this.getAttByName('endereco',j);
				}
				if(this.getAttByName('numero',j)!="" && this.getAttByName('numero',j)!="None" && this.getAttByName('numero',j)!=null){
					html += ',	'+this.getAttByName('numero',j);
				}
				if(this.getAttByName('complemento',j)!="" && this.getAttByName('complemento',j)!="None" && this.getAttByName('complemento',j)!=null){
					html += ' - 	'+this.getAttByName('complemento',j)+'<br />';
				}
				
				if(this.getAttByName('bairro',j)!="" && this.getAttByName('bairro',j)!="None" && this.getAttByName('bairro',j)!=null){
					html += '	'+this.getAttByName('bairro',j)+'<br />';
				}
				if(this.getAttByName('telefone',j)!="" && this.getAttByName('telefone',j)!="None" && this.getAttByName('telefone',j)!=null){
					html += '	'+this.getAttByName('telefone',j);
				}
				html += '</div>';
				html += '<div id="borderResultado"></div>';
			}
			
		}
		
		if(totalRegistros==0 || totalRegistros==undefined){
			totalPags = 0;
			totalRegistros = 0;
			//$('resultadoBusca').style.display = "none";
			
			html += '<div style="position:relative;height:50px;">';
			html += '	<div id="bgVazio"></div>';
			html += '	<div id="segundaCidadeEscolhida">Endere&ccedil;o da F&aacute;brica</div>';
			html += '</div>';
			
			html += '<div id="resultadoBuscaLocal">';
			html += '	Rodovia Washington Luis km 276,5<br />'
			html += '	Araraquara - SP<br />'
			html += '	14803-900<br />'
			html += '	Fone: (16) 3303-4000 | Fax: (16) 3303-4005';
			html += '</div>';
			
			html += '<div id="fotoFabrica" />';
			
			$('resultadoBusca').style.display = "none";
			$('resultadoBusca0').style.display = "none";
			$('resultadoBusca1').style.display = "none";
			
		}else{
			$('resultadoBusca').style.display = "block";
			$('resultadoBusca0').style.display = "none";
			$('resultadoBusca1').style.display = "none";
		}
		
		if(totalRegistros<=8){
			$('bgPaginacaoFale').style.display = "none";
		}else{
			$('bgPaginacaoFale').style.display = "block";
		}
		
		
		$('resultadoBuscaPags').innerHTML = totalPags;
		$('resultadoBuscaQdte').innerHTML = totalRegistros;
		$('paginacao').innerHTML = paginacao(pag,totalPags,'buscaLojas')
		$('resultadoLojasAjax').innerHTML = html;
	}
	xml.execute();		
}

function buscaLojaMenu(palavra,pag){
	pag = pag || 1;
	var xml = new xmlConnection();
	if (xml.create()){
		//xml.setURL('/lupo/buscarLojas/');
		xml.setURL('/buscarLojas/');
		xml.setMethod('GET');
		xml.add('nocache',nocache());
		xml.add('palavra',palavra);
		xml.add('pag',pag);
		
		
	}
	xml.onComplete = function() {
		this.setChildName('loja');
		var numItens = this.getCountItens();
		

		var cidade = "";
		var html = "";
		
		
		for (var j=0; j<8; j++) {
			if (j < numItens){
				var totalRegistros = this.getAttByName('qtde',j);
				var totalPags = this.getAttByName('qtdePags',j);
				
				if(this.getAttByName('cidade',j)!=cidade){
					html += '<div style="position:relative;height:50px;">';
					html += '	<div id="bgVazio"></div>';
					html += '	<div id="segundaCidadeEscolhida">'+this.getAttByName('cidade',j)+' - '+this.getAttByName('estado',j)+'</div>';
					html += '</div>';
					cidade = this.getAttByName('cidade',j);
				}
				
				html += '<div id="resultadoLojas">';
				html += '	'+this.getAttByName('localizacao',j);
				html += '</div>';
				html += '<div id="resultadoBuscaLocal">';
				html += '	'+this.getAttByName('endereco',j)+'<br />'
				html += '	'+this.getAttByName('bairro',j)+'<br />'
				html += '	'+this.getAttByName('telefone',j);
				html += '</div>';
				html += '<div id="borderResultado"></div>';
			}
			
		}
		
		if(totalRegistros==0 || totalRegistros==undefined){
			totalPags = 0;
			totalRegistros = 0;
			$('resultadoBusca0').style.display = "block";
			$('resultadoBusca1').style.display = "none";
			$('resultadoBusca').style.display = "none";
			$('palavraBusca0').innerHTML = palavra;
		}else{
			$('resultadoBusca0').style.display = "none";
			$('resultadoBusca1').style.display = "block";
			$('resultadoBusca').style.display = "block";
			$('palavraBusca1').innerHTML = palavra;
		}
		
		
		if(totalRegistros<=8){
			$('bgPaginacaoFale').style.display = "none";
		}else{
			$('bgPaginacaoFale').style.display = "block";
		}
		
		
		$('resultadoBuscaPags').innerHTML = totalPags;
		$('resultadoBuscaQdte').innerHTML = totalRegistros;
		$('paginacao').innerHTML = paginacao(pag,totalPags,'buscaLojas')
		$('resultadoLojasAjax').innerHTML = html;
	}
	xml.execute();	
}