(function(jQuery) {
	var $ = jQuery;

	$(document).ready(function() {

		var $= jQuery;

		parent.jQuery("#fancybox-close").hide();

		$("input").not(".tunnel-next").keypress(function(e) {
		    if(e.keyCode == 13) {
		        return false;
		    }
		});

		$("#tabcontinue_0").click(function () {

			$("#mytab_0").hide();
			$("#mytab_1").show();
		});

		$("#tabcontinue_1").click(function () {

			var currentForm = $(this).parents("form"); 
			currentForm.find("div.validate").show();
			currentForm.find("#vincular").hide();
			currentForm.find("div.response p").html("");
			currentForm.find("div.default").show();
			$.ajax({
			  url: currentForm.attr("action"),
			  data: currentForm.serialize(),
			  dataType: "json",
			  context: document.body,
			  success: function(resp) {

				if (resp.done == true) {

					$("#mytab_1").hide();
					$("#mytab_2").show();
					$("#tabtrigger_2").attr("src", $("#tabtrigger_1").attr("src"));

				} else {

					$("#mytab_1 div.response p").html(resp.text);
					$("#mytab_1 div.default").hide();

					if (resp.vinculable == true) {

						$("#vincular").show();

					} else {

						$("#vincular").hide();
					}
				}			
			  }
			});

			$(this).blur();
		});

		$("#tabcontinue_2").click(function () {

			var currentForm = $(this).parents("form"); 
			currentForm.find("div.validate").show();
			$.ajax({
			  url: currentForm.attr("action"),
			  data: currentForm.serialize(),
			  dataType: "json",
			  context: document.body,
			  success: function(resp) {

				$("#mytab_2 div.response p").html(resp.text);
				$("#mytab_2 div.default").hide();

				if(resp.isValid == true) {
					
					$("#mytab_2").hide();
					
					if ($("#mytab_3").length >0)
						$("#mytab_3").show();
					else
						$("#fetchAvatar").show();
					//$("#fetchAvatar").show().attr("rel","#revisarEmail");
					//$("#revisarEmail").show();	
					//parent.jQuery.fancybox.close();
				}
			  }
			});

			$(this).blur();
		});
		
		$("#tabcontinue_3").click(function () {

			var currentForm = $(this).parents("form"); 
			currentForm.find("div.validate").show();
			$.ajax({
			  url: currentForm.attr("action"),
			  data: currentForm.serialize(),
			  dataType: "json",
			  context: document.body,
			  success: function(resp) {

				$("#mytab_3 div.response p").html(resp.text);
				$("#mytab_3 div.default").hide();

				if(resp.isValid == true) {

					$("#mytab_3").hide();
					
					$("#fetchAvatar").show();
					//$("#fetchAvatar").show().attr("rel","#revisarEmail");
					//$("#revisarEmail").show();	
					//parent.jQuery.fancybox.close();
				}
			  }
			});

			$(this).blur();
		});
		
		
		$("#vincular_email").click(function () {
			
			$.ajax({
			  url: document.location.href,
			  data: "vincular=" + escape($("#signup_email").attr("value")),
			  context: document.body,
			  success: function(resp) {

				$("#fetchAvatar").show().attr("rel","#vincularEmailInfo");
				//$("#vincularEmailInfo").show();	
			  }
			});
			$("div.tabcontent").hide();

		});

		$("#mensajeFinalizar").click(function () {

			var currentForm = $(this).parents("form");
			$(this).blur();

			if (currentForm.find("input[name=fetchAvatar]:checked").attr("value") == "1") {

				$.ajax({
				  url: currentForm.attr("action"),
				  data: currentForm.serialize(),
				  success: function(resp) {					  
						//$("#fetchAvatar").show().attr("rel","#revisarEmail");

						$("#vincularEmailInfo").show();	
						//parent.jQuery.fancybox.close();
				  }
				});

			}

			var target = $("#fetchAvatar").attr("rel");
			currentForm.hide();
			$(target).show();

		});

		$("#vincularFinalizar, #revisarFinalizar").click(function () {

			parent.jQuery.fancybox.close();
		});
	});
})(jQuery);	
