function hecho(responseText, statusText){
	$("#formAdd").hide('slow').after(responseText);
}

function validate(formData, jqForm, options) {
	var error = "";
    var form = jqForm[0];
    if (!form.nombre.value){
        error += "<p>Introduce un nombre</p>";
		form.nombre.focus();
	}
	
    if(error.length > 0){
		$("#mensaje").html('');
		$("#mensaje").append(error);
		$("#mensaje").slideDown();
		return false;
	}
}


$(document).ready(function(){

	/*- Links de JS -*/
	$('#pet-Random-Rate a').each(function(){
		$(this).attr('href', 'javascript:void(0);');
	});
	
	/*- Form Transform -*/
	$('form.jqtransform').jqTransform();
	$('.jqTransformSelectWrapper div span').click(function(){
		$(this).parents('.jqTransformSelectWrapper').toggleClass('selectFocus');
	});
	$('.jqTransformSelectWrapper ul a').click(function(){
		$(this).parents('.jqTransformSelectWrapper').removeClass('selectFocus');
	});
	
	/*- Value nuevo nombre -*/
	$('#addPet-Name').focus(function(){
		$(this).val('');
	});
	$('#addPet-Name').blur(function(){
		getVal = $(this).val();
		if (getVal == '') {
			$(this).val('Nombre');
		}
	});
	
	/*- Input de nuevo nombre -*/
	$('#addMascota').click(function(){
	    var formAdd = $('#formAdd');
	    //$(formAdd).html('<img src="images/name-ajax-loader.gif" alt="" />');
	    $(formAdd).ajaxForm({ 
	    	beforeSubmit: validate,
	    	success: hecho
	    });	
	});
	
	/*- Ajax de seleccion -*/
	$('#getAllButton').click(function(){
		$('#pet-List').fadeIn('slow');
		$('#pet-List h4').remove();
		var tipo = $('#sortPet-Animal option:selected').text();
		var sexo = $('#sortPet-Genero option:selected').text();
		if(tipo == 'Indistinto' && sexo == 'Indistinto'){
		var tipo = 'Todos';
		var sexo = '';
		}
		$('#pet-List').prepend("<h4>Mostrando nombres para: "+ tipo + " " + sexo +"</h4>");
	    $.post("get_data_all.php",{ 
	    tipo: $('#sortPet-Animal option:selected').val(),sexo: $('#sortPet-Genero option:selected').val() 
	    },
	    function(data){
	    $("#pet-List tbody").html(data);})	
		
	});
	
	/*- Ajax del nombre Random -*/	
	$('#pet-Random-Rate a').each(function(){
	    $(this).click(function(){
	        var randomName = $('#pet-Random-Name');
	        $(randomName).html('<img src="images/name-ajax-loader.gif" alt="" />');
	        $.post("get_data.php",{ 
	        id: $(this).attr("id"),type: $(this).attr("rel")  
	        },
	        function(data){
	        $("#pet-Random-Name").html(data);})	
	    });
	});
	
	/*- Votos de seleccion -*/	
	$('.votos a').each(function(){
	    $(this).click(function(){
	    var randomName = $('.votos');
	    $(randomName).html('<img src="images/name-ajax-loader.gif" alt="" />');
	        $.post("votos.php",{ 
	        id: $(this).attr("id"),type: $(this).attr("rel")  
	        },
	        function(data2){
	        $(".votos").html(data2);})	
	    });
	});
	
	/*- Lista -*/
	$('#pet-List table tr:even').addClass('even');

});
