// ============================================================================
// TreeView.js
// ----------------------------------------------------------------------------
// Nome     : Seletor de dados para treeview via XML
// Home     : http://home.zevallos.com/
// Criacao  : 1/15/04 1:20PM
// Autor    : Ruben Zevallos Jr. <ruben@zevallos.com>
// Versao   : 1.1d
// Local    : Brasília - DF
// Copyright: 97-2004 by Ruben Zevallos(r) Jr.
// License  : Licensed under the terms of the GNU Lesser General Public License
//            http://www.opensource.org/licenses/lgpl-license.php
// ----------------------------------------------------------------------------

var
  conLineImage = 0,
  conBoxImage = 1;

//==================================================
// objetos
//--------------------------------------------------
function TreeView(strName)
{
  this.Name = strName;

  this.SetHome = TVSetHome;
	this.HomeString = "";

	this.NodeList = new Array();

	this.AddNode = TVAddNode;
	this.BeginTree = TVBeginTree;
	this.EndTree = TVEndTree;
	this.BeginNode = TVBeginNode;
	this.EndNode = TVEndNode;
	this.IsLastNode = TVIsLastNode;

	this.ExpandNode = TVExpandNode;
	this.ParentNode = "node0";

	this.Depth = 0;
	this.MaxDepth = 0;

	this.Target = "";
	this.PrecedChar = "&nbsp;";
  this.CanCollapse = navigator.appName == "Microsoft Internet Explorer";

	this.InternalImages = new Array();
  this.InternalImages[conLineImage] = new TreeViewImage("/Library/TreeView/LineMiddle.gif", "/Library/TreeView/LineEnd.gif")
	this.InternalImages[conBoxImage] = new TreeViewImage("/Library/TreeView/LineEndMinus.gif", "/Library/TreeView/LineEndPlus.gif")

	this.ImageList = new Array();
}

function TreeViewImage(strExpandImage, strCollapseImage)
{
  this.Expanded = new Image();
  this.Expanded.src = strExpandImage;

  this.Collapsed = new Image();
  this.Collapsed.src = strCollapseImage;
}

function TreeViewNode(strName, strType, intDepth, strText)
{
	this.Name = strName;
	this.Type = strType;
	this.Depth = intDepth;
	this.Text = strText;
}

//==================================================
// metodos
//--------------------------------------------------
function TVBeginTree()
{
  this.NodeList.length = 0;
	document.write("<TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">");
}

function TVEndTree()
{
	var strColSpan = "", strImage = "";

	if(this.HomeString > "")
	{
	   document.write("<TR><TH COLSPAN=\"" + (this.MaxDepth + 2) + "\">" + this.HomeString + "</TH></TR>");
	}
	for(var i = 0; i < this.NodeList.length; i++)
	{
		strColSpan = ((this.MaxDepth - this.NodeList[i].Depth) ? " COLSPAN=\"" + (this.MaxDepth - this.NodeList[i].Depth + 1) + "\"": "");
    if(this.IsLastNode(i, -1) && this.NodeList[i].Type == this.InternalImages[conLineImage].Expanded.src)
    {
      this.NodeList[i].Type = this.InternalImages[conLineImage].Collapsed.src;
    }
		strImage = "<IMG NAME=\"img" + this.NodeList[i].Name + "\" SRC=\"" + this.NodeList[i].Type + "\" BORDER=\"0\">"

		if(this.NodeList[i].Type == this.InternalImages[conBoxImage].Expanded.src)
		{
			strImage = "<A HREF=\"javascript:if(false){}\" onClick=\"" + this.Name + ".ExpandNode(" + i + ")\">" + strImage + "</A>";
		}
		document.write("<TR ID=\"" + this.NodeList[i].Name + "\">");

		for(var j = 1; j <= this.NodeList[i].Depth; j++)
		{
			document.write("<TD>" + (this.IsLastNode(i, j) ? "<IMG SRC=\"/Library/TreeView/Line.gif\" BORDER=\"0\">" : "") + "</TD>");
		}

		document.write("<TD WIDTH=\"1%\">" + strImage + "</TD>");
		document.write("<TD" + strColSpan + ">" + this.PrecedChar + this.NodeList[i].Text + "</TD></TR>");
	}
	document.write("</TABLE>");
}

function TVBeginNode(strText, strURL, strTitle)
{
  if(strTitle > ""){strTitle = " TITLE=\"" + strTitle + "\""}
  if(strURL > ""){strText = "<A HREF=\"" + strURL + "\" TARGET=\"" + this.Target + "\"" + strTitle + ">" + strText + "</A>"}
	if(this.Depth){this.ParentNode = this.ParentNode + "_" + this.NodeList.length}
	this.NodeList[this.NodeList.length] = new TreeViewNode(this.ParentNode, (this.CanCollapse ? this.InternalImages[conBoxImage].Expanded.src : this.InternalImages[conLineImage].Expanded.src), this.Depth, strText);
	this.Depth++;
	if(this.Depth > this.MaxDepth){this.MaxDepth++}
}

function TVEndNode()
{
	this.Depth--;
	this.ParentNode = "node" + this.NodeList.length;
}

function TVAddNode(strText, strURL, strTitle)
{
  if(strTitle > ""){strTitle = " TITLE=\"" + strTitle + "\""}
  if(strURL > ""){strText = "<A HREF=\"" + strURL + "\" TARGET=\"" + this.Target + "\"" + strTitle + ">" + strText + "</A>"}
	this.NodeList[this.NodeList.length] = new TreeViewNode(this.ParentNode + "_" + this.NodeList.length, this.InternalImages[conLineImage].Expanded.src, this.Depth, strText);
}

function TVSetHome(strText, strURL)
{
   this.HomeString = "<A HREF=\"" + strURL + "\" TARGET=\"" + this.Target + "\">" + strText + "</A>";
}


//==================================================
// outros
//--------------------------------------------------
function TVExpandNode(intNode)
{
	var strDisplayValue, blnExpand;

	eval("strDisplayValue = " + this.NodeList[intNode + 1].Name + ".style.display");
	blnExpand = strDisplayValue == "none";
   document.images["img" + this.NodeList[intNode].Name].src = (blnExpand ? this.InternalImages[conBoxImage].Expanded.src : this.InternalImages[conBoxImage].Collapsed.src);
   for(var i = intNode + 1; (this.NodeList[i].Depth > this.NodeList[intNode].Depth); i++)
   {
     if(blnExpand)
	   {
	      if(this.NodeList[i].Depth == this.NodeList[intNode].Depth + 1){eval(this.NodeList[i].Name + ".style.display = \"inline\"")}
	   }
	   else
	   {
	      eval(this.NodeList[i].Name + ".style.display = \"none\"");
	      if(this.NodeList[i].Type == this.InternalImages[conBoxImage].Expanded.src){document.images["img" + this.NodeList[i].Name].src = this.InternalImages[conBoxImage].Collapsed.src}
	   }
     if(i == this.NodeList.length - 1){break}
   }
}

function TVIsLastNode(intNode, intDepth)
{
  blnIsLast = intDepth == -1;
  if(blnIsLast){intNode++}
  for(var i = intNode; i < this.NodeList.length; i++)
  {
    if(!blnIsLast)
    {
      if(this.NodeList[i].Depth + 1 == intDepth){blnIsLast = true; break}
      else if(this.NodeList[i].Depth < intDepth){break}
    }
    else
    {
      if(this.NodeList[i].Depth == this.NodeList[intNode - 1].Depth){blnIsLast = false; break}
      else if(this.NodeList[i].Depth < this.NodeList[intNode - 1].Depth){break}
    }
  }
  return(blnIsLast);
}
