function GubaMenu( menuArg, menuTypeArg ) {
   this.menu = menuArg;
   menuType = menuTypeArg;
   this.children = new Array();
   host = this.menu.host;
   contentType = this.menu.contentType;
   categoryPathPrefix = '/' + menuType + '/' + contentType.replace (/s$/,'') + '/';

   this.init = function()
   {
      var kidCount = 0;
      for ( var i = 0; i < this.menu.categories.length; i++ ) {
         if ( !this.menu.categories[ i ].authRequired
            || d_menu_data.auth_info.value != 'guest' )
         {
             this.children[ kidCount++ ] = new Category( this.menu.categories[ i ], this );
         }
      }
   }

   this.draw = function( targetArg )
   {
      if ( arguments.length > 0 ) {
         target = targetArg;
      }

      var output = '<table width="165" cellpadding="0" cellspacing="0" border="0">';
      
      for ( var i = 0; i < this.children.length; i++ ) {
         output += this.children[ i ].draw();
      }
      
      output += '</table>';

      document.getElementById( target ).innerHTML = output;
   }
}

function Category( menuItemObjArg, parentRefArg ) {
   this.menuItemObj = menuItemObjArg;
   this.name = this.menuItemObj.name;
   this.page = null;
   this.children = new Array();
   this.itemArray = this.menuItemObj.items;
   this.className = this.menuItemObj.className;
   this.parentRef = parentRefArg;
   this.id = "menu_" + ++menuNum;
   this.itemLinkPrefix = null;
   this.open = false;

   this.selected = false;

   menuContainerMap[ this.id ] = this;
   this.itemLinkPrefix = categoryPathPrefix + escape( this.name ) + '/';

   if ( this.menuItemObj.page != 'directLink' ) {
     this.page = makeLinkUrl( this.menuItemObj.page, this.itemLinkPrefix );
   }
   else {
     this.page = this.menuItemObj.url;
   }
   
   if ( this.itemArray ) {
      this.checkOpen( this.page );
      subMenuNum = 0;

      for ( var i = 0; i < this.itemArray.length; i++ )
      {
         var linkUrl;
         if ( this.itemArray[ i ].page != 'directLink' ) {
            linkUrl = makeLinkUrl( this.itemArray[ i ].page, categoryPathPrefix + escape( this.name ) + '/' );
         }
         else {
            linkUrl = '/' + menuType + this.itemArray[ i ].url;
         }
         this.children[ i ] = new SubCategory( this.itemArray[ i ].name, linkUrl, this );
         this.checkOpen( this.children[ i ].page );
      }
   }

   this.isRoot = function()
   {
      return this.name == 'root'
         && this.parentRef == null;
   }

   this.hasChildren = function()
   {
      return this.children != null
         && this.children.length > 0;
   }

   this.isVisible = function()
   {
      return this.isRoot()
            || this.hasChildren()
            || this.visibility;
   }

   this.draw = function() {
      var className = 'categoryItem';

      if ( this.open ) {
         className += 'Open';
		}

      if ( this.className ) {
        className = this.className;
      }

      var output = '<tr class="' + menuType + 'MenuBar ' + className + '">'
         + '<td width="19" id="td1_' + this.id + '" ';

         if ( this.hasChildren() ) {
		    output += 'onclick="openMenu( \'' + this.id + '\' )"';
         }
         
         output += 'class="' + className + ' ' + menuType + '_left"'
		 + 'onMouseOver="hoverRow( \'td1_' + this.id + '\', \'' + menuType + 'Hover_left\' );" '
         + 'onMouseOut="hoverRow( \'td1_' + this.id + '\', \'' + menuType + '_left\' );">';

      if ( this.hasChildren() ) {
         output += '<img id="img_' + this.id + '" ';
         
         if ( this.open ) {
            output += 'src="/art/img_arrow_down.gif" style="padding-right:7px;"';
         }
         else {
            output += 'src="/art/img_arrow_right.gif"';
         }
         
         output += ' border="0">';
      }
      else {
         output += '&nbsp;';
      }
      output += '</td>'
         + '<td id="td2_' + this.id + '" '
         + 'class="' + className + ' ' + menuType + '_right"'
		 + 'onclick="document.location=(\'' + this.page + '\')"'
         + 'onMouseOver="hoverRow( \'td2_' + this.id + '\', \'' + menuType + 'Hover\' );" '
		 + 'onMouseOut="hoverRow( \'td2_' + this.id + '\', \'' + menuType + '_right\' );">'
         + this.name
         + '</td>'
         + '</tr>';
         
         if ( this.open )
         {
            for ( var i = 0; i < this.children.length; i++ )
            {
               output += this.children[ i ].draw();
            }
         }

      return output;
   }

   this.setOpen = function( isOpen )
   {
      this.open = isOpen;

      if ( isOpen )
      {
         document.getElementById( 'img_' + this.id ).src = '/art/menu_arrow_down.gif';
      }
      else {
         document.getElementById( 'img_' + this.id ).src = '/art/menu_arrow_right.gif';
      }
   }

}

Category.prototype.checkOpen = function( checkPage ) {
   if ( currentLocation.indexOf( this.page ) > -1 ) {
      currentOpenMenu = this;
      this.open = true;
   }
}

function SubCategory( nameArg, pageArg, parentRefArg )
{
   this.name = nameArg;
   this.page = pageArg;
   this.parentRef = parentRefArg;
   this.id = "menu_" + menuNum + "_" + subMenuNum++;

   this.visibility = false;
   this.selected = false;

   if ( isSelectedPage( this.page ) ) {
      this.selected = true;
      this.parentRef.open = true;
      currentOpenMenu = this.parentRef;
   }
   
   this.isRoot = function() {
      return false;
   }
   
   this.hasChildren = function() {
      return false;
   }

   this.setSelected = function( selectedArg ) {
      this.selected = selectedArg;
   }

   this.draw = function() {
      var className = 'subCategoryItem';

      if ( this.selected ) {
         className += 'Selected';
      }
      
      var output = '<tr id="subRow_' + this.id + '" '
        + 'class="' + className + '" '
        + 'onMouseOver="hoverRow( \'subRow_' + this.id + '\', \'' + className + 'Hover\' );" '
        + 'onMouseOut="hoverRow( \'subRow_' + this.id + '\', \'' + className + '\' );">'
        + '<td width="15">&nbsp;</td>'
        + '<td>'
        + '<a href="' + this.page + '">';
      
      if ( this.selected ) {
        output += '<b>';
      }
      
      output += this.name;
      
      if ( this.selected ) {
        output += '</b>';
      }
      
      output += '</a>'
        + '</td>'
        + '</tr>';

      return output;
   }
}

var menuContainerMap = new Object();
var menuNum = 0;
var subMenuNum = 0;
var currentOpenMenu = null;
var currentLocation = String( document.location );
var target = null;
var host;
var contentType;
var categoryPathPrefix;
var menuType;

function isSelectedPage( menuPageUrl ) {
    return currentLocation.indexOf( escape( menuPageUrl ) ) > -1;
}

function openMenu( menuId ) {
   // clearTimeout( trackOne );
   // clearTimeout( trackTwo );

   menuRef = menuContainerMap[ menuId ];

   if ( currentOpenMenu != null ) {
      if ( menuRef != currentOpenMenu ) {
         currentOpenMenu.setOpen( false );
      }
   }

   menuRef.setOpen( !menuRef.open );
   // trackOne = setTimeout( runOne, 50 );

   gubaMenu.draw();
   // trackTwo = setTimeout( runTwo, 100 );

   currentOpenMenu = menuRef;
}

function hoverRow( rowId, hoverClass ) {
   document.getElementById( rowId ).className = hoverClass;
}

function makeLinkUrl( linkPath, itemLinkPrefix )
{
    return ( linkPath.search( /^[a-z]+:\/\//i ) > -1 ? '' : ( ( typeof host == 'string' ? host : '' )
        + ( linkPath.charAt( 0 ) == '/' || !itemLinkPrefix ? '' : itemLinkPrefix))) + linkPath;
}
