//==============================================================================
//
//     Copyright (c) 2006, by Deluxe-Menu.com
//     Version 1.xxx
//     http://deluxe-menu.com
// ------------------------------------
//
//     DYNAMIC FUNCTIONS
//
// Note: It's recommended to set var dm_writeAll=1; when using these functions.
//
//##############################################################################


function dm_ext_hideAllSubmenus(mInd)
// Hides all submenus for specified menu
{
    dm_hideSubmenus(mInd);
}


function dm_ext_changeItem(mInd, smInd, itInd, iParams)
// Changes item parameters and individual style
{
  var menu  = dm_menu[mInd];
  var smVar = menu.m[smInd];
  var itVar = smVar.i[itInd];
  var itObj = dm_getObjectByID(itVar.id+'tbl');
	
  with (itVar)
	{
      text       = (iParams[0]?iParams[0]:text);
      link       = dm_getItemLink(iParams[1]);
      target     = dm_getItemTarget(iParams[5]);
      status     = dm_getStatusText(statusString, text, link);
      tip        = dm_getParam(iParams[4], '');
      itStyle    = dm_getItemStyle(menu, iParams[6], 1);
      icon       = dm_setPathPrefix([dm_getParam(iParams[2],icon[0]),dm_getParam(iParams[3],icon[1])], pathPrefix_img);
      isDisabled = (target=='_')?1:0;

      // Change object parameters on page if item exists
      if (itObj)
      {
          dm_getObjectByID(id+'tdT').innerHTML = text;
          with (itObj)
          {
              title = tip;
              style.width = itStyle.itWidth;
          }
          dm_changeItemStyle(itVar, 0);
      }
  }
}


function dm_browserError() {
  if (b_OP && b_VER<7) return alert('Javascript Menu by Deluxe-Menu.com:\nThis browser doesn\'t support dynamic menu modifications.');
}


function dm_ext_createSubmenu(mInd, smInd, itInd, iParamsArr)
// Creates new submenu with 1 new item
{
  dm_browserError();
  var menu  = dm_menu[mInd];
  var smVar = menu.m[smInd];
  var itVar = smVar.i[itInd];
  
  if (itVar.childID) return 0;

  // Create submenu and items params using new loaded data
  dm_createSubmenuParams(menu, smVar, itVar, iParamsArr[7]);

  curMenu.itInd = curMenu.smVar.i.ln();                                       // set current item index
  dm_createItemParams(menu, curMenu.smVar, curMenu.itInd, iParamsArr, statusString);
  itVar.childID = curMenu.smVar.id;

  // Create item arrow
  var itTbl = dm_gE(itVar.id+'tbl');
  if (itTbl)
  {
      var s    = dm_openItemArrow(itVar);
      var r    = itTbl.rows[0];
      var aObj = d_o.createElement('TD');
  
      with (itVar)
      {
          aObj.id = id+'tdA';
          aObj.style.padding = curMenu.smVar.smStyle.itPadding+dUnits;

          var arrImg = smInd ? itStyle.arrowSub[0] : itStyle.arrowMain[0];
          aObj.innerHTML = dm_openImg(id+'arr', arrImg, arrowW, arrowH, '');
      }
      r.appendChild(aObj);
  }
  
  var mm = dm_getObjectByID(menu.m[0].id).offsetParent;
  mm.innerHTML += dm_getAddDivHTML(mInd, curMenu.smVar.ind);
  dm_writeSubmenuHTML(curMenu.smVar);
  
  return 1;
}


function dm_ext_deleteSubmenu(mInd, smInd)
{
  dm_browserError();
  var menu  = dm_menu[mInd];
  var smVar = menu.m[smInd];

  // Delete submenu ID from parent item property
  dm_getVarByID(smVar.rootItemID).childID = '';

  // hide item arrow
  dm_gE(smVar.rootItemID+'tdA').style.display = 'none';
}


function dm_ext_addItem(mInd, smInd, iParamsArr)
// Adds new item
{
  dm_browserError();
  var menu  = dm_menu[mInd];
  var smVar = menu.m[smInd];
  var itInd = smVar.i.length;

  // Create item variable ------------------------------------------------------
	dm_createItemParams(menu, smVar, itInd, iParamsArr, statusString);
  var itVar = smVar.i[itInd];


  // Get submenu object and create new item cell -------------------------------
  var smObj = dm_getObjectByID(smVar.id+'tbl');
  var frame = (itVar.smInd && menu.isCrossFrame && cfType==1) ? 'parent.frames['+menu.cfMFInd+'].' : '';
  var itObj = d_o.createElement('TD');


  itObj.id = itVar.id + 'td';
  itObj.innerHTML = dm_createItemTable(menu, smVar, itVar, frame);

  // Check if submenu objects exists and then add new item object
  if (smObj)
  {
      with (smObj)
          var rowObj = smVar.isHoriz ? rows[0] : insertRow(rows.length);        // get or create submenu's row

      if (!smVar.isHoriz) rowObj.id = itVar.id+'R';                             // set row ID if item has it's own row (for vertical submenus only)
      rowObj.appendChild(itObj);
	}
}


function dm_changeItemVis(id, vis)
{
  // Hide/show also item <TD>
  var itTDObj = dm_getObjectByID(id+'td');
  if (itTDObj) itTDObj.style.display = vis ? '' : 'none';

  // Hide/show also item <TR>
  var itTRObj = dm_getObjectByID(id+'R');
  if (itTRObj) itTRObj.style.display = vis ? '' : 'none';
}

function dm_ext_deleteItem(mInd, smInd, itInd)
// Deletes item
{
  dm_browserError();
  var smVar = dm_menu[mInd].m[smInd];
  var itVar = smVar.i[itInd];

  itVar.isDeleted = 1;
  dm_changeItemVis(itVar.id, 0);
}


function dm_ext_changeItemVisibility(mInd, smInd, itInd, vis)
// Hides/shows item
{
  dm_browserError();
  var smVar = dm_menu[mInd].m[smInd];
  var itVar = smVar.i[itInd];

  itVar.isVisible = vis;
  dm_changeItemVis(itVar.id, vis);
}


function dm_ext_getItemParams(mInd, smInd, itInd)
// Returns item parameters
{
  with (dm_menu[mInd].m[smInd].i[itInd])
      var iparams = [ id, (childID?1:0),
                      text, link, target, status, tip, align, icon,
                      isDisabled, isPressed, isVisible, isDeleted ];
  return iparams;
}


function dm_ext_getSubmenuParams(mInd, smInd)
// Returns submenu parameters
{
  with (dm_menu[mInd].m[smInd])
      var smparams = [id, i.length, rootItemID, level, isHoriz];
  return smparams;
}


function dm_ext_getMenuParams(mInd)
// Returns menu parameters
{
  with (dm_menu[mInd])
      var mparams = [m.length, curPressedSm, curPressedIt];
  return mparams;
}

