var wordMooEditable = new Class({
	Implements: [Options, Events],

	options: {
		textarea: null,
		actions : 'bold italic underline | createlink unlink',
		// Define all authorized tags and its attributes
		// ex.: we authorize 'a' tag and its attribute 'href'
		authTag: [ 
		          {name : 'p', attr: null},
		          {name : 'a', attr: ['href']},
		          {name : 'strong', attr: null},
		          {name : 'span', attr: null},
		          {name : 'b', attr: null},
		          {name : 'em', attr: null},
		          {name : 'u', attr: null},
				  {name : 'ul', attr: null},
				  {name : 'li', attr: null},
				  {name : 'ol', attr: null},
				  {name : 'blockquote', attr: null},
				  {name : 'hr', attr: null},
				  {name : 'head', attr: null},
				  {name : 'font', attr:['color','size']},
				  {name : 'meta', attr: null},
				  {name : 'link', attr: null},
				  {name : 'style', attr: null}
		         ]
	},
	
	mooEditable: null,
	clean: false,

	initialize: function(options) {
		this.setOptions(options);
		
		this.mooEditable = new MooEditable(this.options.textarea, {
			actions: this.options.actions,
			paragraphise: true,
			rootElement: null,
			onEditorKeyUp: this.manageEditorKeyUp.bind(this),
			onEditorPaste: function(e) {
				// When a user paste some text, we set 'clean' property to 'true' 
				// to indicate that we have to cleanup our MooEditable content
				this.clean = true;
			}.bind(this)
		});
	},
	
	manageEditorKeyUp: function(e) {
		// When a user press a key on his keyboard, if 'clean' property is true,
		// we call 'cleanup' function and set 'clean' to false to indicate that 
		// our MooEditable content has been cleaned
		if (this.clean) {
			this.cleanup();
			this.clean = false;
		}
	},
	
	cleanup: function() {
		// To clean up our pasted text, we will check for each tag if it's authorized or not.
		var content = this.mooEditable.getContent().sanitiseWord();
		 /*content = content.replace(/<[^>]*>/g, function(match) {
			var t = '';
			// We chech if current tag is in the authorized tag list. If it's not, current tag
			// is replace with an empty string
			this.options.authTag.each(function(tag) {
				if (match.contains('<' + tag.name)) {
					if (tag.attr != null && match.contains(tag.attr)) {
						t = match;
					} else {
						t = '<' + tag.name + '>';
					}
				} else if (match.contains('</' + tag.name)) {
					t = match;
				}
			});
			return t;
		}.bind(this));*/
		// Finally, we set the cleaned up content to our MooEditable
		this.mooEditable.setContent(this.mooEditable.cleanup(content));
	}
});


