jq(document).ready(function () {
	jq(".comment:not([class*=comment-level1])").hide();
	jq(".comment-body-responselink").removeClass("responselink-open");
});

function efcs_fix_events () {

	jq(".comment-reply textarea").unbind("keyup").keyup(efcs_comment_reply_keypress);

	jq(".comments .comment-body a:not([class*=outlink])").attr("href", "javascript:void(0);").unbind("click").click(function () {
		alert('x');
		efcs_switch_responses(jq(this).parent().parent().attr("id").replace("comment", ""));
	});

	jq("a.comment-body-responselink").attr("href", "javascript:void(0);").unbind("click").click(function () {
		efcs_switch_responses(jq(this).parent().parent().attr("id").replace("comment", ""));
	});

	jq(".comments .comment-body-lowrating a.showlink").attr("href", "javascript:void(0);").unbind("click").click(function () {
		efcs_show_hidden_comment(jq(this).parent().parent().attr("id").replace("comment", ""));
	});

	jq(".comments .comment-rating a").attr("href", "javascript:void(0);").unbind("click").click(function () {
		var comment_id = jq(this).parent().parent().attr("id").replace("comment", "");
		if (jq(this).hasClass("comment-rating-replylink")) {
			efcs_reply_to(comment_id);
		} else {
			efcs_thumb_click(comment_id, this.className.indexOf("dnlink") == -1 ? "up" : "dn");
		}
	});

	jq(".comments form").unbind("submit").submit(function () {
		return efcs_submit_reply_form(this);
	});

	jq(".comments form .comment-reply-name input").unbind("focus").focus(function () {
		jq(this).select();
	});

	jq(".comments form .comment-captcha a").attr("href", "javascript:void(0);").unbind("click").click(function () {
		efcs_change_captcha_image(this);
	});

	jq(".comments form .comment-captcha-input [name=captchatext]").unbind("keyup").keyup(function () {
		this.value = this.value.replace(/[^a-zA-Z0-9]+/, "");
		this.value = this.value.toUpperCase();
	});

}

efcs_fix_events();

function efcs_comment_reply_keypress () {
	if (this.value.length > 500) {
		this.value = this.value.substring(0, 500);
	} else {
		jq(this).parent().find("span em").text(500 - this.value.length);
	}
}

function efcs_close_comment ( comment_id ) {
	jq(".comment-parent" + comment_id).hide().each(function () {
		jq("#comment-body-responselink" + comment_id).removeClass("responselink-open");
		efcs_close_comment(this.id.replace("comment", ""));
	});
}

function efcs_show_hidden_comment ( comment_id ) {
	jq("#comment" + comment_id).removeClass("comment-hidden");
	jq("#comment-body-responselink" + comment_id).remove().appendTo("#comment" + comment_id + " .comment-body");
	efcs_fix_events();
}

function efcs_switch_responses ( comment_id ) {
	var comments = jq(".comment-parent" + comment_id);
	if (comments.length == 0) {
		return;
	}
	if (jq(comments[0]).css("display") == "none") {
		comments.show();
		jq("#comment-body-responselink" + comment_id).addClass("responselink-open");
	} else {
		efcs_close_comment(comment_id);
	}
}

function efcs_reply_to ( comment_id ) {
	if (document.getElementById("comment-reply" + comment_id) == null) {
		var captchaimg = jq(".comment-reply" + (jq(".comment-reply[id]").length > 0 ? "[id]" : ":not([id])") + " .comment-captcha img").remove();
		jq(".comment-reply[id]").remove();
		jq(".comment-reply").hide();
		jq("#comment" + comment_id).after("<div id=\"comment-reply" + comment_id + "\" class=\"comment-reply\"></div>");
		jq("#comment-reply" + comment_id).html(jq(".comment-reply:not([id])").html());
		jq("#comment-reply" + comment_id + " .comment-captcha img").remove();
		jq("#comment-reply" + comment_id + " .comment-captcha").prepend(captchaimg);
		jq("#comment-reply" + comment_id + " .comment-reply-counter em").html("500");
		jq("#comment-reply" + comment_id + " [type=hidden][name=commentid]").val(comment_id);
		jq("#comment-reply" + comment_id).addClass("comment-reply-noborder").find("h3").text("REPLY TO ABOVE COMMENT");
		jq(".comment-rating-replylink").removeClass("replylink-open");
		jq("#comment-rating-replylink" + comment_id).addClass("replylink-open");
		var comments = jq(".comment-parent" + comment_id);
		if (comments.length > 0) {
			jq("#comment-body-responselink" + comment_id).addClass("responselink-open");
			comments.show();
		}
	} else {
		jq("#comment-rating-replylink" + comment_id).removeClass("replylink-open");
		jq(".comment-reply:not([id]) .comment-captcha").prepend(jq("#comment-reply" + comment_id + " .comment-captcha img").remove());
		jq("#comment-reply" + comment_id).remove();
		jq(".comment-reply").show();
	}
	efcs_fix_events();
}

function efcs_submit_reply_form ( form ) {
	form.elements["submit"].disabled = true;
	var postdata = {
		cid: form.elements["commentid"].value,
		pid: form.elements["postid"].value,
		cpt: form.elements["captchatext"].value,
		nme: form.elements["name"].value,
		msg: form.elements["comment"].value
	}
	if (postdata.nme != readCookie("MB_authorName")) {
		eraseCookie("MB_authorName");
		createCookie("MB_authorName", postdata.nme, 30);
	}
	jq.ajax({
		success: efcs_submit_reply_form_success,
		error: efcs_submit_reply_form_restore,
		dataType: "json",
		type: "GET",
		url: "/pgn/efcs/comment-reply",
		data: postdata
	});
	return false;
}

function efcs_submit_reply_form_success ( json, textstatus ) {
	efcs_submit_reply_form_restore(null, null, null);
	if (json.error) {
		window.alert(json.error_msg);
		var formref = (json.commentid == 0 ? ".comment-reply" : "#comment-reply" + json.commentid);
		switch (json.error_type) {
			case "WRONGNAME":
				jq(formref + " [name=name]").focus().select();
			break;
			case "WRONGCAPTCHA":
				jq(formref + " [name=captchatext]").focus().select();
			break;
			case "NOMESSAGE":
				jq(formref + " [name=comment]").focus();
			break;
			case "NOPOST":
			case "NOCOMMENT":
				window.location.reload(true);
			break;
		}
	} else {
		var hasparent = json.parent > 0;
		var pcomments = jq(hasparent ? ".comment-parent" + json.parent : ".comments .comment");
		var hascomments = pcomments.length > 0;
		var cdiv = "";
		    cdiv = "\r\n			<div class=\"comment comment-parent${comment.parent} comment-level${comment.level}{if comment.moderator} comment-moderator{/if}\" id=\"comment${comment.id}\">"
				 + "\r\n				<p class=\"comment-data\"><span class=\"comment-data-authorname\">${comment.author}</span><span class=\"comment-data-date\">${comment.date}</span></p>"
				 + "\r\n				<p class=\"comment-body\">${comment.comment}{if comment.level < 4}<a href=\"#\" class=\"comment-body-responselink\" id=\"comment-body-responselink${comment.id}\" style=\"display:none;\">0 Responses</a>{/if}</p>"
				 + "\r\n				<p class=\"comment-body-lowrating\">Comment hidden due to its low rating. <a href=\"#\" class=\"showlink\">Show</a></p>"
				 + "\r\n				<p class=\"comment-rating\">"
				 + "\r\n					<span id=\"comment${comment.id}-score\" class=\"comment-rating-score\">0</span>"
				 + "\r\n					<a href=\"#\" class=\"comment-rating-uplink\">&nbsp;</a><a href=\"#\" class=\"comment-rating-dnlink\">&nbsp;</a>"
				 + "{if comment.level < 4}\r\n					<a href=\"#\" class=\"comment-rating-replylink\" id=\"comment-rating-replylink${comment.id}\">Reply</a>{/if}"
				 + "\r\n				</p>"
				 + "\r\n				<div class=\"clear\"></div>"
				 + "\r\n			</div>";
		cdiv = cdiv.process({comment:json});
		if (hasparent) {
			efcs_set_last_child(json.parent);
			efcs_get_last_child(json.parent);
		}
		var getafter = ".comment" + (hasparent ? efcs_last_child : "s " + (hascomments ? ".comment:last" : "h3:first"));
		var getafter = hasparent ? getafter.replace(".comment", "#comment") : getafter;
		jq(getafter).after(cdiv);
		efcs_fix_events();
		if (hasparent) {
			var curparent = json.parent;
			while (1) {
				var curparentrlink = jq("#comment-body-responselink" + curparent).show();
				var curparentrlinktxt = /([\d]+) Response([s]*)?/.exec(curparentrlink.html());
				var newnumresponses = parseInt(curparentrlinktxt[1]) + 1;
				var curparentparentid = parseInt(/comment\-parent([\d]+)/.exec(jq("#comment" + curparent)[0].className)[1]);
				curparentrlink.html(newnumresponses + " Response" + (newnumresponses != 1 ? "s" : ""));
				curparent = curparentparentid;
				if (curparentparentid == 0) {
					break;
				}
			}
			jq(".comment-parent" + json.parent).show();
			jq("#comment-body-responselink" + json.parent).addClass("responselink-open");
		}
		jq(".comments h3:first a").html("DISCUSSION");
		jq("#comment-reply" + json.parent).remove();
		jq(".comment-rating-replylink").removeClass("replylink-open");
		jq(".comment-reply [name=captchatext]").val("");
		jq(".comment-reply [name=comment]").val("");
		jq(".comment-reply .comment-reply-body em").html(500);
		jq(".comment-reply .comment-captcha img").remove();
		jq(".comment-reply .comment-captcha").prepend("<img src=\"/pgn/efcs/images/captcha.png?s=" + new Date().getTime() + "\" width=\"183\" height=\"46\" alt=\"\" />");
		jq(".comment-reply").show();
		var allcomments = jq(".comment").length;
		jq("#comment-count").html(allcomments + " Comment" + (allcomments != 1 ? "s" : ""));
	}
}

function efcs_submit_reply_form_restore ( httpobj, textstatus, error ) {
	jq(".comment-submit").attr("disabled", false);
}

var efcs_last_child = 0;

/**
 * Recursive function to get the bottommost
 * child of a certain comment
 */
function efcs_get_last_child ( parent_id ) {
	jq(".comment-parent" + parent_id).each(function () {
		var comment_id = jq(this).attr("id").replace("comment", "");
		efcs_set_last_child(comment_id);
		efcs_get_last_child(comment_id);
	});
}

/**
 * Sets the global variable gLastChild
 */
function efcs_set_last_child ( parent_id ) {
	efcs_last_child = parent_id;
}

function efcs_change_captcha_image ( elref ) {
	jq(elref).parent().find("img").remove();
	window.setTimeout(function () {
		jq(elref).parent().prepend("<img src=\"/pgn/efcs/images/captcha.png?change=1&s=" + new Date().getTime() + "\" width=\"183\" height=\"46\" alt=\"\" />");
	}, 0);
}

function efcs_thumb_click ( comment_id, direction ) {
	if (jq("#comment" + comment_id).hasClass("comment-hidden")) {
		return;
	}
	jq("#comment" + comment_id + "-score").addClass("comment-rating-score-loading");
	jq.ajax({
		success: efcs_thumb_click_callback,
		error: efcs_thumb_click_error,
		dataType: "json",
		type: "GET",
		url: "/pgn/efcs/comment-vote?s=" + new Date().getTime(),
		data: { cid: comment_id, dir: direction }
	});
}

function efcs_thumb_click_error () {
	jq(".comment-rating-score").removeClass("comment-rating-score-loading");
}

function efcs_thumb_click_callback ( json ) {
	if (json.error) {
		window.alert(json.error_msg);
		efcs_thumb_click_error();
		switch (json.error_type) {
			case "NOCOMMENT":
			case "NOPOST":
				window.location.reload(true);
			break;
		}
		return;
	}
	if (json.success) {
		var cdiv = jq("#comment" + json.comment_id + "-score");
		if (parseInt(json.newrating) < 0) {
			cdiv.addClass("score-negative");
		} else {
			cdiv.removeClass("score-negative");
		}
		cdiv.html(json.newrating);
		if (parseInt(json.newrating) <= -2 && !jq("#comment" + json.comment_id).hasClass("comment-moderator")) {
			jq("#comment" + json.comment_id).addClass("comment-hidden");
			jq("#comment-body-responselink" + json.comment_id).remove().appendTo("#comment" + json.comment_id + " .comment-body-lowrating");
			efcs_fix_events();
		}
		jq(".comment-rating-score").removeClass("comment-rating-score-loading");
	}
}

/**
 * 3rd Party Code
 */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}