var editor;
Editor = function(id, config)
{ this.id = id;
  this.obj = document.getElementById(id);
//alert(this.obj);
  this.html = new Array();
}

Editor.prototype.Config = function(config)
{ this.config = config;
//alert(this.config);

  for (i in config)
  { var c = config[i];
    //alert(c);

    if (c == '|')
    { this.html.push(this._GetButton_spacer());
    } else if (c == '-')
    { this.html.push(this._GetButton_newline());
    } else
    { var button = this._GetButton(c);
      if (button)
      { this.html.push(button);
      }
    }
  }
}

Editor.prototype.Display = function()
{ var buttons = document.createElement('div');
  buttons.className = 'editor';

  for (i in this.html)
  { var o = this.html[i];
    buttons.appendChild(o, this.obj);
  }

  this.obj.parentNode.insertBefore(buttons, this.obj);
}


Editor.prototype.TagAction = function(tag)
{ switch (tag)
  { case 'bold' : return "_AddTag('B')";
    case 'italic' : return "_AddTag('I')";
    case 'underline' : return "_AddTag('U')";
    case 'del' : return "_AddTag('DEL')";
    case 'left' : return "_AddTag('LEFT')";
    case 'center' : return "_AddTag('CENTER')";
    case 'right' : return "_AddTag('RIGHT')";
    case 'list' : return "_AddTag('LIST')";
    case 'quote' : return "_AddTag('QUOTE')";
    case 'link' : return "_AddAdvancedTag('LINK')";
    case 'email' : return "_AddAdvancedTag('EMAIL')";
    case 'image' : return "_AddImageTag('IMAGE')";
    default : return null;
  }
}


Editor.prototype.AddTag = function(tag, action)
{ editor = this;

  if (action)
  { eval('this.' + action);
  }
}


Editor.prototype._CreateButton = function(src)
{ var a = document.createElement('a');
  a.href = '#';

  a.editor = this;

  var button = document.createElement('img');
  button.src = '/images/editor/' + src + '.gif';
  button.className = 'button';

  a.appendChild(button);

  return a;
}


Editor.prototype._GetButton = function(tag)
{ var action = this.TagAction(tag);
  if (!action) return;

  var a = this._CreateButton(tag);
  a.onclick = function() {
                           this.editor.AddTag(tag, action);
                         };
  return a;
}


Editor.prototype._GetButton_spacer = function()
{ var spacer = document.createElement('img');
  spacer.src = external + '/images/editor/spacer.gif';
  spacer.className = 'spacer';

  return spacer;
}


Editor.prototype._GetButton_newline = function()
{ var newline = document.createElement('br');

  return newline;
}


/* generic toolbar functions */
Editor.prototype._Insert = function(val, pos)
{ var scrollTop = this.obj.scrollTop;
  if (document.selection) // Internet Explorer
  { this.obj.focus();
    sel = document.selection.createRange();
    sel.text = val;
  } else if (this.obj.selectionStart || // Mozilla/Netscape
             this.obj.selectionStart == 0)
  { var start = this.obj.selectionStart;
    var end = this.obj.selectionEnd;
    this.obj.value = this.obj.value.substring(0, start)
                 + val
                 + this.obj.value.substring(end, this.obj.value.length);
    this.obj.focus();

    this.obj.selectionStart = start + pos;
    this.obj.selectionEnd = start + pos;
  } else
  { this.obj.value += val;
    this.obj.focus();
  }
  this.obj.scrollTop = scrollTop;
}


Editor.prototype._InsertTag = function(start_tag, txt, end_tag)
{ var full_tag = '[' + start_tag + ']';
  var length = start_tag.length + 2;

  if (end_tag)
  { full_tag += txt + '[/' + end_tag + ']';
    length += txt.length;
  }

  return this._Insert(full_tag, length);
}


Editor.prototype._CreateTag = function(start_tag, end_tag)
{ var txt = ''; // = 'Your text goes here';
  if (document.selection) // Internet Explorer
  { //ctrl.focus();
    if (document.selection.createRange().text != '')
    { txt = document.selection.createRange().text;
    }
  } else if (this.obj.selectionStart != this.obj.selectionEnd) // Mozilla/Netscape
  { var start = this.obj.selectionStart;
    var end = this.obj.selectionEnd;
    txt = this.obj.value.substring(start, end);
  }

  return this._InsertTag(start_tag, txt, end_tag);
}


Editor.prototype._AddTag = function(taga, tagb) // no attr required
{ if (!tagb) tagb = taga;
  return this._CreateTag(taga, tagb);
}
  
  

Editor.prototype._AddEmptyTag = function(tag)
{ return this._CreateTag(tag);
}


Editor.prototype._AddAdvancedTag = function(tag, attr) // attr required
{ var start_tag = this._GetStartTag(tag, attr, 1);
  if (start_tag) return this._CreateTag(start_tag, tag);
}


Editor.prototype._AddEmptyAdvancedTag = function(tag, attr)
{ var start_tag = this._GetStartTag(tag, attr, 1);
  if (start_tag) return this._CreateTag(start_tag);
}


Editor.prototype._GetStartTag = function(tag, attr, check_attr)
{ if (check_attr && attr == null)
  { attr = prompt('Please enter the ' + tag.toLowerCase(), tag);
    if (attr == '' || attr == null) return;
  }
  if (attr == null) // no attributes provided - it is a simple tag
  { return tag;
  } else if (typeof attr != 'object') // single attribute provided
  { return tag + '=' + attr;
  }

  var start_tag = tag;
  if (attr[tag.toLowerCase()] != null) start_tag = tag + '="' + attr[tag.toLowerCase()] + '"';
  for (a in attr)
  { if (a.toLowerCase() == tag.toLowerCase()) continue;
    start_tag += ' ' + a.toLowerCase() + '="' + attr[a] + '"';
  }

  return start_tag;
}


Editor.prototype._AddImageTag = function()
{ editor = this;

  var url = '/cgi-bin/file-upload.cgi?editor=1&type=image';
  var win = window.open(url, 'win', 'top=50, left=50, width=450, height=300');
}


Editor.prototype._AddImageTagFinish = function(attr)
{ if (attr)
  { this._AddEmptyAdvancedTag('IMAGE', attr);
  } else
  { alert('An error occurred while uploading your image - poo');
  }
}
