$(function(){
	// Tabs
	var $tabs = $( "#tabs" ).tabs({
			ajaxOptions: {
				error: function( xhr, status, index, anchor ) {
					$( anchor.hash ).html(
						"Não foi possível carregar esta aba. Tente mais tarde. "
						);
				}
			},

			load : function(event, ui) {
				var selected = $( "#tabs" ).tabs( "option", "selected" );
				//console.log(selected);
				var maxItems = 4;
				$("#ui-tabs-"+selected+" .carousel li").each(function(ij){
					console.log(ij);
					var father = $(this).parent().parent(".carousel"); 
						if (!father.hasClass("fixednav")){ //se .carousel não tem a classe .fixednav
							if(ij%maxItems == 0 && ij>=maxItems){ //cria a divisão por 4
								father.append("<ul class='productfeaturelist'></ul>");
							}
							father.children(".productfeaturelist").last().append(this);	
						}
				});
				$("#ui-tabs-"+selected+" .carouselm li").each(function(ik){
					//console.log(ik);
					//console.log($(this));
					var father1 = $(this).parent().parent(".carouselm"); 
						if (!father1.hasClass("fixednav")){ //se .carousel não tem a classe .fixednav
							if(ik%maxItems == 0 && ik>=maxItems){ //cria a divisão por 4
								father1.append("<ul class='productfeaturelist'></ul>");
							}
							father1.children(".productfeaturelist").last().append(this);	
						}
				});
				
				$(".carousel").each(function(i){
					if (!$(this).hasClass("fixednav")){
						$(this).addClass("fixednav");
					}
				});
				$(".carouselm").each(function(i){
					if (!$(this).hasClass("fixednav")){
						$(this).addClass("fixednav");
					}
				});
			
				//filtro remove vazios
				$('ul.productfeaturelist').filter(function() { 
					return $.trim($(this).text()) === ''
				}).remove();
			
				$('.carousel').each(function(){
					var show = $(this);
					show.cycle({ 
					fx:     'scrollHorz', 
					speed:  '600', 
					timeout: 0, 
					next:   show.next('.archiveNext'),
					prev:   show.siblings('.archivePrev'),
					pager: show.siblings().next('.cnav')
					});
				});
		
				$('.carouselm').each(function(){
					var showm = $(this);
					showm.cycle({ 
					fx:     'scrollHorz', 
					speed:  '600', 
					timeout: 0, 
					next:   showm.next('.archiveNext'),
					prev:   showm.siblings('.archivePrev'),
					pager: showm.siblings().next('.cnav')
					});
				});

			}

	});

	// carousel HOME
	var maxItems = 4;
				$(".carousel li").each(function(i){
					var father = $(this).parent().parent(".carousel"); 
						if (!father.hasClass("fixednav")){ //se .carousel não tem a classe .fixednav
							if(i%maxItems == 0 && i>=maxItems){ //cria a divisão por 4
								father.append("<ul class='productfeaturelist'></ul>");
							}
							father.children(".productfeaturelist").last().append(this);	
						}
				});
				$(".carouselm li").each(function(i){
					var father1 = $(this).parent().parent(".carouselm"); 
						if (!father1.hasClass("fixednav")){ //se .carousel não tem a classe .fixednav
							if(i%maxItems == 0 && i>=maxItems){ //cria a divisão por 4
								father1.append("<ul class='productfeaturelist'></ul>");
							}
							father1.children(".productfeaturelist").last().append(this);	
						}
				});
				
	$(".carousel").each(function(i){
		if (!$(this).hasClass("fixednav")){
			$(this).addClass("fixednav");
		}
	});
	$(".carouselm").each(function(i){
		if (!$(this).hasClass("fixednav")){
			$(this).addClass("fixednav");
		}
	});

	//filtro remove vazios
	$('ul.productfeaturelist').filter(function() { 
        return $.trim($(this).text()) === ''
    }).remove();

	$('.carousel').each(function(){
		var show = $(this);
		show.cycle({ 
		fx:     'scrollHorz', 
		speed:  '600', 
		timeout: 0, 
		next:   show.next('.archiveNext'),
		prev:   show.siblings('.archivePrev'),
		pager: show.siblings().next('.cnav')
		});
	});
	var showm = $('.carouselm');
	$('.carouselm').cycle({ 
		fx:     'scrollHorz', 
		speed:  '600', 
		timeout: 0, 
		next:   showm.next('.archiveNext'),
		prev:   showm.siblings('.archivePrev'),
		pager: showm.siblings().next('.cnav')
	});

//fix para tab de ver todos

$('#tabnav6').hover(function () {
    $("#todas_cat").css("display","inline-block");
	$("#todas_cat").stop().animate({ opacity: 1 }, 'fast');
  }, function () {
    $("#todas_cat").stop().delay(800).animate({ opacity: 0 }, 'fast').css("display","none");;
  });

//função de modal

function MostraMensagem(titulo,conteudo){
	titulo = typeof(titulo) != 'undefined' ? titulo : "Mensagem";
	if (typeof(conteudo) != 'undefined'){
		//alert(conteudo);
		$("#dialog-message").html("<p>"+conteudo+"</p>");
	}

	$("#dialog-message").dialog({
		modal: true,
		minWidth: 400,
		title: titulo,
		dialogClass: 'modalmsg',
		buttons: {
			OK: function() {
				$( this ).dialog( "close" );
			}
		}

	});
}

//form de atendimento
$("#contact").submit(function() {
  if ($("#contact input:first").val() == "") {
	  MostraMensagem("Ops.. ocorreu um problema!","Preencha o seu Nome");
	return false;
  }else if ($("#contact input").eq(1).val() == "") {
	  MostraMensagem("Ops.. ocorreu um problema!","Preencha o seu Telefone");
	return false;
  }else if ($("#contact input").eq(2).val() == "") {
	  MostraMensagem("Ops.. ocorreu um problema!","Preencha o seu E-mail");
	return false;
  }else if ($("#contact textarea").val() == "") {
	  MostraMensagem("Ops.. ocorreu um problema!","Escreva sua Mensagem");
	return false;
  }else{

	  /*var formdata = $("#contact").serialize();

	  $.ajax({

		type: "POST",

		url: form.attr('action'),

		data: formdata,

		success: function(msg){

			MostraMensagem("Mensagem Enviada","Sua mensagem foi enviada com sucesso, em breve entraremos em contato para atendê-lo.");

		}

	  });*/

	  MostraMensagem("Mensagem Enviada","Sua mensagem foi enviada com sucesso, em breve entraremos em contato para atendê-lo.");
	  return false;
  }

});

//form de busca avançada
$("#advancedSearch").submit(function() {
  if ($("#advancedSearch input").val() == "") {
	  MostraMensagem("Ops.. ocorreu um problema!","Preencha uma palavra-chave");
	return false;
  }
  return false;
});

//carrega mais resultados

/*$().ajaxStart(function() { $('.loading').show(); });

$().ajaxStop(function() { $('.loading').hide(); });*/

$("#mais_resultados a").click(function() {

  	$.ajax({
		url: "ajax/todas.html",
		ajaxStart: function() {
			$(".loading").show();
		},
		ajaxComplete: function() {
			$(".loading").fadeOut();
		},
		success: function(data) {
			$(".productfeaturelist").append(data);
			//alert('Load was performed.');
		}
	});
});

//placeholder para ie e browsers antigos
if ($("html").hasClass("oldie")){
	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
		input.val('');
		input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
		input.addClass('placeholder');
		input.val(input.attr('placeholder'));
	  }
	}).blur();

	$('[placeholder]').parents('form').submit(function() {
	  $(this).find('[placeholder]').each(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
		  input.val('');
		}

	  });

	});
}


}); //fim jquery
