﻿function showUIGift(username) {
	if(username) {
		$.get(
			HOST_HAT + 'ajax/uigift?username=' + username,
			function(html){
				$.blockUI({
					message: html,
					css: {
						border: 'none',
						top:  ($(window).height() - 400)/2 + 'px',
						left: ($(window).width() - 600)/2 + 'px',
						cursor: 'pointer'
					}
				});
			}
		);
	}
}
uimfquestion = function(pusername) {
	$.get (
		HOST_HAT + 'ajax/uimfquestion?username='+pusername,
		function(html) {
			$.blockUI({
				message: html,
				css: {
					background: 'none',
					border: 'none',
					top:  ($(window).height() - 500)/2 + 'px',
					left: ($(window).width() - 500)/2 + 'px',
					cursor: 'pointer'
				}
			});
		}
	);
}
uimfanswerresult = function(pquestionid) {
	$.get (
		HOST_HAT + 'ajax/uimfanswerresult?questionid='+pquestionid,
		function(html) {
			$.blockUI({
				message: html,
				css: {
					background: 'none',
					border: 'none',
					top:  ($(window).height() - 500)/2 + 'px',
					left: ($(window).width() - 640)/2 + 'px',
					cursor: 'pointer'
				}
			});
		}
	);
}
uimfanswer = function(pusername, pquestionid, pparentid) {
	$.get (
		HOST_HAT + 'ajax/uimfquestion?username='+pusername + '&questionid='+pquestionid + '&parentid='+pparentid,
		function(html) {
			$.blockUI({
				message: html,
				css: {
					background: 'none',
					border: 'none',
					top:  ($(window).height() - 500)/2 + 'px',
					left: ($(window).width() - 500)/2 + 'px',
					cursor: 'pointer'
				}
			});
		}
	);
}
deletemfquestion = function(pquestionid, page) {
	if (confirm(vhome_msg_confirmdeletemfquestion)) {
		$.post (
			HOST_HAT + 'ajax/deletemfquestion?isajax=1&page='+page,
			{questionid: pquestionid},
			function (response) {
				$('#questionItems').html(response);
			}
		);
	}
}
showmfanswer = function (username, ausername, questionid, isread, parentid, type, isshowresult) {
	if (parentid == 0) {
		var display = $("#questionAnswerItems_"+questionid).css('display');
		if ($("#questionAnswerItems_" + questionid).html()=='' || isshowresult == 1) {
			$("#questionAnswerItems_"+questionid).css('display','block');
			$.get(HOST_HAT + 'ajax/mfanswer?username='+username+'&ausername='+ausername + '&questionid='+questionid + '&isread='+isread + '&parentid='+parentid + '&type='+type + '&isshowresult='+isshowresult, function(result){$("#questionAnswerItems_" + questionid).html(result);});
		} else if(display == 'none') {
			$("#questionAnswerItems_"+questionid).css('display','block');
		} else {
			$("#questionAnswerItems_"+questionid).css('display','none');
		}
	} else {
		var display = $("#subAnswer_"+parentid).css('display');
		if ($("#subAnswer_"+parentid).html() == '') {
			$("#updown_"+parentid).attr('src', HOST_STATIC+'img/icon/makefriend/bnt_hide.gif');
			$("#updown_"+parentid).attr('title', 'Thu gọn');
			$.get(
				HOST_HAT + 'ajax/mfanswer?username='+username+'&ausername='+ausername+'&questionid='+questionid + '&isread='+isread + '&parentid='+parentid + '&type='+type + '&isshowresult='+isshowresult,
				function(result){
					$("#subAnswer_"+parentid).html(result);
				}
			);
		} else if(display == 'none') {
			$("#updown_"+parentid).attr('src', HOST_STATIC+'img/icon/makefriend/bnt_hide.gif');
			$("#updown_"+parentid).attr('title', 'Thu gọn');
			$("#subAnswer_"+parentid).css('display','block');
		} else {
			$("#updown_"+parentid).attr('src', HOST_STATIC+'img/icon/makefriend/bnt_unhide.gif');
			$("#updown_"+parentid).attr('title', 'Xem chi tiết');
			$("#subAnswer_"+parentid).css('display','none');
		}
	}
}

deletemfanswer = function (pownerusername, pusername, panswerid, pparentid, pquestionid, ppage, ppagename) {
	if (confirm(vhome_msg_confirmdeletemfanswer)) {
		$.post(
			HOST_HAT + 'ajax/deletemfanswer',
			{username:pownerusername, questionid:pquestionid, answerid:panswerid, parentid:pparentid, page:ppage, pagename:ppagename},
			function(response) {
				if (ppagename == '') {
					var numAnswer = parseInt($("#spanNumAnswer_"+pquestionid).html());
					if (pownerusername != pusername) {
						$("#spanNumAnswer_"+pquestionid).html(numAnswer-1);
					}
					if (pparentid == 0) {
						$("#questionAnswerItems_"+pquestionid).html(response);
					} else {
						$('#subAnswer_'+pparentid).html(response);
					}
				} else {
					location.reload();
				}
			}
		);
	}
}
votemfanswer = function(pusername, panswerid, pparentid, pquestionid, ptype, newpoint, ppage, ppagename) {
	$.getJSON (
		HOST_HAT + 'ajax/votemfanswer?callback=?',
		{username:pusername, answerid:panswerid, parentid:pparentid, questionid:pquestionid, type:ptype, page:ppage, pagename:ppagename},
		function (response) {
			if (ppagename == '') {
				var error = response['error'];
				var msg = response['msg'];
				if (error == 0) {
					if (pparentid == 0)
						$('#answerPoint_'+panswerid).html(newpoint);
					else
						$('#subAnswerPoint_'+panswerid).html(newpoint);
					showUIMessage(msg);
				} else {
					alert(msg);
				}
			} else {
				location.reload();
			}
		}
	);
}
votemfquestion = function(pusername, pquestionid, ptype, newpoint) {
	$.getJSON (
		HOST_HAT+'ajax/votemfquestion?callback=?',
		{username:pusername,questionid:pquestionid,type:ptype},
		function (response) {
			var error = response['error'];
			var msg = response['msg'];
			if (error == 0) {
				$('#questionPoint_'+pquestionid).html(newpoint);
				showUIMessage(msg);
			} else {
				alert(msg);
			}
		}
	);
}
replyanswer = function(pparentid) {
	$("#replyanswer_"+pparentid).css('display','block');
	$("#txtSubAnswer_"+pparentid).focus();
}
deletemfa = function (pownerusername, pquestionid, panswerid, isshowtop, page) {
	if (confirm(vhome_msg_confirmdeletemfanswer)) {
		$.get (
			HOST_HAT+'ajax/deletemfa?username='+pownerusername+'&pquestionid='+pquestionid+'&panswerid='+panswerid+'&isshowtop='+isshowtop,
			function (msg){
				if (msg) {
					showUIMessage(msg);
					mfa(pownerusername, pownerusername, pquestionid, 0, 0, 1, 1, isshowtop, page);
				}
			}
		);
	}
}
deletemfq = function (pquestionid) {
	if (confirm(vhome_msg_confirmdeletemfanswer)) {
		location.href=HOST_HAT+'vhome/deletemfq?pquestionid='+pquestionid;
	}
}
mfa = function (username, ausername, questionid, isread, parentid, type, isshowresult, isshowtop, page) {
	if (page==undefined) page = 1;
	var display = $("#idAnswer_"+questionid).css('display');
	if ($("#idAnswer_"+questionid).html()==null || isshowresult==1) {
		$("#idAnswer_"+questionid).css('display','block');
		$.get (
			HOST_HAT+'ajax/mfa?username='+username+'&ausername='+ausername + '&questionid='+questionid + '&isread='+isread + '&parentid='+parentid + '&type='+type + '&isshowresult='+isshowresult+'&isshowtop='+isshowtop+'&page='+page,
			function(result){
				$("#idQuestion_"+questionid).html(result);
			}
		);
	} else {
		$("#pgcontent_"+questionid).focus();
	}
}
uimfq = function (pusername) {
	if(pusername) {
		$.get (
			HOST_HAT+'ajax/uimfq?username='+pusername,
			function (html) {
				$.blockUI({
					message: html,
					css: {
						background: 'none',
						border: 'none',
						top:  ($(window).height() - 500)/2 + 'px',
						left: ($(window).width() - 400)/2 + 'px',
						cursor: 'pointer'
					}
				});
			}
		);
	}
}

function guestbook(page, limit) {
	var purl = HOST_HAT+'ajax/guestbook?username='+username+'&page='+page+'&limit='+limit;
	$.ajax({
		cache	: false,
		async	: true,
		type	: 'POST',
		data	: null,
		url		: purl,
		success	: function(html) {
			$("#idGuestBook").html(html);
		}
	});
	/*
	$.get(
		HOST_HAT+'ajax/guestbook?username='+username+'&page='+page+'&limit='+limit,
		function(html){
			$("#idGuestBook").html(html);
		}
	);
	*/
}
deleteguestbook = function(username, id, page, limit) {
	if (confirm(com_msg_confirmdelete)) {
		$.get(
			HOST_HAT+'ajax/deleteguestbook?username='+username+'&id='+id+'&page='+page,
			function(msg){
				if (msg) {
					showUIMessage(msg);
					guestbook(page, limit);
				}
			}
		);
	}
}
