//=====================================================================
// Comment object
// represent a comment in the thread
//=====================================================================

function Comment() {
	this.tagsHTML = "";
	this.tags = [];
	this.geoTags = [];
}

//=====================================================================
// public methods
//=====================================================================


// get the body of the comment
Comment.prototype.getBody = function () { 
	return this.body; 
};

//add a tag to the comment
Comment.prototype.addTag = function (tag) {
	keyword = tag.getKeyword();
	this.tags[keyword] = tag;
};

//return a tag by keyword
Comment.prototype.getTagByKeyword = function (keyword) {
	return this.tags[keyword];
}

//remove a tag from the comment
Comment.prototype.removeTag = function (tag) {
	keyword = tag.getKeyword();
	delete this.tags[keyword];
};

//add a geotag to the comment
Comment.prototype.addGeotag = function (geotag) {
	keyword = geotag.getKeyword();
	this.geoTags[keyword] = geotag;
};

//remove a geotag from the comment
Comment.prototype.removeGeotag = function (geotag) {
	keyword = geotag.getKeyword();
	delete this.geoTags[keyword];
};

// set the body of block
Comment.prototype.setBody = function (body) {
	this.body = body;
};

// set the date the comment was posted
Comment.prototype.setDatePosted = function (date) {
	this.datePosted = date;
};

// get the date the comment was posted
Comment.prototype.getDatePosted = function () {
	return this.datePosted;
};

// get the type of block
Comment.prototype.getCommentBody = function () {
	return this.commentBody;
};

// set the type of block
Comment.prototype.setCommentBody = function (commentBody) {
	this.commentBody = commentBody;
};

// set the type of block
Comment.prototype.setEditBody = function (editBody) {
	this.editBody = editBody;
};


Comment.prototype.setPostActions = function (postActions) {
	this.postActions = postActions;
};

Comment.prototype.setTagsHTML = function (tags) {
	this.tagsHTML = tags;
};


Comment.prototype.setTags = function (tags) {
	this.tags = tags;
};

Comment.prototype.getTags = function () {
	return this.tags;
};

Comment.prototype.setGeotags = function (geotags) {
	this.geoTags = geotags;
};

Comment.prototype.getGeotags = function () {
	return this.geoTags;
};

Comment.prototype.setUsername = function (username) {
	this.username = username;
};

Comment.prototype.setId = function (id) {
	this.id = id;
};

Comment.prototype.getId = function (id) {
	return this.id;
};

Comment.prototype.setUsernameFontSize = function (fontSize) {
	this.usernameFontSize = fontSize;
};

Comment.prototype.setDate = function (date) {
	this.date = date;
};

Comment.prototype.setParentId = function (pid) {
	this.parentId = pid;
};

Comment.prototype.appendToElement = function (element, id) {
	var theCommentDiv = document.createElement('div');
	theCommentDiv.className = "comment";
	theCommentDiv.id = "comment_"+id;

	var theDetailsDiv = document.createElement('div');
	theDetailsDiv.className = "userDetails";
	theDetailsDiv.innerHTML = "<div style='clear: both; font-size: "+this.usernameFontSize+"px; font-weight: bold;'>"+this.username+"</div><div class='userSays' style='height:"+this.usernameFontSize+"px;'><div style='position:absolute;bottom:0px;'>says:</div></div>";
	
	var thePostContentDiv = document.createElement('div');
	thePostContentDiv.className = "postContent";
	
	theCommentSectionDiv = element;
	
	theCommentDateSpan = document.createElement('span');
	theCommentDateSpan.id = "postDate";
	theCommentDateSpan.innerHTML = this.date;
	
	theCommentBodyDiv = document.createElement('div');
	theCommentBodyDiv.id = "commentBody_"+id;
	theCommentBodyDiv.className = "commentBody";
	theCommentBodyDiv.innerHTML = this.commentBody;

	theCommentEditBodyDiv = document.createElement('div');
	theCommentEditBodyDiv.id = "commentEditBody_"+id;
	theCommentEditBodyDiv.innerHTML = this.editBody;

	theCommentTagDiv = document.createElement('div');
	theCommentTagDiv.className = "tags";
	theCommentTagDiv.id = "commentTags_"+id;	
	theCommentTagDiv.innerHTML = this.tagsHTML;
	
	theBodyDiv = document.createElement('div');
	theBodyDiv.id = "body_"+id;
	theBodyDiv.className = "body";
	
	thePostActionsDiv = document.createElement('div');
	thePostActionsDiv.id = "postActions_"+id;
	thePostActionsDiv.innerHTML = this.postActions;

	thePostContentDiv.appendChild(theDetailsDiv);	
	thePostContentDiv.appendChild(theCommentDateSpan);
	thePostContentDiv.appendChild(theBodyDiv);
	theBodyDiv.appendChild(theCommentBodyDiv);
	theBodyDiv.appendChild(theCommentEditBodyDiv);	
	theBodyDiv.appendChild(theCommentTagDiv);	
	thePostContentDiv.appendChild(thePostActionsDiv);	
	

	theCommentDiv.appendChild(thePostContentDiv);

	theCommentSectionDiv.appendChild(theCommentDiv);
	
};


//=====================================================================
// non member methods
//=====================================================================

var commentsSection = "reply_comment";
			
function createNewComment(request,body,newTags,newGeotags,username,thread) {
	var theComment = thread.getCommentById("reply");

	var commentResults = request.responseText.evalJSON(true);

	if(commentResults[0].success === true) {

		var newCommentId = commentResults[1].answer[0].id;
			
		theComment.setId(newCommentId);
	
		thread.addComment(theComment);
		body = stripHTML(body);
		var bodyHTML = addURLLinks(body);
		bodyHTML = bodyHTML.replace(/\r\n|\r|\n/g,'<br/>');

		var commentsSectionDiv = $(commentsSection);
		
		var theCommentDiv = new Element('div', { 'class': 'comment', 'id': 'comment_'+newCommentId});

		commentsSectionDiv.insert({'before': theCommentDiv});

		var thePostContentDiv = new Element('div', {'class': 'post_content'});
		
		theCommentDiv.insert(thePostContentDiv);

		var theBylineDiv = new Element('div', { 'class': 'byline'}).update("<div style='clear: both; font-size: "+commentResults[2].usernameFontSize+"px; font-weight: bold;'>"+username+"</div><div class='userSays' style='height:"+commentResults[2].usernameFontSize+"px;'>&nbsp;</div>");
		var theCommentDateSpan = new Element('div', {'id': 'postDate'}).update("a few seconds ago...");

		theBylineDiv.insert(theCommentDateSpan);
		thePostContentDiv.insert(theBylineDiv);


		var theBodyDiv = new Element('div', {'class': 'body', 'id': 'body_'+newCommentId});
												
		thePostContentDiv.insert(theBodyDiv);
		var theCommentBodyDiv = new Element('div', {'class': 'commentBody', 'id': 'commentBody_'+newCommentId}).update(bodyHTML);
												
		theBodyDiv.insert(theCommentBodyDiv);
		theBodyDiv.insert("<div id=\"editBox_"+newCommentId+"\" style=\"display:none;\">" +
								"<div id=\"editResult_"+newCommentId+"\"></div>" +
									"<div id=\"editPost_"+newCommentId+"\">" +
										"<form onsubmit=\"new Ajax.Request('/discussion/commentEdit', {asynchronous:true, evalScripts:false, onComplete:function (request, json){updateBody(request,"+newCommentId+",theThread)}, parameters:Form.serialize(this)}); return false;\" action=\"/discussion/commentEdit\" method=\"post\">			<input type=\"hidden\" name=\"commentId\" id=\"commentId\" value=\""+newCommentId+"\" />"+
											"<div id=\"newCommentBody_"+newCommentId+"\" class=\"commentBody\"><textarea name=\"commentBody\" id=\"editBody_"+newCommentId+"\" style=\"border: 1px solid black;\" rows=\"5\" cols=\"70\">"+body+"</textarea></div>"+
											"<input type=\"hidden\" value=\""+thread.getTitle()+"\" id=\"title\" class=\"edit_title\" name=\"title\"/>"+
											"<input type=\"hidden\" value=\"\" id=\"commentTags_"+newCommentId+"\" name=\"commentTags_"+newCommentId+"\"/><input type=\"submit\" name=\"commit\" value=\"Save\" class=\"action button\" />"+
											"<a onClick='$(\"commentBody_"+newCommentId+"\").style.display=\"block\";$(\"editBox_"+newCommentId+"\").style.display=\"none\";$(\"editLink_"+newCommentId+"\").style.display=\"inline\";$(\"replyLink_"+newCommentId+"\").style.display=\"inline\";return false;' class=\"action\" >cancel</a>"+
										"</form>"+
									"</div>"+
								"</div>"+
							"</div>");
							
		var postActions = "<a href=\"javascript:void(0);\" class=\"replyLink\" onClick=\"moveToReply("+newCommentId+",theThread)\" id=\"replyLink_"+newCommentId+"\">Reply</a>"+
							"<div id=\"editLink_"+newCommentId+"\" class=\"editLink\">&nbsp;|&nbsp;<a href=\"javascript:;\" onClick=\"$('commentBody_"+newCommentId+"').style.display='none';$('editBox_"+newCommentId+"').style.display='block';$('editLink_"+newCommentId+"').style.display='none';$('replyLink_"+newCommentId+"').style.display='none';$('editBody_"+newCommentId+"').focus()\">Edit</a></div>	<div class=\"editTime\"><span id=\"editTimeValue_"+newCommentId+"\" style=\"display:none;\">600000</span><span id=\"editTime_"+newCommentId+"\">(10 minutes left)</span></div>";


		var thePostActionsDiv = new Element('div', { 'class': 'post_actions', 'id': 'postActions'}).update(postActions);

		thePostContentDiv.insert(thePostActionsDiv);		

		document.fire("comment:created");
		setInterval ("updateEditTime("+newCommentId+")", 1000 );	

		new TextAreaResize($('editBody_'+newCommentId));
		
	}
	
}


