
var CommentsClass = new Class({

		//implements
	Implements: [Options],

	options: {
		commentBox						: 'comment'

	},

	initialize: function( options) {
		this.setOptions( options);
	},

	reply: function ( authorId, commentId) {
		var author		= $(authorId).get('html');
		//var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n|\r\n/g, "") + '</a> \n';
		var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n|\r\n/g, "") + '</a>\n\n';

		//appendReply( insertStr, this.options.commentBox);
		//alert( insertStr);
		this.appendReply(insertStr);
	},

	appendReply: function (insertStr) {

		commentBox = $(this.options.commentBox);
		if (commentBox.value.indexOf(insertStr) > -1) {
			//alert("You've already appended this reply!");
			alert( 'U heeft dit antwoord al toegevoegd!');
			return false;
		}

		if (commentBox.value.replace(/\s|\t|\n/g, "") == '') {
			commentBox.value = insertStr;
		} else {
			commentBox.value = commentBox.value.replace(/[\n]*$/g, "") + '\n\n' + insertStr;
		}

		this.exit();
	},

	quote: function ( authorId, commentId, commentBodyId) {
		// var author = MGJS.$(authorId).innerHTML;
		var author		= $(authorId).get('html');
		// var comment = MGJS.$(commentBodyId).innerHTML;
		var comment		= $(commentBodyId).get('html');

		/*
		var insertStr = '<blockquote cite="#' + commentBodyId + '">';
		insertStr += '\n<strong><a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a> :</strong>';
		insertStr += comment.replace(/\t/g, "");
		insertStr += '</blockquote>\n';
		*/
		var insertStr = '<blockquote cite="#' + commentBodyId + '">\n';
		insertStr += '<strong><a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a> :</strong>';
		insertStr += comment.replace(/\t/g, "");
		insertStr += '\n</blockquote>\n\n';

		this.insertQuote(insertStr);
	},

	insertQuote: function insertQuote(insertStr) {
		if($(this.options.commentBox) && $(this.options.commentBox).type == 'textarea') {
			commentBox = $(this.options.commentBox);

		} else {
			//alert( 'The comment box does not exist!');
			alert( 'The comment box bestaat niet!');
			return false;
		}

		commentBox.value += insertStr;
		this.exit();

	},

	exit: function() {
		//commentBox.focus();
		var _fxs1 = new Fx.Scroll( window, { 'duration': 300}).toElement( commentBox).chain(function(){

			/*+ http://snipplr.com/view/7487/set-text-cursor-to-the-beginend-of-inputtextarea/ */
			if (commentBox.createTextRange) {
				var r = commentBox.createTextRange();
				r.collapse(false);
				r.select();
			}
			commentBox.focus();
		});
	}

})



window.addEvent('domready', function() {
	Comments = new CommentsClass({'commentBox': 'comment'});
})