
// Global variables
var items               = new Array();        // array of all items in the sitemap
var topItems            = new Array();        // top level items only
//var browser             = "OTHER";          // browser version (IE, NS or OTHER)
var browser             = "IE";               // browser version (IE, NS or OTHER)
var selectedItem        = null;               // current selected item
var style               = null;               // site map style
var scriptURL           = null;               // URL to the script folder
var mouseOverColor      = null;               // mouse over color
var selectedColor       = null;               // selected color
var selectedBackground  = null;               // selected background color



// ----------------------------------------------------------------------------
// Calculates the Y coordinates of all visible sitemap items.
// ----------------------------------------------------------------------------
function adjustY()
  {
    if ( browser != "NS" )
      return;

    var y = 0;
    var minWidth = 0;
    var minHeight = 0;
    for ( var i=0; i<items.length; i++ )
      {
        items[i].obj.top = y;
        items[i].obj.left = 0;
        if ( items[i].obj.visibility == "show" )
          {
            y += items[i].obj.clip.height;
            if ( items[i].obj.clip.width > minWidth )
              minWidth = items[i].obj.clip.width;
            minHeight += items[i].obj.clip.height;
          }
      }
    document.sitemap.clip.right = minWidth;
    document.sitemap.clip.bottom = minHeight;
  }



// ----------------------------------------------------------------------------
// Colapses a given item, hiding its entire subtree.
// ----------------------------------------------------------------------------
function colapse( siteItem )
  {
    if ( selectedItem == siteItem )
      selectItem( null );
    for ( var i=0; i<siteItem.children.length; i++ )
      {
        if ( browser == "IE" )
          siteItem.children[i].obj.style.display = "none";
        else
          siteItem.children[i].obj.visibility = "hide";
        colapse( siteItem.children[i] );
      }
  }



// ----------------------------------------------------------------------------
// Displays the sitemap
// ----------------------------------------------------------------------------
function display()
  {
    if ( browser == "IE" )
      _writeln( "<table border='0' cellspacing='0' cellpadding='0' style='position:relative'><tr><td valign=top align=left>" );
      
    for ( var i=0; i<topItems.length; i++ )
      renderItem( topItems[i], "" );

    if ( browser == "IE" )
      _writeln( "</td></tr></table>" );
    else if ( browser == "NS" )
      window.onResize = restoreIcons;
    else
      return;

    for ( var i=0; i<topItems.length; i++ )
      {
        if ( browser == "IE" )
          topItems[i].obj.style.display = "block";
        else if ( browser == "NS" )
          topItems[i].obj.visibility = "show";
      }
    adjustY();
  }



// ----------------------------------------------------------------------------
// Expands a given item, restoring its entire subtree.
// ----------------------------------------------------------------------------
function expand( siteItem )
  {
    for ( var i=0; i<siteItem.children.length; i++ )
      {
        if ( browser == "IE" )
          siteItem.children[i].obj.style.display = "block";
        else
          siteItem.children[i].obj.visibility = "show";
        if ( siteItem.children[i].opened )
          expand( siteItem.children[i] );
      }
  }



// ----------------------------------------------------------------------------
// Expands a given item and colapses every other at the same level.
// ----------------------------------------------------------------------------
function expandColapse( siteItem )
{
  var children;
  if ( siteItem.parent == null )
    children = topItems;
  else
    children = siteItem.parent.children;
  for ( var i=0; i<children.length; i++ )
    if ( (children[i].opened) && (children[i] != siteItem) )
      {
        children[i].opened = false;
        colapse( children[i] );
        updateIcons( children[i] );
      }
  expand( siteItem );
}


  
// ----------------------------------------------------------------------------
// Returns the tree icon for a given item.
// ----------------------------------------------------------------------------
function getTreeIcon( siteItem )
  {
    var icon;
    if ( siteItem.children.length == 0 )
      if ( style == "OUTLINE" )
        icon = "../images/blank.gif";
      else if ( siteItem.last )
        icon = "../images/simple.GIF";
      else
        icon = "../images/simple.gif";
    else if ( siteItem.opened )
      if ( style == "OUTLINE" )
        icon = "../images/minus.gif";
      else if ( siteItem.last )
        icon = "../images/colapse.GIF";
      else
        icon = "../images/colapse.gif";
    else if ( style == "OUTLINE" )
      icon = "../images/plus.gif";
    else if ( siteItem.last )
      icon = "../images/expand.GIF";
    else
      icon = "../images/expand.gif";
    return scriptURL+icon;
  }


  
// ----------------------------------------------------------------------------
// Called when an item is clicked. Expands/collapses the item and selects
// the item.
// ----------------------------------------------------------------------------
function itemClicked( siteItem )
  {
    if ( (siteItem.children.length != 0) && (browser != "OTHER") )
      {
        siteItem.opened = !siteItem.opened;
        if ( siteItem.opened )
          expandColapse( siteItem );
        else
          colapse( siteItem );
      }
    selectItem( siteItem );
    adjustY();
  }


  
// ----------------------------------------------------------------------------
// Called when the mouse moves out of an item.
// ----------------------------------------------------------------------------
function itemMouseOut( siteItem )
  {
    siteItem.mouseOver = false;
    if ( (siteItem.style != null) && (!siteItem.selected) )
      siteItem.style.color = "";
    updateIcons( siteItem );
    adjustY();
  }


  
// ----------------------------------------------------------------------------
// Called when the mouse moves over an item from outside.
// ----------------------------------------------------------------------------
function itemMouseOver( siteItem )
  {
    siteItem.mouseOver = true;
    if ( (siteItem.style != null) && (!siteItem.selected) )
      siteItem.style.color = mouseOverColor;
    updateIcons( siteItem );
    adjustY();
    var message = "";
    if ( siteItem.message != null )
      message = siteItem.message;
    else if ( siteItem.url != null )
      message = siteItem.url;
    return message;
  }



// ----------------------------------------------------------------------------
// Outputs an anchor (<A>) tag for one item.
//
// Parameters:
//   siteItem     item for which to output the link
//   element      HTML code to write inside the anchor tag
// ----------------------------------------------------------------------------
function outputItemAnchor( siteItem, element )
  {
    _write( "<td nowrap>" );
    if ( siteItem.url == null )
      _write( "<a href='javascript:itemClicked(items["+siteItem.id+"])'" );
    else
      {
        _write( "<a href='"+siteItem.url+"'" );
        if ( siteItem.target != null )
          _write( " target='"+siteItem.target+"'" );
        _write( " onClick='itemClicked(items["+siteItem.id+"])'" );
      }
    _write( " onMouseOver='window.status=itemMouseOver(items["+siteItem.id+"]); return true'" );
    _write( " onMouseOut='itemMouseOut(items["+siteItem.id+"]); window.status=\"\"; return true'" );
    _write( ">" );
    _write( element );
    _write( "</a>" );
    _write( "</td>" );
  }


  
// ----------------------------------------------------------------------------
// Generates the HTML code for a given item.
//
// Parameters:
//   siteItem   item for which to generate the HTML
//   left       piece of HTML to "ident" the item
// ----------------------------------------------------------------------------
function renderItem( siteItem, left )
  {
    if ( browser == "IE" )
      _write( "<table border='0' cellspacing='0' cellpadding='0' id='item"+siteItem.id+"' style='position:relative;display:none'>" );
    else if ( browser == "NS" )
      {
        _write( "<layer id='item"+siteItem.id+"' position='relative' visibility='hide'>" );
        _write( "<table border='0' cellspacing='0' cellpadding='0'>" );
      }
    else
      _write( "<table border='0' cellspacing='0' cellpadding='0'>" );
    _write( "<tr align=left valign=middle>" );
    _write( left );
    if ( style != "LIST" )
      {
        if ( siteItem.children.length > 0 )
          _write( "<td><a href='javascript:treeIconClicked(items["+siteItem.id+"])' onMouseOver='window.status=\"\"; return true'><img name='treeIcon"+siteItem.id+"' src='"+getTreeIcon(siteItem)+"' border='0' width='16' height='16'></a></td>" );
        else
          _write( "<td><img src='"+getTreeIcon(siteItem)+"' width='16' height='16'></td>" );
        _write( "<td><img src='../images/space.gif' width='3' height='16'></td>" );
      }
    var icon = siteItem.colapsedIcon;
    if ( (browser == "OTHER") && (siteItem.children.length > 0) )
      icon = siteItem.expandedIcon;
    if ( icon != null )
      {
        outputItemAnchor( siteItem, "<img name='icon"+siteItem.id+"' src='"+scriptURL+icon+"' border='0' width='"+siteItem.iconWidth+"' height='"+siteItem.iconHeight+"'>" );
        outputItemAnchor( siteItem, "<img src='../images/space.gif' border='0' width='3' height='16'>" );
      }
    if ( siteItem.text != null )
      {
        var s = siteItem.text;
        if ( (siteItem.font != null) || (siteItem.size != null) || (siteItem.color != null) || (browser == "IE") )
          {
            var s1 = "<font";
            if ( browser == "IE" )
              s1 += " id='style"+siteItem.id+"'";
            if ( siteItem.font != null )
              s1 += " face='"+siteItem.font+"'";
            if ( siteItem.size != null )
              s1 += " size='"+siteItem.size+"'";
            if ( siteItem.color != null )
              s1 += " color='"+siteItem.color+"'";
            s1 += ">";
            s = s1+s+"</font>";
          }
        if ( siteItem.bold )
          s = "<b>"+s+"</b>";
        if ( siteItem.italic )
          s = "<i>"+s+"</i>";
        outputItemAnchor( siteItem, s );
      }
    _write( "<td width=100%></td>" );
    _write( "</tr>" );
    if ( browser == "IE" )
      {
        _write( "</table>" );
        siteItem.obj = document.all["item"+siteItem.id];
        siteItem.treeIcon = document.all["treeIcon"+siteItem.id];
        siteItem.icon = document.all["icon"+siteItem.id];
        if ( siteItem.text != null )
          siteItem.style = document.all["style"+siteItem.id].style;
      }
    else if ( browser == "NS" )
      {
        _write( "</table>" );
        _writeln( "</layer>" );
        siteItem.obj = document.sitemap.document.layers["item"+siteItem.id];
        siteItem.treeIcon = siteItem.obj.document.images["treeIcon"+siteItem.id];
        siteItem.icon = siteItem.obj.document.images["icon"+siteItem.id];
      }
    else
      _writeln( "</table>" );
    if ( (siteItem.last) || (style != "TREE") )
      left += "<td><img src='../images/blank.gif' width='16' height='16'></td>";
    else
      left += "<td><img src='../images/treeline.gif' width='16' height='16'></td>";
    for ( var i=0; i<siteItem.children.length; i++ )
      renderItem( siteItem.children[i], left );
  }



// ----------------------------------------------------------------------------
// Restores all item icons after the window was resized, under NS.
// ----------------------------------------------------------------------------
function restoreIcons()
  {
    for ( var i=0; i<items.length; i++ )
      updateIcons( items[i] );
  }



// ----------------------------------------------------------------------------
// Selects an item (and deselects whichever is selected).
//
// Parameters:
//   siteItem   item to select
// ----------------------------------------------------------------------------
function selectItem( siteItem )
  {
    if ( selectedItem != null )
      {
        selectedItem.selected = false;
        if ( selectedItem.style != null )
          {
            selectedItem.style.color = "";
            selectedItem.style.backgroundColor = "";
          }
        updateIcons( selectedItem );
      }
    selectedItem = siteItem;
    if ( siteItem != null )
      {
        siteItem.selected = true;
        if ( siteItem.style != null )
          {
            siteItem.style.color = selectedColor;
            siteItem.style.backgroundColor = selectedBackground;
          }
        updateIcons( siteItem );
      }
  }



// ----------------------------------------------------------------------------
// Called when the tree icon of an item is clicked. Expands/collapses the
// item.
//
// Parameters:
//   siteItem   sitemap item that was clicked
// ----------------------------------------------------------------------------
function treeIconClicked( siteItem )
  {
    if ( (browser == "OTHER") || (siteItem.children.length == 0) )
      return;
    if ( siteItem.opened )
      colapse( siteItem );
    else
      expandColapse( siteItem );
    siteItem.opened = !siteItem.opened;
    updateIcons( siteItem );
    adjustY();
  }



// ----------------------------------------------------------------------------
// Updates the icons of an item according to its state.
// ----------------------------------------------------------------------------
function updateIcons( siteItem )
  {
    if ( (style != "LIST") && (siteItem.children.length != 0) )
      siteItem.treeIcon.src = getTreeIcon(siteItem);
    if ( siteItem.icon == null )
      return;
    if ( (siteItem.selected) && (siteItem.selectedIcon != null) )
      siteItem.icon.src = scriptURL+siteItem.selectedIcon;
    else if ( (siteItem.mouseOver) && (siteItem.mouseOverIcon != null) )
      siteItem.icon.src = scriptURL+siteItem.mouseOverIcon;
    else if ( siteItem.opened )
      siteItem.icon.src = scriptURL+siteItem.expandedIcon;
    else
      siteItem.icon.src = scriptURL+siteItem.colapsedIcon;
  }



// ----------------------------------------------------------------------------
// Writes a string to the sitemap document.
// ----------------------------------------------------------------------------
function _write( s )
  {
    if ( browser == "NS" )
      document.sitemap.document.write( s );
    else
      document.write( s );
  }



// ----------------------------------------------------------------------------
// Writes a string and a CRLF to the sitemap document.
// ----------------------------------------------------------------------------
function _writeln( s )
  {
    if ( browser == "NS" )
      document.sitemap.document.writeln( s );
    else
      document.writeln( s );
  }



// ----------------------------------------------------------------------------
// Public section.
//
// The functions in this section comprise the interface between
// the script and outside code.
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Item object; packs together all item data for rendering and managing
// its state.
//
// Parameters:
//   colapsedIcon   item icon or icon to be used when the item is colapsed
//   expandedIcon   icon to be used when the item is expanded
//   mouseOverIcon  icon to be used when the mouse is over the item
//   selectedIcon   icon to be used when the item is selected
//   iconWidth      width of the icons
//   iconHeight     height of the icons
//   text           item text
//   font           item font
//   bold           true to make the font bold
//   italic         true to make the font italic
//   size           font size
//   color          text color
//   message        status bar mouse over message
//   url            item url
//   target         url target
//   last           true if the item is the last children of its parent
// ----------------------------------------------------------------------------
function Item( colapsedIcon, expandedIcon, mouseOverIcon, selectedIcon, iconWidth, iconHeight, text, font, bold, italic, size, color, message, url, target, last )
  {
    this.id = items.length;
    this.colapsedIcon = colapsedIcon;
    this.expandedIcon = expandedIcon;
    this.mouseOverIcon = mouseOverIcon;
    this.selectedIcon = selectedIcon;
    this.iconWidth = iconWidth;
    this.iconHeight = iconHeight;
    this.text = text;
    this.font = font;
    this.bold = bold;
    this.italic = italic;
    this.size = size;
    this.color = color;
    this.message = message;
    this.url = url;
    this.target = target;

    this.opened = false;
    this.selected = false;
    this.mouseOver = false;

    this.children = new Array();
    this.last = last;
    this.obj = null;
    this.treeIcon = null;
    this.icon = null;
    this.style = null;
    this.parent = null;

    items[this.id] = this;
  }



// ----------------------------------------------------------------------------
// Adds a new item to the sitemap.
//
// Parameters:
//   siteItem     item to add
//   parent       item parent (if null, adds to top level)
// ----------------------------------------------------------------------------
function addItem( siteItem, parent )
  {
    if ( parent == null )
      topItems[topItems.length] = siteItem;
    else
      parent.children[parent.children.length] = siteItem;
    siteItem.parent = parent;
  }



// ----------------------------------------------------------------------------
// Generates the HTML code for the entire sitemap. Notice that the "items"
// array must contain the sitemap items in the order they will appear.
//
// Parameters:
//   _style               sitemap style
//   _reserved            reserved for future use
//   _mouseOverColor      color to use when the mouse is over an item
//   _selectedColor       color to use when an item is selected
//   _selectedBackground  background color to use when an item is selected
//   _scriptURL           URL to the script directory
// ----------------------------------------------------------------------------
function renderSitemap( _style, _reserved, _mouseOverColor, _selectedColor, _selectedBackground, _scriptURL )
  {
    if ( document.all )
      browser = "IE";
    else if ( document.layers )
      browser = "NS";

    if ( browser == "OTHER" )
      _style = "LIST";
    style = _style;

    scriptURL = _scriptURL;
    mouseOverColor = _mouseOverColor;
    selectedColor = _selectedColor;
    selectedBackground = _selectedBackground;

    if ( browser == "NS" )
      {
        document.writeln( "<ilayer id='sitemap'></ilayer>" );
        window.onLoad = display;
      }
    else
      display();
  }
  

