$(document).ready(function() {
	
	// Functions
	
	function bindRemoveChildLink(object) {
		object.bind('click', function() {
			$(this).parent().parent().remove();
			return false;
		});
	}
	
	function collapseChildren() {
		if ($("input[name$='[is_parent]']:checked").val() == "false") {
			$(".children").css("display", "none");
		}
	}

	function collapsePregnancy() {
		if ($("input[name$='[is_pregnant]']:checked").val() == "false") {
			$(".pregnancy").css("display", "none");
		}
	}
	
	function addRemoveChildLink (object) {
		object.html('<a href="#" class="remove_child">(-) Borrar información de este niño</a>');
	}
	
	// Initial Add Child Link
	
	$(".child:last").after('<a href="#" class="add_child">(+) Agrega otro hijo</a>');
	

	// Add New Children
	 
	$(".add_child").click(function() {
		$(".new.child:first").clone().insertAfter(".child:last");
		$(".child:last input").val("");
		addRemoveChildLink($(".child:last .remove_child_container"))
		bindRemoveChildLink($(".child:last .remove_child"));
		return false;
	});
		
	// Add and bind remove child links for existing children
	
	addRemoveChildLink($(".existing .remove_child_container"));
	bindRemoveChildLink($(".remove_child"));
	
	// Toggle Children
	
	$("input[name$='[is_parent]']").click(function () {
    if ($("input[name$='[is_parent]']:checked").val() == "true") {
			$(".children").show("slow");	    
	  } else {
	    $(".children").hide("slow");
	  }
	});

	// Toggle pregnancy

	$("input[name$='[is_pregnant]']").click(function () {
	  if ($("input[name$='[is_pregnant]']:checked").val() == "true") {
			$(".pregnancy").show("slow");	    
	  } else {
	    $(".pregnancy").hide("slow");
	  }
	});

	// Set the initial collapse state for pregnancy and children based on radio button values

	collapseChildren();
	collapsePregnancy();

	// Login box reset
	$("#header_login_username").click(function() {
		if (this.value == 'Nombre de Usuario') {
    		$("#header_login_username").attr("value", "");
		}
  }); 
	$("#header_login_username").blur(function() {
		if (this.value == '') {
    		$("#header_login_username").attr("value", "Nombre de Usuario");
		}
  }); 
	$("#header_login_password").click(function() {
		if (this.value == '**********') {
    		$("#header_login_password").attr("value", "");
		}
  }); 
	$("#header_login_password").blur(function() {
		if (this.value == '') {
    		$("#header_login_password").attr("value", "**********");
		}
  });

});
