// JavaScript Document

$(document).ready(function(e) {
	loadcroll = false;
	type = 0;// 1: la load thong tin dang tren tuong, 0 la load album detail, 2: load danh sach nam
	
	// xem thong tin ca nhan	
    $(document).on("click", "#nam-intros-link", function(){
		elem = $(this);
		$(".selected").attr("class", "");
		$(elem).attr("class", "selected");
		//window.location= 'index.jsp?acc=' + $('#info-id-account-name').text();
		$(".main-content").load('gioi_thieu.jsp?acc=' + $('#info-id-account-name').text(), function(){
			loadcroll = false;
			return false;
		});
	});
	
	// xem thong tin tren tuong
	$(document).on("click", "#nam-newinfo-link", function(){
		elem = $(this);
		$(".selected").attr("class", "");
		$(elem).attr("class", "selected");
		$(".main-content").load("news.jsp", function(){
			$(window).bind('scroll', bindScroll);
			pagenumber = 1;// page number load data
			endpage = 0;// set end page = 0
			loadcroll = true; // set accept load
			type = 1;
			
			var avatar = $('.headerTinymanPhoto').attr('src');
			var fullname = $('#name-account-loadinfo').text();
			var email = $('#info-id-accountlogin-name').text();
			
			html = '<div class="post-view-info"><div class="post-view-left"><div class="post-view-avatar" style="background: url(' + avatar + ') no-repeat; background-size: 100% auto;" title="' + fullname + '"></div></div><div class="ii"><div class="load-image ie"> &nbsp; </div><div class="post-view-right"><div class="post-view-header"><div class="post-status-contentaccount" style="width: 100%; min-height: 70px; border: 1px solid rgb(233, 234, 237); background: #FFF;"><textarea style="width:99%; min-height:70px; resize: none; outline: none; border: 0px;"></textarea></div><div style="text-align:right; margin-top: 5px;"><div class="post-status-account" style="cursor: pointer; float: right; width:67px; height:30px; background: url(images/dangbai.png) no-repeat; background-size: 100% auto;"></div></div></div><div class="post-view-footer" style="background-color: #F8F8F8;"></div></div></div></div><div class="space">&nbsp;</div>';
			
			$('.post-status-commnetaccount').append(html);
			// load danh sach ban be 
			loadfriendlistshowpagepost();
			
			loadnewstatusacc();
			
			
			return false;
		});
	});
	
	function loadfriendlistshowpagepost() {
		$.ajax({
			type: "POST",
			url: "getlistfriendsacclimit",
			data:{idacc: idaccountload, pagere: pagenumber},
			dataType: "json",
			success: function(response){
				if(response != "null"){
					var html = '';
					// drag friend list
					$.each(response, function(entryIndex, entry){
						html += '<li><a href="#"><div class="friendlist-avatar-small" style="background: url(images/member/dlduynam.jpg)"></div></a></li>';
					});
					
					$('.friend-list-post ul').append(html);
				}
			}
		});
	}
	
	// xem danh sach ban be tren tuong
	$(document).on("click", "#nam-friends-link", function(){
		elem = $(this);
		$(".selected").attr("class", "");
		$(elem).attr("class", "selected");
		$(".main-content").load("friends.jsp", function() {
			$(window).bind('scroll', bindScroll);
			pagenumber = 1;// page number load data
			endpage = 0;// set end page = 0
			loadcroll = true; // set accept load
			type = 2;
			loadfriendlistshow();
			return false;
		});
	});
	
	// xem album anh
	$(document).on("click", "#nam-albums-link", function(){
		elem = $(this);
		$(".selected").attr("class", "");
		$(elem).attr("class", "selected");
		$(".main-content").load("albums.jsp", function(){
			loadalbumshow();
			return false;
		});
		
	});
	
	
	//-----------load news-----------------------------
	function loadnewstatusacc() {
		var idaccountload = $('#info-id-account-name').text();
		$('#paging-loading').show();
		$(window).unbind("scroll");
		$.ajax({
			type: "POST",
			url: "getnewsacc",
			data:{idacc: idaccountload, pagere: pagenumber},
			dataType: "json",
			success: function(response){
				if(response != "null"){
					if(response == "noload") {
						endpage = 1;
					} else {
						var html = '';
						// drag friend list
						for (var i = 0; i < response.length; i++) {
							html += response[i];
						}
						pagenumber += 1;
						$('.space:last').after(html);
						$(window).bind('scroll', bindScroll);
					}
				} else {
					new Messi('Có lỗi kết nối với máy chủ, vui lòng nhấn F5!', {
						title: 'Kết Nối Máy Chủ',  
						modal: 	true, 
						titleClass: 'info', 
						buttons: [{
							id: 0, 
							label: 'Đóng', 
							val: 'X'
						}]
					});
				}
				$('#paging-loading').hide();
			}
		});
	}
	//----------------End Load news------------------------
	
	//-----------load friends-----------------------------
	function loadfriendlistshow() {
		var idaccountload = $('#info-id-account-name').text();
		$('#paging-loading').show();
		$(window).unbind("scroll");
		$.ajax({
			type: "POST",
			url: "getlistfriendsacclimit",
			data:{idacc: idaccountload, pagere: pagenumber},
			dataType: "json",
			success: function(response){
				if(response != "null"){
					if(response == "noload") {
						endpage = 1;
					} else {
						var html = '';
						// drag friend list
						$.each(response, function(entryIndex, entry){
							html += '<div class="friedlist-detail"><a class="friendlist-avatar" href="index.jsp?acc=' + entry['id'] +'"><img src="' + entry['avatar'] +'" /></a><div class="friendlist-info"><a href="index.jsp?acc=' + entry['id'] +'">' + entry['realname'] +'</a></div></div>';
						});
						pagenumber += 1;
						$(window).bind('scroll', bindScroll);
						$('.show-friedlist').append(html);
					}
				} else {
					new Messi('Có lỗi kết nối với máy chủ, vui lòng nhấn F5!', {
						title: 'Kết Nối Máy Chủ',  
						modal: 	true, 
						titleClass: 'info', 
						buttons: [{
							id: 0, 
							label: 'Đóng', 
							val: 'X'
						}]
					});
				}
				$('#paging-loading').hide();
			}
		});
	}
	//----------------End Load Friends------------------------
	
	// --------album----------------------------
	iduser = $('#info-id-account-name').text();
	idaccountlogin = $('#info-id-accountlogin-name').text();
	
	// ------------show album-------------------
	function loadalbumshow() {
		loadcroll = false; // set accept load
		$.ajax({
			type: "POST",
			url: "getalbumviewprofile",
			data: {emailget: iduser},
			dataType: "json",
			success: function(response){
				var html = '';
				$('.albumphotoshow').empty();
				if(response != "null"){
					if(iduser != idaccountlogin) {
						$.each(response, function(entryIndex, entry){
							if(entry['sharetype'].idshare == 1){
								html += '<div style="width:164px; height:158px; float: left; margin-right: 20px;"><a href="#" class="view-album-detail-photos" idalb="' + entry['idalbum'] + '" tabindex="0" style="width:164px; height:158px; top: 0px; left: 0px; margin: 10px;"><div class="zMxm7c"><img src="' + entry['description'] + '" class="tDMXke" style="width: 164px; height: 110px; margin-top: 0px; margin-left: 0px;" alt="' + entry['name'] + '" /><div class="y761Rd k83ZBc E2BdXb" style="height: 48px;"><div class="jwD3Ob">' + entry['name'] + '</div><div class="onSq9d">' + entry['datecreate'] + '</div></div> </div></a></div>';
							}
						});
					} else {
						$.each(response, function(entryIndex, entry){
							html += '<div style="width:164px; height:158px;float: left; margin-right: 20px;"><a href="#" class="view-album-detail-photos" idalb="' + entry['idalbum'] + '" tabindex="0" style="width:164px; height:158px; top: 0px; left: 0px; margin: 10px;"><div class="zMxm7c"><img src="' + entry['description'] + '" class="tDMXke" style="width: 164px; height: 110px; margin-top: 0px; margin-left: 0px;" alt="' + entry['name'] + '" /><div class="y761Rd k83ZBc E2BdXb" style="height: 48px;"><div class="jwD3Ob">' + entry['name'] + '</div><div class="onSq9d">' + entry['datecreate'] + '</div></div> </div></a><br><a class="delete delete-album" idalb="' + entry['idalbum'] + '" original-title=""></a></div>';
						});
					}
				} else {
					html = '<h4>Chưa có album nào</h4>';
				}
				$('.albumphotoshow').fadeOut(300).append(html).fadeIn(300);
			}
			
		});
	}
	
	// delete album
	
	$(document).on("click", ".delete-album", function(){
		var idalbum = $(this).attr('idalb');
		alert(idalbum);
	});
	
	// show album
	$(document).on("click", ".view-album-profile-click", function(){
		loadalbumshow();
		return false;
	});

	//view-albumdang-profile-click
	$(document).on("click", ".view-albumdang-profile-click", function(){
		loadalbumshow();
		return false;
	});
	
	// view-album-detail-photos
	$(document).on("click", ".view-album-detail-photos", function(){
		idalbum = $(this).attr("idalb");
		$('.main-show-albumphoto').load("albumdetails.jsp", function() {
			pagenumber = 1;// page number load data
			endpage = 0;// set end page = 0
			loadcroll = true; // set accept load
			type = 0;
			loadalbumdetailsshow(idalbum);
		});
		return false;
	});


//--------------album detail------------------
	pagenumber = 1;
	endpage = 0;
	
	// su kien scrollbar ve cuoi trang
	$(window).scroll(bindScroll);
	
	function bindScroll() {
		if($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
			$(window).unbind("scroll");
			if(loadcroll == true) {
				if(endpage == 0) {
					if(type == 0) { // load data album
						loadalbumdetailsshow(idalbum);
					} else if(type == 1) { // load info news
						loadnewstatusacc();
					} else if(type == 2) { // load list friends
						loadfriendlistshow();
					}
				}
			}
		}
	}
	
	//-----------load photos for album-----------------------------
	function loadalbumdetailsshow(idalbum) {
		$('#paging-loading').show();
		$(window).unbind("scroll");
		$.ajax({
			type: "POST",
			url: "getalbumdetailviewprofile",
			data:{idalbumre: idalbum, pagere: pagenumber},
			dataType: "json",
			success: function(response){
				var html = '';				
				if(response != "null"){
					if(response == "noload") {
						endpage = 1;
					} else {
						if(pagenumber == 1) {
							$('#title-album-detail').text(response.name);
							$('#datatime-album-detai').text(response.datecreate);
						}
						$.each(response.linkphotos, function(entryIndex, entry){
							html += '<div class="details-linkphots-images"><a href="#" lightbox-loadimage-id="' + entry['idphoto'] + '" class="" idalb="" tabindex="0" style="width:164px; height:120px; top: 0px; left: 0px;"><img src="' + entry['link'] + '" class="tDMXke" style="width: 164px; height: 110px; margin-top: 0px; margin-left: 0px;" alt="' + entry['discription'] + '" /></a></div>';
						});
						pagenumber += 1;
						$(window).bind('scroll', bindScroll);
					}
				} else {
					new Messi('Có lỗi kết nối với máy chủ, vui lòng nhấn F5!', {
						title: 'Kết Nối Máy Chủ',  
						modal: 	true, 
						titleClass: 'info', 
						buttons: [{
							id: 0, 
							label: 'Đóng', 
							val: 'X'
						}]
					});
				}
				$('#nam-album-details-linkphots-content').append(html);
				$('#paging-loading').hide();
			}
		});
	}
	//----------------------------------------

	//----------comment post news--------------------------------
	$(document).on("keypress", ".commentstatus-friend textarea", function(ev) {
		var keycode = (ev.keyCode ? ev.keyCode : ev.which);
		if (keycode == 13 && ev.shiftKey){
			
		} else if (keycode == 13) {
			if($.trim($(this).val()).length > 0) {
				var id = $(this).closest('.post-view-info').attr('idcommentpost');
				var content = $.trim($(this).val()).replace(/\n\r?/g, '<br>');
				var typecomment = $(this).closest('.post-view-info').attr('type');
				if(typecomment == 'status') {
					ajaxinsertcommentpoststatuss(id, content);
				}
			}
			$(this).val('');
			return false;
		}
	});
	
	
	//----------------ajax comment status post----------
	function ajaxinsertcommentpoststatuss(idcommentstatus, content) {
		$.ajax({
			type: "POST",
			url: "ajaxaccountcommentstatus",
			data:{id: idcommentstatus, contentre: content},
			success: function(response){
				
				if(response.result == "true") {
					var avatar = $('.headerTinymanPhoto').attr('src');
					var fullname = $('#name-account-loadinfo').text();
					var email = $('#info-id-accountlogin-name').text();
					
					var isstatus = response.idstatuscom;
					content = response.contentcom;
					
					// commment status post completed
					insertrowcommentstatus(idcommentstatus, isstatus, avatar, email, fullname, content);
				} else {
					new Messi('Bình luận không thành công!', {
						title: 'Bình Luận Trạng Thái',  
						modal: 	true, 
						titleClass: 'info', 
						buttons: [{
							id: 0, 
							label: 'Đóng', 
							val: 'X'
						}]
					});
				}
			}
		});
	}
	//------------end ajax comment status post--------------
	
	//-------------------append comment status------------
	function insertrowcommentstatus(idcommentstatus, isstatus, avatar, email, fullname, content) {
		var html ="<div class='friend-comment friendcommentstatus-post-id-" + isstatus + "' idcommentstatus='" + isstatus + "'><div class='friend-avatar' style='background:url(" + avatar + ") no-repeat; background-size: 100% auto;'></div><div class='noidungfriend'><div class='comment-deleted-commentstatus' iddelecomment='" + isstatus + "' title='Xóa'>X</div><a href='index.jsp?acc=" + email + "'><span>" + fullname + "</span></a> " + content + "</div></div>"
		$('.poststatus-comment-id-' + idcommentstatus + ' .content .comment-containt-post').append(html);
	}
	
	//-------------------end append comment status------------
	
	// ----------------------dang status-----------------------
	$(document).on("click", ".post-status-account", function(){
		var content = $('.post-status-contentaccount textarea').val();
		content = $.trim(content).replace(/\n\r?/g, '<br>');
		if(content.length > 0) {
			$.ajax({
				type: "POST",
				url: "accountupdatestatus",
				data:{contentre: content},
				success: function(response) {
					if(response.result == "true") {
						var avatar = $('.headerTinymanPhoto').attr('src');
						var fullname = $('#name-account-loadinfo').text();
						var email = $('#info-id-accountlogin-name').text();
						
						var idaccountload = $('#info-id-account-name').text();
						var id = response.id;
						content = response.content;
						var date = new Date();
						
						var html = '<div class="space">&nbsp;</div><div class="post-view-info poststatus-comment-id-' + id + '" idcommentpost="' + id + '" type="status"><div class="post-view-left"><div class="post-view-avatar" style="background: url(' + avatar + ') no-repeat; background-size: 100% auto;"></div></div><div class="ii"><div class="load-image ie"> &nbsp; </div><div class="post-view-right"><div class="comment-deleted-status" iddelestatus="' + id + '" title="Xóa">X</div><div class="post-view-header"><div style="display: inline;"><h3 class="ck"><a href="index.jsp?acc=' + email + '">' + fullname + '</a></h3><span>' + date.getFullYear() + '/' + (date.getMonth() + 1) + '/' +  date.getDate() + '</span></div></div><div class="post-view-content">' + content + '</div><div class="comment"><div class="content"><div class="likecomment"><a href="#" title="Thích"><img class="like strech" src="icon/7.png" width="15px" height="15px" />Thích</a></div><hr /><div class="comment-containt-post"></div><div class="friend-comment"><div class="friend-avatar" style="background:url(' + avatar + ') no-repeat; background-size: 100% auto;"></div><div class="noidungfriend"><div class="commentstatus-friend" style="width: 100%; min-height: 40px; border: 1px solid rgb(233, 234, 237); background: #FFF;"><textarea style="width:99%; height:38px; resize: none; outline: none; border: 0px;"></textarea></div></div></div> </div></div><div class="post-view-footer"></div></div></div></div>';
						// insert complete send orther user
						//sendMessage(msg);
						
						$('.post-view-info:first').after(html);
						$('.post-status-contentaccount textarea').val('');
					} else {
						new Messi('Có lỗi khi cập nhật, vui lòng thử lại!', {
							title: 'Cập Nhật Trang Thái',  
							modal: 	true, 
							titleClass: 'info', 
							buttons: [{
								id: 0, 
								label: 'Đóng', 
								val: 'X'
							}]
						});
					}
				}
			});
		}
		return false;
	});
	// ---------------------end dang status--------------------


	//----------------delete status for account----------------------
	$(document).on("click", ".comment-deleted-status", function(){
		elem = $(this);
		var idstatus = elem.attr('iddelestatus');
		$.ajax({
			type: "POST",
			url: "ajaxdeletestatusacc",
			data:{idstatusre: idstatus, typere: 'status'},
			success: function(response) {
				if(response == true) {
					$('.poststatus-comment-id-' + idstatus).css({"background-color": "#FF9"}).fadeOut(400, function() {
						elem.hide();
						$('.poststatus-comment-id-' + idstatus).next().hide();
					});
				} else {
					new Messi('Có lỗi khi cập nhật trạng thái, vui lòng thử lại!', {
						title: 'Xóa Trạng Thái',  
						modal: 	true, 
						titleClass: 'info', 
						buttons: [{
							id: 0, 
							label: 'Đóng', 
							val: 'X'
						}]
					});
				}
			}
		});
	});	
	//----------------end delete status for account----------------------
	
	//-------------delete comment status for account------------------------
	$(document).on("click", ".comment-deleted-commentstatus", function(){
		elem = $(this);
		var idcommentsta = elem.attr('iddelecomment');
		
		$.ajax({
			type: "POST",
			url: "ajaxdeletecommentstatusacc",
			data:{idcommentre: idcommentsta},
			success: function(response) {
				if(response == true) {
					$('.friendcommentstatus-post-id-' + idcommentsta).css({"background-color": "#FF9"}).fadeOut(300, function() {elem.hide();});
				} else {
					new Messi('Có lỗi khi cập nhật bình luận, vui lòng thử lại!', {
						title: 'Xóa Bình Luận',  
						modal: 	true, 
						titleClass: 'info', 
						buttons: [{
							id: 0, 
							label: 'Đóng', 
							val: 'X'
						}]
					});
				}
			}
		});
		
		
		//alert(idcommentsta);
	});	
	
	//-----------------like or unlike status-------------------
	$(document).on("click", ".status-post-likess", function() {
		elem = $(this);
		var idstatus = elem.closest(".post-view-info").attr('idcommentpost');
		var type = elem.attr('type');
		// request like
		$.ajax({
			type: "POST",
			url: "ajaxlikestatuspostacc",
			data:{idstatusre: idstatus, typere: type},
			success: function(response) {
				if(response == true) {
					if(type == 'like') {
						elem.after("<span class='ban-post-like-status'> Bạn </span>");
						elem.attr('type', 'unlike').attr('title', 'Bỏ Thích').html("<img class='like strech' src='icon/7.png' width='15px' height='15px' />Bỏ Thích");
					} else if(type == 'unlike') {
						//request unlike
						elem.siblings('.ban-post-like-status').remove();
						elem.attr('type', 'like').attr('title', 'Thích').html("<img class='like strech' src='icon/7.png' width='15px' height='15px' />Thích");
						
					}
				} else {
					new Messi('Có lỗi khi cập nhật thích bình luận, vui lòng thử lại!', {
						title: 'Bình Luận',  
						modal: 	true, 
						titleClass: 'info', 
						buttons: [{
							id: 0, 
							label: 'Đóng', 
							val: 'X'
						}]
					});
				}
			}
		});
		return false;
	});
	//---------------end like or unlike status-----------------
});