$(document).ready(function(){
	// listens to either the text-link or the icon-link
	$(".comments-summary").find("a").click(function(){
		var commentsContainer = $(this).parents("li").find(".comments");
		// couldnt use slideToggle, would cause loadComments to run even on close
		if(commentsContainer.is(":hidden")){
			commentsContainer.show(300);
			loadComments(commentsContainer);
		}else{
			commentsContainer.hide(300);
		}
		
		// loads author cookie if available
		if($.cookie("author") != ""){ 
			commentsContainer.find("#author").val($.cookie("author")); 
		}
		
		// removes focus from open link
		$(this).blur(); 
	});
	
	// handles "close" link at bottom of comments div
	$(".close-comments").click(function(){
		$(this).parent(".comments").hide(300);
	});

	
	// listens for form submission
	$(".submit").click(function(){
		$(this).attr("disabled", "true"); // disable submit button
			
		// gets the "comments" div parent of the submit button
		// newer jQuery has "closest" parent function that obviates need for all the "parent()" calls
		var commentsContainer = $(this).parents(".comments");
		
		addComment(commentsContainer);

		$(this).attr("disabled", ""); // re-enable submit button
	});
	
	// toggles textarea box
	$(".copy.button").click(function(){
		$(this).next().toggle();
		this.blur();
	});
	// autoselects all text	
	$(".clipboard-box textarea").click(function(){
		this.select();
	});

});



function addComment(commentsContainer){
	$.ajax({
		type: 		"POST",
		url:	 	"index.php", 
		data: 		"mode=comments&action=add&" +
					"submissionId=" + commentsContainer.find("#submissionId").val() + "&" + 
					"author=" + commentsContainer.find("#author").val() + "&" + 
					"comment=" + commentsContainer.find("#comment").val(),
		success:	function(data){ // loop through each comment in the JSON "comments" object 
						// use data to pass error states, otherwise no data should pass
						if(data){
							alert(data);
						}else{
							// updates comment count
							var commentCount = commentsContainer.parent().find(".comment-count");
							commentCount.text(parseInt(commentCount.text())+1);
							// updates comment list, with new comment
							loadComments(commentsContainer);
							// stores author name in cookie so it can be prefilled later
							if($.cookie("author") != commentsContainer.find("#author").val()){
								$.cookie("author", commentsContainer.find("#author").val(), {expires: 30});
							}
							// disable comment form
							commentsContainer.find(".comment-form").hide(600); // hide comment form
						}
					},
		cache: "false",
		dataType: "text"
	});		
					
	return true;
}

function deleteComment(commentId){

	$.ajax({
		type: 		"POST",
		url:	 	"index.php", 
		data: 		"mode=comments&action=delete&" +
					"commentId=" + commentId + "&",
		success:	function(data){ // loop through each comment in the JSON "comments" object 
						// use data to pass error states, otherwise no data should pass
						if(data){
							alert(data);
						}else{
							var comment = $("#comment_" + commentId);
							
							// decriments comment count
							var commentCount = comment.parent().parent().parent().find(".comment-count");
							commentCount.text(parseInt(commentCount.text())-1);
							
							// hide newly deleted comment
							comment.hide(100);
						}
					},
		cache: "false",
		dataType: "text"
	});		
}

// handles the loading / refreshing of comments
function loadComments(commentsContainer){
	// update "lastUpdated" field
	$.ajax({
		type: 		"POST",
		url:	 	"index.php", 
		data: 		"mode=comments&action=get&" +
					"submissionId=" + commentsContainer.find("#submissionId").val() + "&" + 
					"lastUpdated=" + commentsContainer.find("#lastUpdated").text() + "&" +
					"rand=" + new Date().getTime() ,
		success:	function(data){ // loop through each comment in the JSON "comments" object 
						$.each(data.comments, function(i, comment){
							appendComment(commentsContainer, comment);
						});
					},
		cache: "false",
		dataType: "json"
	});
	commentsContainer.find("#lastUpdated").text(getDateAsMySQLTimestamp());					
}		

// append a comment to a comment-list in a commentContainer
function appendComment(commentsContainer, comment){
	// starts hidden then is animated for smoother appearance
	var commentHTML = "<li style='display:none' id='comment_" + comment.comment_id + "'>";

	// if admin cookie set, show delete
	if($.cookie("admin")){ 
		commentHTML += "<a class='delete button' title='Delete Comment' href='#' onClick='deleteComment(" + comment.comment_id + "); return false;'>Delete</a>";
	}

	commentHTML += "<b>" + comment.author + ":</b> " + comment.comment + "</li>";
	commentsContainer.find(".comment-list").append(commentHTML);
	commentsContainer.find("#comment_" + comment.comment_id).show(100); 
}


function getDateAsMySQLTimestamp(){
	var d = new Date();
	return(d.getFullYear() + "-" + parseInt(d.getMonth()+1) + "-" + d.getDate() + " " + parseInt(d.getHours()-1) + ":" + d.getMinutes() + ":" + d.getSeconds());
}

function confirmDelete(title, destination){
	var answer = confirm("Are you sure you want to delete ''" + title + "''");
	if(answer){
		window.location=destination;
	}
}



