// =====================================================================================================
// JavaScript Document
// <script language="JavaScript1.2" type="text/JavaScript">
// Copyright (c) 2005 Rewritten Software.  http://www.rewrittensoftware.com
// This script is supplied "as is" without any form of warranty. Rewritten Software
// shall not be liable for any loss or damage to person or property as a result of using this script.
// Use this script at your own risk!
// You are licensed to use this script free of charge for commercial or non-commercial use providing you do not remove
// the copyright notice or disclaimer.
//
// Object-oriented modifications / browser independence / formatting by Stephen C. Fedder - March 2009
// =====================================================================================================
// global tree views list
var TreeViews=new Array();
// =====================================================================================================
// allow toggling the tree view using the views index
function TreeViewToggle(idx,uid) {
  TreeViews[idx].Toggle(uid);
}
// =====================================================================================================
// define the constructer
function TreeView(width) {
  // Define the array that will contain the mapping table for ids to images.
  this.itemList=new Object();
  // This is the root of the tree. It must be supplied as the parent for anything at the top level of the tree.
  this.rootCell=null;
  this.index=TreeViews.length;
  this.width=(typeof(width)=="number")?(""+width+"px"):width;
  this.levelspacing=[0,20,40,60,80,100,120,140]
  TreeViews[this.index]=this;
}
// =====================================================================================================
// define the properties
TreeView.prototype.itemList;      // icon unique id object reference
TreeView.prototype.iconSet;       // icon set index
TreeView.prototype.rootCell;      // root cell for the TreeView
TreeView.prototype.index;         // index in TreeViews of this node
TreeView.prototype.width;         // width of TreeView embedded table
TreeView.prototype.levelspacing;  // level spacing array (for each level, graphic x offset)
// =====================================================================================================
TreeView.prototype.CollapseAll=function() {
  // collapse all items
  var uid,citem,cics,cdiv,icon;
  for (uid in this.itemList) {
    cdiv=document.getElementById("D"+uid); if (cdiv) {
      if (cdiv.style.display!="none") {
        cdiv.style.display="none";
        citem=this.itemList[uid];
        cics=citem.iconSet; if (cics&&cics.ic) {
          icon=document.getElementById("F"+uid); if (icon) {
            icon.setAttribute("src",cics.ic);
          }
        }
      }
    }
  }
}
// =====================================================================================================
TreeView.prototype.Toggle=function(uid) {
  var i;
  // get the item parameters definition
  var iobj=this.itemList[uid]; if (iobj) {
    var ics=iobj.iconSet;
    // get the folder container div
    var div=document.getElementById("D"+uid); if (div) {
      // get visibility
      var visible=(div.style.display!="none");
      // get the folder icon link
      var key=document.getElementById("P"+uid); if (key!=null) {
        // only make it visible if it actually contains anything
        var hasChildren=div.hasChildNodes(); if (hasChildren) {
          // if visible, just make invisible
          if (visible) {
            div.style.display="none";
            if (ics&&ics.ic) {
              var icon=document.getElementById("F"+uid); if (icon) {
                icon.setAttribute("src",ics.ic);
              }
            }
          } else {
            // collapse all items
            this.CollapseAll();
            // expand clicked item
            div.style.display="block";
            if (ics&&ics.io) {
              var icon=document.getElementById("F"+uid); if (icon) {
                icon.setAttribute("src",ics.io);
              }
            }
          }
        }
      }
    }
  } else if (uid=="collapse") {
    // collapse all items
    this.CollapseAll();
  }
}
// =====================================================================================================
TreeView.prototype.AddImage=function(parent,imgFileName,size,iid) {
  var img=document.createElement("IMG");
  img.setAttribute("src",imgFileName);
  var ics=(typeof(size)=="string")?iconSizes[size]:size;
  if (ics) {
    img.setAttribute("width",ics.w);
    img.setAttribute("height",ics.h);
    img.setAttribute("hspace",(typeof(ics.hs)=="number")?ics.hs:0);
    img.setAttribute("vspace",(typeof(ics.vs)=="number")?ics.vs:0);
    img.setAttribute("border",(typeof(ics.b)=="number")?ics.b:0);
    //img.style.verticalAlign=(typeof(ics.va)=="string")?ics.va:"middle";
  } else {
    img.setAttribute("hspace",0);
    img.setAttribute("vspace",0);
    img.setAttribute("border",0);
    img.setAttribute("valign","middle");
  }
  if (iid)
    img.setAttribute("id",iid);
  parent.appendChild(img);
}
// =====================================================================================================
TreeView.prototype.AddSpacer=function(parent,size) {
  this.AddImage(parent,"images/spacer.gif",size);
}
// =====================================================================================================
// get the unique menu id for a given sco reference for this treeview instance
// passed parameter can either be the SCO instance or the SCO instance index
TreeView.prototype.GetUID=function(sco_ref) {
  var uniqueId;
  if (typeof(sco_ref)=="number")
    uniqueId="_"+this.index+"_"+sco_ref;
  else if (typeof(sco_ref)=="object")
    uniqueId="_"+this.index+"_"+sco_ref.index;
  return uniqueId;
}
// =====================================================================================================
// Creates a new DIV tag and appends it to parent if parent is not null.
// Returns the new DIV tag.
//
TreeView.prototype.CreateDiv=function(parent,id) {
  var div=document.createElement("DIV");
  parent.appendChild(div);
  div.setAttribute("id","D"+id);
  div.style.display="none";
  div.style.margin="0px";
  div.style.padding="0px";
  return div;
}
// =====================================================================================================
// Creates a new package under a parent.
// Returns a TABLE tag to place child elements under.
//
// creates unique id of the form _n_m where n is the index of the TreeView and m is the index of the SCO
// folder icon link id is of the form "P"+uniqueid
// folder icon id is of the form "F"+uniqueid
// bookmark icon link id is of the form "I"+uniqueid
// bookmark icon id is of the form "B"+uniqueid
// text cell id is of the form "TDA"+uniqueid
// container div cell id is of the form "D"+uniqueid
// =====================================================================================================
TreeView.prototype.CreateTreeItem=function(sco_obj,container) {
  var cell,a,text,ls;
  // get the icon set for this menu item
  var ics=iconSets[sco_obj.iconSet];
  // create the unique id and object cross reference
  var uniqueId="_"+this.index+"_"+sco_obj.index;
  sco_obj.uniqueId=uniqueId;
  var iobj=new Object();
  iobj.uniqueId=uniqueId;
  iobj.iconSet=sco_obj.iconSet;
  iobj.index=sco_obj.index;
  this.itemList[uniqueId]=iobj;
  // create the table for the item in the container object for fixed sizing
  var table=document.createElement("TABLE");
  container.appendChild(table);
  table.style.borderCollapse="collapse";
  table.style.border="none";
  table.style.emptyCells="show";
  table.style.margin=ics.margin?ics.margin:"0px";
  table.style.padding="0px";
  table.style.borderSpacing="0px";
  var tablebody=document.createElement("TBODY");
  table.appendChild(tablebody);
  var row=document.createElement("TR");
  tablebody.appendChild(row);
  // create the cell for the lead spacing
  cell=document.createElement("TD");
  // set the level spacing tab position if non-zero
  ls=(sco_obj.level>=this.levelspacing.length)?this.levelspacing[this.levelspacing.length-1]:this.levelspacing[sco_obj.level];
  cell.style.width=ls;
  row.appendChild(cell);
  if (ls>0) this.AddSpacer(cell,{w:ls,h:1});
  // create the cell for the folder icon
  cell=document.createElement("TD");
  cell.setAttribute("align","left");
  cell.style.verticalAlign="middle";
  row.appendChild(cell);
  // is there a folder icon for this item?
  if (ics&&ics.ic) {
    // create the folder icon only for container nodes
    a=document.createElement("A");
    cell.appendChild(a);
    a.setAttribute("id","P"+uniqueId);
    a.style.verticalAlign="bottom";
    // does the cell have children?
    if (sco_obj.hasChild) {
      a.setAttribute("href","javascript:TreeViewToggle("+this.index+",\""+uniqueId+"\");");
    } else {
      a.setAttribute("href","javascript:ClickFolder("+this.index+","+sco_obj.index+");");
    }
    a.className="icon";
    this.AddImage(a,ics.ic,ics.isize?ics.isize:"icon","F"+uniqueId);
  }
  // create the cell for the folder icon to bookmark icon spacer
  cell=document.createElement("TD");
  var bms=NavBMSpacing+(ics.bmsep?ics.bmsep:0);
  cell.style.width=bms;
  row.appendChild(cell);
  if (bms>0) {
    this.AddSpacer(cell,{w:bms,h:1});
  }
  // create the cell for the bookmark image
  cell=document.createElement("TD");
  cell.setAttribute("align","left");
  cell.style.verticalAlign="middle";
  if (ics.bw) cell.style.width=""+ics.bw+"px";
  // place the bookmark icon if present
  if (ics&&ics.b0) {
    row.appendChild(cell);
    // all the event to call when the icon is clicked.
    a=document.createElement("A");
    a.setAttribute("id","I"+uniqueId);
    // container node not go to ref
    a.setAttribute("href","javascript:ClickFolder("+this.index+","+sco_obj.index+");");
    cell.appendChild(a);
    // add the bookmark image to the cell
    this.AddImage(a,ics.b0,ics.bsize?ics.bsize:"none","B"+uniqueId);
  }
  // create the cell for the text spacing
  cell=document.createElement("TD");
  var nts=NavTXSpacing+(ics.txsep?ics.txsep:0);
  cell.style.width=nts;
  row.appendChild(cell);
  if (nts>0) {
    this.AddSpacer(cell,{w:nts,h:1});
  }
  // create the cell for the text
  cell=document.createElement("TD");
  cell.style.whiteSpace="normal";
  cell.Wrap=true;
  cell.setAttribute("id","TDA"+uniqueId);
  a=document.createElement("A");
  a.setAttribute("id",uniqueId);
  cell.appendChild(a);
  a.setAttribute("href","javascript:Click("+this.index+","+sco_obj.index+");");
  text=document.createTextNode(sco_obj.title);
  a.appendChild(text);
  row.appendChild(cell);
  return this.CreateDiv(container,uniqueId);
}
// =====================================================================================================
// This is the initialization method for the Tree View. It builds an initial single row, single cell table that will
// contain the tree. It initializes "this.rootCell". This object must be used as the parent for all
// top-level tree elements.
// There are two methods for creating tree elements: CreatePackage() and CreateNode(). The images for the
// package are hard coded. CreateNode() allows you to supply your own image for each node element.
// =====================================================
TreeView.prototype.Initialize=function(body) {
  // default body container if not passed to the #2 div in the body
  if (!body)
    body=document.getElementsByTagName("div").item(2);
  // if body passed is numeric, select the nth div in the document as the container
  else if (typeof(body)=="number") {
    body=document.getElementsByTagName("div").item(body);
  // if body passed is string, select the named div or element in the document as the container
  } else if (typeof(body)=="string") {
    body=document.getElementById(body);
  }
  var table=document.createElement("TABLE");
  this.table=table;
  body.appendChild(table);
  table.style.borderCollapse="collapse";
  table.style.border="none";
  table.style.emptyCells="show";
  table.style.margin="0px";
  table.style.padding="0px";
  table.style.borderSpacing="0px";
  var tablebody=document.createElement("TBODY");
  table.appendChild(tablebody);
  var row=document.createElement("TR");
  tablebody.appendChild(row);
  var cell=document.createElement("TD");
  cell.setAttribute("valign","top");
  cell.setAttribute("align","left");
  cell.style.width=this.width;
  row.appendChild(cell);
  this.rootCell=cell;  // Initialize the root of the tree view.
}
// =====================================================================================================

