	$(document).ready(function(){
		// Khởi động bản thông báo
		init();
		var user_arr = new Array();
		
		loadlistFriendscomment();
		// load friends
		function loadlistFriendscomment() {
			$.ajax({
				type: "POST",
				url: "getlistfriendsacc",
				dataType: "json",
				success: function(response){
					if(response != "null") {
						var html = '';
						if (response.length > 0) {
							$.each(response, function(entryIndex, entry){
								user_arr.push(entry['id']);
							});
						} 						
					}
				}
			});
		}
		
		// kiem tra co acc nay la ban khong
		function checkuserfriend(user){
			var result = false;
			for (var i = 0; i < user_arr.length; i++){
				if (user_arr[i] == user){
					result = true;
					break;
				}
			}
			return result;
		}
		
		// Web socket
		var ws = new WebSocket("ws://" + document.location.host + "/Notificate");
		
		
		ws.onmessage = function(data) {
			analyseData(data.data);
			 
		};
		
		function analyseData(data){
			var array = data.split("\0");
			var com = array[1];
			if (com == "addfriend"){
				var type = array[2];
				if (type == "add") {
					var email = array[3];
					var fullname = array[4];
					var avatar = array[5];
					var content = array[6];
					announcer(email, fullname, avatar, content);// hien thong bao 
					if ($("#info-id-account-name").text() == email)
						$(".addfriendx-directx").removeClass().addClass("addfriendx-directx-accept").text("Xác nhận lời mời kết bạn").attr("com", "accept");
				} else if (type == "accept"){
					var email = array[3];
					var fullname = array[4];
					var avatar = array[5];
					var content = array[6];
					announcer(email, fullname, avatar, content);
					if ($("#info-id-account-name").text() == email)
						$(".addfriendx-directx-sending").removeClass().addClass("addfriendx-directx-sending").text("Hủy bạn").attr("com", "cancel");
				} else if (type == "cancel"){
					var email = array[3];
					if ($("#info-id-account-name").text() == email){
						$(".addfriendx-directx-accept").removeClass().addClass("addfriendx-directx").text("Thêm bạn").attr("com", "add");
						$(".addfriendx-directx-sending").removeClass().addClass("addfriendx-directx").text("Thêm bạn").attr("com", "add");
					}
				}
				addNumFriendRequest(type);
			} else if(com == "status") {
				var type = array[2];
				if (type == "news") {
					var email = array[3];
					if(checkuserfriend(email)) {
						var fullname = array[4];
						var avatar = array[5];
						var content = array[6];
						announcer(email, fullname, avatar, content);
					}
				} else if(type == "like") {
					var email = array[3];
					var fullname = array[4];
					var avatar = array[5];
					var content = array[6];
					announcer(email, fullname, avatar, content);
				}
			}
		}
		
		function sendMessage(msg) {
		     ws.send(msg);
		}
		
		ws.onopen = function(){

		};
		 
		ws.onclose = function() {
			
		};

		//*********************
		// Thuộc về init
		//*********************
		function init(){
			// Check hiện nút Add friend trên tường (ko hiện nếu là mình)
			if ($("#info-id-account-name").text() == $("#info-id-accountlogin-name").text())
				$(".addfriendx-directx").remove();
			else 
				checkFriendRequestStatus();
			
			// Đếm số lượng friend request trên bảng thông báo
			$.ajax({
				type: "POST",
				url: "countUnviewedMyFriendRequest",
				success: function(res){
					if (res == "0") 
						$("#btn-show-friendrequestx").empty();
					else 
						$("#btn-show-friendrequestx").empty().html('<span>' + res + '</span>');
				}
			});
			
			// Đếm số lượng message offline
			$.ajax({
				type: "POST",
				url: "getMsgOffline",
				success: function(res){
					var data = $.parseJSON(res);
					var amount = "0";
					$.each(data, function(entryIndex, entry){
						amount = "" + entry.isActive;
						return false;
					});
					if (amount != "0")
						$("#btn-show-msgofflinex").empty().html('<span>' + amount + '</span>');
				}
			});	
		}
		
		function checkFriendRequestStatus(){
			$.ajax({
				type: "POST",
				url: "checkFriendRequestStatus",
				data: "to=" + $("#info-id-account-name").text(),
				success: function(res){
					if (res == "sending")
						$(".addfriendx-directx").removeClass().addClass("addfriendx-directx-sending").text("Hủy lời mời kết bạn").attr("com", "cancel");
					if (res == "accept") 
						$(".addfriendx-directx").removeClass().addClass("addfriendx-directx-accept").text("Xác nhận lời mời kết bạn").attr("com", "accept");
					if (res == "friend") 
						$(".addfriendx-directx").removeClass().addClass("addfriendx-directx-sending").text("Hủy bạn").attr("com", "cancel");
				}
			});
		}
		
		//****************************
		
		function addNumFriendRequest(type){
			var elem = $("#btn-show-friendrequestx");
			var num = $("#btn-show-friendrequestx").text();
			if (type == "add") {
				if ((num != "0") && (num != "")) {
					elem.empty().html('<span>' + (parseInt(num) + 1) + '</span>');
				} else {
					elem.empty().html('<span>1</span>');
				}
			} else if (type == "cancel") {
				if (num != "" && num > 0){
					elem.empty().html('<span>' + (parseInt(num) - 1) + '</span>');
					if ($("#btn-show-friendrequestx").text() == 0) $("#btn-show-friendrequestx").empty();
				}
			}
		}
		
		//*********************
		// Thuộc về announcer
		//*********************
		
		function announcer(email, user, avatar, content){
			if ($(".announcerx-noti-box").length >= 5) $(".announcerx-noti-box").eq(0).remove();
			var html = '<div class="announcerx-noti-box"><div user="' + email + '" class="announcerx-noti-content"><div class="announcerx-noti-user-avatar"><img class="bdrad2" src="';
			html += avatar;
			html += '"/></div><div class="announcerx-noti-user-info"><b>';
			html += user;
			html += '</b><br><span class="otrif">';
			html += content;
			html += '</span></div></div></div>';
			$("body").append(html);
			ringChat();
			fixAnnouncerPos();
			showHideAnnouncer();
		}
		
		function showHideAnnouncer(){
			$(".announcerx-noti-box").last().hide().fadeIn(400).delay(5000).fadeOut(2000, function(){
				$(this).remove();
			});
		}
		
		// KHI RÊ CHUỘT VÀO BẢNG THÔNG BÁO
		$(document).on("mouseenter", ".announcerx-noti-box", function(){
			$(this).stop(true, false).css("opacity", "1");
		});
		
		$(document).on("mouseleave", ".announcerx-noti-box", function(){
			$(this).delay(4000).fadeOut(2000, function(){
				$(this).remove();
			});
		});
		//
		
		// Báo âm thanh có tin nhắn mới
		function ringChat(){
			if (!$("#announce-ring-notification").is(":hidden")){
				$("#announce-ring-notification").empty().html('<audio hidden="true" autoplay="autoplay" src="sound/notification-ring.mp3"></audio>');
				$("#announce-ring-notification").fadeOut(0).delay(1000).fadeIn(0);
			}
		}
		
		function fixAnnouncerPos(){
			var pos = 20;
			$(".announcerx-noti-box").each(function(){
				$(this).css("bottom", pos);
				pos += 85;
			});
		}
	});