$(document).ready(function(){

	/* animais - cadastro de cidade */
	$("#AnimalUf").click( carregarCidades );
	$("#AnimalUf").change( carregarCidades );
	function carregarCidades() {
		$("#AnimalUf option:selected").each(function () {
			var uf = $(this).attr('value')
			$("#AnimalCidadeId").html('<option>Carregando...</option>')
			$.get("/animais/cidades_ajax/"+uf, function(data) {
				$("#AnimalCidadeId").html(data)
			})
		})
	}
	
	/* animais - verificacao js */
	$("form.animal").submit(function() {
		var erro = 0
		if ($("#AnimalTitulo").val().length < 3) {
			$("#erro_titulo").show('slow')
			erro++
		} else $("#erro_titulo").hide('slow')
		
		if ($("#AnimalTipoAnimaiId").val().length == 0) {
			$("#erro_tipo").show('slow')
			erro++
		} else $("#erro_tipo").hide('slow')

		if ($("#AnimalSexoAnimaiId").val().length == 0) {
			$("#erro_sexo").show('slow')
			erro++
		} else $("#erro_sexo").hide('slow')

		if ($("#AnimalCidadeId").val().length == 0) {
			$("#erro_cidade").show('slow')
			erro++
		} else $("#erro_cidade").hide('slow')

		if ($("#AnimalTexto").val().length < 3) {
			$("#erro_texto").show('slow')
			erro++
		} else $("#erro_texto").hide('slow')

		if ($("#AnimalContato").val().length < 3) {
			$("#erro_contato").show('slow')
			erro++
		} else $("#erro_contato").hide('slow')

		if (! erro) return true;
		return false;
	})
		

	/* novo comentario */
	$("#novo_comentario").submit(function() {
		var erro = 0
		if ($("#nome").val().length < 3) {
			$("#erro_nome").show('slow')
			erro++
		} else $("#erro_nome").hide('slow')

		if ($("#texto").val().length < 3) {
			$("#erro_texto").show('slow')
			erro++
		} else $("#erro_texto").hide('slow')

		if ($("#texto").val().length > 300) {
			$("#erro_textoGrande").show('slow')
			erro++
		} else $("#erro_textoGrande").hide('slow')

		if (! erro) {
			return true;
		}
		return false;
	});

	/* links interessantes */
	$("#links").change(function() {
		$("#links option:selected").each(function () {
			var url = $(this).attr('value')
			if (url.length > 3) window.location.href = "http://"+url
		});
	});

	/* lista de animais */
	$("ul.animal li").mouseover(function() {
		$(this).find("div").css("display","block")
	});
	$("ul.animal li").mouseout(function() {
		$(this).find("div").css("display","none")
	});


	/* filtro */
	$("h2 span select").change(function() {

		$("select#tipo option:selected").each(function () {
			reloadAnimaisAdocao()
			// var tipo = $(this).text()
//			if (tipo != '.todos os animais') $("ul.animal li:not("+tipo+")").css('display','none')
//			if (tipo != '.todos os animais') $("ul.animal li:not("+tipo+")").fadeOut('slow')
			
		});

		$("select#uf option:selected").each(function () {
			var uf = "."+$(this).text()
//			if (uf != '.todos os estados') $("ul.animal li:not("+uf+")").css('display','none')
			if (uf != '.todos os estados') $("ul.animal li:not("+uf+")").fadeOut('slow')
		});

		$("select#sexo option:selected").each(function () {
			var classe = ".sexo"+$(this).val()
//			if (classe != '.sexo') $("ul.animal li:not("+classe+")").css('display','none')
			if (classe != '.sexo') $("ul.animal li:not("+classe+")").fadeOut('slow')
		});
	});
	function reloadAnimaisAdocao() {
		$("ul.animal").before('<h4 id="loading"><br /><br /><br /><br />Carregando...</h4>')
		$("ul.animal").remove()
		$("p.aviso").remove()

		var tipo = $('select#tipo option:selected').val()
		var sexo = $('select#sexo option:selected').val()
		var uf   = $('select#uf option:selected').val()
		
		if ( !tipo ) tipo = 0
		if ( !sexo ) sexo = 0
		if ( !uf ) uf = 0
		var url = '/animais/paraadocao/'+ tipo +'/'+ sexo +'/'+ uf +'/0/1' 

		$.ajax({
		  url: url,
		  async: true,
		  success: function( content ) {
			$("h4#loading").before( content )
			$("h4#loading").remove()
		  }
		})
	}

});