/*
 * $Log: xbTreeWidgetStatic.js,v $
 * Revision 1.6  2002/07/22 14:19:42  bc6ix
 * fix license path
 *
 * Revision 1.5  2002/07/07 08:23:08  bc6ix
 * fix line endings
 *
 * Revision 1.4  2002/05/14 16:52:53  bc6ix
 * use CVS Log for revision history
 *
 */

/* ***** BEGIN LICENSE BLOCK *****
 * Licensed under Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * Full Terms at /lib/js/license/mpl-tri-license.txt
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Bob Clary <bclary@netscape.com>
 *
 * ***** END LICENSE BLOCK ***** */
var lastOpen;
var treeCounter = 0;

xbDEBUG.on = false;

xbTreeWidgetStatic._id = 0;
xbTreeWidgetStatic._hash = new Object();
xbTreeWidgetStatic._dynamic = (document.documentElement && typeof(document.documentElement.innerHTML) == 'string');

function xbTreeWidgetStatic(handles, labels, classprefix)
{
  this.id = 'treewidgetid' + xbTreeWidgetStatic._id++;
  xbTreeWidgetStatic._hash[this.id] = this;
  this.handles      = handles;
  this.labels       = labels;
  this.classprefix  = classprefix ? classprefix : 'treewidget';
  this.children     = new Array();
  this.isRoot		= false; // ? root : false;
  this.isOpen		= false;
}
function setAsRoot(widget)
{
	widget.isRoot = true;	
}

xbTreeWidgetStatic.prototype.appendChild =
function xbTreeNodeAppendChild(child)
{
  if (child)
    this.children[this.children.length] = child;
  return child;
}

if (document.getElementById || document.all)
{
  xbTreeWidgetStatic.prototype.toHTML =
  function xbTreeWidgetStaticToHTMLDOMIE(level)
  {
    var i;
    var html = '';

    if (typeof(level) == 'undefined')
      level = 0;

    html += '<div class="' + this.classprefix + 'container">\n';

    html += '<div ID="' + this.id + '" class="' + this.classprefix + 'handle" onclick="xbTreeWidgetStaticToggleHandle(this);">';
    if (xbTreeWidgetStatic._dynamic  && level > 0 && this.children.length > 0)
    {
      html += this.handles.closed;
      html += this.labels.closed;
    }
    else
    {
      html += this.handles.open;
      html += this.labels.open;
    }
    html += '<\/div>\n';

    if (this.children.length)
    {
      html += '<div class="' + this.classprefix + 'children" ';

      if (xbTreeWidgetStatic._dynamic  && level > 0)
      {
        html += 'style="display:none;">';
      }
      else
      {
        html += 'style="display:block;">';
      }

      for (i = 0; i < this.children.length; i++)
      {
        html += this.children[i].toHTML(level + 1);
      }

      html += '<\/div>\n';
    }

    html += '<\/div>\n';

    //alert(html);
    return html;
  };
}
else 
{
  xbTreeWidgetStatic.prototype.toHTML =
  function xbTreeWidgeToHTMLLegacy(depth)
  {
    var i;
    var html = '';

    if (typeof(depth) == 'undefined')
    {
      depth = 0;
    }

    html += '<table>\n';
    html += '<tr>\n';
    html += '<td>\n';

    if (document.layers)
      html += '<ilayer visibility="hidden">\n';
    else
      html += '<span style="visibility: hidden">\n';

    for (i = 0; i < depth; i++)
    {
      html += '..';
    }

    if (document.layers)
      html += '<\/ilayer>\n';
    else
      html += '<\/span>\n';

      html += this.handles.open + this.labels.open;

    for (i = 0; i < this.children.length; i++)
    {
      html += this.children[i].toHTML(depth+1);
    }

    html += '<\/td>\n';
    html += '<\/tr>\n';
    html += '<\/table>\n';

    return html;
  };
}

function xbGetNextElement(node)
{
  var next;

  for (next = node.nextSibling; next; next = next.nextSibling)
  {
    if (next.nodeType == 1)
      return next;
  }
  
  return null;
}

function xbTreeWidgetStaticToggleHandle(handlediv)
{
  if (!handlediv)
    return false;

  var widget;
  var next = xbGetNextElement(handlediv);
  if (next)
  {
    switch(next.style.display)
    {
      case '':
      case 'block':
        next.style.display = 'none';
        if (typeof(handlediv.innerHTML) == 'string')
        {
          widget = xbTreeWidgetStatic._hash[handlediv.id];
          handlediv.innerHTML = widget.handles.closed + widget.labels.closed;
		  widget.isOpen = false;
        }
        break;
      case 'none':
        next.style.display = 'block';
        if (typeof(handlediv.innerHTML) == 'string')
        {
          widget = xbTreeWidgetStatic._hash[handlediv.id];
          handlediv.innerHTML = widget.handles.open + widget.labels.open;
        }

		var tmpwidget;
		for (i=0;i<xbTreeWidgetStatic._id;i++)
		{
			tmpwidget = xbTreeWidgetStatic._hash["treewidgetid"+i];
			if (tmpwidget.isRoot && tmpwidget.isOpen)
			{
				xbTreeWidgetStaticToggleHandle(document.getElementById(tmpwidget.id));
			}
		}
		widget.isOpen = true;
		lastOpen = handlediv;

		break;
      default:
        return false;
     }
   }

   return true;
}

function xbCreateTreeWidgetStaticFromObject(getChildren, getHandles, getLabels, obj, classprefix)
{
  xbDEBUG.dump('xbCreateStaticTreeWidgetFromObject()');

  var i;
  var children;
  var root = null;
  var handles = getHandles(obj, classprefix);
  var labels = getLabels(obj, classprefix);

  children = getChildren(obj);
  root = new xbTreeWidgetStatic(handles, labels, classprefix);

  for (i = 0; i < children.length; i++)
  {
    var child = children[i];
    root.appendChild( xbCreateTreeWidgetStaticFromObject(getChildren, getHandles, getLabels, child, classprefix) );
  }

  return root;
}


