//==============================================================================
//
//     Copyright (c) 2006, Deluxe Menu
//     http://deluxe-menu.com
//
//     The addon for Netscape 4
//
//==============================================================================


function dm_init4()
// Creates a menu
{
    if (popupMode || curMenu.ind>0) return;
    dm_checkGlobalParams();                           // check and correct users' parameters

    dm_createMenuParams(curMenu.ind);                 // create common params of the menu
    if (!curMenu.ind) dm_setOnLoad();
    dm_parseItemsData();

    // Create HTML code
    var s      = '';
    var ss     = '';
    var menu   = dm_menu[curMenu.ind];
    var smVar  = null;
    var pos    = menu.absPos    ? 'absolute':'static';
    var vis    = menu.isVisible ? 'visible':'hide';
    var zOrder = 1000;
    var smCnt  = menu.m.length;
    var colInd = -1;
    var addRow = 0;

    // Correct top menu width
    if (menu.m[0].mainWidth=='0') menu.m[0].mainWidth = '1';


    for (var smInd=0; smInd<smCnt; smInd++)        // run submenus
    {
        smVar = menu.m[smInd];

        // Create submenu DIV --------------------------------------------------
        with (smVar)
            s += '<LAYER ID="'+id+'" position='+pos+' bgcolor='+smStyle.backColor +
                 ((pos=='absolute')?' left='+left+' top='+top : '') + ' visibility='+vis+' z-index='+zOrder+'>';


        // Create submenu table ------------------------------------------------
        with (smVar.smStyle)
            var tblStyle = 'width='+(smInd ? (smW?smW:'1') : smVar.mainWidth)+' background="'+backImage+'" border='+brdWidth;

        s += dm_openTableSimple(smVar.id+'tbl', smVar.smStyle.itSpacing, 0, '', '', tblStyle);


        colInd = -1;
        addRow = 1;


        // RUN SUBMENU ITEMS ===================================================
        for (var iInd=0; iInd<smVar.i.length; iInd++)
        {
            ss = '';
            var itVar = smVar.i[iInd];
            ss += dm_openTdSimple(itVar.id+'td', '', '');


            // Create columns --------------------------------------------------
            if (smVar.smStyle.columnsCnt>1)
            {
                colInd++;
                if (colInd==smVar.smStyle.columnsCnt)
                {
                    colInd = 0;
                    addRow = 1;
                }
                if (colInd>0)
                {
                    addRow = 0;
                    ss += dm_closeTd() + dm_openTdSimple('col'+smInd+'i'+iInd, 'padding: 0px;', '');
                }
            }

            // Create new item row for a vertical submenu or for item with '$' in item text
            if (addRow)
                if ((iInd && !smVar.isHoriz) || itVar.nextRow) s += '</TR><TR>'


            // Create item table ===============================================
            ss += dm_createItemTable(menu, smVar, itVar);


            s += ss + dm_closeTd();
        }// for (items)

        s += dm_closeTable() + '</LAYER>';

        // Write a top menu

        pos = 'absolute';
        vis = 'hide';
        zOrder += 10;

    }// for (submenus)

    document.write(s);

    s = '';
    curMenu.ind++;
    curMenu.curPressedIt = -1;
}//end dm_init()


function dm_createItemTable(menu, smVar, itVar)
// Creates item table (menu item object)
{
  var ss = '';

  // Create events ---------------------------------------------------
  var events = ' onMouseOver="dm_over(event,\''+itVar.id+'\')" '+
               ' onMouseOut="dm_out(event,\''+itVar.id+'\')" ';

  with (itVar.itStyle) var itW = (itWidth?itWidth:'100%');
  
  with (itVar)
      ss += '<ILAYER ID="'+id+'" width='+itW+'><LAYER width='+itW+'><font style="font-size:1px">';


  // Create 2 item layers
  for (var jj=0; jj<2; jj++)
  {
      ss += '<LAYER ID="'+itVar.id+'sub'+jj+'" height=1 width=100% visibility='+(jj?'hide':'show') + events + '>';


      with (itVar.itStyle)
      {
          // Make a border
          ss += "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR='" + brdColor[0] + "' height=1><TR><TD width=100%>";
          ss += "<TABLE WIDTH=100% BORDER=0 CELLSPACING=" + (brdWidth-2) + " CELLPADDING=" + brdWidth + " height=1><TR><TD width=100%>";

          var tblStyle = 'width=100% height=1 bgcolor="'+backColor[jj]+'" background="'+backImage[jj]+'" ';
      }
      ss += dm_openTableSimple(itVar.id+'tbl', 0, smVar.smStyle.itPadding, '', '', tblStyle);



      // Create item cells =====================================================
      if (itVar.text=="-")                       // item is separator
      {
          ss += dm_openTdSimple(itVar.id+'tdText', 'padding:0px; text-align:'+separatorAlignment, ' height=1 valign=middle');
          ss += '<font style="font-size:1px">';
          if (itVar.smInd>0) ss += dm_openSeparator(separatorImage,  separatorWidth,  separatorHeight);
          else               ss += dm_openSeparator(separatorVImage, separatorVWidth, separatorVHeight);
          ss += '</font>';
          ss += dm_closeTd();
      }
      else
      {
          // Create item arrow-image -------------------------------------------
          if (menu.smView>=2 && itVar.childID) ss += dm_openItemArrow4(itVar, 0);


          // Create item icon --------------------------------------------------
          if (jj && !itVar.icon[jj]) itVar.icon[jj] = itVar.icon[0];

          if (itVar.icon[jj]) ss += dm_openTdSimple(itVar.id+'tdIcon', 'padding:0px;', 'align=center valign=middle') +
                                   dm_openImg(itVar.id+"icon", itVar.icon[jj], itVar.iconW, itVar.iconH, ' align=left')+
                                   dm_closeTd();
          else
              ss += dm_openTdSimple('', 'padding:0px;', '') +
                    dm_openImg('', blankImage, 1, 1, '')+
                    dm_closeTd();


          // Create item text --------------------------------------------------
          if (itVar.text)
          {
              with (itVar.itStyle)
              {
                  var textStyle = 'text-align:'+itVar.align;

                  ss += dm_openTdSimple(itVar.id+'tdText', textStyle, (noWrap?' NOWRAP ':'') + 'width=100%')+

                        ((jj && itVar.link)?'<a id="'+itVar.id+'link" href="#" onClick="dm_click(event,\''+itVar.id+'\');">' : '')+
                        '<font style="font:'+fntStyle+'; color:'+(itVar.isDisabled?menu.fntColorDisabled:fntColor[jj])+'; text-decoration:'+fntDecor[jj]+';">'+
                        itVar.text+'</font>'+
                        ((jj && itVar.link)?'</a>':'')+
                        
                        dm_closeTd();
              }
          }

          // Create item arrow-image -------------------------------------------
          if (menu.smView<2 && itVar.childID) ss += dm_openItemArrow4(itVar, 0);
      }


      ss += dm_closeTable() + '</td></tr></table> </td></tr></table>';
      ss += '</LAYER>';
  }
  ss += "</font></LAYER></ILAYER>"

  return ss;
}


function dm_openItemArrow4(itVar)
{
  var s='';
  var smVar = dm_menu[itVar.mInd].m[itVar.smInd];
  with (itVar)
  {
      var arrImg = (smInd?itStyle.arrowSub[0]:itStyle.arrowMain[0]);
      if (arrImg)
          var s = dm_openTdSimple(itVar.id+'tdArrow', 'padding:0px;', ' align=center')+
                  dm_openImg(id+"arrow", arrImg, arrowW, arrowH, '')+
                  dm_closeTd();
  }
  return s;
}


function dm_getObjectByID4(id)
{
  var e = (id.indexOf("i")>0) ? ex=regexp_i.exec(id) : regexp_m.exec(id);
  var l = document.layers[id];
  if (!l && e) l = document.layers[e[2]].document.layers[id];
  return l;
}


function dm_showMenu4(id)
{
    if (!id) return;
    var smVar = dm_getVarByID(id);
    var menu  = dm_menu[smVar.mInd];
    var smObj = dm_getObjectByID(id);

    var smXY  = dm_getSubmenuXY(smVar);
    var it    = dm_getVarByID(smVar.rootItemID);

    with (smObj)
    {
        left = smXY[0];
        top  = smXY[1];
    }

    // Highlight selected item -------------------------------------------------
    menu.m[it.smInd].shownChildID = id;
    it.isHighlighted = menu.saveNavigation;

    with (smObj)
    {
        left = smXY[0];
        top  = smXY[1];
        if (visibility != "show") visibility="show";
    }
}


function dm_hideMenu4(id)
{
    if (!id) return;
    var smObj = dm_getObjectByID(id);
    var smVar = dm_getVarByID(id);
    var menu  = dm_menu[smVar.mInd];

    // Hide all visible submenus
    if (smVar.shownChildID) dm_hideMenu(smVar.shownChildID);
    smVar.shownChildID = '';

    // Dehighlight a root item -------------------------------------------------
    dm_dehighlightRootItem(menu, smVar);


    if (smVar.showTimer)
    {
        clearTimeout(smVar.showTimer);   smVar.showTimer = null;
    }

    // Hide the submenu
    smObj.visibility = 'hide';

    // Show submenus on click: if all submenus were hidden -> enable "need-to-click"
    if (menu.smShowClick && menu.hideAllTimer) menu.smShowClicked = 0;
}



function dm_changeItemStyle4(it, over)
// Changes item appearance
{
  var itObj = dm_getObjectByID4(it.id);
  if (!it.isPressed)
  {
      itObj.document.layers[0].document.layers[1].visibility = (over?'show':'hide');
      itObj.document.layers[0].document.layers[0].visibility = (over?'hide':'show');
  }

  over = (it.isHighlighted?1:over);
  var itObj = dm_getObjectByID(it.id);

  with (it.itStyle)
  {
      var is = itObj;
      is.borderWidth = brdWidth;
      if (backColor[over]) is.backgroundColor = backColor[over];
      if (brdColor[over])  is.borderColor     = brdColor[over];
      if (brdStyle[over])  is.borderStyle     = brdStyle[over];
      if (backImage[over]) is.backgroundImage = "url("+backImage[over]+")";

  }
}

