/*

$Id: articleCommentManager.js 5700 2008-10-01 15:35:52Z warren.chin $

(c)2006 - 2007 The New York Times Company  */

// REQUIRES: /js/article/comments/crnrXHR.js

function commentCount() {
	this.div = "commentCount";
        this.container = null;
        this.data = null;
        return this;
}

commentCount.prototype.write = function(){
	this.container = (document.getElementById) ? document.getElementById(this.div) : null;
        if ((document.createTextNode) && (this.container != null) && (this.data != null)){
                var num = (this.data.response.commentCount) ? this.data.response.commentCount : 0;
                var output = ((!isNaN(num)) && (num > 0)) ? "(" + num + ")" : "";
                this.container.appendChild(document.createTextNode(output));
        }
}

function readerComment() {
	this.div = "readerscomment";
	this.container = null;
	this.data = null;
	return this;
}

readerComment.prototype.write = function(){
	this.container = (document.getElementById) ? document.getElementById(this.div) : null;
	if ((document.createElement) && (this.container != null) && (this.data != null) && (this.countData != null)) {
		var r = this.data.response;
		var numComments = (this.countData.response.commentCount && this.countData.response.commentCount > 0) ? "("+this.countData.response.commentCount+")" : "";
		if ((r.excerpt) || (r.question) || r.statusCode == "1001") {
			var hasExcerpt = (r.hasExcerpt == "true");
			var url = "http://" + r.communityHost + "/article/comments" + document.location.pathname;
			var h = "<h3>Readers\' Comments</h3>";
			h += "<div class=\"content\">";
			h += "<blockquote>"
			if (hasExcerpt) {
			    h += "\"" + r.excerpt.body + "\"";
			} else if (r.question && r.statusCode != "1001") {
			    h += r.question.body;
			} else {
			    h += 'Readers shared their thoughts on this article.';
			}
			h += "</blockquote>";

			if (hasExcerpt){ 
				var user = r.excerpt.user;
				if ((user.userDisplayName.length > 0) && (user.userLocation.length > 0)) {
					h += "<cite>" + user.userDisplayName + ", " + user.userLocation + "</cite>";
				}
			}
			
			h += "<ul class=\"more\">";
			
			if (hasExcerpt) {
				h += "<li><a href=\"" + url + "?permid="+r.excerpt.commentSequence+"#comment" + r.excerpt.commentSequence + "\" ";
				h += "onclick=\"dcsMultiTrack('DCS.dcssip','www.nytimes.com','DCS.dcsuri','/article comments/view-promo2.html','WT.ti','Article Comments View Promo2','WT.z_aca','Promo2-View','WT.gcom','Com')\">";
				h += "Read Full Comment " + String.fromCharCode(187) + "</a></li>";
				if (r.canSubmit == "true") {
					h += "<li><a href=\"" + url + "#postComment" + "\" ";
					h += "onclick=\"dcsMultiTrack('DCS.dcssip','www.nytimes.com','DCS.dcsuri','/article comments/post-promo2.html','WT.ti','Article Comments Post Promo2','WT.z_aca','Promo2-Post','WT.gcom','Com')\">";
					h += "Post a Comment " + String.fromCharCode(187) + "</a></li>";
				}
			} else {
				if (r.canSubmit == "true") {
					h += "<li><a href=\"" + url + "#postComment" + "\" ";
					h += "onclick=\"dcsMultiTrack('DCS.dcssip','www.nytimes.com','DCS.dcsuri','/article comments/post-promo2.html','WT.ti','Article Comments Post Promo2','WT.z_aca','Promo2-Post','WT.gcom','Com')\">";
					h += "Post a Comment " + String.fromCharCode(187) + "</a></li>";
					if (this.countData.response.commentCount > 0) {
						h += "<li><a href=\"" + url + "\" ";
						h += "onclick=\"dcsMultiTrack('DCS.dcssip','www.nytimes.com','DCS.dcsuri','/article comments/view-promo3.html','WT.ti','Article Comments View Promo3','WT.z_aca','Promo3-View','WT.gcom','Com')\">";
						h += "Read All Comments "+numComments+" " + String.fromCharCode(187) + "</a></li>";
					}
				} else {
					h += "<li><a href=\"" + url + "\" ";
					h += "onclick=\"dcsMultiTrack('DCS.dcssip','www.nytimes.com','DCS.dcsuri','/article comments/view-promo3.html','WT.ti','Article Comments View Promo3','WT.z_aca','Promo3-View','WT.gcom','Com')\">";
					h += "Read All Comments "+numComments+" " + String.fromCharCode(187) + "</a></li>";
				}
			}
			h += "</ul></div>";
			this.container.innerHTML = h;
		}
	}
}

function articleCommentManager(){
	//create helper objects to render content
	this.cc = new commentCount();
    this.rc = new readerComment();

	//create XMLHttpRequest object
	this.xhr = new crnrXHR();
	this.id = getMetaTagValue("articleid");
	var encodedJSON = encodeURIComponent('{"getPromo":{"request":{"vendorID": "' + this.id + '", "source":"nytd-cms","requestType":"getPromo"},"response":{}},"commentCount":{"request":{ "vendorID":"' + this.id + '", "source":"nytd-cms","requestType":"commentCount"},"response":{}}}');
	this.xhr.requestData = "requestData=" + encodedJSON;
	myArticleCommentManager = this;
	this.xhr.callbacks.push(function() {myArticleCommentManager.update()});
        this.xhr.send();
	
	return this; 
}

articleCommentManager.prototype.update = function() {
	this.cc.data = (this.xhr.responseData.commentCount) ? this.xhr.responseData.commentCount : null;
	this.rc.data = (this.xhr.responseData.getPromo) ? this.xhr.responseData.getPromo : null;
	this.rc.countData = (this.xhr.responseData.commentCount) ? this.xhr.responseData.commentCount : null;
	if ((this.cc.container != null) && (this.rc.container != null)) {
		this.rc.write();
		this.cc.write();
	}
}

var acm = new articleCommentManager();

