$(function(){
		   
	//----------------------------------//
	// Faq abre/fecha pergunta
	//----------------------------------//
	jQuery('#faq li').bind('click', function() {
		jQuery('#faq li a').removeClass('active');
		
		if( jQuery('.resposta', this).eq(0).is(':hidden') ) {
			jQuery('.resposta').hide();
			jQuery('.resposta', this).show();
			jQuery('a', this).addClass('active');
		} else {
			jQuery('.resposta', this).hide();
			jQuery('#faq li a').removeClass('active');
		}
	});
	
		   
	//----------------------------------//
	// Arquivos abre/fecha
	//----------------------------------//
	$('dt','.listaArquivo').click(function(){
		arquivo.aba(this,'dl');
	});
	$('h6','.listaArquivo').click(function(){
		arquivo.aba(this,'dd');
	});
	
	// SWF
	swfobject.embedSWF("_swf/AegerPrincipal.swf", "main_swf_holder", 720, 320, "9.0.0", null, null, { wmode: "transparent" }, null);
	swfobject.embedSWF("_swf/MarcasAeger.swf", "marca_swf_holder", 369, 136, "9.0.0", null, null, { wmode: "transparent" }, null);
	swfobject.embedSWF("_swf/AegerNoticias.swf", "noticias_swf_holder", 379, 136, "9.0.0", null, null, { wmode: "transparent" }, null);
	
	// Fancybox
	jQuery(".fancyboxYT, .fancyModal, .fancybox").fancybox();
	
	
	// Revenda
	jQuery('#formRevenda').bind('submit', function(e) {
		e.preventDefault(); e.stopPropagation();
		
		jQuery.ajax({
			url: '_services/revenda_lista.php',
			data: jQuery('#formRevenda').serialize(),
			type: 'post',
			dataType: 'html',
			beforeSend: function() {
				jQuery('#resultadoBuscaRevenda').html('<br />Buscando...');
			},
			success: function(xhr) {
				jQuery('#resultadoBuscaRevenda').html( xhr ).show();
			},
			error: function() {
				jQuery('#resultadoBuscaRevenda').html('Houve algum erro ao efetuar sua busca. Tente novamente mais tarde.');
			}
		});
	});
	
	jQuery('#encontreMarca, #encontreEstado').bind('change', popOndeComprar);
	jQuery('#encontreCidade').bind('change', function() {
		if(jQuery(this).val() != '') {
			jQuery('#btBuscar').show();
		} else {
			jQuery('#btBuscar').hide();
		}
	});
	
	
	//
	jQuery('.btShare').bind('click', function(e) {
		e.preventDefault(); e.stopPropagation();
		var contextDiv 		= jQuery(this).closest('div').parent();
		var share_bar 		= jQuery('.share_bar', contextDiv);
		var related_posts = jQuery('.related_posts', contextDiv);
		
		related_posts.hide();
		if( jQuery(share_bar).is(':visible') ) {
			share_bar.stop(true, true).slideUp('fast');//hide();
		} else {
			share_bar.stop(true, true).slideDown('fast');//show();
		}
	});
	
	jQuery('.btPostsRelacionados').bind('click', function(e){
		e.preventDefault(); e.stopPropagation();
		var contextDiv 		= jQuery(this).closest('div').parent();
		var share_bar 		= jQuery('.share_bar', contextDiv);
		var related_posts = jQuery('.related_posts', contextDiv);
		
		share_bar.hide();
		if( jQuery(related_posts).is(':visible') ) {
			related_posts.stop(true, true).slideUp('fast');//hide();
		} else {
			related_posts.stop(true, true).slideDown('fast');//show();
		}
	});
	
	// Se uma imagem de lightbox tiver mais de 600px de altura, não abre em um lightbox e sim em uma nova aba
	jQuery('.fancyModal img').each(function() {
		if(jQuery(this).height() > 600) {
			var aLink = jQuery(this.parentNode);
			aLink.removeClass('fancyModal');
			jQuery(this).bind('click', function(e) { e.preventDefault(); e.stopPropagation(); window.open( aLink.attr('href') ); });
		}
	});
	
});

function popOndeComprar(elm) {
	var idMarca = jQuery('#encontreMarca').val();
	var estado  = jQuery('#encontreEstado').val();
	var cidade  = jQuery('#encontreCidade').val();
	
	jQuery.ajax({
		url: '_services/revenda_select.php',
		data: {'idMarca': idMarca, 'cidade': cidade, 'estado': estado},
		dataType: 'json',
		type: 'get',
		beforeSend: function() {
			jQuery('#encontreMarca, #encontreEstado, #encontreCidade').addClass('cursorProgress').attr('disabled', 'disabled');
		},
		success: function(xhr) {
			if(xhr == '' || xhr == null) {
				alert('Desculpe, mas os filtros que você escolheu não retornaram nenhum resultado.');
				jQuery('#encontreMarca, #encontreEstado, #encontreCidade').removeAttr('disabled').removeClass('cursorProgress');
				return false;
			}
			
			// Estado
			if(xhr.estado != undefined ) {
				var selectEstado = '';
				for(i in xhr.estado) selectEstado += '<option value="'+ xhr.estado[i] +'">'+ xhr.estado[i] +'</option>';
				
				jQuery('#encontreEstado').html(selectEstado);
			}
			
			// Cidade
			if( xhr.cidade != undefined ) {
				var selectCidade = '<option value="">Selecione...</option>';
				for(i in xhr.cidade) selectCidade += '<option value="'+ xhr.cidade[i] +'">'+ xhr.cidade[i] +'</option>';
				
				jQuery('#encontreCidade').html(selectCidade);
			}
			
			// Remove o disabled
			jQuery('#encontreMarca, #encontreEstado, #encontreCidade').removeAttr('disabled').removeClass('cursorProgress');
			
			// Se todos estiverem preenchidos
			if(idMarca != '' && cidade != '' && estado != '') jQuery('#btBuscar').show();
		}
	});
	
}


var arquivo = {
	aba: function(obj,parent){
		//$(obj).parent().find('dd').toggle();
		$(obj).parent().toggleClass('active');
		$($(obj).parent()).siblings(parent).each( function(){
			//$('dd',this).hide();
			$(this).removeClass('active');
		});
	}
}
