$(document).ready(function(){
	
	var closeTellFrom = function() {
		$(".tell_img").click(function(){
			$(".tell_pop").hide("slow", function(){
				$(".tell_pop").remove();
			});
		});
	};
	var initTellFrom = function(loading){
		$("#btn_tellFriends_submit").click(function(){
			 $(".loading").show();
			 $("#tellFriends").validate({
					submitHandler: function(form) {
					 	$.post(form.action,
					 			{yourEmails:[$("#tellFriends_yourEmails").val(),$("#tellFriends_yourEmails2").val()],
					 			 friendsEmails:[$("#tellFriends_friendsEmails").val(),$("#tellFriends_friendsEmails2").val(),$("#tellFriends_friendsEmails3").val()],
					 			 id:parseInt($("#tellFriends_id").val())},
					 			function(){
					 				$(".loading").hide();
					 				alert("Thank you,Your message has already emailed to your friend.");
					 				$(".tell_img").click();
					 				
					 			});
					   	
				 	},
				 	invalidHandler: function(form, validator) {
				 	      var errors = validator.numberOfInvalids();
				 	      if (errors) {
				 	    	 $(".loading").hide(); 	
				 	      }
				 	    },
				 	errorElement:"Div"
			 });
		});
	};
		
	$(".tellfr").click(function(){
		var link = this;
		var loading = $(link).next().next();
		var tellfrom = $(".tell_pop");
		
		var showTellfrom = function(){
			loading.show();
			$.get(link.href, null, function(data, textStatus){
				$(link).parent().parent().append($(data));
				$(".tell_pop").show("slow", function(){
					closeTellFrom();
					initTellFrom(loading);
					loading.hide();
				});
			});
		}
		if (tellfrom.length == 0) {
			showTellfrom();
		} else {
			tellfrom.hide("slow", function(){
				tellfrom.remove();
			});
			showTellfrom();
		}
		return false;
	});	

})