var comment_articleid = 2;
var comment_offset = 0;

function comment_tabelize(rows, offset) {

	//ac_ui_api_callback();
	//ac_dom_remove_children($("comments"));

	// only use this if you are passing a single comment into this function - this emulates an array, so the loop below does not need to be changed
	var rows = [rows];

	for (var i = 0; i < rows.length; i++) {

		if (rows[i].approved == 1 || ac_js_site["comments_require_approval"] == 0) {

			ac_liveedit_setparams("comment_subject" + rows[i].id, rows[i].subject, "comment.comment_update_value", "subject", rows[i].id);
			ac_liveedit_setparams("comment_comment" + rows[i].id, rows[i].comment, "comment.comment_update_value", "comment", rows[i].id);

			// indent level - single integer - we add this to the CSS below - multipled by 10 (we add the 0 automatically)
			var replyto_indent = (rows[i].replyto_indent) ? rows[i].replyto_indent : '';

			var comment_subject = (ac_js_admin["pg_comment_edit"]) ? ac_liveedit_text("comment_subject" + rows[i].id, ac_str_htmlescape(rows[i].subject)) : ac_str_htmlescape(rows[i].subject);
			var comment_body = (ac_js_admin["pg_comment_edit"]) ? ac_liveedit_area("comment_comment" + rows[i].id, ac_str_htmlescape(rows[i].comment)) : ac_str_htmlescape(rows[i].comment);
			var comment_name = (ac_js_site["comments_display_name"]) ? ac_str_htmlescape(rows[i].name) : "";
			var comment_email = (ac_js_site["comments_display_email"]) ? Builder.node("a", { mailto: ac_str_htmlescape(rows[i].email) }, ac_str_htmlescape(rows[i].email)) : "";
			var reply_link = Builder.node("a", { href: "javascript: comment_reply_prepare(" + rows[i].id + ", 1);", id: "comment_reply_prepare_link_" + rows[i].id }, comment_reply);
			var reply_cancel_link = Builder.node("a", { href: "javascript: comment_reply_prepare(" + rows[i].id + ", 0);", id: "comment_reply_cancel_link_" + rows[i].id, className: "ac_hidden" }, comment_reply_cancel);

			var comment_info_array = [];
			if (comment_name) {
				comment_info_array.push(comment_name);
				comment_info_array.push(" | ");
			}
			if (comment_email) {
				comment_info_array.push(comment_email);
				comment_info_array.push(" | ");
			}
			comment_info_array.push(rows[i].tstamp);
			if (rows[i].replyto_indent < 2) {
				comment_info_array.push(" | ");
				comment_info_array.push(reply_link);
				comment_info_array.push(reply_cancel_link);
			}

			var comment_div = Builder.node(
				"div",
				{ id: "comment_" + rows[i].id, className: "comments", style: "margin-left: " + replyto_indent + "0px;" },

				[
					Builder.node(
						"div",
						{ className: "comment_avatar" },
						Builder.node("img", { border: "0", src: "http://www.gravatar.com/avatar/" + rows[i].gravatar + "?s=40&d=" + escape(plink).replace(/\//g, '%2F') + "%2Fimages%2Fgravatar.gif" })
					),
					Builder.node(
						"div",
						{ className: "comment_subject" },
						comment_subject
					),
					Builder.node(
						"div",
						{ className: "comment_body" },
						comment_body
					),
					Builder.node(
						"div",
						{ className: "comment_info" },
						comment_info_array
					),
					Builder.node(
						"div",
						{ id: "comment_reply_" + rows[i].id, className: "ac_hidden" },
						""
					)
				]
			);

			if (rows[i].replyto) {
				// insert in DOM after comment being replied to
				$("comments").insertBefore(comment_div, $("comment_" + rows[i].replyto).nextSibling);
			}
			else {
				$("comments").appendChild(comment_div);
			}
		}
	}
}

function comment_paginate(offset) {
//	if (!ac_loader_visible() && !ac_result_visible() && !ac_error_visible())
//		ac_ui_api_call(jsLoading);

	comment_offset = parseInt(offset, 10);

	ac_ajax_call_cb(this.ajaxURL, this.ajaxAction, paginateCB, this.id, "05", comment_offset, ac_js_site["comments_display_perpage"], "A" + comment_articleid);
}

function comment_form_save(id) {
	var post = ac_form_post("form");

	if (post.comment == "") {
		alert("Comment cannot be empty.");
		return false;
	}

	if (ac_js_site["comments_require_email"]) {
		if (post.email == "") {
			alert("Email is required.");
			return false;
		}
	}

	if (ac_str_email(post.email) == false) {
		alert("Not a valid email address.");
		return false;
	}

	ac_ui_api_call(jsSaving);

	ac_ajax_post_cb(apipath, "comment.comment_insert_post", comment_form_save_cb, post);
}

function comment_form_save_cb(xml) {
	var ary = ac_dom_read_node(xml, null);
	ac_ui_api_callback();

	if (ary.succeeded != "0") {

		ac_result_show(ary.message);

		comment_tabelize(ary, 0);

		if (ary.replyto)
			comment_reply_prepare(ary.replyto, 0);

		/*
		if ( ac_js_site.comments_require_approval != 1 ) {
			if ( ac_js_site.comments_display_perpage == paginators[1].total ) {
				$('someid').className = 'ac_block';
			}
		}
		if ( paginators[1].lastOffset == 0 && paginators[1].fetched == ac_js_site.comments_display_perpage ) {
			paginators[1].paginate(ac_js_site.comments_display_perpage);
		} else {
			paginators[1].paginate(paginators[1].lastOffset);
		}
		*/

		if (ac_js_admin.id == 0) {
			$("name").value = "";
			$("email").value = "";
		}
		$("subject").value = "";
		$("comment").value = "";
	} else {
		ac_error_show(ary.message);
	}
}

function comment_reply_prepare(commentid, prepare) {

	// the div for the comment box that we've just hit "Reply" for
	var comment_reply_div = $('comment_reply_' + commentid);

	// if not 0, meaning we already have a reply div open, and we are clicking on another reply div (with the previous one still open)
	if ( $('replyto').value != 0 ) {
		// the previous div holding the form - if we try to open more than one at the same time
		var comment_reply_previous_id = $('replyto').value;
		var comment_reply_div_previous = $('comment_reply_' + comment_reply_previous_id);
	}
	else {
		var comment_reply_previous_id = 0;
		var comment_reply_div_previous = false;
	}

	var form_clone = $('form').cloneNode(true); // clone <div id="form"> wherever it is on the page (it moves if replying to specific comment)

	if (prepare) { // if showing comment form underneath specific comment (when they hit "Reply")

		// remove <div id="form"> from any previous spots, so we don't have more than one on the page
		if (comment_reply_previous_id) {
			ac_dom_remove_children(comment_reply_div_previous);
			$('comment_reply_prepare_link_' + comment_reply_previous_id).className = ''; // show "Reply" link
			$('comment_reply_cancel_link_' + comment_reply_previous_id).className = 'ac_hidden'; // hide "Cancel" link
		}

		comment_reply_div.appendChild(form_clone); // append <div id="form"> to the appropriate spot on the page
		comment_reply_div.className = ''; // show this comments' reply div (where we moved <div id="form"> to)
		$('replyto').value = commentid; // change the hidden value that holds the comment ID that we are replying to
		$('comment_reply_prepare_link_' + commentid).className = 'ac_hidden'; // hide "Reply" link
		$('comment_reply_cancel_link_' + commentid).className = ''; // show "Cancel" link

		// remove content from initial spot where form loads - we know we are in "reply" context here, so always remove
		ac_dom_remove_children( $('form_holder') );
	}
	else { // if they hit "Cancel"
		ac_dom_remove_children(comment_reply_div); // remove <div id="form"> from it's previous spot, so we don't have more than one on the page
		comment_reply_div.className = 'ac_hidden'; // hide this comments' reply div (where we move <div id="form"> to)
		$('form_holder').appendChild(form_clone); // append <div id="form"> to original spot (at the bottom) on the page
		$('replyto').value = 0; // change the hidden replyto value back to 0
		$('comment_reply_prepare_link_' + commentid).className = ''; // show "Reply" link
		$('comment_reply_cancel_link_' + commentid).className = 'ac_hidden'; // hide "Cancel" link
	}
}
