
var addBoxOpen = false;

function createCommentDiv(commentID, location, username, message, date, replies)
{
	comment = '<div class="comment">'
	comment += '<div class="commentTitle"><a href="index.php?id=' + commentID + '">' + username + ' : ' + commentID + '</a></div>'
	comment += '<div class="commentBody">' + message + '</div>'
	comment += '<div class="commentLinks"> (' + replies + ') children ['
	comment += '<a href="#" onclick="showComments(' + commentID + ", '" + location + '\');return false">show</a> | '
	comment += '<a href="#" onclick="hideComments(\'' + location + '\');return false">hide</a>] - '
	comment += '<a href="#" onClick="showAddBox(\'' + location + '\', \'' + commentID + '\');return false">add comment</a></div>'
	comment += '<div class="commentGroup" id="' + location + '"></div>'
	comment += '</div>'
	
	return comment;
}

function showComments(commentid, location)
{
	var appendTo = '#' + location
	var commentLoad= '#commentLoaded'+ location;
	
	if ($(appendTo).is(":hidden")) {
		$(appendTo).slideDown("slow");
	}

	//if there are no children, ajax call hasn't happened
	var children = $(appendTo).children("div");
	
	//won't show if addbox is open, box must be closed
	// - pop-up?
	
	if (children.length <= 0)
	{
		//add default child to show AJAX call happened
		$(appendTo).append('<div id="commentLoaded' + location + '">loaded</div>');
	
		var sendID = commentid;
		$.getJSON("comments.php",{id: sendID, ajax: 'true'}, function(j){

			for (var i = 0; i < j.length; i++) {
				var currentID = location + '_' + i;
				var comment = createCommentDiv(j[i].commentID, currentID, j[i].username, j[i].message, j[i].date, j[i].replies);
				$(appendTo).append(comment);
			}
		
		})
	}
	
	$(commentLoad).html("");
}

function showComment(commentid, location)
{
	var appendTo = '#' + location
	
	if ($(appendTo).is(":hidden")) {
		$(appendTo).slideDown("slow");
	}
	
	else
	{

		//if there are no children, ajax call hasn't happened
		var children = $(appendTo).children("div");
		
		if (children.length <= 0)
		{
			var sendID = commentid;
			$.getJSON("comments.php",{comment: sendID}, function(j){
				if (j.length > 0)
				{
					var currentID = location + '_' + 0;
					var comment = createCommentDiv(j[0].commentID, currentID, j[0].username, j[0].message, j[0].date, j[0].replies);
					$(appendTo).append(comment);
				}
				
			
			})
		}
	
	}
}

function showCommentTree(commentid, location)
{
	var appendTo = '#' + location
	
	if ($(appendTo).is(":hidden")) {
		$(appendTo).slideDown("slow");
	}
	
	else
	{

		//if there are no children, ajax call hasn't happened
		var children = $(appendTo).children("div");
		
		if (children.length <= 0)
		{
			var sendID = commentid;
			$.getJSON("comments.php",{comment: sendID}, function(j){
				if (j.length > 0)
				{
					var currentID = location + '_' + 0;
					var comment = createCommentDiv(j[0].commentID, currentID, j[0].username, j[0].message, j[0].date, j[0].replies);
					$(appendTo).append(comment);
				}
				
			
			})
		}
	
	}
}

function hideComments(location)
{
	var commentToHide = '#' + location
	if (!$(commentToHide).is(":hidden")) { //+ ":first" ?
		$(commentToHide).slideUp();
	}

}

function showAddBox(location, parentid)
{
	//check if an add box is already open
	if ( $('.addBox').length )
	{
		//alert('addbox already open');
		var openNew = confirm("abandon current comment?");
		if(openNew)
			$(".addBox").remove();
		else
			return false;
	}

	var appendTo = '#' + location
	
	//show if hidden
	if ($(appendTo).is(":hidden")) {
		$(appendTo).slideDown("slow");
	}
	
	var comment = '';
	
	comment += '<div class="addBox" id="' + location + '_add">'
	comment += '<form id="' + location + '_addform">';
	comment += '<div class="commentTitle">Add Comment:</div>'
	comment += '<div class="addError" id="' + location + '_add_error"></div>'
	comment += '<div class="commentBody">Name:<br /><input name="username" id="' + location + '_addusername" type="text" /><br />'
	comment += 'Comment:<br /><textarea name="message" id="' + location + '_addmessage" cols="30" rows="6"></textarea><br />'
	comment += '</div>';
	comment += '<div class="commentLinks"> <input type="button" onClick="addComment(\'' + location + '\', \'' + parentid + '\')" value="Add Comment"></input>';
	comment += '<input type="button" onClick="hideAddBox(\'' + location + '\')" value="Cancel"></input></div>';
	comment += '</form></div>';
	$(appendTo).prepend(comment);
	
	//scroll screen to top?
	$('.addBox').focus();

}

function hideAddBox(location)
{
	var boxToRemove = '#' + location + "_add";
	$(boxToRemove).remove();
}

function addComment(location, parent)
{
	var appendTo = $('#' + location);
	var errorWrite = $('#' + location + '_add_error');
	
	var username = $('#' + location + "_addusername").val();
	var message = $('#' + location + "_addmessage").val();
	
	if(message.length > 0)
	{
		$.getJSON("add.php",{id: parent, username: username, message: message, parent: parent, ajax: 'true'}, function(j){
			//if comments returned remove add
			if( j.length > 0)
			{
				//error returns a comment with id: -1
				if(j[0].commentID > -1)
				{

					//remove  all comments and the add box to load new one
					$(appendTo).children().remove();
					
					for (var i = 0; i < j.length; i++) {
						var currentID = location + '_' + i;
						var comment = createCommentDiv(j[i].commentID, currentID, j[i].username, j[i].message, j[i].date, j[i].replies);
						$(appendTo).append(comment);
					}
				
				}
				else
				{
					alert("server says: You must enter a message");
				}
				
			}
			else
			{
				alert("nothing returned from server... what the hell did you do?");	
			}
		})
	}
	else
	{
		//alert("You must enter a message.");
		errorWrite.html("You must enter a message.");
	}
}
