/*
	Comments section Javascript
*/


//
// Adding Comments:
//

function addComment()
{	
	if($('commentBody').value != '') {
	
		// Set the spinner going:
		$('commentAjaxResponse').style.display = 'block';
	
		// Go ahead and send the ajax call:
		new Ajax.Request('/fatcontroller/comments/add', {
			method: 'post',
			parameters: $('commentsForm').serialize(true),
			
			onSuccess: function(transport) {
				var response = transport.responseText.evalJSON();

				if(response['status'] == 0) {
					$('commentAjaxLoader').style.display = 'block';
					$('commentAjaxText').innerHTML = 'Sorry, something\'s gone a bit wrong. Try again later.';
				} else {
					$('commentBody').value = '';
					$('commentAjaxResponse').style.display = 'none';
					$('commentlist').insert({'bottom': response['payload']});
					
					// Setup the event listener on this as well:
					var iid = 'comment' + response['insert_id'];
					var i_id = 'comment_' + response['insert_id'];
					$(i_id).observe('click', confirmCommentDelete);
					$(iid).scrollTo();
					
					// And increment the count:
					var count = $('commentCount').innerHTML;
					
					if(count == 0) {
						// Remove the fillerText:
						$('fillerText').hide();
					}
					
					count ++;
					$('commentCount').innerHTML = count;
					try {
						$('commentSectionLink').innerHTML = count + ' comments';
					} catch(e) {}
					
				}
			}
		});
	}
}


//
// Deleting Comments:
//

var commentToDelete = false;

// Loop through and grab all of the comment link deletion buttons:
var deleteButtons = $$('#comments .commentDeleteLink');

for(var i = 0; i < deleteButtons.length; i ++) {
	// Attach the event listener:
	var element = deleteButtons[i];
	element.observe('click', confirmCommentDelete);
}


function confirmCommentDelete(event) {
	if(commentToDelete != false) {
		alert('Please deal with the current confirmation window');
		return false;
	}
	
	var element = Event.element(event);
	var viewport = document.viewport.getDimensions();
	var panel = $('commentConfirm').getDimensions();
	var top, left;
	
	// Get the id:
	var id = element.id;
	commentToDelete = id.replace(/comment_/i, '');
	
	top = (viewport['height'] / 2) - (panel['height'] / 2);
	left = (viewport['width'] / 2) - (panel['width'] / 2);
	
	$('commentConfirm').style.position = 'fixed';
	$('commentConfirm').style.top = top + 'px';
	$('commentConfirm').style.left = left + 'px';
	$('commentConfirm').style.width = panel['width'] + 'px';
	$('commentConfirm').style.height = 'auto';
	
	// Display it:
	Effect.Appear('commentConfirm');
	
	// Events:
	$('commentDeleteConfirm').observe('click', deleteComment);
	
	$('commentDeleteCancel').observe('click', function(e) {
	
		// They clicked cancel:
		commentToDelete = false;
		$('commentConfirmLoading').innerHTML = '';
		Effect.Fade('commentConfirm');
	
	});	
}


function deleteComment() {
	// Requires commentToDelete to be set:
	if(commentToDelete == false || isNaN(commentToDelete)) {
		return false;
	}
	
	// Chuck in the loading text:
	$('commentConfirmLoading').innerHTML = 'please wait...';
	
	//var str = "comment_id=" + commentToDelete;
	
	// Setup the Ajax call:
	var url = '/fatcontroller/comments/delete';
	new Ajax.Request(url, {
		method: 'post',
		parameters: "comment_id=" + commentToDelete,
		onSuccess: function(transport) {
			if(transport.responseText == '1') {
				$('commentConfirmLoading').innerHTML = '';
				Effect.Fade('commentConfirm');
				
				// Remove the element:
				var id = 'comment' + commentToDelete;
				Effect.Fade(id);
				commentToDelete = false;
				
				// And now decrement the comments:
				var count = $('commentCount').innerHTML;
				count--;
				
				if(count == 0) {
					$('fillerText').style.display = 'block';
				}
				
				$('commentCount').innerHTML = count;
				try {
					$('commentSectionLink').innerHTML = count + ' comments';
				} catch(e) {}
				
			} else {
				$('commentConfirmLoading').innerHTML = 'an error occurred. please try again.';
			}
		}
	});	
}


try {
	$('commentsForm').observe('submit', function(event) {
		event.stop();
		addComment();
	});
} catch(e) {}
