/**
 * Modified - add support for latest mootools build
 */
Element.extend( {
   getSelectedText : function() 
   {
      if(window.ie) 
      {
        return document.selection.createRange().text;
      }
      else
      {
        return this.value.substring(this.selectionStart, this.selectionEnd);
      } 
   },
   replaceSelectedText : function(newtext, isLast) 
   {
      var isLast = (isLast == null) ? true : isLast;
      var scroll_top = this.scrollTop; 
      
      if(window.ie) 
      {
         this.focus(); 
         var range = document.selection.createRange(); 
         range.text = newtext;
          
         if(isLast) 
         {
            range.select(); this.scrollTop = scroll_top; 
         }
      }
      else {
         originalStart = this.selectionStart;
         originalEnd = this.selectionEnd;
         this.value = this.value.substring(0, originalStart) + newtext + this.value.substring(originalEnd); 
         if(isLast == false) 
         {
            this.setSelectionRange(originalStart, originalStart + newtext.length); 
         }
         else 
         {
            this.setSelectionRange(originalStart + newtext.length, originalStart + newtext.length); this.scrollTop = scroll_top; 
         }
         this.focus(); 
      }
   }
}
); 
var nawte = new Class( {
   initialize : function(element, list, options) 
   {
      this.el = $(element); 
      this.options = Object.extend( {
         dispatchChangeEvent : false, changeEventDelay : 200, interceptTabs : true
      }, options || {});
      if(this.options.dispatchChangeEvent) {
         this.el.addEvents( 
         {
            'focus':function() 
            {   
              this.timer = this.watchChange.periodical(this.options.changeEventDelay, this); 
            }
            .bind(this),
            'blur':function() 
            {
               this.timer = $clear(this.timer); 
            }
            .bind(this)
         }
         );
      }
      if(this.options.interceptTabs) {
         this.el.addEvent('keypress', function(event) {
            if(event.keyCode == 9) {
               event.preventDefault(); this.replaceSelection("\t"); }
            }
         .bind(this)); }
      if(!$defined(list) || list == "") {
         list = new Element('li'); list.injectBefore(this.el); this.list = list; }
      else {
         this.list = $(list); }
      this.oldContent = this.el.value; }
   , watchChange : function() {
      if(this.oldContent != this.el.value) {
         this.oldContent = this.el.value; this.el.fireEvent('change'); }
      }
   , getSelection : function() 
   {
      return this.el.getSelectedText(); 
   }
   , wrapSelection : function(wrapper, isLast) {
      var isLast = (isLast == null) ? true : isLast; this.el.replaceSelectedText(wrapper + this.el.getSelectedText() + wrapper, isLast); }
   , insertBefore : function(insertText, isLast) {
      var isLast = (isLast == null) ? true : isLast; this.el.replaceSelectedText(insertText + this.el.getSelectedText(), isLast); }
   , insertAfter : function(insertText, isLast) {
      var isLast = (isLast == null) ? true : isLast; this.el.replaceSelectedText(this.el.getSelectedText() + insertText, isLast); }
   , replaceSelection : function(newText, isLast) 
   {
      var isLast = (isLast == null) ? true : isLast; this.el.replaceSelectedText(newText, isLast); 
   }
   , processEachLine : function(callback, isLast) {
      var isLast = (isLast == null) ? true : isLast; var lines = this.el.getSelectedText().split("\n"); var newlines = []; lines.each(function(line) {
         if(line != "")newlines.push(callback.attempt(line, this)); elsenewlines.push(""); }
      .bind(this)); this.el.replaceSelectedText(newlines.join("\n"), isLast); }
   , getValue : function() {
      return this.el.value; }
   , setValue : function(text) {
      this.el.setProperty('value', text); this.el.focus(); }
   , addFunction : function(name, callback, args) {
      var item = new Element('li');
       
      var itemlink = new Element('a', {
         'events': {
            'click':function(e) 
            {
              new Event(e).stop(); 
               callback.attempt(null, this); 
            }
            .bind(this)
         }
         , 'href':'#'
      });
      
      itemlink.setHTML('<span>' + name + '</span>');
      itemlink.setProperties(args || {    });
      itemlink.injectInside(item); 
      item.injectInside(this.list); 
     }
   }
); 


