var page = 1;
var cmttype = 0; //show toplike
var _parentid = 0;
var emoTypePicked = 0;
function buildyournews(news, ind) {
var n = "";
if (ind < 5) {
n = "
" +
"
" +
"" + news.litleTitle + "" +
"" + news.categoryName + "\" +
"";
} else {
n = "" + news.litleTitle + "";
}
return n;
}
function loadtincuaban() {
var ind = 1;
$.getJSON("/tincuaban/0", function (data) {
var lst = "";
$.each(data, function (key, news) {
if (ind < 5) {
lst = lst + buildyournews(news, ind);
}
ind = ind + 1;
});
$("#tincuaban").html("");
});
}
function loadcmt() {
$.get("/binh-luan/" + $("#objectid").val() + "/" + $("#objecttype").val() + "/" + page + "/" + cmttype, function (data) {
$("#NewsComments").append(data);
if (parseInt($("#commentcountpage").val()) > 5) {
$("#btnloadviewmore").removeClass("hide");
}
const timeoutid = setInterval(function () {
loadCommentEmotion();
}, 3000);
const stop = function (id) {
clearTimeout(id);
};
setTimeout(function () {
stop(timeoutid);
}, 3000);
});
}
function loadviewmorecomment() {
for (var i = (page * 5); i < ((page + 1) * 5 + 1);i++) {
$("#parentcmt_" + i).removeClass("hide");
if (i >= parseInt($("#commentcountpage").val())) {
$("#btnloadviewmore").addClass("hide");
return;
}
}
page = page + 1;
}
function loadCommentEmotion() {
$.get("/GetCommentEmotion/" + $("#objectid").val() + "/" + $("#objecttype").val(), function (data) {
for (var cmt in data) {
if (data[cmt].emotionType == 1) {
$('#btnlikecmt_' + data[cmt].commentId).addClass("btnlikedislikecmt");
} else {
$('#btndislikecmt_' + data[cmt].commentId).addClass("btnlikedislikecmt");
}
}
});
}
function loadNewsEmotion() {
$.getJSON("/getNewsEmotion/" + $("#objectid").val() + "/" + $("#objecttype").val(), function (data) {
var acty = data.newsUserActivity;
if (acty.liked > 0) {
$("#numemo1_1").html(acty.liked);
$("#numemo1").html(acty.liked);
}
if (acty.heart > 0) {
$("#numemo2").html(acty.heart);
}
if (acty.wow > 0) {
$("#numemo4").html(acty.wow);
}
if (acty.comments > 0) {
$("#ncmt_1").html(acty.comments);
$("#ncmt").html(acty.comments);
}
var useract = data.logNewsEmotion;
if (useract.emotionType > 0) {
$("#btnEmo" + useract.emotionType).addClass("emopicked");
emoTypePicked = useract.emotionType;
}
});
}
function btnNewsEmotion_click(emotiontype) {
var numemo = 0;
if (!isNaN(parseInt($("#numemo" + emotiontype).html())))
{
numemo = parseInt($("#numemo" + emotiontype).html());
};
var numemopicked = 0;
if (!isNaN(parseInt($("#numemo" + emoTypePicked).html()))) {
numemopicked = parseInt($("#numemo" + emoTypePicked).html());
};
// da picked vao 1 emotype
if (emoTypePicked > 0)
{
$("#btnEmo" + emoTypePicked).removeClass("emopicked");
if (numemopicked > 0)
{
numemopicked = numemopicked - 1;
if (numemopicked > 0) {
$("#numemo" + emoTypePicked).html(numemopicked);
$("#numemo" + emoTypePicked + "_1").html(numemopicked);
}else{
$("#numemo" + emoTypePicked).html("");
$("#numemo" + emoTypePicked + "_1").html("");
}
};
if (emoTypePicked == emotiontype) {
emoTypePicked = 0;
} else
{ // picked emotion khac
$("#btnEmo" + emotiontype).addClass("emopicked");
$("#btnEmo" + emotiontype + "_1").addClass("emopicked");
numemo = numemo + 1;
$("#numemo" + emotiontype).html(numemo);
$("#numemo" + emotiontype + "_1").html(numemo);
emoTypePicked = emotiontype;
};
} else
{ // bai nay chua pick emotion nao
$("#btnEmo" + emotiontype).addClass("emopicked");
$("#btnEmo" + emotiontype + "_1").addClass("emopicked");
numemo = numemo + 1;
$("#numemo" + emotiontype).html(numemo);
$("#numemo" + emotiontype + "_1").html(numemo);
emoTypePicked = emotiontype;
};
$.post('/setNewsEmotion/' + $("#objectid").val() + '/' + $("#objecttype").val() + '/' + emotiontype)
.done(function (msg) {
//const timeoutid = setInterval(function () {
// loadNewsEmotion();
//}, 1000);
//const stop = function (id) {
// clearTimeout(id);
//};
//setTimeout(function () {
// stop(timeoutid);
//}, 10000);
})
.fail(function (xhr, textStatus, errorThrown) {
});
}
function loadviewmore() {
var viewmore_page = parseInt($("#viewmore_page").val()) + 1;
$.get("/loadviewmore/" + $("#viewmore_type").val() + "/" + $("#viewmore_id").val() + "/" + viewmore_page, function (data) {
$("#viewmore_contain").append(data);
$("#viewmore_page").val(viewmore_page);
});
}
function btnComEmo_clickthumup(cmtid) {
if (Cookies.get('thumup' + cmtid) !== 'y') {
$('#thumup' + cmtid).text(parseInt($('#thumup' + cmtid).text()) + 1);
$('#btnlikecmt_' + cmtid).addClass("btnlikedislikecmt");
Cookies.set('thumup' + cmtid, 'y', { expires: 10000 });
if (Cookies.get('thumdw' + cmtid) === 'y') {
Cookies.set('thumdw' + cmtid, '1', { expires: 10000 });
$('#thumdw' + cmtid).text(parseInt($('#thumdw' + cmtid).text()) - 1);
$('#btndislikecmt_' + cmtid).removeClass("btnlikedislikecmt");
}
} else {
Cookies.set('thumup' + cmtid, '1', { expires: 10000 });
$('#thumup' + cmtid).text(parseInt($('#thumup' + cmtid).text()) - 1);
$('#btnlikecmt_' + cmtid).removeClass("btnlikedislikecmt");
}
$.post('/setCommentEmotion/' + $("#objectid").val() + '/' + cmtid + '/' + 1);
}
function btnComEmo_clickthumdw(cmtid) {
if (Cookies.get('thumdw' + cmtid) !== 'y') {
$('#thumdw' + cmtid).text(parseInt($('#thumdw' + cmtid).text()) + 1);
$('#btndislikecmt_' + cmtid).addClass("btnlikedislikecmt");
Cookies.set('thumdw' + cmtid, 'y', { expires: 10000 });
if (Cookies.get('thumup' + cmtid) === 'y') {
Cookies.set('thumup' + cmtid, '1', { expires: 10000 });
$('#thumup' + cmtid).text(parseInt($('#thumup' + cmtid).text()) - 1);
$('#btnlikecmt_' + cmtid).removeClass("btnlikedislikecmt");
}
} else {
Cookies.set('thumdw' + cmtid, '1', { expires: 10000 });
$('#thumdw' + cmtid).text(parseInt($('#thumdw' + cmtid).text()) - 1);
$('#btndislikecmt_' + cmtid).removeClass("btnlikedislikecmt");
}
$.post('/setCommentEmotion/' + $("#objectid").val() + '/' + cmtid + '/' + 7);
}
function btnComEmo_click(emotiontype, commentid) {
$.post('/setCommentEmotion/' + $("#objectid").val() + '/' + commentid + '/' + emotiontype)
.done(function (msg) {
})
.fail(function (xhr, textStatus, errorThrown) {
var i = 0;
});
}
function btnComment_click(parentid, replyid) {
var objectid = $('#objectid').val();
var objecttype = $('#objecttype').val();
var comment = "";
var replyname = "";
if (replyid == 0) {
comment = $("#txtcomment_" + parentid).val();
} else {
comment = $("#txtcomment_" + replyid).val();
replyname = $("#replyname_" + replyid).text();
}
if ($('#txtUserid').val() === '') {
showcommentloginform();
}
if (comment.length == 0) {
$('.cmterr').html("Mời bạn viết bình luận.");
return;
}
sendpost(objectid, objecttype, parentid, replyid, replyname, comment);
}
function showreplybox(parentid, replyid) {
if (replyid == 0) {
var htmlbox = "" + $("#usercommentavatar").html() + "
";
$("#reply_" + _parentid).html("");
$("#reply_" + _parentid).addClass("hide");
$("#reply_" + parentid).html(htmlbox);
$("#reply_" + parentid).removeClass("hide");
_parentid = parentid;
} else {
var htmlbox = "" + $("#usercommentavatar").html() + "
";
$("#reply_" + _parentid).html("");
$("#reply_" + _parentid).addClass("hide");
$("#reply_" + replyid).html(htmlbox);
$("#reply_" + replyid).removeClass("hide");
_parentid = replyid;
}
}
function sendpost(objectid, objecttype, parentid, replyid, replyname, comment) {
$.post('/postcomment/', { objectid: objectid, objecttype: objecttype, parentid: parentid, replyid: replyid, replyname: replyname, comment: comment })
.done(function (msg) {
$('.cmterr').html("Bình luận của bạn đã được gửi tới bộ phận duyệt.");
if (replyid == 0) {
$("#txtcomment_" + parentid).val("");
} else {
$("#txtcomment_" + replyid).val("");
}
})
.fail(function (xhr, textStatus, errorThrown) {
if ($('#txtUserid').val() === '') {
$('.cmterr').html("Bạn cần phải đăng nhập tài khoản Bongdaplus");
} else {
$('.cmterr').html("Đã xảy ra lỗi trong khi gửi bình luận.");
}
});
}
//======================================================
function btnsetnewsletteremail() {
$.post('/setemailnnewsletter/', { email: $('#emailnewsletter').val() })
.done(function (msg) {
})
.fail(function (xhr, textStatus, errorThrown) {
});
}
function showloginform() {
$("#loginview").removeClass("hide");
$(".over-layer").removeClass("hide");
}
function showcommentloginform() {
$("#commentloginview").removeClass("hide");
$(".over-layer").removeClass("hide");
}
function memberhideloginform() {
$("#loginview").addClass("hide");
$(".over-layer").addClass("hide");
//alert("hide");
location.reload()
}
//======================================================
function onMessage(event) {
if (event.origin !== "https://member.bongdaplus.vn") return;
var data = event.data;
if (typeof (window[data.func]) == "function") {
window[data.func].call(null, data.message);
}
}
if (window.addEventListener) {
window.addEventListener("message", onMessage, false);
}
else if (window.attachEvent) {
window.attachEvent("onmessage", onMessage, false);
}
//======================================================
$(document).ready(function () {
if ($("#commentnum").length) {
loadNewsEmotion();
loadcmt();
}
if ($("#viewmore_contain").length && ($("#viewmore_id").val() != 'homepage')) {
loadviewmore();
}
if ($("#tincuaban").length) {
loadtincuaban();
}
});