jQuery(function(){
	var myTimeout, myTimeoutNews, myTimeoutSelectBox;
	
	//$("a[rel^='prettyPhoto']").prettyPhoto({ deeplinking:false, social_tools:false, hideflash:true, overlay_gallery:false });
	
	Cufon.replace('.headerContent h5, #pesquisaAssessor h4, #headCidadeSelecionada h5 var, .tituloBarra h3, #pesquisaPostos h4,', { fontFamily:'AvantGarde LT Book Bold' });
	Cufon.replace('.slider_content div .textoDestaque h2, #combustiveis ul li a', { fontFamily:'AvantGarde LT Book Bold' });
	
	Cufon.replace('#pesquisaPostos > span', { fontFamily:'AvantGarde LT Book' });
	
	
	// • • • • • • • • • • • Holover menu principal
	jQuery('#menu > ul > li').mouseenter(function(){
		jQuery(this).children('a').addClass('menuHover');
		jQuery(this).find('.subMenu').fadeIn('fast').show();
		
	}).mouseleave(function(){
		jQuery(this).children('a').removeClass('menuHover');
		jQuery(this).find('.subMenu').fadeOut('medium').hide();
	});
	
	
	// • • • • • • • • • • • Botao voltar ao topo
	jQuery('.voltaTopo').click(function(){ jQuery('html, body').animate({scrollTop: jQuery('body').offset().top}, 'slow'); });
	
	
	// • • • • • • • • • • • Botao voltar ao topo
	jQuery('.fechar').click(function(){ jQuery('#breadcrumbs').slideToggle('fast'); });
	
	
	// • • • • • • • • • • • Abrir login de acesso restrito
	jQuery('#areaRestrita > a').click(function(){
		var formLogin = jQuery(this).next('div');
		
		jQuery('#menu').children('ul').fadeOut('fast', function(){
			formLogin.fadeIn();
		});
	});
	// após 1seg o painel some
	jQuery('#areaRestrita > div').mouseleave(function(){
		var formLogin = jQuery('#areaRestrita').children('div');
		
		myTimeout = setTimeout(function() {
			formLogin.fadeOut('fast', function(){
				jQuery('#menu').children('ul').fadeIn();
			});	
		}, 700);
	}).mouseenter(function(){
		clearTimeout(myTimeout);
		jQuery('#logar').show();
		jQuery('#recSenha').hide();
	});
	
	
	// • • • • • • • • • • • Botoes para recuperar senha e voltar ao login
	jQuery('#btn_recSenha').click(function(){
		jQuery(this).parent('div').fadeOut('fast', function(){
			jQuery(this).find('input').val('');
			jQuery(this).next('div').fadeIn();
		})
	});
	jQuery('#btn_voltarLogin').click(function(){
		jQuery(this).parent('div').fadeOut('fast', function(){
			jQuery(this).find('input').val('');
			jQuery(this).prev('div').fadeIn();
		})
	});
	
	
	// • • • • • • • • • • • Abrindo form news letter
	jQuery('.newsLetter').click(function(){
		$(this).next('form').slideDown('medium');
		$(this).parents('#cadastroNews').css({ 'border-radius':5, 'box-shadow':'0 0 7px 1px #666' });
	});
	// Fechar form de cadastro de news letter em 2seg
	jQuery('#cadastroNews').mouseleave(function(){
		var formNewsLetter = jQuery(this).children('div');
		
		myTimeoutNews = setTimeout(function() {
			jQuery('#cadastroNews').find('form').slideUp('medium', function(){
				jQuery('#cadastroNews').css({ 'border-radius':0, 'box-shadow':'none' });
			});
			
		}, 800);
	}).mouseenter(function(){
		clearTimeout(myTimeoutNews);
	});
	
	
	// • • • • • • • • • • • • • • • SelectBoxes personalizados
	jQuery('.selectBox > a').click(function(){
		//jQuery(this).parents('.selectBox').css({ 'z-index':9999 });
		//var listaDown = jQuery(this).next('div').children('ul').slideDown('fast');
		var listaDown = jQuery(this).next('div').children('ul');
		
		if($(listaDown).is(':visible')){
			jQuery(this).parents('.selectBox').removeAttr('style');
			$(listaDown).removeAttr('style');
			
		} else {
			jQuery(this).parents('.selectBox').css({ 'z-index':9999 });
			$(listaDown).slideDown('fast');
		};
	});
	jQuery('.selectBox > div').mouseleave(function(){
		var comboListagem = jQuery(this).children('ul');
		var contentList = jQuery(this).parents('.selectBox');
		
		myTimeoutSelectBox = setTimeout(function() {
			comboListagem.slideUp('fast');
			contentList.removeAttr('style');
			
		}, 500);
	}).mouseenter(function(){
		clearTimeout(myTimeoutSelectBox);
	});

	// • • • • • • • • • • • • • • • Ajax do SelectBox dos Postos (Estados -> Cidades)
	jQuery('#encontrePosto #estado li').click(function(){
		var estado = $(this).children('a').html();
		var id_estado = $(this).attr('id');
		$('#estado var').html(estado);
		$('#estado ul').slideUp('fast');
		$.getJSON('ajaxPostos.php', {estado:id_estado}, function(data) {
			var items = [];
			var conta=1;
			$.each(data, function(key, val) {
				if (conta==1) {
					itens='<li id="' + key + '"><a href="javascript:void(0);">' + val + '</a></li>\n';
					$('#cidade var').html(val);
					$('#cidade var').attr('id',key);
					conta=2;
				} else {
					itens=itens+'<li id="' + key + '"><a href="javascript:void(0);">' + val + '</a></li>\n';
				}
			});
			$('#cidades').html(itens);
		});
		return false;
	});
	
	// • • • • • • • • • • • • • • • Ajax do SelectBox dos Postos (Cidades)
	jQuery('#cidades').on('click','li',function(){
		var cidade = $(this).children('a').html();
		var id=$(this).attr('id');
		$('#cidade var').html(cidade);
		$('#cidade ul').slideUp('fast');
		return false;
	});
	
	// • • • • • • • • • • • • • • • Manda para a página dos postos com a cidade selecionada
	jQuery('#procuraPostos').on('click', function(){
		var id_cidade = $('#cidade var').attr('id');
		window.location="postos.php?cidade="+id_cidade;
		return false;
	});
	
	// • • • • • • • • • • • • • • • Ajax do SelectBox dos Assessores (Estados -> Assessores e primeiro Assessor)
	jQuery('#pesquisaAssessor #estado li').click(function(){
		var estado = $(this).children('a').html();
		var id_estado = $(this).attr('id');
		$('#estado var').html(estado);
		$('#estado var').attr('id',id_estado);
		$('#estado ul').slideUp('fast');
		
		//Fecha as divs abertas
		$('#assessor').slideUp('fast',
			function() {
				$('#paginador').slideUp('fast',
					function() {
						$('#assessorListagem').slideUp('fast',
							function() {
								$('#headCidadeSelecionada').slideUp('fast');
		
								$('#headCidadeSelecionada var.estado').html(estado);
								$('#headCidadeSelecionada').slideDown('fast',
									function(){
										$.ajax({
											url: 'ajaxAssessores.php',
											data: {estado:id_estado},
											dataType: 'json',
											success:
												function(data) {
													var items = [];
													var conta=1;
													$.each(data, function(key, val) {
														if (conta==1) {
															itens='<li id="' + key + '"><a href="javascript:void(0);" title="' + val + '" class="onView">' + val + '</a></li>\n';
															primeiroAssessor=key;
															conta=2;
														} else {
															itens=itens+'<li id="' + key + '"><a href="javascript:void(0);" title="' + val + '">' + val + '</a></li>\n';
														}
													});
													$('#assessorListagem').html(itens).delay(50).slideDown('fast',
														function() {
															$.ajax({
																url: 'paginasAssessores.php',
																data: {estado:id_estado},
																dataType: 'json',
																success:
																	function(data) {
																		var paginas = [];
																		$.each(data, function(key, val) {
																			paginas=paginas+'<li class="separador">:</li><li id="' + key + '"><a href="javascript:void(0);" title="' + val + '" class="onView">' + val + '</a></li>\n';
																		});
																		$('#paginador').html(paginas);
																		$('#paginador').delay(50).fadeIn('fast',
																			function() {
																				$.ajax({
																					url: 'ajaxAssessor.php',
																					data: {id:primeiroAssessor},
																					dataType: 'text',
																					success:
																						function(data) {
																							$('#assessor').html(data).delay(50).slideDown('slow');
																						},
																				});
																			}
																		);
																	}
															});
														}
													);
												}
										});
									}
								);
							}
						)
					}
				);
			}
		);
		return false;
	});

	// • • • • • • • • • • • • • • • Pega os dados do Assessor e mostra.
	$('#assessorListagem').on('click','li',function(){
		var idAssessor = $(this).attr('id');
		$('#assessorListagem').find('li a').removeClass('onView');
		$(this).children('a').addClass('onView');
		
		$('#assessor').slideUp('fast',
		function() {
			$.ajax({
				url: 'ajaxAssessor.php',
				data: {id:idAssessor},
				dataType: 'text',
				success:
					function(data) {
						$('#assessor').html(data).delay(50).slideDown('slow');
					},
			});
		});
	});
		
	// • • • • • • • • • • • • • • • Pega os dados da pagina escolhida e mostra os resultados.
	$('#paginador').on('click','li a',function(){
		var idPagina = $(this).parent('li').attr('id');
		var idEstado = $('#estado var').attr('id');
		$('#assessorListagem').slideUp('fast',
			function(){
				$.ajax({
					url: 'ajaxAssessores.php',
					data: {estado:idEstado, pagina:idPagina},
					dataType: 'json',
					success:
						function(data) {
							var assessores = [];
							$.each(data, function(key, val) {
								assessores=assessores+'<li id="' + key + '"><a href="javascript:void(0);" title="' + val + '">' + val + '</a></li>\n';
							});
							$('#assessorListagem').html('');
							$('#assessorListagem').html(assessores).delay(50).slideDown('fast');
						}
				});
			}
		);
	});
		
	// • • • • • • • • • • • • • • • Ajax do SelectBox dos Postos (Estados -> Cidades)
	jQuery('#sideBarPostos #estado li').click(function(){
		var estado = $(this).children('a').html();
		var id_estado = $(this).attr('id');
		$('#estado var').html(estado);
		$('#estado var').attr('id',id_estado);
		$('#estado ul').slideUp('fast');
		$.getJSON('ajaxPostos.php', {estado:id_estado}, function(data) {
			var items = [];
			var conta=1;
			$.each(data, function(key, val) {
				if (conta==1) {
					itens='<li id="' + key + '"><a href="javascript:void(0);">' + val + '</a></li>\n';
					$('#cidade var').html(val);
					$('#cidade var').attr('id',key);
					conta=2;
				} else {
					itens=itens+'<li id="' + key + '"><a href="javascript:void(0);">' + val + '</a></li>\n';
				}
			});
			$('#cidades').html(itens);
		});
		return false;
	});
		
	// • • • • • • • • • • • • • • • Ajax do SelectBox dos Postos (Cidades -> Postos e primeiro Posto)
	jQuery('#cidades').on('click', 'li', function(){
		var cidade = $(this).children('a').html();
		var id_cidade = $(this).attr('id');
		$('#cidade var').html(cidade);
		$('#cidade var').attr('id',id_cidade);
		$('#cidade ul').slideUp('fast');
		
		//Fecha as divs abertas
		$('#postos').slideUp('fast',
			function() {
				$('#paginadorPostos').slideUp('fast',
					function() {
						$('#assessorListagem').slideUp('fast',
							function() {
								$('#headCidadeSelecionada').slideUp('fast');
		
								$('#headCidadeSelecionada var.cidade').html(cidade);
								$('#headCidadeSelecionada').slideDown('fast',
									function(){
										$.ajax({
											url: 'ajaxPostosCidades.php',
											data: {cidade:id_cidade},
											dataType: 'json',
											success:
												function(data) {
													var items = [];
													var conta=1;
													$.each(data, function(key, val) {
														if (conta==1) {
															itens='<li id="' + key + '"><a href="javascript:void(0);" title="' + val + '" class="onView">' + val + '</a></li>\n';
															primeiroPosto=key;
															conta=2;
														} else {
															itens=itens+'<li id="' + key + '"><a href="javascript:void(0);" title="' + val + '">' + val + '</a></li>\n';
														}
													});
													$('#assessorListagem').html(itens).delay(50).slideDown('fast',
														function() {
															$.ajax({
																url: 'paginasPostos.php',
																data: {cidade:id_cidade},
																dataType: 'json',
																success:
																	function(data) {
																		var paginas = [];
																		$.each(data, function(key, val) {
																			paginas=paginas+'<li class="separador">:</li><li id="' + key + '"><a href="javascript:void(0);" title="' + val + '" class="onView">' + val + '</a></li>\n';
																		});
																		$('#paginadorPostos').html(paginas);
																		$('#paginadorPostos').delay(50).fadeIn('fast',
																			function() {
																				$.ajax({
																					url: 'ajaxPosto.php',
																					data: {id:primeiroPosto},
																					dataType: 'text',
																					success:
																						function(data) {
																							$('#postos').html(data).delay(50).slideDown('slow');
																						},
																				});
																			}
																		);
																	}
															});
														}
													);
												}
										});
									}
								);
							}
						)
					}
				);
			}
		);
		return false;
	});

	// • • • • • • • • • • • • • • • Pega os dados da pagina escolhida e mostra os resultados.
	$('#paginadorPostos').on('click','li a',function(){
		var idPagina = $(this).parent('li').attr('id');
		var idCidade = $('#cidade var').attr('id');
		$('#assessorListagem').slideUp('fast',
			function(){
				$.ajax({
					url: 'ajaxPostosCidades.php',
					data: {cidade:idCidade, pagina:idPagina},
					dataType: 'json',
					success:
						function(data) {
							var assessores = [];
							$.each(data, function(key, val) {
								assessores=assessores+'<li id="' + key + '"><a href="javascript:void(0);" title="' + val + '">' + val + '</a></li>\n';
							});
							$('#assessorListagem').html('');
							$('#assessorListagem').html(assessores).delay(50).slideDown('fast');
						}
				});
			}
		);
	});
		
	// • • • • • • • • • • • • • • • Pega os dados do Assessor e mostra.
	$('#assessorListagem.jQueryPostos').on('click','li',function(){
		var idAssessor = $(this).attr('id');
		$('#assessorListagem').find('li a').removeClass('onView');
		$(this).children('a').addClass('onView');
		
		$('#postos').slideUp('fast',
		function() {
			$.ajax({
				url: 'ajaxPosto.php',
				data: {id:idAssessor},
				dataType: 'text',
				success:
					function(data) {
						$('#postos').html(data).delay(50).slideDown('slow');
					},
			});
		});
	});

	// • • • • • • • • • • • • • • • Abrindo/fechando menu de noticias
	jQuery('#menuHierarquico > li > a').click(function(){
		jQuery(this).parent('li').siblings('li').removeAttr('class').children('ul').slideUp('medium');
		
		if(jQuery(this).parent('li').hasClass('onView')){
			jQuery(this).parent('li').removeAttr('class').children('ul').slideUp('medium');
			
		} else {
			jQuery(this).parent('li').addClass('onView').children('ul').slideDown('fast');
		}
	});
	
	
	// • • • • • • • • • • • • • • • Abrir/Fechar sidebar endereços
	jQuery('#sideBarEnderecos ul li > a').click(function(){
		jQuery(this).parent('li').addClass('onView').children('div').slideDown('medium').parent('li').siblings('li').removeClass('onView').children('div').slideUp('medium');
	});
	
	
	// • • • • • • • • • • • • • • • Abre/fecha rodutos, descrição de combustiveis
	jQuery('#combustiveis ul li a').click(function(){
		combustivelAbrirDados($(this));
	});
	jQuery('#combustiveis ul li span').click(function(){
		combustivelAbrirDados($(this).parent('li').children('a'));
	});
		
	function combustivelAbrirDados(e){
		e.parent('li').siblings('li').removeAttr('class').children('div').slideUp('fast');
		
		if(e.parent('li').hasClass('onView')){
			e.parent('li').removeAttr('class').children('div').slideUp('fast');
			jQuery('html, body').animate({scrollTop: $('#main').offset().top}, 'normal');
			
		} else {
			e.parent('li').addClass('onView').children('div').slideDown('fast', function(){
				jQuery('html, body').animate({scrollTop: e.parent('li').offset().top}, 'normal');
			});
		}
	};
	
	
	// • • • • • • • • • • • • • • • Focus/Blur formulario contato
	jQuery('form input[type=text], form input[type=tel], form input[type=email], form input[type=password], form textarea').each(function() {
		var default_value = this.value;
		jQuery(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		jQuery(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	
	$('#contatoSite #setor li').click(
		function() {
			var setor = $(this).children('a').html();
			var id_setor = $(this).attr('id');
			$('#setor var').html(setor);
			$('#setor var').attr('id',id_setor);
			$('#setor ul').slideUp('fast');
		}
	);
	
	$('#contatoSite #botao_enviar').click(
		function() {
			var nome = $('#contatoSite input[name="nome"]').val();
			var telefone = $('#contatoSite input[name="telefone"]').val();
			var email = $('#contatoSite input[name="email"]').val();
			var empresa = $('#contatoSite input[name="empresa"]').val();
			var assunto = $('#contatoSite input[name="assunto"]').val();
			var setor = $('#setor var').attr('id');
			var mensagem = $('#contatoSite textarea[name="mensagem"]').val();
			if (nome=="" || nome=="Nome") {
				$('.alertContato').slideUp('fast', 
					function() {
						$('#OK').hide();
						$('#ERRO span').html("Preencha seu nome corretamente");
						$('#ERRO').show();
						$('.alertContato').slideDown('slow') 
					}																
				);
				return false;
			}
			if (telefone=="" || telefone=="Fone") {
				$('.alertContato').slideUp('fast', 
					function() {
						$('#OK').hide();
						$('#ERRO span').html("Preencha seu telefone corretamente");
						$('#ERRO').show();
						$('.alertContato').slideDown('slow') 
					}																
				);
				return false;
			}
			if (email=="" || email=="E-mail") {
				$('.alertContato').slideUp('fast', 
					function() {
						$('#OK').hide();
						$('#ERRO span').html("Preencha seu email corretamente");
						$('#ERRO').show();
						$('.alertContato').slideDown('slow') 
					}																
				);
				return false;
			}
			if (empresa=="" || empresa=="Empresa") {
				$('.alertContato').slideUp('fast', 
					function() {
						$('#OK').hide();
						$('#ERRO span').html("Preencha sua empresa corretamente");
						$('#ERRO').show();
						$('.alertContato').slideDown('slow') 
					}																
				);
				return false;
			}
			if (assunto=="" || assunto=="Assunto") {
				$('.alertContato').slideUp('fast', 
					function() {
						$('#OK').hide();
						$('#ERRO span').html("Preencha o assunto corretamente");
						$('#ERRO').show();
						$('.alertContato').slideDown('slow') 
					}																
				);
				return false;
			}
			if (setor=="") {
				$('.alertContato').slideUp('fast', 
					function() {
						$('#OK').hide();
						$('#ERRO span').html("Escolha um setor");
						$('#ERRO').show();
						$('.alertContato').slideDown('slow') 
					}																
				);
				return false;
			}
			if (mensagem=="" || mensagem=="Mensagem") {
				$('.alertContato').slideUp('fast', 
					function() {
						$('#OK').hide();
						$('#ERRO span').html("Preencha a mensagem corretamente");
						$('#ERRO').show();
						$('.alertContato').slideDown('slow') 
					}																
				);
				return false;
			}
			$.ajax({
				url: 'enviaContato.php',
				data: {tipo:'2', nome:nome, telefone:telefone, email:email, empresa:empresa, assunto:assunto, setor:setor, mensagem:mensagem},
				dataType: 'html',
				type: 'POST',
				success:
					function(data) {
						if (data=="true") {
							$('.alertContato').slideUp('fast', 
								function() {
									$('#OK').show();
									$('#ERRO').hide();
									$('.alertContato').slideDown('slow') 
								}																
							);
						} else {
							$('.alertContato').slideUp('fast', 
								function() {
									$('#OK').hide();
									$('#ERRO span').html(data);
									$('#ERRO').show();
									$('.alertContato').slideDown('slow') 
								}																
							);
						}															
					}
			});
		return false;
		}
	)

	$('#newsletter #botao_enviar').click(
		function() {
			var nome = $('#newsletter input[name="nome"]').val();
			var email = $('#newsletter input[name="email"]').val();
			if (nome=="" || nome=="Nome") {
				alert("Preencha seu nome corretamente");
				return false;
			}
			if (email=="" || email=="E-mail") {
				alert("Preencha seu email corretamente")
				return false;
			}
			$.ajax({
				url: 'cadastroNews.php',
				data: {nome:nome, email:email},
				dataType: 'html',
				type: 'POST',
				success:
					function(data) {
						if (data=="true") {
							$('#dadosNews form').fadeOut('fast', 
								function() {
									$('#dadosNews .sucesso').fadeIn('slow').delay(3000);
									$('#dadosNews .sucesso').fadeOut('fast', 
										function() {
											$('#dadosNews form').fadeIn('slow');
										}
									);
								}																
							);
						} else {
							$('#dadosNews form').fadeOut('fast', 
								function() {
									$('#dadosNews .erro').fadeIn('slow').delay(3000);
									$('#dadosNews .erro').fadeOut('fast', 
										function() {
											$('#dadosNews form').fadeIn('slow');
										}
									);
								}																
							);
						}															
					}
			});
		return false;
		}
	)
	
	
	
	
	
	// • • • • • • • • • • • • • • • Carregando pagina ao clicar no menu
    var hash = window.location.hash.substr(1);
	
	if( hash.length > 0 ){
		console.log('existe hash');
		jQuery('li#'+ hash).addClass('onView').children('ul').slideDown('fast');
		return false;
	}
	
	
});







