function HtmlDocument__makeToolBar (cssUri)
{
  if (!cssUri)
    cssUri = CSS_PATH + "toolbar.css";

  this.createStyleSheet (cssUri);
  var body = window.top._customizeBody (this._body);

  var table = this._table();
  table.border = 0;
  table.height = '100%';

  table._template  = ToolBar__createButtonTemplate (this);
  table._separator = this.createElement ("td");
  table._separator.className = "separator";

  table._addButton    = ToolBar__addButton;
  table._addToggleButton = ToolBar__addToggleButton;
  table._removeButton = ToolBar__removeButton;
  table._clear        = ToolBar__clear;
  table._setEnableButton = ToolBar__enableButton;

  body.appendChild (table);
  table._tBody.appendChild (this.createElement ("tr"));
  return table;
}

function ToolBar__createButtonTemplate (doc)
{
  var td = doc.createElement ("td");
  td.className = "button";
  td.innerHTML = "<img border=0>";
  return td;
}


function ToolBar__addButton (action, tooltip, handler)
{
  var doc = this.ownerDocument;
  var tr = this._tBody.firstChild;
  var td;
  if (action == '-') {
    td = this._separator.cloneNode (true);
  }
  else {
    td = this._template.cloneNode  (true);

    td._setIcon     = ToolBarButton__setIcon;
    td._setIcons    = ToolBarButton__setIcons;

    td.onmouseover  = ToolBarButton__onmouseover;
    td.onmouseout   = ToolBarButton__onmouseout;
    td.onmousedown  = ToolBarButton__onmousedown;
    td.onmouseup    = ToolBarButton__onmouseup;

    td.__processOnClick__ = ToolBarButton__processOnClick;

    td._icons       = TOOLBAR_ICONS[action];

    td.title = tooltip;
    td._setIcon ('default');
    if (handler)
      td._handler = handler;
  }
  td.id = action;
  td.name = action;
  td.disabled = false;
  tr.appendChild (td);
  return td;
}

function ToolBar__addToggleButton (action, tooltip, state, handler)
{
  var td = this._addButton (action, tooltip, handler);

  td._setIcon           = ToolBarToggleButton__setIcon;
  td.__processOnClick__ = ToolBarToggleButton__processOnClick;

  td._state = state;
  td._setIcon ('default');
}


function ToolBar__findActionButton (tr, action)
{
  var childNodes = tr.childNodes;
  for (var i = 0; i < childNodes.length; ++i) {
    var button = childNodes[i];
    if (button.name == action)
      return button;
  }
  return null
}

function ToolBar__removeButton (action)
{
  var tr = this._tBody.firstChild;
  var button = ToolBar__findActionButton (tr, action);
  if (button != null)
      tr.removeChild (button);
  }

function ToolBar__enableButton (enabled, action)
{ 
  var tr = this._tBody.firstChild;
  var td = ToolBar__findActionButton (tr, action);
  if (td != null) {
    td.disabled = !enabled;
    if (enabled) {
      td.onmouseover  = ToolBarButton__onmouseover;
      td.onmouseout   = ToolBarButton__onmouseout;
      td.onmousedown  = ToolBarButton__onmousedown;
      td.onmouseup    = ToolBarButton__onmouseup;
      td.__processOnClick__ = ToolBarButton__processOnClick;
      td._setIcon ('default');
      td.disabled = false;
    }
    else {
      td.onmouseover  = null;
      td.onmouseout   = null;
      td.onmousedown  = null;
      td.onmouseup    = null;
      td.__processOnClick__ = null;
      td._setIcon ('disabled');
      td.disabled = true;
    }
  }
  return td;
}

function ToolBar__clear()
{
  var doc = this.ownerDocument;
  var tr = this._tBody.firstChild;
  tr.parentNode.replaceChild (doc.createElement ("tr"), tr);
}


function ToolBarButton__setIcon(icon)
{
  if (this._icons) {
    var img = this.firstChild;
    img.src = this._icons[icon];
  }
}

function ToolBarToggleButton__setIcon (icon)
{
  if (this._state) {
    if (icon == 'default')
      icon = 'active';
    else if (icon == 'active')
      icon = 'default';
    else if (icon == 'hilite')
      icon = 'active';
  }
  ToolBarButton__setIcon.call (this, icon);
}


function ToolBarButton__onmouseover () { this._setIcon ('hilite');  }
function ToolBarButton__onmouseout  () { this._setIcon ('default'); }
function ToolBarButton__onmousedown () { this._setIcon ('active');  }
function ToolBarButton__onmouseup   () { this._setIcon ('hilite');  }

function ToolBarButton__processOnClick()
{
  if (this._handler)
    this._handler();
  else
    window.top._executeRequest (this.name);
}

function ToolBarToggleButton__processOnClick()
{
  this._state = !this._state;

  if (this._handler)
    this._handler (this._state);
  else
    window.top._executeRequest (this.name + (this._state ? "_on" : "_off"));

  this._setIcon ('hilite');
}


function ToolBarButton__setIcons (defaultIcon, hiliteIcon, activeIcon)
{
  var icons = new Object();
  icons['default'] = defaultIcon;
  icons['hilite']  = hiliteIcon;
  icons['active']  = activeIcon;
  this._icons = icons;
  this._setIcon ('default');
}

function ToolBar__getState()
{
  var state = Widget__getState.call (this);
  return state;
}
