$(document).ready(function() {
    configureOndeComprar();
});


function configureOndeComprar() {
	//REFERENTE AO ONDE COMPRAR
		$('#selEstado').change(selecionarEstado);
		$('#selSegmento').change(selecionarSegmentos);
		$('#selBairro').change(selecionarBairro);
		$('#selCidade').change(selecionarCidade);
	

	//REFERENTE AO EXTRATO ONLINE
	if($("#bt-extrato-online").length > 0){
		$("#bt-extrato-online").click(abrirExtratoOnline);
	}
}

//BUSCA LOJAS
var timeSelecionarLojas;

function selecionarLojas(pagina) {
	var estado = $("#uf").val();
	var cidade = $('#selCidade').val();
	var bairro = $('#selBairro').val();
	var segmento = $('#selSegmento').val();
	var paginaAtual = pagina;
	
	if (paginaAtual == null || paginaAtual == "undefined" || paginaAtual == "") {
		paginaAtual = 1;
	}
	if(segmento == null || segmento == "undefined" || segmento == ""){
		segmento = "todos";
	}	
	if (bairro == "" && segmento == "") {
		$("#onde-comprar-resultado").html("");
	} else {
		//$('html,body').animate({scrollTop:0},500);
		$("#onde-comprar-resultado").slideUp("slow");
		clearTimeout(timeSelecionarLojas);
		timeSelecionarLojas = setTimeout(function() {
			$.ajax({ 
				url: "../php/web_local_busca_lojas.php",
				type:"GET",
				data:{uf:estado, cidade:cidade, segmento:segmento, bairro:bairro, paginaAtual:paginaAtual},
				context: document.body,
				success: function(retorno){
					$("#onde-comprar-resultado").html('');
					if (retorno != "") {
						$("#onde-comprar-resultado").html(retorno).slideDown("slow");
						paginacao();
					} else {
						$("#onde-comprar-resultado").html('<div class="onde-comprar-estabelecimento">Nenhum estabelecimento encontrado.</div>');
					}
			}});
		}, 600);
	}
}

//BUSCA BAIRROS APARTIR DA CIDADE
function selecionarCidade() {
	estado = $("#uf").val();
	$("#onde-comprar-resultado").html("");
	var cidade = $('#selCidade').val();
	
	$.ajax({ 
		url: "../php/web_local_busca_bairros.php",
		type:"GET",
		data:{uf:estado,cidade:cidade},
		context: document.body,
		success: function(retorno){
			retorno = retorno.split("|");
			if(retorno != ''){
				$('#selBairro').html("<option value=''>Selecione o bairro</option><option value=''>TODOS</option>");
					for(x=0; x < retorno.length; x++){
						$('#selBairro').append("<option value='"+retorno[x]+"'>"+retorno[x]+"</option>");
					}
			}else{
				$('#selBairro').html("<option value=''>Bairro : Selecione a Cidade</option>");
			}
	}});
	
	$.ajax({ 
		url: "../php/web_local_busca_segmentos.php", 
		type: "GET",
		data:{uf:estado, cidade:cidade},	
		context: document.body, 
		success: function(retorno){
			if (retorno != "") {
				retorno = retorno.split("|");
				$('#selSegmento').html("<option value=''>Selecione o Segmento</option>");
					for(x=0; x < retorno.length; x++){
						segmento = retorno[x].split('#');
						$('#selSegmento').append("<option value='"+segmento[1]+"'>"+segmento[0]+"</option>");
					}
			} else {
				$('#selSegmento').html("<option value=''>Nenhum segmento encontrado</option>");
			}
	}});
}


//BUSCA SEGMENTOS APARTIR DA ESCOLHA DO BAIRRO
function selecionarBairro() {
	estado = $("#uf").val();
	cidade = $("#selCidade").val();
	bairro = $("#selBairro").val();
	
	selecionarLojas();
	$.ajax({
		url: "../php/web_local_busca_segmentos.php",
		type:"GET",
		data:{uf:estado, cidade:cidade, bairro:bairro},
		context: document.body,
		success: function(retorno){
			if (retorno != "") {
				retorno = retorno.split("|");
				$('#selSegmento').html("<option value=''>Selecione o Segmento</option>");
					for(x=0; x < retorno.length; x++){
						segmento = retorno[x].split('#');
						$('#selSegmento').append("<option value='"+segmento[1]+"'>"+segmento[0]+"</option>");
					}
			} else {
				$('#selSegmento').html("<option value=''>Nenhum segmento encontrado</option>");
			}
		}
		});
}


//LIMPA O RESULTADO E BUSCA AS LOJAS NOVAMENTE
function selecionarSegmentos(){
	$('#onde-comprar-resultados').html("");
	selecionarLojas();
} 



function abrirExtratoOnline() {
	var win = window.open("../php/extrato-online.php","ExtratoOnline","width=800,height=600");
}



// Mapa em Flash

function mapaRetornoCidades(retorno) {
	if (retorno != '') {
		$('#selEstado').val($('#uf').val());
		retorno = retorno.split("|");
		$('#selCidade').html("<option value=''>Selecione a cidade</option>");
		for (x=0; x < retorno.length; x++) {
			$('#selCidade').append("<option value='"+retorno[x]+"'>"+retorno[x]+"</option>");
		}
	} else {
		$('#selCidade').html("<option value=''>Cidade : Selecione o Estado</option>");
	}
}



function mapaRetornoSegmentos(retorno) {
	if (retorno != "") {
		retorno = retorno.split("|");
		$('#selSegmento').html("<option value=''>Selecione o Segmento</option>");
		for(x=0; x < retorno.length; x++){
			segmento = retorno[x].split('#');
			$('#selSegmento').append("<option value='"+segmento[1]+"'>"+segmento[0]+"</option>");
		}
	} else {
		$('#selSegmento').html("<option value=''>Segmento : Selecione o Estado</option>");
	}
}



function mapaEstados(uf){
	uf = uf.replace(" ","");
	
	$("#uf").val(uf);
	
	$.ajax({ 
		url: "../php/web_local_busca_cidades.php?uf="+uf,
		context: document.body,
		success: mapaRetornoCidades
	});
	
}

function selecionarEstado(){
	var uf = $('#selEstado').val();
	$.ajax({ 
		url: "../php/web_local_busca_cidades.php",
		type:"GET",
		data:{uf:uf},
		context: document.body,
		success: function(retorno){
			if (retorno != '') {
				retorno = retorno.split("|");
				$('#selCidade').html("<option value=''>Selecione a cidade</option>");
					for (x=0; x < retorno.length; x++) {
						$('#selCidade').append("<option value='"+retorno[x]+"'>"+retorno[x]+"</option>");
					}
			} else {
				$('#selCidade').html("<option value=''>Cidade : Selecione o Estado</option>");
			}
		}
	});
}


//PAGINACAO
function paginacao() {
	$('html,body').animate({scrollTop:0},500);
	
	var paginaAtual = $('#paginaAtual').val();
	var totalPaginas = $('#numTotalPaginas').val();
	
	
	if (paginaAtual > 1){
		$('.bt-anterior').click(
			function(){
				var pagina = Number(paginaAtual) - 1;
				selecionarLojas(pagina);
			}		
		);
	} else {
		$('.bt-anterior').css('visibility','hidden');
	}
	
	if (paginaAtual < totalPaginas){
		$('.bt-proxima').click(
			function() {
				var pagina = Number(paginaAtual) + 1;
				selecionarLojas(pagina);
			}		
		);
	} else {
		$('.bt-proxima').css('visibility','hidden');
	}
}



