// JavaScript Document

$(document).ready(function(){
						   
     
	 //selecionando as cidades
	 $(".ondecomprar").click(function(event){
		 
		 $(".logos").fadeOut("fast",function(){
			 $(this).css('display','none');
		 });
			
		   event.preventDefault();
		   
		   href = $(this).attr('href').replace("#","");	
           $("#cidades").fadeOut("fast");
		   $("#lojas").fadeOut("fast");
		   $("#divprod").fadeOut("fast");
		   
		   
		     //fazendo o ajax
		     $.ajax({
				  
				  url:'ajaxondecomprar.php',
				  cache: false,
		          //tipo de retorno
		          dataType:'html',
		          //metodo
		          type: 'POST',
		          //tempo limite
		          timeout: '80000',
				  data: {busca: 'municipio', key: href},
				  success: function(data, textStatus){
		    
					       $("#cidades").fadeIn("slow",function(){
								 $("#estado").text(href);	
								 $("#select-cidades").html(data);
				
						   });
				  },
				  error: function(xhr,err){
					  alert('Ocorreu um erro, por favor clique no estado novamente!');
				  }
		     });
     });
	 
	 
	 //selecionando os produtos
    $("#select-cidades").change(function(){
											 
		   valor = $(this).val();
		   $("#lojas").fadeOut("fast");
           $("#divprod").fadeOut("fast");
		   
		
		     //fazendo o ajax
		     $.ajax({
				  
				  url:'ajaxondecomprar.php',
				  cache: false,
		          //tipo de retorno
		          dataType:'html',
		          //metodo
		          type: 'POST',
		          //tempo limite
		          timeout: '50000',
				  data: {busca:'produtos', key: valor},
				  success: function(data, textStatus){
                            
					        $("#divprod").fadeIn("slow",function(){
						         $("#municipio").text(valor);
								 $("#select-produtos").html(data);
				           
					        }); 		
				  },
				  error: function(xhr,err){
					  alert('Ocorreu um erro, por favor selecione a cidade novamente!');
				  }
		      });
     
	  });
	
	
	
	//selecionando as lojas
    $("#select-produtos").change(function(){
											 
		   valor = $(this).val();
		   $("#lojas").fadeOut("fast");
           	   
		   texto = $("#select-produtos option:selected").text();
		     //fazendo o ajax
		     $.ajax({
				  
				  url:'ajaxondecomprar.php',
				  cache: false,
		          //tipo de retorno
		          dataType:'html',
		          //metodo
		          type: 'POST',
		          //tempo limite
		          timeout: '50000',
				  data: {busca:'lojas', key: valor, extra: $("#select-cidades").val()},
				  success: function(data, textStatus){

					        $("#lojas").fadeIn("slow",function(){
						          $("#produto").text(texto);
								  
								  $("#minhas-lojas").html(data);
								  
								  lojas = new Array();
								  x = 0;
								  $(data).each(function(){
									  
									  texto = $($(this),".lojashome").html();
									  texto = texto.split("<br>");
									  texto = texto[0];
									  texto = $.trim($(texto).text());
									  lojas[x] = texto;
									  x++;
								  });
								  
								  tem = jQuery.inArray("RICARDO ELETRO",lojas);
								  
								  if(tem != "-1"){
								     $(".logos").fadeIn("slow",function(){
										    
											$(this).css('display','block');
									 }); 
								  }else{
									  
									     		 $(".logos").fadeOut("fast",function(){
			                                       $(this).css('display','none');
		                                         });
								  }
				           
					        }); 		
				  },
				  error: function(xhr,err){
					  alert('Ocorreu um erro, por favor selecione a cidade novamente!');
				  }
		     });
     });
	 
	 
	 
	 
						   
});
