/* 
////////////////////////////////////////////////////////////////////////
// ProtoEdit                                                          //
// v. 2.01                                                            //
////////////////////////////////////////////////////////////////////////

For license see LICENSE.TXT
*/
var isDOM = document.getElementById //DOM1 browser 
var isO   = isO5 = window.opera && isDOM; //Opera 5+
var isO6  = isO && window.print //Opera 6+
var isO7  = isO && document.readyState //Opera 7+
var isO8  = isO && document.createProcessingInstruction && (new XMLHttpRequest()).getAllResponseHeaders //Opera 8+
var isIE  = document.all && document.all.item && !isO //Microsoft Internet Explorer 4+
var isIE5 = isIE && isDOM //MSIE 5+
var isMZ  = isDOM && (navigator.appName=="Netscape")
var ua = navigator.userAgent.toLowerCase();
var isSafari = (ua.indexOf("safari") != -1);

var ProtoEdit = function(){
 this.enabled = true;
 this.MZ=isMZ;
 this.buttons = new Array();
}

ProtoEdit.prototype._init = function(id, rte) {
 this.id = id;                           //id - id of textarea   
 this.area = document.getElementById(id);//area - textarea object
 this.area._owner = this; //area._owner - this , rte - id of rte frame 
 if(isIE) {
   this.area.onfocus = this.hideDDmenu;
 }else if (isMZ) {
   this.area.addEventListener("focus", this.hideDDmenu, false);	
 }else if (isO8) {
   this.area.onfocus = this.hideDDmenu;
 }

}

ProtoEdit.prototype.enable = function() {
 this.enabled = true;
}

ProtoEdit.prototype.disable = function() {
 this.enabled = false;
}

ProtoEdit.prototype.insTag = function (Tag,Tag2) {
  if (isMZ)
  {
   var ss = this.area.scrollTop;
   sel1 = this.area.value.substr(0, this.area.selectionStart);
   sel2 = this.area.value.substr(this.area.selectionEnd);

   sel = this.area.value.substr(this.area.selectionStart,
                   this.area.selectionEnd - this.area.selectionStart);

   this.area.value = sel1 + Tag + sel + Tag2 + sel2;

   selPos = Tag.length + sel1.length + sel.length + Tag2.length;
   this.area.setSelectionRange(sel1.length, selPos);
   this.area.scrollTop = ss;
  }
  else
  {
   this.area.focus();
   sel = document.selection.createRange();
   sel.text = Tag+sel.text+Tag2;
   this.area.focus();
  }


  return true;
}

ProtoEdit.prototype.createToolbar = function (id, width, height, readOnly) {
  wh = "";

  html = '<table id="buttons_' + id + '" cellpadding="1" cellspacing="0" class="toolbar">'
          + '  <tr>';
  //if (this.editorName) html += '<td class="'+this.editorNameClass+'">'+this.editorName+'</td>';

  for (var i = 0; i<this.buttons.length; i++) 
  {
   var btn = this.buttons[i];
   if (btn.name==" ")
    html += ' <td>&nbsp;</td>\n';
   else if (btn.name=="customhtml")
    html += btn.desc;
   else
    html += ' <td><div id="' + btn.name + '_' + id + '" onmouseover=\'this.className="btn-hover";\' '
          + 'onmouseout=\'this.className="btn-";\' class="btn-" '
          + 'onclick="this.className=\'btn-pressed\';' + btn.actionName + '('//\'' + id + '\', ' 
          + btn.actionParams + ')"><img src="' + this.imagesPath 
          + btn.name + '.gif" ' + wh + ' alt="' + btn.desc + '" title="' + btn.desc 
          + '"></div></td>\n';
  }
  html += '</tr></table>\n';
  html += '<div class="dropdown_menu" style="position:absolute;top:0px;left:0; display:none" id="dropdown_menu_'+id+'"></div>';
//  html += '<div style="position:absolute;top:0px;left:0;display:none" id="fontsize_box_'+id+'"></div>';
//  html += '<div style="position:absolute;top:0px;left:0;display:none" id="codelist_box_'+id+'"></div>';
  return html;
}

ProtoEdit.prototype.addButton = function (name, desc, actionParams, actionName) {
 if (actionName == null) actionName = this.actionName;
 var i = this.buttons.length;
 this.buttons[i] = new Object();
 this.buttons[i].name = name;
 this.buttons[i].desc = desc;
 this.buttons[i].actionName   = actionName;
 this.buttons[i].actionParams = actionParams;
}

ProtoEdit.prototype.trim = function(s2) {
   if (typeof s2 != "string") return s2;
   var s = s2;
   var ch = s.substring(0, 1);
  
   while (ch == " ") { // Check for spaces at the beginning of the string
      s = s.substring(1, s.length);
      ch = s.substring(0, 1);
   }
   ch = s.substring(s.length-1, s.length);
  
   while (ch == " ") { // Check for spaces at the end of the string
      s = s.substring(0, s.length-1);
      ch = s.substring(s.length-1, s.length);
   }
  
  // Note that there are two spaces in the string - look for multiple spaces within the string
   while (s.indexOf("  ") != -1) {
    // Again, there are two spaces in each of the strings
      s = s.substring(0, s.indexOf("  ")) + s.substring(s.indexOf("  ")+1, s.length);
   }
   return s; // Return the trimmed string back to the user
}

