	$(document).ready(function(){
		
		$("#btn-show-friendrequestx").click(function(){
			$.ajax({
				type: "POST",
				url: "getFriendRequest",
				beforeSend:  function() {
					$(".loading").show();
				},
				success: function(res){
					$(".loading").hide();
					$("#btn-show-friendrequestx").empty();
					$(".searchingx-result-line, .friendrequestx-resultx-other").remove();
					var data = $.parseJSON(res);
					if (data.length > 0) {
						for (var i = 0; i < data.length; i++){
							insertRow(data[i].id, data[i].fullname, data[i].avatar, data[i].country);
						}
						// Set
						setViewedFriendRequest();
					} else 
						$(".wink-notif").append('<div class="friendrequestx-resultx-other">Không có yêu cầu nào</div>');
				}
			});
		});
		
		// Add friend at wall
		$(document).on("click", ".addfriendx-directx, .addfriendx-directx-sending, .addfriendx-directx-accept", function(e){
			var elem = $(this);
			var com =  $(this).attr("com");
			var to = $("#info-id-account-name").text();
			if (com == "add"){
				$.ajax({
					type: "POST",
					url: "sendFriendRequest",
					data: "to=" + to,
					success: function(res){
						if (res == "false")
							alert("Không thể gửi yêu cầu lúc này");
						else 
							elem.removeClass().addClass("addfriendx-directx-sending").hide().fadeIn(500).text("Hủy lời mời kết bạn").attr("com", "cancel");
					}
				});
			} else if (com == "cancel"){
				$.ajax({
					type: "POST",
					url: "cancelFriendRequest",
					data: "to=" + to,
					success: function(res){
						elem.removeClass().addClass("addfriendx-directx").hide().fadeIn(500).text("Thêm bạn").attr("com", "add");
					}
				});
			} else if (com == "accept"){
				$.ajax({
					type: "POST",
					url: "acceptFriendRequest",
					data: "to=" + to,
					success: function(res){
						elem.empty().hide().fadeIn().text("Bạn bè").attr("com", "friend");
						setViewedFriendRequest();
						var elem2 = $("#btn-show-friendrequestx");
						var num2 = $("#btn-show-friendrequestx").text();
						if (num2.length > 0){
							elem2.empty().html('<span>' + (parseInt(num2) - 1) + '</span>');
							if ($("#btn-show-friendrequestx").text() == 0) $("#btn-show-friendrequestx").empty();
						}
					}
				});
			}
		});
		
		function setViewedFriendRequest(){
			$.ajax({
				type: "POST",
				url: "setViewedFriendRequest",
			});	
		}
		
		// Thuộc về search
		$(document).on("mousedown", ".btn-friendrequestx-cancel", function(e){
			e.stopPropagation();
			var elem = $(this);
			$.ajax({
				type: "POST",
				url: "cancelFriendRequest",
				data: "to=" + elem.closest(".searchingx-result-line").attr("user"),
				success: function(res){
					elem.closest(".searchingx-result-line").fadeOut(300, function(){
						elem.remove();
						if ($(".searchingx-result-line").length == 1)
							$(".wink-notif").append('<div class="friendrequestx-resultx-other">Không có yêu cầu nào</div>');
					});
				}
			});
		});
		
		// Thuộc về search
		$(document).on("mousedown", ".btn-friendrequestx-accept", function(e){
			e.stopPropagation();
			var elem = $(this);
			$.ajax({
				type: "POST",
				url: "acceptFriendRequest",
				data: "to=" + elem.closest(".searchingx-result-line").attr("user"),
				success: function(res){
					elem.siblings(".searchingx-result-user-info-noti-friend").children(".otrif").text("Đã trở thành bạn");
					elem.siblings(".btn-friendrequestx-cancel").remove();
					elem.closest(".searchingx-result-line").hide().css("background-color", "#FFF8C6").fadeIn(600, function(){$(this).css("background-color", "");});
					elem.remove();
				}
			});
		});
		
		function insertRow(email, fullname, avatar, country){
			var html = '<div user="' + email + '" class="searchingx-result-line">';
			html +=  '<div class="searchingx-result-user-avatar"><img class="bdrad2" src="' + avatar + '"/></div>';
			html +=  '<div class="searchingx-result-user-info-noti-friend"><b>' + fullname + '</b><br><span class="otrif">' + country + '</span></div>';
			html +=  '<div title="Từ chối" class="btn-friendrequestx-cancel">Từ chối</div>';
			html +=  '<div title="Thêm bạn" class="btn-friendrequestx-accept">Thêm bạn</div>';
			html +=  '</div>';
			$(".wink-notif").append(html);
		}
	});