function Comment() { };
Comment.lastopen = false;
Comment.data =
{
	aid: null,
	cid: null,
	text: ''
};

Comment.postcomment = function (aid)
{
	var o_comment_form_area = $('#comment_form_area_'+aid);
	var o_comment_form = $('#comment_form_'+aid);
	var url = '/json/comment.php';

	if(o_comment_form[0].comment.value.length < 4)
	{
		$.popup.inline("Comments have to be at least 4 characters.");
		return false;
	}
	$("#comment_btn_"+aid).hide();
	o_comment_form_area.html('<div style="margin-bottom: 5px;">Posting Comment...</div>');
	
	var comment_text = o_comment_form[0].getElementsByTagName("textarea");
	Comment.data =
	{
		aid: aid,
		comment: comment_text[0].value,
		text: comment_text[0].value
	};

	$.ajax({
		type: "GET",
		url: url,
		data: this.data,
		dataType: "json",
		success: Comment.postcomment_handler,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$.popup.inline("Oops, there was a problem posting your comment! Please try again");
			Comment.showbox(Comment.data.aid, Comment.data.text);
		}		
	});
	//$.getJSON(url, this.data, Comment.postcomment_handler);
}

Comment.postcomment_handler = function (obj, status)
{
	if(obj.error == 'Double Post')
	{
		$.popup.inline('The post text already exists');
		Comment.showbox(Comment.data.aid, Comment.data.text);
	}
	Comment.hidebox(obj.aid);
	Comment.drawcomments(obj.comment_list, obj.aid);
}

Comment.drawcomments = function(comment_list, id) // id is the answerid
{
	$('#comment_list_'+id).html(comment_list);
}

Comment.showbox = function (id, text)
{
	if(!text)
		text = '';
	
	if(!user.exists)
	{
		var error = '<b>Share what you know!</b><br />\
		To comment on answers, please '+ createReferrerLink('/register', '<b>sign up</b>') +'</a> for a free account or '+ createReferrerLink('/login', '<b>sign in</b>', 'return') +'.';
		$.popup.inline(error);
		return false;
	}
	else if(!user.confirmed)
	{
		$.popup.inline("You must confirm your account in order to comment.<br/><br />  Please check your email for confirmation instructions or "+ createReferrerLink('/user/request_confirmation', '<b>request another confirmation</b>'));
		return false;
	}
			
	$('#comment_btn_'+id).hide();

	var html = ''+
		'<div style="margin-left: 70px;">'+
		'<form id="comment_form_'+id+'" name="comment_form_'+id+'">'+
		'<input type="hidden" name="aid" value="'+id+'" />'+
		'<textarea name="comment" id="comment_text_'+id+'" onkeyup="textCounter(this.form.comment, 1000, \'charcounter\');"  style="width: 550px; height: 150px;" tabindex="' + id + '">'+text+'</textarea><BR>'+ 
		'<table><tr>'+ 
		'<td  style="width: 230px;" style="white-space: nowrap;">'+
		'<img src="/images/question/btn_post_comment.gif" style="cursor: pointer;" onclick="return Comment.postcomment(' + id + ')"  tabindex="' + id + '"/>'+
		'&nbsp; <img src="/images/question/btn_cancel.gif" style="cursor: pointer;" onclick="return Comment.hidebox(' + id + ')" />'+
		'</td>'+
		'<td><span class="small" style="vertical-align: top; text-align: right;"><span id="charcounter">1000</span> characters remaining</span></td>'+
		'</tr></table>'+
		'</form>'+
		'</div>'+
		'<script type="text/javascript" language="javascript">textCounter(document.comment_form_' + id + '.comment, 1000, "charcounter")</script>'+
	'';
	var o_cfa = $('#comment_form_area_'+id);
	o_cfa.html(html);


	// Hide the ads, forever!
	if($('#comments_adsense').css('display') == 'block')
	{
		$('#comments_adsense').fadeOut('normal', function(){
			o_cfa.fadeIn('normal');
			o_cfa.find('textarea[@name=comment]').focus();
		});
	} else {
		o_cfa.fadeIn('normal');
		o_cfa.find('textarea[@name=comment]').focus();
	}
}

Comment.hidebox = function (id)
{
	$('#comment_btn_'+id).fadeIn();
	$('#comment_form_area_'+id).hide();
	return false;
}

Comment.Edit = function(id, aid)
{
	// There's already a comment being edited
	if(Comment.lastopen)
	{
		Comment.show('answer_comments_'+aid, 'all'); // running into a problem here
		Comment.lastopen = false;
	}
	Comment.EditLock(id, aid);
}

Comment.EditLock = function(id, aid)
{
	var next = "Comment.EditLock(" + id + ", " + aid +")";
	
	if(!Comment.showallthreadlock) 
	{
		Comment.EditGet(id);
	}
	else
	{
		window.setTimeout(next, 100);
	}
}

// Get the comment text from the database
Comment.EditGet = function(id)
{
	var url = '/json/comment_get.php';
	$.getJSON(url, {cid: id}, Comment.EditFinish);	
}

Comment.EditFinish = function(o)
{
	var id = o.commentid;
	var aid = o.answerid;
	var o_text_container = $('#text_container_'+id); 
	var txt = o.comment;
	var frmid = "cupdate" + id;
	
	Comment.lastopen = id;
		
	// This is what appears when they edit a comment
	var html = ''+
		'<div style="position: absolute;">'+
		"<form onsubmit='return Comment.EditSave(" + id + ")' id = '" + frmid + "' name = '"+frmid+"'>" + 
		"<input type = 'hidden' name = 'cid' value = '" + id + "'> " +
		"<input type = 'hidden' name = 'aid' value = '" + aid + "'> " +
		'<textarea name="comment" style="width: 550px; height: 150px;" >' + txt + "</textarea><BR>"+
		'<button type="submit" class="submit_secondary">Save Changes</button>' +
		' &nbsp; <button type="reset" onclick="Comment.show(\'answer_comments_' + aid + '\', \'all\')" class="submit_secondary" style="color: #c00;">Cancel</button>' +
		"</form>"+
		'</div>'+
	''		
	$('#comments_adsense').hide();
	o_text_container.html(html);
	o_text_container.css('height', '180px');
	o_text_container.css('position', 'relative');
}

Comment.EditSave = function(id)
{
	var o_comment_form = $('#cupdate' + id);
	var url = '/json/comment.php';

	if(o_comment_form[0].comment.value.length < 4)
	{
		$.popup.inline("Comments have to be at least 4 characters.");
		return false;
	}
	var data =
	{
		aid: o_comment_form[0].aid.value,
		cid: o_comment_form[0].cid.value,
		comment: o_comment_form[0].comment.value
	};
	$.getJSON(url, data, Comment.postcomment_handler);

	return false;
}

Comment.show = function(id, show)
{
	
	Comment.showallthreadlock = true;
	
	var info = id.split('_');
	var aid = info[2]; // The Answer ID
	var url = '/json/comment.php?aid=' + aid + '&show=' + show;
	
	$.getJSON(url, Comment.show_handler);
}

Comment.show_handler = function(obj)
{
	
	var o = $('#answer_comments_'+obj.aid);
	var show = (obj.show == 'all') ? 'less' : 'all';

	if(o.size() > 0)
	{
		o[0].onclick = function() { Comment.show(this.id, show); }

		if(obj.show == 'all')
			o.html('show fewer comments <img src="/images/arrow_up.gif" alt="Show Fewer" height="4" />');
		else
			o.html('show all comments <img src="/images/arrow_down.gif" alt="Show More" height="4" />');
	}

	Comment.drawcomments(obj.comment_list, obj.aid);
	Comment.lastopen = false;
	Comment.showallthreadlock = false;
}

Comment.clearEdit = function(comment_id)
{
	$("#comment_"+comment_id).hide();
}

// Handles deleting of the comments for the user
Comment.Delete = function(answerid, commentid)
{
	var input_box=confirm('Are you sure you want to delete this comment?');
	if (input_box==true) 
	{ 
		var url = '/json/comment.php?aid='+answerid+'&delete=' + commentid;
		$.getJSON(url, Comment.Delete_handler);
	}
}

Comment.Delete_handler= function(obj)
{
	if(!obj.error)
		Comment.drawcomments(obj.comment_list, obj.aid);
}
