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 if (eval('this._GetButton_' + c))
    { this.html.push(eval('this._GetButton_' + c + '()'));
    }
  }
}

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.AddTag = function(tag)
{ editor = this;

  switch (tag)
  { case 'bold' : this._AddTag('B');
                  break;
    default : alert('blah');
  }
}


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_spacer = function()
{ var spacer = document.createElement('img');
  spacer.src = '/images/editor/spacer.gif';
  spacer.className = 'spacer';

  return spacer;
}


/* toolbar specifics */
Editor.prototype._GetButton_newline = function()
{ var newline = document.createElement('br');

  return newline;
}


Editor.prototype._GetButton_bold = function()
{ var a = this._CreateButton('bold');
  a.onclick = function() {
                           this.editor.AddTag('bold');
                         };
  return a;
}


Editor.prototype._GetButton_italic = function()
{ var a = this._CreateButton('italic');
  a.onclick = function() {
                           this.editor._AddTag('I');
                         };
  return a;
}


Editor.prototype._GetButton_underline = function()
{ var a = this._CreateButton('underline');
  a.onclick = function() {
                           this.editor._AddTag('U');
                         };
  return a;
}


Editor.prototype._GetButton_del = function()
{ var a = this._CreateButton('strikethrough');
  a.onclick = function() {
                           this.editor._AddTag('DEL');
                         };
  return a;
}


Editor.prototype._GetButton_left = function()
{ var a = this._CreateButton('justifyleft');
  a.onclick = function() {
                           this.editor._AddTag('LEFT');
                         };
  return a;
}


Editor.prototype._GetButton_center = function()
{ var a = this._CreateButton('justifycenter');
  a.onclick = function() {
                           this.editor._AddTag('CENTER');
                         };
  return a;
}


Editor.prototype._GetButton_right = function()
{ var a = this._CreateButton('justifyright');
  a.onclick = function() {
                           this.editor._AddTag('RIGHT');
                         };
  return a;
}


Editor.prototype._GetButton_list = function()
{ var a = this._CreateButton('list');
  a.onclick = function() {
                           this.editor._AddTag('LIST');
                         };
  return a;
}


Editor.prototype._GetButton_quote = function()
{ var a = this._CreateButton('quote');
  a.onclick = function() {
                           this.editor._AddTag('QUOTE');
                         };
  return a;
}


Editor.prototype._GetButton_link = function()
{ var a = this._CreateButton('link');
  a.onclick = function() {
                           this.editor._AddAdvancedTag('LINK');
                         };
  return a;
}


Editor.prototype._GetButton_email = function()
{ var a = this._CreateButton('email');
  a.onclick = function() {
                           this.editor._AddAdvancedTag('EMAIL');
                         };
  return a;
}


Editor.prototype._GetButton_image = function()
{ var a = this._CreateButton('image');
  a.onclick = function() {
                           this.editor._AddImageTag('IMAGE');
                         };
  return a;
}


/* generic toolbar functions */
Editor.prototype._Insert = function(val)
{ 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 + val.length;
    this.obj.selectionEnd = start + val.length;
  } 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 + ']';
  if (end_tag) full_tag += txt + '[/' + end_tag + ']';
  return this._Insert(full_tag);
}


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');
  }
}
