//##############################################################################
// KEYSTROKES FUNCTIONS
//##############################################################################

function dm_switchItem(d)
{
  with (keyMode)
  {
      if (d>0)
      {
          if (itVar.ind+d < smVar.i.length) itVar = smVar.i[itVar.ind+d];
          else                              itVar = smVar.i[0];
      }
      else
      {
          if (itVar.ind+d >= 0) itVar = smVar.i[itVar.ind+d];
          else                  itVar = smVar.i[smVar.i.length-1];
      }
      if (itVar.text=='-' || itVar.isDisabled || itVar.isDeleted || !itVar.isVisible) dm_switchItem(d);
      if (smVar==rsmVar) ritVar = itVar;            // change selected item of root menu
  }
}


function dm_switchSubmenu(d)
{
  with (keyMode)
      if (d>0)                                                   // need to show child submenu
      {
          if (itVar.childID)                                     // if child submenu exists
          {
              dm_showMenu(itVar.childID);
              if (menu.saveNavigation) dm_highlightItem(1);
              smVar = dm_getVarByID(itVar.childID);
              itVar = smVar.i[0];
          }
          else                                                  // item doesn't have submenu -> return to root menu
          {
              dm_hideSubmenus(menu.ind);
              itVar = ritVar;
              smVar = rsmVar;
              dm_switchItem(+1);
          }
      }
      else                                                       // need to hide current submenu
      {
          if (itVar==ritVar) return;                             // exit if current item is root item
          dm_hideMenu(smVar.id);
          itVar = dm_getVarByID(smVar.rootItemID);
          smVar = menu.m[itVar.smInd];
      }
}


function dm_highlightItem(over)
{
  with (keyMode)
  {
      if (!itVar.isPressed) dm_changeItemStyle(itVar, over);

      // Show focus rect
      if (!_dmFDIV) return;
      var iObj = dm_getObjectByID(itVar.id+'tbl');
      if (!iObj) return;
      var di = dm_getObjectSize(iObj);


      if (di[2]>2 && di[3]>2)
          with (_dmFDIV.style)
          {
              left    = di[0]+1 + 'px';
              top     = di[1]+1 + 'px';
              width   = di[2]-2 + 'px';
              height  = di[3]-2 + 'px';
              display = '';
          }
  }
}


function dm_disableKeyMode(mInd, hideSm)
{
  if (hideSm) dm_hideSubmenus(mInd);
  with (keyMode)
  {
      dm_highlightItem(0);
      if (_dmFDIV) _dmFDIV.style.display = 'none';
      _dmFDIV = null;
      
      isEnabled = 0;
    /*
      menu      = null;
      rsmVar    = null;
      ritVar    = null;
      smVar     = null;
      itVar     = null;
      */
  }
}


function dm_enableKeyMode()
{
  with (keyMode)
  {
      if (dm_focus) _dmFDIV = dm_gE('dmFDIV'+_dmKSInd);

      for (var i=0; i<dm_menu.ln(); i++) dm_hideSubmenus(i);
      isEnabled = 1;
      menu = dm_menu[_dmKSInd];
      rsmVar = menu.m[0];
      ritVar = rsmVar.i[0];
      smVar  = rsmVar;
      itVar  = ritVar;
      dm_highlightItem(1);
  }
}


function dm_convertCode(code)
// Convert key codes for Opera 7-
{
  if (b_OP && b_VER<8)
      switch (code)
      {
         case 57346: return 113; break;      // F2
         case 57354: return 121; break;      // F10
         case 57375: return 37;  break;      // left
         case 57373: return 38;  break;      // up
         case 57376: return 39;  break;      // right
         case 57374: return 40;  break;      // down
      }
  return code;
}


var _dmKSInd = 0;
var _dmFDIV = null;

function dm_ext_keystrokes(e, win)
{
  if (b_IE) e = win ? win.event : event;
  var k = dm_convertCode(e.keyCode);
  
  // Esc key was pressed
  if (keyMode.isEnabled)
  {
      // Escape key
      if (k==27)
      {
          dm_disableKeyMode(keyMode.menu.ind, 1);
          return false;
      }
      
      // Ctrl+<act>
      if (e.ctrlKey && k==dm_actKey && dm_menu.ln()>1)
      {
          dm_disableKeyMode(keyMode.menu.ind, 1);

          // Switch to next menu on the page
          var _old = _dmKSInd;
          do
          {
              _dmKSInd++;
              if (_dmKSInd==dm_menu.ln()) _dmKSInd = 0;
          }
          while (dm_menu[_dmKSInd].isPopup && _dmKSInd!=_old);
          
          dm_enableKeyMode();
          return false;
      }
  }


  // Proceed the key -----------------------------------------------------------
  with (keyMode)
      if (!isEnabled)                                                         // activate the keystrokes mode
      {
          if (e.ctrlKey && k==dm_actKey) dm_enableKeyMode();                  // proceed with Ctrl+dm_actKey only
          else
              return true;

      }
      else  // navigation
      {
          dm_highlightItem(0);
          var f=0;

          if (smVar.isHoriz)
              switch (k)
              {
                  case 39:  dm_switchItem(+1); f=1; break;        // right
                  case 37:  dm_switchItem(-1); f=1; break;        // left
                  case 38:  f=1; break;                           // up
                  case 40:  dm_switchSubmenu(+1); f=1; break;     // down
              }
          else
              switch (k)
              {
                  case 39:  dm_switchSubmenu(+1); f=1; break;     // right
                  case 37:  dm_switchSubmenu(-1); f=1; break;     // left
                  case 38:  dm_switchItem(-1);    f=1; break;     // up
                  case 40:  dm_switchItem(+1);    f=1; break;     // down
              }

          clearInterval(menu.hideAllTimer);
          menu.hideAllTimer = null;

          // Enter key was pressed
          if (k==13 && !itVar.isDisabled)
          {
              if (menu.toggleMode!=-2) dm_ext_setPressedItem(menu.ind, itVar.smInd, itVar.ind, true);
              dm_openItemLink(menu, itVar);
              dm_disableKeyMode(keyMode.menu.ind, 1);
              return false;
          }

          dm_highlightItem(1);
          return (f ? false : true);
      }


  return false;
}

