/*
Custom Collapsible JavaScript Menu (for City of Sherwood)
Created by Sua T. Thov
Copyrighted (C) 2004 by Pop Art, Inc.
All Rights Reserved.
*/

// MenuItem object, contains information a menu item 
paMenuItem = function( menuId, displayName, linkto, parentId )
{
   this.parentId = parentId;        // contains identifier to parent node if there is one
   this.menuId = menuId;            // unique identifier 
   this.displayName = displayName;  // what will be shown
   this.childItemIds = new Array(); // holds a collection of child menu ids

   this.className = "LeftMenuItem"; // default class name menu item
   this.linkClassName = "LeftMenuLink";  // default class name for the menu item link

   this.linkto = linkto;         // goes in the "href" of the "<a>" tag
   
   this.AddChild = function( childId )
   {
      this.childItemIds[ this.childItemIds.length ] = childId;
   }
}

// The mother object, controls the creation and output of the menu
paMenu = function()
{
   this.ImagePath = "images";       // points to the Image path to find spacer images  
   this.MenuItems = new Array();    // holds the collection of all menu items
   this.RootMenuIds = new Array();  // holds the collection of ids to the top level menu items
   this.ActiveMenuId = null;

   this.RootClassName = "LeftMenuItem";
   this.RootLinkClassName = "LeftMenuLink";

   this.AddMenu = function( menuId, displayName, linkto, parentId )
   {
      if( this.MenuItems[ menuId ] == null )
      {
         if( parentId == null )
         {
            this.MenuItems[ menuId ] = new paMenuItem( menuId, displayName, linkto, null );
            this.RootMenuIds[ this.RootMenuIds.length ] = menuId;
         }
         else
         {
            if( this.MenuItems[ parentId ] != null )
            {
               this.MenuItems[ menuId ] = new paMenuItem( menuId, displayName, linkto, parentId );
               this.MenuItems[ parentId ].AddChild( menuId ); 
            }
            else
            {
               throw( 'Cannot add menu to Menu (' + parentId + ') because it does not exists.' );
            }
         }
      }
      else
      {
         throw( 'Menu (' + menuId + ') already exists.' );
      }
      return this.MenuItems[ menuId ];
   }
   
   this.CreateMenu = function()
   {
      var sBuffer = "";
      var level = 0;

      if( this.RootMenuIds.length > 0 )
      {
         for( var i = 0; i < this.RootMenuIds.length; i++ )
         {
            var menuId = this.RootMenuIds[i];
            var menuObj = this.MenuItems[ menuId ];

            sBuffer += this.RootItemTemplate( menuObj );
            if( menuObj.childItemIds.length > 0 )
            {
               sBuffer += this.CreateSubMenus( menuObj, level + 1 );
            }
         }
      }
      document.write( this.MainTemplateWrapper( sBuffer ) );
   }
   
   this.CreateSubMenus = function( parentObj, level ) {
      var sBuffer =  '<tr id="tr_' + parentObj.menuId + '" class="MenuHide"><td>' +
                 '<table width="100%" cellspacing="0" ' +
                 'cellpadding="0" border="0">';

      for( var i = 0; i < parentObj.childItemIds.length; i++ )
      {
         var menuItemObj = this.MenuItems[ parentObj.childItemIds[i] ];
      
         var className = this.MenuItems[ parentObj.menuId ].className;
         var linkClassName = this.MenuItems[ parentObj.menuId ].linkClassName;

         sBuffer += '<tr><td><div class="' + className + level + '">';
         sBuffer += '<a class="' + linkClassName + level + '" href="' + 
                     menuItemObj.linkto + '">' + menuItemObj.displayName +
                    '</div></td></tr>'; 
      }
      sBuffer += '</table></td></tr>';

      return sBuffer;   
   }
   
   this.MainTemplateWrapper = function( buffer )
   {
      return '<table id="MenuTable" width="100%" cellspacing="0" ' +
             'cellpadding="0" border="0">' +
             buffer + '</table>';
   }
   
   this.RootItemTemplate = function( menuItemObj )
   {
      var className = this.RootClassName;
      var linkClassName = this.RootLinkClassName;
      
      if( menuItemObj.childItemIds.length > 0 && ( menuItemObj.linkto == null ) )
         ahref = 'javascript:ToggleMenu( \'' + menuItemObj.menuId + '\' )';
      else
         ahref = menuItemObj.linkto
      
      sb  = '<tr><td><img src="' + this.ImagePath + 
            '/spacer.gif" width="1" alt="----------" title="" height="10"></td></tr>' +
            '<tr><td><div class="' + className + '">' +
            '<a class="' + linkClassName + '" href="' + 
            ahref + '">' + menuItemObj.displayName + 
            '</a></div></td></tr>';
             
       return sb;
   }
   
   this.SetActiveMenu = function( menuId ) 
   {
      this.ActiveMenuId = menuId;

      obj = document.getElementById( 'tr_' + menuId );
      obj.className = 'MenuShowActive';
   }
}

function ToggleMenu( menuId ) {
   obj = document.getElementById( 'tr_' + menuId );

   menuArray = document.getElementById( "MenuTable" ).getElementsByTagName( "TR" )
   if( obj.className == 'MenuHide' )
   {
      for( var i = 0; i < menuArray.length; i++ )
      {
         if( menuArray[i].className == 'MenuShow' )
         {
            menuArray[i].className = 'MenuHide';
         }
      }
      obj.className = 'MenuShow';
   }
   else if( obj.className == 'MenuShow' )
   {
      obj.className = 'MenuHide';
   }
}

function SubPageTemplateWrapper( buffer )
{
   return '<table id="MenuTable" width="100%" cellspacing="0" ' +
          'cellpadding="0" border="0">' +
          buffer + '<tr><td><img src="images/spacer.gif" ' +
          'width="1" alt="----------" title="" height="150" alt=""></tr></table>';
}
