//window.addEvent('domready', function() {
function setup_nawte() {
	
	/*
	I need to look into this for RAM cleanup...
	http://mootools.net/docs/Element/Element#Element:destroy
	*/
	nawte_toolbar = new nawte('comment_body', 'toolbar');

	nawte_toolbar.addFunction("Bold", function(){
		var selection = this.getSelection();
		this.replaceSelection("<b>" + selection + "</b>");
	});
	
	nawte_toolbar.addFunction("Italic", function() {
		var selection = this.getSelection();
		this.replaceSelection("<i>" + selection + "</i>");
	});
	
	
	nawte_toolbar.addFunction("Code", function() {
		var selection = this.getSelection();
		this.replaceSelection("<code>" + selection + "</code>");
	});
	
	nawte_toolbar.addFunction("Quote", function() {
		var selection = this.getSelection();
		this.replaceSelection("<blockquote>" + selection + "</blockquote>");
	});
	
	nawte_toolbar.addFunction("Link", function() {
		var selection = this.getSelection();
		var response = prompt("Enter the link url.");
		if(response == null)
			return;
		this.replaceSelection("<a href=\"" + (response == '' ? 'http://link_url.com' : response) + '\">' + (selection == '' ? 'Link Text' : selection) + "</a>");
	});
	
	nawte_toolbar.addFunction("List", function() {
		var selection = this.getSelection();
		if (selection == "") {
			this.replaceSelection("<ul>\n   <li>Item 1</li>\n   <li>...</li>\n</ul>");
		} else {
		
			this.processEachLine(function(line) {
				newline = "  <li>" + line + "</li>";
				return newline;
			}, false);
			this.insertBefore("<ul>\n", false);
			this.insertAfter("\n</ul>", true);
			
		}
	});
	
	/*
	nawte_toolbar.addFunction("Table", function() {
		this.insertAfter("<table>\n  <tr>\n    <td></td>\n    <td></td>\n  </td>\n</table>", true);
	});
	*/
	
	nawte_toolbar.addFunction("Strip Tags", function() {
		var selection = this.getSelection();
		if(selection == "") {
			currentValue = this.getValue();
			currentValue = currentValue.replace(/<(.|\n)+?>/g, "");
			this.setValue(currentValue);
		} else {
			selection = selection.replace(/<(.|\n)+?>/g, "");
			this.replaceSelection(selection);
		}
	});
	
	
	//Set the comment form to post data from AJAX!
	$('comment_form').addEvent('submit', function(e) {
		
		//Prevents the default submit event from loading a new page.
		e.stop();
		
		//Makes string from elements "email=bob@bob.com&zipCode=90210".
		var comment_form_data = this.toQueryString(); 
		var comment_form_action = this.getProperty('action');
		
		//alert(comment_form_action + comment_form_data);
		
		//Send the comment form!
		send_request(comment_form_action, comment_form_data);
		
		return false;
		
	});
	
}
/*
	$('post').addEvents({
		'keydown': function(){
			updatePreview();
		},
		'change': function(){
			updatePreview();
		}
	});

});

function updatePreview() {
	$('preview').set('html', $('post').value);
}
*/
	
