	$(document).ready(function(){
		
		$("#txt-quick-searchx-friend").keyup(function(){
			var keyword = $.trim($(this).val());
			if (keyword.length > 0){
				$(".searchingx-result-box").empty().show();
				$(".searchingx-result-box").append('<div class="searchingx-result-line"><div class="searchingx-result-other"><img src="images/loading-small.gif"/></div></div>');
				$.ajax({
					type: "POST",
					url: "getSearchingResult",
					data: "keyword=" + keyword + "&offset=0",
					beforeSend: function(){
						
					},
					success: function(res){
						var data = $.parseJSON(res);
						if (data.length > 0) {
							for (var i = 0; i < (data.length - 1); i++){
								insertRow(data[i].id, data[i].fullname, data[i].country, data[i].avatar);
							}
							
							if (data[data.length - 1].id == "more")
								$(".searchingx-result-box").append('<div class="searchingx-result-line"><div offset="' + data[data.length - 1].fullname + '" com="more" class="searchingx-result-other">Xem thêm ▼</div></div>');
							else
								insertRow(data[data.length - 1].id, data[data.length - 1].fullname, data[data.length - 1].country, data[data.length - 1].avatar);
						} else {
							$(".searchingx-result-box").append('<div class="searchingx-result-line"><div class="searchingx-result-other">Không tìm thấy kết quả nào</div></div>');
						}
						$(".searchingx-result-line:first").remove();
					}
				});
			} else { // Xóa dữ liệu nhập
				$(".searchingx-result-box").empty().hide();
			}
		});
		
		function insertRow(id, name, country, avatar){
			var html = '<div user="' + id + '" class="searchingx-result-line">';
			html += '<div class="searchingx-result-user-avatar"><img class="bdrad2" src="' + avatar + '"/></div>';
			html += '<div class="searchingx-result-user-info"><b>' + name + '</b><br><span class="otrif">' + country + '</span></div>';
			html += '</div>';
			$(".searchingx-result-box").append(html);
		}
		
		$("#txt-quick-searchx-friend").blur(function(){
			$(this).val("");
			$(".searchingx-result-box").empty().hide();
		});
		
		// Search more
		$(document).on("mousedown", ".searchingx-result-other", function(e){
			e.stopPropagation();
			var com = $(this).attr("com");
			var offset = $(this).attr("offset");
			if (com == "more"){
				if ($(".graybg").length == 0){
					$("body").append('<div class="graybg"></div>');
					$("body").append('<div class="search-more-box"><div class="searchingx-result-title">Kết quả tìm kiếm với: ' + $("#txt-quick-searchx-friend").val() + '</div><div class="search-more-box-content"></div></div>');
					loadMoreFriend($("#txt-quick-searchx-friend").val(), 0);
				} else {
					$(this).parent().remove();
					loadMoreFriend($("#txt-quick-searchx-friend").val(), offset);
				}
			}
		});
		
		function loadMoreFriend(keyword, offset){
			$.ajax({
				type: "POST",
				url: "getSearchingResult",
				data: "keyword=" + keyword + "&offset=" + offset,
				beforeSend: function(){
					
				},
				success: function(res){
					var data = $.parseJSON(res);
					if (data.length > 0) {
						for (var i = 0; i < (data.length - 1); i++){
							insertRowPopup(data[i].id, data[i].fullname, data[i].country, data[i].avatar);
						}
						
						if (data[data.length - 1].id == "more")
							$(".search-more-box-content").append('<div class="searchingx-result-line"><div offset="' + data[data.length - 1].fullname + '" com="more" class="searchingx-result-other">Xem thêm ▼</div></div>');
						else
							insertRowPopup(data[data.length - 1].id, data[data.length - 1].fullname, data[data.length - 1].country, data[data.length - 1].avatar);
					} else {
						$(".search-more-box-content").append('<div class="searchingx-result-line"><div class="searchingx-result-other">Không tìm thấy kết quả nào</div></div>');
					}
					if (offset == 0) $(".search-more-box-content").hide().fadeIn(200);
					$(".search-more-box-content").animate({scrollTop: ($(".search-more-box-content").height() + 100)}, 1000);
					checkExistentFriends();
					checkAlreadyRequests();
				}
			});
		}
		
		function insertRowPopup(id, name, country, avatar){
			var html = '<div user="' + id + '" class="searchingx-result-line">';
			html += '<div class="searchingx-result-user-avatar"><img class="bdrad2" src="' + avatar + '"/></div>';
			html += '<div class="searchingx-result-user-info"><b>' + name + '</b><br><span class="otrif">' + country + '</span></div>';
			html += '<div title="Thêm bạn" com="add" class="search-more-box-addfriend">Thêm bạn</div>';
			html += '</div>';
			$(".search-more-box-content").append(html);
		}
		
		$(document).on("mousedown", ".searchingx-result-line", function(e){
			 document.location.href = "index.jsp?acc=" + $(this).attr("user");
		});
		
		function checkExistentFriends(){
			// username array
			var user_arr = new Array();
			user_arr.push($('#info-id-accountlogin-name').text());
			$.ajax({
				type: "POST",
				url: "getlistfriendsacc",
				dataType: "json",
				success: function(response){
					if(response != "null") {
						$.each(response, function(entryIndex, entry){
							user_arr.push(entry['id']);
						});
					}
					$(".searchingx-result-line").each(function(){
						for (var i = 0; i < user_arr.length; i++){
							if ($(this).attr("user") == user_arr[i]){
								$(this).children(".search-more-box-addfriend").remove();
								break;
							}
						}
					});
				}
			});
		}
		
		function checkAlreadyRequests(){
			// username array
			var my_req = new Array();
			$.ajax({
				type: "POST",
				url: "getMyFriendRequest",
				dataType: "json",
				success: function(response){
					if(response != "null") {
						$.each(response, function(entryIndex, entry){
							my_req.push(entry['id']);
						});
					}
					$(".searchingx-result-line").each(function(){
						for (var i = 0; i < my_req.length; i++){
							if ($(this).attr("user") == my_req[i]){
								$(this).children(".search-more-box-addfriend").removeClass().addClass("search-more-box-addfriend-sending").text("Hủy bỏ").attr("com", "cancel");
								break;
							}
						}
					});
				}
			});
		}
		
		// Addfriend
		$(document).on("mousedown", ".search-more-box-addfriend, .search-more-box-addfriend-sending", function(e){
			e.stopPropagation();
			var elem = $(this);
			var com =  $(this).attr("com");
			var to = $(this).closest(".searchingx-result-line").attr("user");
			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("search-more-box-addfriend-sending").hide().fadeIn(500).text("Hủy bỏ").attr("com", "cancel");
					}
				});
			} else {
				$.ajax({
					type: "POST",
					url: "cancelFriendRequest",
					data: "to=" + to,
					success: function(res){
						elem.removeClass().addClass("search-more-box-addfriend").hide().fadeIn(500).text("Thêm bạn").attr("com", "add");
					}
				});
			}
		});
			// Clear gray background
		$(document).on("click", ".graybg", function(e){
			$(".search-more-box").remove();
			$(this).remove();
		});
	});