//
// NAVIBAR.JS
// ~~~~~~~~~~
// This script provides functionality for building dynamic navigation bar
// Features: 
// * Expandable/collapseable sections
// * Cross-browser compatibility including Netscape 4, Netscape 6, MSIE 5+
//
// Copyright © SITE4U
// Author: Eduard Klein 
// This version date: 29 Oct 2006
//
//-------------------------------------------------------

//-------------------------------------------------------
// public use variables
//-------------------------------------------------------

var G_NB_iInitialSelected; // section initially selected
var G_NB_iInitialExpanded; // section initially expanded
var G_NB_iNxtSecTop;      // y-coordinate of the next section top
var G_NB_iButtonWidth;   // width of navigation bar button
var G_NB_iButtonHeight;   // height of navigation bar button
var G_NB_iButtonVSpace;     // space between navigation bar buttons
var G_NB_iButtonHSpace;     // space between navigation bar left edge and navigation button left edge
var G_NB_iCaptionVOffset;   // vertical offset of button text from the button top edge
var G_NB_iCaptionHOffset;  // horizontal offset of button text from the navigation bar left edge
var G_NB_sImageSpacer;      // URL of transparent "spacer" image
var G_NB_iItemsVPadTop;     // vertical space between section and first item
var G_NB_iItemsVPadBottom;  // vertical space between last item and section end
var G_NB_iItemHeight;       // height of section item
var G_NB_iItemBulletW;     // width of section item bullet
var G_NB_iItemBulletH;     // height of section item bullet
var G_NB_sSectionAClass;   // class of section A element
var G_NB_sItemAClass;      // class of item A element

//-------------------------------------------------------
// private use variables
//-------------------------------------------------------

var G_sUA;
var G_NB_oRoot;
var G_NB_iTransitionStep;
var G_NB_iTransitionInterval;

var G_NB_sFixedImg;
var G_NB_sFixedLayer;
var G_NB_sFixedSrc;

var G_NB_aLayerIds;
var G_NB_aMinHeights;
var G_NB_aMaxHeights;
var G_NB_aUrls;
var G_NB_aTargets;
var G_NB_aExpandable;
var G_NB_aBtnImgOff;
var G_NB_aBtnImgOn;
var G_NB_aImgPreload;

var G_NB_iExpanded;       // index of layer currently expanded which is to be collapsed, -1 if none
var G_NB_iCollapsed;      // index of layer currently collapsed which is to be expanded, -1 if none
var G_NB_iPhase;          // transition phase: 0=collapse previous, 1=expand next, -1=idle

var G_NB_sSectionHtml;
var G_NB_iItemCount;
var G_NB_iNxtSecNdx;       // number of the next section (1-based)
var G_NB_iCustomDivCount;  // number of added custom DIV's

var G_NB_sHTML;

//-------------------------------------------------------
// public use functions
//-------------------------------------------------------

function NB_Open()
{
  if( document.layers )
    G_sUA = 'NS4';
  else if( document.all )
    G_sUA = 'IEX';
  else 
    G_sUA = 'NS6';

  G_NB_oRoot = ( ( G_sUA == 'NS4') ? document : null );
  G_NB_iTransitionStep = 10;
  G_NB_iTransitionInterval = 25;

  G_NB_sFixedImg = '';
  G_NB_sFixedLayer = '';
  G_NB_sFixedSrc = '';

  G_NB_aLayerIds = new Array();
  G_NB_aMinHeights = new Array();
  G_NB_aMaxHeights = new Array();
  G_NB_aUrls = new Array();
  G_NB_aTargets = new Array();
  G_NB_aExpandable = new Array();
  G_NB_aBtnImgOff = new Array();
  G_NB_aBtnImgOn = new Array();
  G_NB_aImgPreload = new Array();

  G_NB_iExpanded = -1;  
  G_NB_iCollapsed = -1; 
  G_NB_iPhase = -1;     

  G_NB_sSectionHtml = "";
  G_NB_iItemCount = 0;
  G_NB_iNxtSecNdx = 1;        
  G_NB_iCustomDivCount = 0;  

  G_NB_sHTML = '';
}

function NB_AddSection(p_sCaption, p_sUrl, p_sTarget, p_iHeight, p_sImageOff, p_sImageOn, p_bHasItems)
{
  //window.alert('AddSection');
  var sHtml = '';

  NB_PreloadImg(p_sImageOff);
  NB_PreloadImg(p_sImageOn);

  G_NB_aExpandable[G_NB_aExpandable.length] = p_bHasItems;
  G_NB_aBtnImgOn[G_NB_aBtnImgOn.length] = p_sImageOn;
  G_NB_aBtnImgOff[G_NB_aBtnImgOff.length] = p_sImageOff;

  var h = ( ( p_iHeight < 0 ) ? G_NB_iButtonHeight : p_iHeight );
  var sAClass = ( ( G_NB_sSectionAClass != '' ) ? ' class="' + G_NB_sSectionAClass + '" ' : '' );
 
  sHtml += '<div ID="divImg' + G_NB_iNxtSecNdx + '" style="position:relative;left:' + G_NB_iButtonHSpace + 'px;top:' + G_NB_iNxtSecTop + 'px;width:' + G_NB_iButtonWidth + 'px;height:' + h + 'px;z-index:2;"><a \n';
  sHtml += 'href="javascript:NB_Go(\'' + p_sUrl + '\',\'' + p_sTarget + '\', \'divImg' + G_NB_iNxtSecNdx + '\',\'img' + G_NB_iNxtSecNdx + '\',\'' + p_sImageOn + '\',\'' + p_sImageOff + '\',' + G_NB_iNxtSecNdx + ')" \n';
  sHtml += 'onmouseover="NB_SetImg(\'divImg' + G_NB_iNxtSecNdx + '\',\'img' + G_NB_iNxtSecNdx + '\',\'' + p_sImageOn + '\')"  \n';
  sHtml += 'onmouseout="NB_SetImg(\'divImg' + G_NB_iNxtSecNdx + '\',\'img' + G_NB_iNxtSecNdx + '\',\'' + p_sImageOff + '\')"><img  \n';
  sHtml += 'name="img' + G_NB_iNxtSecNdx + '" src="' + p_sImageOff + '" border=0 width=' + G_NB_iButtonWidth + ' height=' + h + ' \n';
  sHtml += '></a></div> \n';
  sHtml += '<div class=MenuBg ID="divRef' + G_NB_iNxtSecNdx + '" style="text-align:right;padding:0px 18px 0px 8px; position:relative;left:' + G_NB_iCaptionHOffset + 'px;top:' + (G_NB_iNxtSecTop+G_NB_iCaptionVOffset).toString() + 'px;z-index:3;"><a \n';
  sHtml += 'href="javascript:NB_Go(\'' + p_sUrl + '\',\'' + p_sTarget + '\', \'divImg' + G_NB_iNxtSecNdx + '\',\'img' + G_NB_iNxtSecNdx + '\',\'' + p_sImageOn + '\',\'' + p_sImageOff + '\',' + G_NB_iNxtSecNdx + ')" \n';
  sHtml += sAClass + '\n';
  sHtml += 'onmouseover="NB_SetImg(\'divImg' + G_NB_iNxtSecNdx + '\',\'img' + G_NB_iNxtSecNdx + '\',\'' + p_sImageOn + '\')"  \n';
  sHtml += 'onmouseout="NB_SetImg(\'divImg' + G_NB_iNxtSecNdx + '\',\'img' + G_NB_iNxtSecNdx + '\',\'' + p_sImageOff + '\')">' + p_sCaption + '</a></div> \n';

  G_NB_sHTML += sHtml;
  //document.write(sHtml);

  NB_RegisterSection('divImg' + G_NB_iNxtSecNdx, -1, 0, '', '');
  NB_RegisterSection('divRef' + G_NB_iNxtSecNdx, -1, 0, '', '');
  G_NB_iNxtSecTop += h + G_NB_iButtonVSpace;

  if( p_bHasItems )
  {
    G_NB_iItemCount = 0;
    G_NB_sSectionHtml = '<div ID="divSec' + G_NB_iNxtSecNdx + '" style="padding:0px 40px 0px 0px; position:relative;left:' + G_NB_iButtonHSpace + 'px;top:' + G_NB_iNxtSecTop + 'px;width:' + G_NB_iButtonWidth + 'px;height:1px;z-index:2;overflow-y:hidden"> \n';
    G_NB_sSectionHtml += '<table cellspacing=0 cellpadding=0 border=0 width=' + G_NB_iButtonWidth + '> \n';
    G_NB_sSectionHtml += '<tr><td><img src="' + G_NB_sImageSpacer + '" width=' + G_NB_iButtonWidth + ' height=' + G_NB_iItemsVPadTop + '></td></tr> \n';
  }
  else
  {
    G_NB_iNxtSecNdx++;
  }
}

function NB_AddSectionItem(p_sCaption, p_sUrl, p_sTarget, p_sBulletImage)
{
  var sAClass = ( ( G_NB_sItemAClass != '' ) ? ' class="' + G_NB_sItemAClass + '" ' : '' );

  G_NB_sSectionHtml += '<tr> \n';
  G_NB_sSectionHtml += '<td valign=top width=' + G_NB_iButtonWidth + ' height=' + G_NB_iItemHeight + '><a \n';
  G_NB_sSectionHtml += 'href="javascript:NB_Go(\'' + p_sUrl + '\', \'' + p_sTarget + '\', \'divImg' + G_NB_iNxtSecNdx +'\',\'img' + G_NB_iNxtSecNdx + '\',\'' + G_NB_aBtnImgOn[G_NB_iNxtSecNdx-1] + '\',\'' + G_NB_aBtnImgOn[G_NB_iNxtSecNdx-1] + '\',' + G_NB_iNxtSecNdx + ')"  \n';
  G_NB_sSectionHtml += sAClass + '><img src=' + p_sBulletImage + ' width=' + G_NB_iItemBulletW + ' height=' + G_NB_iItemBulletH + ' border=0 align=absmiddle>' + p_sCaption + '</a></td> \n';
  G_NB_sSectionHtml += '</tr> \n';
  G_NB_iItemCount++;
}

function NB_EndSection()
{
  G_NB_sSectionHtml += '</table></div>\n';
  G_NB_sHTML += G_NB_sSectionHtml;
  //document.write(G_NB_sSectionHtml);
  NB_RegisterSection('divSec' + G_NB_iNxtSecNdx, 1, G_NB_iItemCount * G_NB_iItemHeight + G_NB_iItemsVPadTop + G_NB_iItemsVPadBottom, '', '');
  G_NB_iNxtSecNdx++;
  G_NB_iItemCount = 0;
}

function NB_AddCustom(p_iLeft, p_iWidth, p_iPadTop, p_iHeight, p_iPadBottom, p_sHtml)
{
  //window.alert('AddCustom');
  G_NB_iCustomDivCount++;

  var sHtml = '';
  var iTop = G_NB_iNxtSecTop + p_iPadTop;
  var sId = 'idCU' + G_NB_iCustomDivCount;

  sHtml += '<div ID="' + sId + '" style="position:relative;left:' + p_iLeft + 'px;top:' + iTop + 'px;widht:' + p_iWidth + 'px;height:' + p_iHeight + 'px">';
  sHtml += p_sHtml;
  sHtml += '</div>\n';
  G_NB_sHTML += sHtml;
  //document.write(sHtml);

  NB_RegisterSection(sId, -1, 0, '', '');
  G_NB_iNxtSecTop += p_iPadTop + p_iHeight + p_iPadBottom;
}

function NB_InitialSelection()
{
  if( G_NB_iInitialSelected > 0 )
  {
    NB_FixImg(
      'divImg' + G_NB_iInitialSelected, 'img' + G_NB_iInitialSelected,
      G_NB_aBtnImgOn[G_NB_iInitialSelected-1], G_NB_aBtnImgOff[G_NB_iInitialSelected-1]
    );
  }
  if( G_NB_iInitialExpanded > 0 )
  {
    NB_ExpandSection('divSec' + G_NB_iInitialExpanded);
  }
}

function NB_Close()
{
  document.write(G_NB_sHTML);
  G_NB_sHTML = '';

  var i = 0;
  for( i = 0 ; i < G_NB_aLayerIds.length ; i++ )
  {
    if( G_NB_aMinHeights[i] >= 0 )
      NB_SetHeight(NB_GetLayer(G_NB_aLayerIds[i]), G_NB_aMinHeights[i]);
  }
}

//-------------------------------------------------------
// private use functions
//-------------------------------------------------------

function NB_PreloadImg(p_sUrl)
{
  var x = G_NB_aImgPreload.length;
  G_NB_aImgPreload[x] = new Image();
  G_NB_aImgPreload[x].src = p_sUrl;
}

function NB_SetImg(sLayer,sImg,sSrc)
{
  if( sImg == G_NB_sFixedImg )
    return;
  //if( G_sUA == 'NS4' )
  //  document.layers[sLayer].document.images[sImg].src = sSrc;
  //else
  //  document.images[sImg].src = sSrc;
  if( G_sUA != 'NS4' )
    document.images[sImg].src = sSrc;
  else
    document.layers[sLayer].document.images[sImg].src = sSrc;
}


function NB_FixImg(sLayer,sImg,sFixedSrc,sNormalSrc)
{
  var sFixedImg = G_NB_sFixedImg;
  if( sFixedImg != '' )
  {
    G_NB_sFixedImg = '';
    NB_SetImg(G_NB_sFixedLayer, sFixedImg, G_NB_sFixedSrc);
  }
  
  NB_SetImg(sLayer, sImg, sFixedSrc);
  G_NB_sFixedImg = sImg;
  G_NB_sFixedLayer = sLayer;
  G_NB_sFixedSrc = sNormalSrc;
}

function NB_Go(sUrl, sTarget, sLayer, sImg, sFixedSrc, sNormalSrc, iSection)
{
  var sSecId = '';

  if( sImg != '' )
    NB_FixImg(sLayer, sImg, sFixedSrc, sNormalSrc);
  if( iSection > 0 )
{
  if( G_NB_aExpandable[iSection-1] )
      NB_ExpandSection('divSec' + iSection);  
    else
      NB_ExpandSection('divImg' + iSection);
  }
  if( sUrl != '' )
    NB_OpenUrl(sTarget, sUrl);
}     

//-------------------------------------------------------

function NB_RegisterSection(p_sId, p_iMinHeight, p_iMaxHeight, p_sUrl, p_sTarget)
{
  G_NB_aLayerIds[G_NB_aLayerIds.length] = p_sId;
  G_NB_aMinHeights[G_NB_aMinHeights.length] = p_iMinHeight;
  G_NB_aMaxHeights[G_NB_aMaxHeights.length] = p_iMaxHeight;
  G_NB_aUrls[G_NB_aUrls.length] = p_sUrl;
  G_NB_aTargets[G_NB_aTargets.length] = p_sTarget;
//  if( p_iMinHeight >= 0 )
//    NB_SetHeight(NB_GetLayer(p_sId), p_iMinHeight);
}

//-------------------------------------------------------

function NB_ExpandSection(p_sId)
{
  if( G_NB_iPhase >= 0 )
    return; //still busy

  var iIndex = -1;
  var i = 0;

  for( i = 0 ; i < G_NB_aLayerIds.length ; i++ )
  {
    if( G_NB_aLayerIds[i] == p_sId )
    {
      iIndex = i;
      break;
    }
  }
  if( G_NB_iExpanded == iIndex || iIndex < 0 )
    return;

  G_NB_iPhase = ( ( G_NB_iExpanded >= 0 ) ? 0 : 1 );
  G_NB_iCollapsed = iIndex;
  NB_TransitionStep();
}

//-------------------------------------------------------

function NB_CollapseStep()
{
  if( G_NB_aMinHeights[G_NB_iExpanded] < 0 )
    return( false );

  var oLayer = NB_GetLayer(G_NB_aLayerIds[G_NB_iExpanded]);
  var h = NB_GetHeight(oLayer);
  var dh = G_NB_iTransitionStep;
  var i = 0;

  if( h - dh < G_NB_aMinHeights[G_NB_iExpanded] )
    dh = h - G_NB_aMinHeights[G_NB_iExpanded];

  if( dh > 0 )
  {
    NB_ResizeBy(oLayer, -dh);
    for( i = G_NB_iExpanded + 1 ; i < G_NB_aLayerIds.length ; i++ )
      NB_MoveBy(NB_GetLayer(G_NB_aLayerIds[i]), -dh);
  }

  return( NB_GetHeight(oLayer) > G_NB_aMinHeights[G_NB_iExpanded] );
}

function NB_ExpandStep()
{
  if( G_NB_aMinHeights[G_NB_iCollapsed] < 0 )
    return( false );

  var oLayer = NB_GetLayer(G_NB_aLayerIds[G_NB_iCollapsed]);
  var h = NB_GetHeight(oLayer);
  var dh = G_NB_iTransitionStep;
  var i = 0;

  if( h + dh > G_NB_aMaxHeights[G_NB_iCollapsed] )
    dh = G_NB_aMaxHeights[G_NB_iCollapsed] - h;

  if( dh > 0 )
  {
    NB_ResizeBy(oLayer, dh);
    for( i = G_NB_iCollapsed + 1 ; i < G_NB_aLayerIds.length ; i++ )
      NB_MoveBy(NB_GetLayer(G_NB_aLayerIds[i]), dh);
  }

  return( NB_GetHeight(oLayer) < G_NB_aMaxHeights[G_NB_iCollapsed] );
}

function NB_TransitionStep()
{
  if( G_NB_iPhase == 0 && !NB_CollapseStep() )
    G_NB_iPhase = 1;
  if( G_NB_iPhase == 1 && !NB_ExpandStep() )
  {
    G_NB_iPhase = -1;
    G_NB_iExpanded = G_NB_iCollapsed;
    G_NB_iCollapsed = -1;
    if( G_NB_aUrls[G_NB_iExpanded] != '' )
      NB_OpenUrl(G_NB_aTargets[G_NB_iExpanded], G_NB_aUrls[G_NB_iExpanded]);
  }
  if( G_NB_iPhase >= 0 )
    setTimeout('NB_TransitionStep()', G_NB_iTransitionInterval);
}

function NB_OpenUrl(p_sTarget, p_sUrl)
{
  if( p_sTarget == '' || p_sTarget == '_self' )
    window.location.href = p_sUrl;
  else if( p_sTarget == '_blank' )
    window.open(p_sUrl);
  else
    window.top.frames[p_sTarget].location.href = p_sUrl;
}

//-------------------------------------------------------
// cross-browser compatibility functions
//-------------------------------------------------------

function NB_GetLayer(p_sId)
{
  if( G_sUA == 'NS4' ) return( NB_GetLayer_NS4(p_sId) );
  else if( G_sUA == 'IEX' ) return( NB_GetLayer_IEX(p_sId) );
  else return( NB_GetLayer_NS6(p_sId) );
}

function NB_GetHeight(p_oLayer)
{
  if( G_sUA == 'NS4' ) return( NB_GetHeight_NS4(p_oLayer) );
  else if( G_sUA == 'IEX' ) return( NB_GetHeight_IEX(p_oLayer) );
  else return( NB_GetHeight_NS6(p_oLayer) );
}

function NB_SetHeight(p_oLayer, p_iHeight)
{
  if( G_sUA == 'NS4' ) NB_SetHeight_NS4(p_oLayer, p_iHeight);
  else if( G_sUA == 'IEX' ) NB_SetHeight_IEX(p_oLayer, p_iHeight);
  else NB_SetHeight_NS6(p_oLayer, p_iHeight);
}

function NB_ResizeBy(p_oLayer, p_iDH)
{
  if( G_sUA == 'NS4' ) NB_ResizeBy_NS4(p_oLayer, p_iDH);
  else if( G_sUA == 'IEX' ) NB_ResizeBy_IEX(p_oLayer, p_iDH);
  else NB_ResizeBy_NS6(p_oLayer, p_iDH);
}

function NB_MoveBy(p_oLayer, p_iDY)
{
  if( G_sUA == 'NS4' ) NB_MoveBy_NS4(p_oLayer, p_iDY);
  else if( G_sUA == 'IEX' ) NB_MoveBy_IEX(p_oLayer, p_iDY);
  else NB_MoveBy_NS6(p_oLayer, p_iDY);
}

//-------------------------------------------------------

function NB_GetLayer_NS4(p_sId)
{
  return( G_NB_oRoot.layers[p_sId] );
}

function NB_GetLayer_IEX(p_sId)
{
  return( document.all[p_sId] );
}

function NB_GetLayer_NS6(p_sId)
{
  return( document.getElementById(p_sId) );
}

//-------------------------------------------------------

function NB_GetHeight_NS4(p_oLayer)
{
  return( p_oLayer.clip.height );
}

function NB_GetHeight_IEX(p_oLayer)
{
  return( p_oLayer.style.pixelHeight );
}

function NB_GetHeight_NS6(p_oLayer)
{
  return( parseInt(p_oLayer.style.height) );
}

//-------------------------------------------------------

function NB_SetHeight_NS4(p_oLayer, p_iHeight)
{
  p_oLayer.clip.height = p_iHeight;
}

function NB_SetHeight_IEX(p_oLayer, p_iHeight)
{
  p_oLayer.style.pixelHeight = p_iHeight;
}

function NB_SetHeight_NS6(p_oLayer, p_iHeight)
{
  var sLeft = p_oLayer.style.left;
  if( sLeft == '' )
    sLeft = '0px';
  p_oLayer.style.height = p_iHeight.toString() + 'px';
  p_oLayer.style.clip = 'rect(' + sLeft + ' ' + p_oLayer.style.width + ' ' + p_iHeight + 'px 0px)';
}

//-------------------------------------------------------

function NB_ResizeBy_NS4(p_oLayer, p_iDH)
{
  p_oLayer.resizeBy(0, p_iDH);
}

function NB_ResizeBy_IEX(p_oLayer, p_iDH)
{
  p_oLayer.style.pixelHeight = p_oLayer.style.pixelHeight + p_iDH;
}

function NB_ResizeBy_NS6(p_oLayer, p_iDH)
{
  NB_SetHeight_NS6(p_oLayer, parseInt(p_oLayer.style.height) + p_iDH);
}

//-------------------------------------------------------

function NB_MoveBy_NS4(p_oLayer, p_iDY)
{
  p_oLayer.moveBy(0, p_iDY);
}

function NB_MoveBy_IEX(p_oLayer, p_iDY)
{
  p_oLayer.style.pixelTop = p_oLayer.style.pixelTop + p_iDY;
}

function NB_MoveBy_NS6(p_oLayer, p_iDY)
{
  p_oLayer.style.top = parseInt(p_oLayer.style.top) + p_iDY;
}

