///////////////////////////////////
// AJAX COMMENT INSERTER

function submitComment() {

	if (validate()) {
	  ajaxNewComment();
	}
	else {
	  alert('Please enter your name and comment then copy \n           the text you can see in the image.');
	}	
	return false;
}

function validate(name, comment) {

	var name = $('nameField').getValue();
	var comment = $('commentField').getValue();
	var code = $('security_code').getValue();
	return (name.length !=0 && comment.length!=0 && code.length!=0);
}


function ajaxNewComment() {

	new Ajax.Request('plugins/comments/newComment.php',
	{
	
		parameters: $('newCommentForm').serialize(true),

		onLoading: function() {
			formWaiting();
		},
	
		onSuccess: function(transport){
			
			var r = transport.responseText.trim();
			
			if (r=='fail') {
				commentFailed();
				formDone();
			}
			else if (r=='captcha') {
				captchaFailed();
				formDone();
			}
			else {				
				var comment = transport.responseText;				
				addCommentToUI(comment);
			}
		},

		onFailure: function() {
			commentFailed();
			formDone();
		},

		onComplete: function() {
			formDone();
		}

	});
}

if (document.images) {
    spin = new Image(16,16);
    spin.src='plugins/comments/media/ajax-loader.gif';
    spin.id='ajaxLoading';
}


function formWaiting() {	
	$('addComment').disable();
	removeDivs();		
	$('commentForm').insert({Top : spin});	
}


function formDone() {

	$('addComment').enable();	
	$('ajaxLoading').remove();		
	newCaptcha();
}

function newCaptcha() {	
	var time = getTime();
	var code = "<img id='captcha' src='plugins/comments/php/CaptchaSecurityImages.php?width=200&height=40&characters=6&time=" + time + "' />";	
	$('captcha').replace(code);	
}

function commentFailed() {

	fail('Could not insert comment');
}

function captchaFailed() {

	fail('Incorrect security code');
}

function addCommentToUI(comment) {

	if ($('pages') == null) {
		$('comments').insert({Top : comment});

	}
	else {
		$('pages').insert({After : comment});
	}
	success('Comment added');
	$('nameField').clear();
	$('commentField').clear();
	$('security_code').clear();
	removeDiv('noComments');
}


///////////////////////////////////
// AJAX COMMENT EDITER

function commentEditToggle(commentID) {

	var tag = 'cb' + commentID;
	
	var comment = $(tag).firstDescendant();		
			
	//alert(comment.firstDescendant());
	
	if (comment.match('p')) {		
		textToTextArea(comment);		
	}
	else {		
		textareaToText(comment, commentID);
	}

}

function textareaToText(comment, commentID) {
	var text = comment.getValue();
	updateComment(commentID, text);
	comment.replace("<p>" + text + "</p>");
}

function textToTextArea(comment) {
	var text = comment.innerHTML;
	comment.replace("<textarea rows='7' cols='30'>" + text + "</textarea>");
}

function updateComment(id, data) {

	new Ajax.Request('plugins/comments/updateComment.php', {
		parameters: {id: id, comment: data}
	});

}


///////////////////////////////////
// AJAX DELETE COMMENT

function deleteComment(commentID) {

	removeDivs();

	var answer = confirm("Delete Comment?");
	if (answer) {
		ajaxDeleteComment(commentID);
	}
}

function ajaxDeleteComment(commentID) {

	new Ajax.Request('plugins/comments/deleteComment.php',
			{
				parameters: {id: commentID},

				onSuccess: function(transport){
					  var response = transport.responseText || "x";
					  if (response!="x") {
						 commentDeleted(response);
					  }
					},

				onFailure: function(){ alert('Something went wrong...') }


			});
}

function commentDeleted(cid) {
	
	var p = new String(cid);
	p = p.trim();
	var pid = "c" + p;
	$(pid).remove();
	addNoComment();		
	success('Comment Deleted');		
}

function addNoComment() {
	removeDiv('noComments');
	var t = $$('div.comment').size();
	if (t==0) {
		addNoComments();
	}
}


String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function addNoComments() {

	var code= "<div id='noComments'><p>No comments here</p></div>";
	$('comments').insert({Top : code});
}

///////////////////////////////////
// OTHER BITS

function success(msg) {
	removeDivs();
	var html = "<div id='success'><p>" + msg + "</p></div>";
	$('comments').insert({Top : html});
}

function fail(msg) {	
	removeDivs();
	var html = "<div id='fail'><p>" + msg + "</p></div>";
	$('comments').insert({Top : html});
}

function removeDiv(id) {
	if ($(id)!=null) {
		$(id).remove();
	}
}

function removeDivs() {	
	removeDiv('success');
	removeDiv('fail');
}

function getTime() {
	var d = new Date();
	return d.getTime();
}

