function TPCComment(a) {
	this.objectId = a.objectId;
	this.objectType = a.objectType;
	this.objectSource = a.objectSource;
	this.thread = undefined;
	this.sort = "";
	this.sliceSize = 0;
	this.sliceFloor = 0;
	this.commentTotal = 0;
	this.commentPosition = 0;
	this.user = (a.user !== undefined) ? a.user: undefined;
	this.initialized = false;
	this.loading = false;
	this.reset = false;
	this.ajaxUrl = "/components/content/comment/list";
	this.canComment = false;
	this.isUserLoggedIn = function () {
		try {
			return (this.user.id > 0)
		} catch(b) {
			alert(b);
			return false
		}
	};
	this.init = function () {
		var d = this,
		g = $("<option/>").attr("id", "c-sort-desc").attr("selected", "selected").attr("value", "comment_create_date desc").html("Newest First"),
		f = $("<option/>").attr("id", "c-sort-asc").attr("value", "comment_create_date asc").html("Oldest First"),
		e = $("<option/>").attr("id", "c-sort-score").attr("value", "score_up desc, score_down asc").html("Top Rated"),
		c = $("<select/>").attr("id", "c-sort").append(g).append(f).append(e).hide(),
		b = $("<span/>").attr("id", "c-live").html("");
		c.change(function () {
			d.sort = $(this).val();
			d.resetWidgets();
			d.loadThread()
		});
		$("#c-info-panel").append(b).append(c).append($("<br />").attr("clear", "all"));
		this.user = tpc.getUser();
		this.examinePrivileges();
		d.loadThread()
	};
	this.examinePrivileges = function () {
		if (this.user.privileges) {
			for (var b = 0; b < this.user.privileges.length; b++) {
				if (this.user.privileges[b] == "comment") {
					this.canComment = true;
					return
				}
			}
		}
		this.canComment = false
	};
	this.loadThread = function () {
		var b = this;
		$.getJSON(this.buildCommentRequestUrl(), function (d) {
			b.thread = d;
			if (!b.initialized) {
				if (b.thread.threadState == "hidden") {
					return false
				}
				$("#c-container").show();
				if (b.thread.threadState == "open") {
					b.initCommentForm()
				}
				var c = tpc.param("postCode"),
				f;
				if (c) {
					f = $("<div/>").addClass("c-post-message").attr("id", "c-post-message-id");
					var e = /\d+/;
					if (c.length <= 3) {
						$.ajax({
							async: true,
							url: "/components/content/code?code=" + c + "&response=json",
							dataType: "json",
							success: function (g) {
								f.html(g.message);
								if (f.html() != "") {
									$("#c-create").after(f);
									setTimeout(function () {
										$("#c-post-message-id").hide("blind", {},
										1000)
									},
									20000)
								}
							}
						})
					} else {
						f.html(c);
						if (f.html() != "") {
							$("#c-create").after(f);
							setTimeout(function () {
								$("#c-post-message-id").hide("blind", {},
								1000)
							},
							20000)
						}
					}
				}
				b.reset = true;
				b.initialized = true
			}
			b.loadComments()
		})
	};
	this.initCommentForm = function () {
		var b = this;
		if (b.user.id > 0) {
			if (!b.user.accountVerified) {
			     $("#c-create-text").show();
				$("#c-create-text").html("You must ").append($("<a/>").attr("href", "/components/account/forgotverificationcode/" + b.user.loginName).html("validate your email address")).append(" before you can publish a comment.");
				$("#c-create-form-container").remove()
			} else {
                $('#c-create-text').remove();
                $('#c-create-form-container').show();
				$("#c-create-form").append($('<input type="hidden"/>').attr("name", "objectId").attr("value", b.objectId)).append($('<input type="hidden"/>').attr("name", "objectType").attr("value", b.objectType)).append($('<input type="hidden"/>').attr("name", "source").attr("value", b.objectSource));
				$("#c-chars").html('You have <span id="c-char-count" style="color: #0000cc">1000 characters</span> remaining.');
				$("#c-text").keyup(function (d) {
					var f = $("#c-text").val(),
					c = f.length,
					e,
					g = "#00c";
					if (c >= 1000) {
						$("#c-text").val(f.substring(0, 1000));
						$("#c-char-count").html("no more characters").css("color", "#c00")
					} else {
						e = 1000 - c;
						if (e < 200) {
							g = "#c00"
						} else {
							if (e < 400) {
								g = "#903"
							} else {
								if (e < 600) {
									g = "#606"
								} else {
									if (e < 800) {
										g = "#309"
									}
								}
							}
						}
						$("#c-char-count").html(e + " character" + ((e != 1) ? "s": "")).css("color", g)
					}
				});
				if (b.thread.moderated) {
					$("#c-text-container").append($('<div style="text-align: center; font-size: 0.9em; padding: 3px; margin: 5px 0; background-color: #ffc; border: 2px solid #fc3; color: #666; font-weight: bold;">This thread is moderated. Your comment will be reviewed before appearing online.</div>'))
				}
			}
		} else {
			$("#c-create-text").html("You must ").append($("<a/>").attr("href", "/components/account/login").html("Login")).append(" or ").append($("<a/>").attr("href", "/components/account/registration").html("Register")).append(" to publish a comment.");
			$("#c-create-form-container").remove()
		}
		$("#c-create").show()
	};
	this.loadComments = function () {
		$("#c-load-more-comments").html('<img src="/universal/images/bc-loading-video.gif" alt="Loading..." border="0" />');
		var c = this,
		b, d;
		c.commentTotal = c.thread.totalComments;
		c.sliceSize = c.thread.comments.length;
		c.sliceFloor = c.thread.start;
		$("#c-live").html((c.thread.totalComments == 1) ? "There is 1 comment": "There are " + c.thread.totalComments + " comments");
		if (c.reset) {
			$("#c-view").html($("<div/>").attr("id", "c-list"));
			$("#c-info-panel").show();
			c.reset = false
		}
		if (c.thread.comments.length >= 1) {
			b = c.thread.comments,
			d = new TPCDateFormat();
			$.each(b, function (e, f) {
				d.setTime(f.postDate);
				_icon = c.createCommentIcon(f);
				_body = c.createCommentBody(f);
				$("#c-list").append($("<div/>").attr("id", "c-" + f.id).addClass("tb-comment").append($("<a/>").attr("name", "c-" + f.id)).append(_icon).append(_body));
				c.commentPosition++
			});
			if ((c.sliceSize + c.sliceFloor) < c.commentTotal) {
				$("#c-list").append(c.createLoadMoreCommentsAnchor())
			}
			$("#c-sort").show()
		}
	};
	this.resetWidgets = function () {
		this.reset = true;
		this.commentPosition = 0;
		$("#c-view").html($("<div/>").attr("id", "c-list").html($("<div />").addClass("comments-loading").attr("align", "center").append($("<strong/>").html("Loading comments...")).append($("<br />")).append($("<img />").attr("src", "/universal/images/bc-loading-video.gif").attr("alt", "Loading...").attr("border", "0"))))
	};
	this.buildCommentRequestUrl = function () {
		var b = this.ajaxUrl + "?objectId=" + this.objectId + "&type=" + this.objectType + "&key=" + this.objectSource + "&start=" + this.commentPosition + "&sort=" + escape(this.sort);
		if (this.sliceSize > 0) {
			b += "&slice=" + this.sliceSize
		}
		return b
	};
	this.createLoadMoreCommentsAnchor = function () {
		var e = this,
		b, d, c;
		b = this.sliceFloor + this.sliceSize + 1;
		d = (b + this.sliceSize) - 1;
		if (d > this.commentTotal) {
			d = this.commentTotal
		}
		c = "Click here to load comments " + b + "-" + d + " of " + this.commentTotal;
		return $("<a/>").attr("id", "c-load-more-comments").attr("href", "javascript:void(0);").bind("click", function () {
			e.loadThread();
			$(this).fadeOut("slow")
		}).html(c)
	};
	this.createCommentIcon = function () {
		var b = $("<div />").addClass("comment-icon").append($("<img/>").attr("src", "/universal/images/woo/comment_user_32-rev.png").attr("alt", "").attr("border", "0"));
		return b
	};
	this.createCommentBody = function (i) {
		var j = this,
		f, c, h, e, g, b, d;
		f = $("<div/>").addClass("site-comment");
		c = $("<div />").addClass("comment-name");
		c.append($("<span/>").addClass((!i.user) ? "comment-anonymous": "comment-user").html((i.user) ? i.user.loginName: (i.legacyName) ? i.legacyName: "Anonymous")).append(" wrote:");
		h = $("<div/>").addClass("comment-body").html(i.body);
		e = $("<div/>").addClass("comment-footer");
		g = new TPCDateFormat();
		g.setTime(i.postDate);
		b = $("<div/>").addClass("comment-date").html(g.format("MMM dx, yyyy h:ii a"));
		d = $("<div/>").addClass("comment-abuse");
		if (j.thread.voteState == "open") {
			if (j.isUserLoggedIn()) {
				d.append($('<a class="c-thumb-vote c-thumb-up-1" />').attr("id", "c-thumb-up-" + i.id).attr("href", "javascript:void(0);").html(i.scoreUp + "").bind("click", function () {
					var k = $(this).html();
					$(this).html('<img src="/universal/images/loading-12.gif" alt="Loading" border="0" />');
					j.vote(i.id, 1, k)
				}));
				d.append($('<a class="c-thumb-vote c-thumb-down-1" />').attr("id", "c-thumb-down-" + i.id).attr("href", "javascript:void(0);").html(i.scoreDown + "").bind("click", function () {
					var k = $(this).html();
					$(this).html('<img src="/universal/images/loading-12.gif" alt="Loading" border="0" />');
					j.vote(i.id, -1, k)
				}))
			} else {
				d.append($('<span class="c-thumb-vote c-thumb-up-0" />').html(i.scoreUp + "").hover(function () {
					$(this).html('<a href="/components/account/login">Log in to vote</a>')
				},
				function () {
					$(this).html(i.scoreUp + "")
				}));
				d.append($('<span class="c-thumb-vote c-thumb-down-0" />').html(i.scoreDown + "").hover(function () {
					$(this).html('<a href="/components/account/login">Log in to vote</a>')
				},
				function () {
					$(this).html(i.scoreDown + "")
				}))
			}
		}
		if (j.thread.reportState == "open" || (j.thread.reportState == "user" && j.isUserLoggedIn())) {
			d.append($("<a/>").addClass("comment-report-abuse").html("Report Abuse").attr("href", "javascript:void(0)").attr("id", "c-abuse-" + i.id).bind("click", function () {
				$(this).html("Sending report...");
				j.reportAbuse(i.id)
			}))
		}
		if (j.user && j.user.admin == 1) {
			d.append($("<a/>").addClass("comment-admin").html("Delete").attr("href", "javascript:void(0);").attr("id", "c-abuse-" + i.id).bind("click", function () {
				j.admrm(i.id)
			}))
		}
		e.append(b);
		e.append(d);
		e.append($("<br />").attr("clear", "all"));
		f.append(c);
		f.append(h);
		f.append(e);
		return f
	};
	this.admrm = function (b) {
		$.getJSON("/components/content/comment/admrm?cid=" + b, function (c) {
			var d = (c.message) ? c.message: "No response from server. Please try again";
			$("#c-" + b).after($("<div/>").addClass("c-reported-message").attr("id", "c-report-msg-" + b).html(d));
			if (c.message) {
				$("#c-" + b).hide("slow")
			}
			setTimeout("tpcc_hideReportedMessage(" + b + ")", 5000)
		})
	};
	this.reportAbuse = function (b) {
		$.getJSON("/components/content/comment/report?cid=" + b, function (c) {
			if (!c.data) {
				var d = c.message;
				$("#c-" + b).after($("<div/>").addClass("c-reported-message").attr("id", "c-report-msg-" + b).html(d));
				$("#c-" + b).hide("slow");
				setTimeout("tpcc_hideReportedMessage(" + b + ")", 5000)
			} else {
				$("#c-" + b).after($("<div/>").addClass("c-reported-message").attr("id", "c-report-msg-" + b).html("Thank you for reporting this comment."));
				$("#c-" + b).hide("slow");
				setTimeout("tpcc_hideReportedMessage(" + b + ")", 5000)
			}
			$("#c-abuse-" + b).html("Report Abuse")
		})
	};
	this.vote = function (d, c, b) {
		$.getJSON("/components/content/comment/vote?cid=" + d + "&score=" + c, function (f) {
			var e = (c > 0) ? "#c-thumb-up-" + d: "#c-thumb-down-" + d;
			$(e).html((f.data) ? ++b: b);
			$("#c-report-msg-" + d).hide();
			$("#c-" + d).after($("<div/>").addClass("c-reported-message").attr("id", "c-report-msg-" + d).html(f.message));
			setTimeout("tpcc_hideReportedMessage(" + d + ")", 5000)
		})
	}
}
function tpcc_hideReportedMessage(a) {
	$("#c-report-msg-" + a).fadeOut("slow")
};
