﻿/*
	Pagii Forum 
*/

var MSG_EMPTY_TITLE_MSG = "Please enter a subject and message!";
var MSG_EMPTY_MESSAGE = "Please enter your message!";
var MSG_REMOVE_POST = "Are you sure you want to delete this reply?";
var MSG_REMOVE_MYTOPIC = "Are you sure you want to remove this topic?";

function onDummy() {
}

function onGotoTopics(mode, tag) {
	NTopic.onTracker("goto", mode+"_topic");
	if (mode == "tag") {
		document.location.href = FORUM_PATH + "?page=1" + "&tag=" + tag;
	}
	else {
		if (!nasc.User.checkLogin())
			return;
		if (mode == "saved") 
			document.location.href = FORUM_PATH + "mysaved.php?page=1";
		else if (mode == "my")
			document.location.href = FORUM_PATH + "?page=1" + "&flag=my";
	}
}


function onSideMostTab(zone) {
	$("s_most_post").style.display = 'none';
	$("s_select_post").style.display = 'none';
	$("s_most_view").style.display = 'none';
	$("s_select_view").style.display = 'none';
	$("s_most_recent").style.display = 'none';
	$("s_select_recent").style.display = 'none';

	$("s_most_" + zone).style.display = 'block';
	$("s_select_" + zone).style.display = 'block';
}

var CommentList = {
	cur_postsrl: 0,
	init: function() {
		NForumNet.init();
	},
	onComment: function onCommentTopic(srl) {
		if (!nasc.User.checkLogin())
			return;
		this.cur_postsrl = srl;
		var edit_box = $("edit_box");
		var entry_div = $("comment_box_"+srl);
		if (!entry_div) return;
		entry_div.insertAdjacentElement("beforeEnd", edit_box);
		edit_box.style.display = "block";
		$("edit_text_box").value = "";
		$("edit_text_box").focus();
	},
	NewComment: function(post_srl, c_srl) {
		var entry_div = $("comment_box_" + post_srl);
		if (!entry_div) return;
		var text_box = $("edit_text_box")
		var text = text_box.value;
		text = text.stripTags();
		var commentHTML = [
			"<div id=comment_list_{c_srl} style='margin-top:10px'>",
				"<div class='comment_header'>",
					"<div style='margin-left:10px;margin-top:5px'>",
						"<a href='#' class='fixed_link' style='float:left;'>{name}</a><span style='float:left;margin-left:3px'> said, - a few seconds ago</span>",
						"<a href='#' style='margin-right:12px; float:right'  onclick='CommentList.onDelete({c_srl}); return false' class='fixed_link'><img src='/Home/forum/images/icon_del.gif' /></a>", 
					"</div>",
				"</div>",
				"<pre>{text}</pre>",
			"</div>"
		];
		entry_div.insertAdjacentHTML("beforeEnd", 
					commentHTML.toHTML({name: page_info.name, text: text, c_srl : c_srl}));	
		this.onCancel();
	},
	onCancel: function() {
		$("edit_box").style.display = "none";
	},
	onDelete: function(c_srl) {
		var post_comment = $("comment_list_" + c_srl);
		if (post_comment) {
			post_comment.innerHTML = "";
		}
		NForumNet.DeleteComment(c_srl);
	},
	onSubmit: function() {
		var entry_div = $("comment_box_" + this.cur_postsrl);
		if (!entry_div) return;
		  if ($("edit_text_box").value.trim() == "") {
			return;
		}
		NForumNet.SaveNewComment(this.cur_postsrl, $("edit_text_box").value);
	},
	onSaveCommentRet: function(status, response) {
		var rsp;
		eval("rsp="+response);
		nus.ui.hideThrobber();
		if (rsp.statusCode != "ok") {
			return;
		}
		this.NewComment(rsp.articlesrl, rsp.replysrl);
	},
	onDeleteCommentRet: function(rsp) {
		if (rsp.statusCode != "ok") {
			return;
		}
	}
}


function forumGetLink(callback) {
	nus.dom.IframeDialog.createInstance({
		width: 360, 
		height: 105,
		hasVisualEffect:false,
		contentURL: $URL("forum/link_box.html"),
		parent: this, 
		onInitDialog: function() {
		},
		onDestroy: function() {
			nus.dom.IframeDialog.superCall(this, "onDestroy", arguments);
		},
		onCommand: function(cmd, result) {
			if (cmd == "cancel") {
				this.destroy();
			}
			else if (cmd == "apply") {
				callback(result);
				this.destroy();
			}
		}
	});
}


var NPost = {
	bEdit: false,
	init: function(bEdit) {
		NForumNet.init();
		$("post_content").focus();
		this.bEdit = bEdit;
	},
	onGotoPage: function(page) {
		document.location.href = FORUM_PATH + "forum.php?page=" +  page + "&topicsrl=" + g_topic_srl;
	},
	onNextPage: function() {
		this.onGotoPage(++page_no);
	},
	onPrevPage: function() {
		this.onGotoPage(--page_no);
	},
	onEdit: function(p_srl) {
		var edit = "&cmd=edit&forumsrl=" + p_srl;
		document.location.href = FORUM_PATH + "post_add.php?page=" + page_no + "&topicsrl=" + g_topic_srl + edit;
	},
	onReply: function() {
		if (!nasc.User.checkLogin())
			return;
		document.location.href = FORUM_PATH + "post_add.php?page=" + page_no + "&topicsrl=" + g_topic_srl;
	},
	onDelete: function(p_srl) {
		if (!confirm(MSG_REMOVE_POST)) 
			return;
		nus.ui.showThrobber("Deleting...");
		NForumNet.DeletePost(p_srl);
	},
	onLinkComplete: function(url) {
		var text;
		text = $("post_content").value;
		text += "<a href='"+url+"' target='_blank'>" + url + "</a>";
		$("post_content").value = text;
		$("post_content").focus();
	},
	onInsertLink: function() {
		forumGetLink($callback(NPost, NPost.onLinkComplete));			
	},
	onUploadImageComplete: function(result) {
		var text;
		text = $("post_content").value;
		text += "<img src='"+result[0].path+"' />\n";
		$("post_content").value = text;
		$("post_content").focus();
	},
	onInsertImage:function() {
		nasc.ImageUpload.doUpload(1, $callback(NPost, NPost.onUploadImageComplete));
	},
	onCancel: function() {
		document.location.href = FORUM_PATH + "forum.php?page=" + page_no + "&topicsrl=" + g_topic_srl;
	},
	onSave: function(p_srl) {
		if ($("post_content").value.trim() == "") {
			alert(MSG_EMPTY_MESSAGE);
			return;
		}
		nus.ui.showThrobber("Posting...");
		var text = $("post_content").value;
		text = text.stripScripts().stripEvents();
		if (this.bEdit)
			NForumNet.UpdatePost(p_srl, text);
		else
			NForumNet.SaveNewPost(text);
	},
	onSavePostRet: function(status, response) {
		var rsp;
		eval("rsp="+response);
		nus.ui.hideThrobber();
		if (rsp.statusCode != "ok") {
			return;
		}
		this.onCancel();
	},
	onDeletePostRet: function(rsp) {
		nus.ui.hideThrobber();
		if (rsp.statusCode != "ok") {
			return;
		}
		document.location.href = FORUM_PATH + "forum.php?page=" + page_no + "&topicsrl=" + g_topic_srl;
	}
}

var NTopic = {
	bEdit: false,
	mode: "all",
	tag : "",	
	init: function(bEdit, mode, tag) {
		NForumNet.init();
		this.bEdit = bEdit;
		this.mode = mode;
		this.tag = tag; 
		if (bEdit)
			$("topic_content").focus();
		else {
			if ($("topic_name").value == "")
				$("topic_name").focus();
			else
				$("topic_content").focus();
		}
		// document.addEventListener("keydown", this._keydown, true);
	},
	onTracker: function(category, type) {
		nus.Tracker.register("Forum/" + category, type);
	},
	onLinkComplete: function(url) {
		var text;
		text = $("topic_content").value;
		text += "<a href='"+url+"' target='_blank'>" + url + "</a>";
		$("topic_content").value = text;
		$("topic_content").focus();
	},
	onInsertLink: function() {
		forumGetLink($callback(NTopic, NTopic.onLinkComplete));			
	},
	onUploadImageComplete: function(result) {
		var text;
		text = $("topic_content").value;
		text += "<img src='"+result[0].path+"' />\n";
		$("topic_content").value = text;
		$("topic_content").focus();
	},
	onInsertImage:function() {
		nasc.ImageUpload.doUpload(1, $callback(NTopic, NTopic.onUploadImageComplete));
	},
	onGotoPage: function(page) {
		var modes = "";
		var php = "";
		if (this.mode == "my")
			modes = "&flag=my"
		else if (this.mode == "tag")
			modes = "&tag=" + this.tag;
		else if (this.mode == "mysaved") 
			php = "/mysaved.php";
		document.location.href = FORUM_PATH + php + "?page=" +  page + modes;
	},
	onNextPage: function(inc) {
		if (!inc)
			this.onGotoPage(++page_no);
		else
			this.onGotoPage(page_no + inc);
	},
	onPrevPage: function(inc) {
		if (!inc)
			this.onGotoPage(--page_no);
		else
			this.onGotoPage(page_no - inc);
	},
	onCreateTopic: function() {
		if (!nasc.User.checkLogin())
			return;
		var subject = "";
		if ($("topic_name").value.trim() != "") 
			subject = "&topictitle=" + $("topic_name").value;
		document.location.href = FORUM_PATH + "topic_add.php?categorysrl=1" + subject;
	},
	onCancel: function() {
		document.location.href = FORUM_PATH;
	},
	onEdit: function(t_srl) {
		document.location.href = FORUM_PATH + "topic_add.php?categorysrl=1&cmd=edit&topicsrl=" + t_srl;
	},
	onSaveNewTopic: function(t_srl) {
		if (this.bEdit) {
			var contentStr = $("topic_content").value;
			if (contentStr.trim() == "") {
				alert(MSG_EMPTY_MESSAGE);
				return;
			}
			//if (contentStr.length > 200)
			//	contentStr = contentStr.substr(0, 200);
			nus.ui.showThrobber("Posting...");
			contentStr = contentStr.stripScripts().stripEvents();
			NForumNet.UpdateTopic(t_srl, contentStr, $("topic_tag").value);
		}
		else {
			var subjectStr = $("topic_name").value;
			var contentStr = $("topic_content").value;
			if (subjectStr.trim() == "" || contentStr.trim() == "") {
				alert(MSG_EMPTY_TITLE_MSG);
				return;
			}
			if (subjectStr.length > 100)
				subjectStr = subjectStr.substr(0, 100);
			//if (contentStr.length > 200)
			//	contentStr = contentStr.substr(0, 200);
			nus.ui.showThrobber("Posting...");
			NForumNet.CreateNewTopic(subjectStr, contentStr, $("topic_tag").value);
		}
	},
	AdminUpdateTopic: function(t_srl) {
			var subjectStr = $("topic_name").value;
			var contentStr = $("topic_content").value;
			if (subjectStr.trim() == "" || contentStr.trim() == "") {
				alert(MSG_EMPTY_TITLE_MSG);
				return;
			}
			if (subjectStr.length > 100)
				subjectStr = subjectStr.substr(0, 100);
			//if (contentStr.length > 200)
			//	contentStr = contentStr.substr(0, 200);
			nus.ui.showThrobber("Posting...");
			NForumNet.AdminUpdateTopic(t_srl, subjectStr, contentStr, $("topic_tag").value);
	},
	onSaveRet: function(status, response) {
		var rsp;
		eval("rsp="+response);
		nus.ui.hideThrobber();
		if (rsp.statusCode != "ok") {
			return;
		}
		document.location.href = FORUM_PATH + "forum.php?page=1" + "&topicsrl=" + rsp.topicsrl;
	},
	onSaveMyTopic: function(t_srl) {
		NTopic.onTracker("topic", "saved");
		nus.ui.showThrobber("Saving this topic...");
		NForumNet.SaveMyTopic(t_srl);
	},
	onSaveMyTopicRet: function(rsp) {
		nus.ui.hideThrobber();
		if (rsp.statusCode != "ok") {
		}
	},
	onDelMyTopic: function(t_srl) {
		if (!confirm(MSG_REMOVE_MYTOPIC)) 
			return;
		nus.ui.showThrobber("Removing this topic...");
		NForumNet.DeleteMyTopic(t_srl);
	},
	onDelMyTopicRet: function(rsp) {
		nus.ui.hideThrobber();
		if (rsp.statusCode != "ok") {
		}
		document.location.href = FORUM_PATH + "mysaved.php?page=1";
	},
	onDummyRet: function(rsp) {
		if (rsp.statusCode != "ok") {
		}
	}
}

var NForumNet = {
	init: function() {
		this.API_URL = nasc.forumURL + "/_api/";
	},
	CreateNewTopic : function(p_title, p_content, p_tag) {
		var params = { 
				method: "forum.inserttopic",
				categorysrl: 1,
				writermsrl: nasc.User.getSSN()
		};
		var postdata = "title=" + escape(p_title) + "&content=" + escape(p_content) + "&tag=" + escape(p_tag);
		nus.io.sendRequest(this.API_URL, params, postdata, NTopic.onSaveRet, null); 
	},
	UpdateTopic : function(t_srl, p_content, p_tag) {
		var params = { 
				method: "forum.updatetopic",
				topicsrl: t_srl,
				categorysrl: 1,
				writermsrl: nasc.User.getSSN()
		};
		var postdata = "content=" + escape(p_content) + "&tag=" + escape(p_tag);
		nus.io.sendRequest(this.API_URL, params, postdata, NTopic.onSaveRet, null); 
	},
	AdminUpdateTopic: function(t_srl, p_title, p_content, p_tag) {
		var params = { 
				method: "forum.updatetopic",
				topicsrl: t_srl,
				categorysrl: 1,
				writermsrl: nasc.User.getSSN()
		};
		var postdata = "title=" + escape(p_title) + "&content=" + escape(p_content) + "&tag=" + escape(p_tag);
		nus.io.sendRequest(this.API_URL, params, postdata, NTopic.onSaveRet, null); 
	},
	SaveNewComment : function(post_srl, p_content) {
		var params = { 
				method: "forum.insertreply",
				writermsrl: nasc.User.getSSN(),
				writername: page_info.name,
				articlesrl: post_srl  
		};
		var postdata = "content=" + escape(p_content);
		nus.io.sendRequest(this.API_URL, params, postdata, $callback(CommentList, CommentList.onSaveCommentRet), null); 
	},
	DeleteComment : function(reply_srl) {
		var params = { 
				method: "forum.deletereply",
				callback: "CommentList.onDeleteCommentRet",		
				writermsrl: nasc.User.getSSN(),
				replysrl : reply_srl
		};
		nus.io.ScriptRequest.sendRequest(this.API_URL, params, null, null); 
	},
	SaveNewPost : function(p_content) {
		var params = { 
				method: "forum.insertarticle",
				writermsrl: nasc.User.getSSN(),
				writername: page_info.name,
				topicsrl: g_topic_srl,  
				title: ""
		};
		var postdata = "content=" + escape(p_content);
		nus.io.sendRequest(this.API_URL, params, postdata, $callback(NPost, NPost.onSavePostRet), null); 
	},
	UpdatePost : function(p_srl, p_content) {
		var params = { 
				method: "forum.updatearticle",
				writermsrl: nasc.User.getSSN(),
				writername: page_info.name,
				topicsrl: g_topic_srl,  
				title: "",
				articlesrl : p_srl
		};
		var postdata = "content=" + escape(p_content);
		nus.io.sendRequest(this.API_URL, params, postdata, $callback(NPost, NPost.onSavePostRet), null); 
	},
	DeletePost:function(article_srl ) {
		var params = { 
				method: "forum.deletearticle",
				callback: "NPost.onDeletePostRet",		
				writermsrl: nasc.User.getSSN(),
				articlesrl: article_srl
		};
		nus.io.ScriptRequest.sendRequest(this.API_URL, params, null, null); 
	},
	SaveMyTopic: function(t_srl) {
		var params = { 
				method: "forum.insertmytopic",
				callback: "NTopic.onSaveMyTopicRet",		
				ownermsrl: nasc.User.getSSN(),
				topicsrl: t_srl  
		};
		nus.io.ScriptRequest.sendRequest(this.API_URL, params, null, null); 
	},
	DeleteMyTopic: function(t_srl) {
		var params = { 
				method: "forum.deletemytopic",
				callback: "NTopic.onDelMyTopicRet",		
				ownermsrl: nasc.User.getSSN(),
				topicsrl: t_srl  
		};
		nus.io.ScriptRequest.sendRequest(this.API_URL, params, null, null); 
	}
}

