//******************************************************************************
//
//    Deluxe Tree 1.x
//    Additional module for dynamic Tree Menu (JavaScript API)
//    http://deluxe-tree.com
//    (c) 2006, by deluxe-tree.com
//
//******************************************************************************


function dtreet_ext_getItemIDByIndex(mInd, itInd)
// Returns item ID by menu index and by global item index in menu
{
  var itVar = dt_menu[mInd].i[0];
  var j=0;
  while (j!=itInd && itVar)
  {
      itVar=dt_getNextItem(itVar);
      j++;
  }
  return (itVar ? itVar.id : null);
}


function dtreet_ext_getNextItemID(itID)
// Returns ID of next item after specified item
{
  return dt_getNextItem(dt_getItemVarByID(itID));
}


function dtreet_ext_getLastItemID(mInd)
// Returns ID of last item in menu
{
  return dt_getLastItem(mInd).id;
}


function dtreet_ext_showItem(itID, show)
// Shows/hides item
{
  var itVar = dt_getItemVarByID(itID);
  if (!itVar) return null;
  
  with (itVar)
  {
      if (isDeleted) return;
      if (dt_menu[mInd].isXPStyle && level==0)                                   // XP title => hide/show XP submenu
      {
          dt_getObjectByID(id).style.display = show ? '' : 'none';               // hide XP submenu title
          dt_getObjectByID(id+'divXP').style.display = show ? '' : 'none';     // hide XP submenu DIV
      }
      else
      {
          if (!show) dt_Expand(itVar, 0);                                        // collapse item
          dt_getObjectByID(id+'TR').style.display = (show && isVisible) ? '' : 'none';
      }

      isHidden = !show;
  }
}


function dtreet_ext_expandItem(itID, expand)
// Expands/collapses item
{
  var itVar = dt_getItemVarByID(itID);
  if (!itVar) return null;
  
  with (itVar)
      if (dt_menu[mInd].isXPStyle && level==0)                                  // XP title item
      {
          if (expanded!=expand) dt_XPTitleClick(itID);
      }
      else
          dt_Expand(itVar, expand);
}


function dtreet_ext_deleteItem(mInd, itID)
// Deletes item
{
  var itVar = dt_getItemVarByID(itID);
  if (!itVar) return null;

  with (itVar)
  {
      var menu = dt_menu[mInd];
      with (menu)
      {
          if (pressedItemID==itVar.id) pressedItemID = '';            // check if item is pressed
          itCount--;                                                  // decrease items count
      }

      if (hasChild)
          while (i.ln()) dtreet_ext_deleteItem(mInd, i[0].id);          // delete all child items


      if (menu.isXPStyle && !level)
      {
          if (isNS && isVER<7) dtreet_ext_showItem(itID, 0);            // hide submenu objects in NS 6
          else                                                        // delete submenu objects in other browsers
          {
              dt_getObjectByID(id).removeNode(true);
              dt_getObjectByID(id+'divXP').removeNode(true);
          }
      }
      else                                                            // Delete item's row from items table
      {
          var itRow = dt_getObjectByID(id+'TR');
          itRow.parentNode.parentNode.deleteRow(itRow.rowIndex);
      }


      dt_increaseItemIndexes(parentIt, ind+1, -1);



      // Delete item's object from items array
      with (parentIt)
      {
          var si = i.slice(0, itVar.ind);
          var ei = i.slice(itVar.ind+1, i.ln());
          i = si.concat(ei);

          // Change expand-button of parent
          if (!i.ln())
          {
              hasChild = 0;
              dt_changeParentExpandButton(menu, itVar);
          }
      }
  }
}


function dt_getRowIndex(mInd, pitVar, itInd, tblObj, rootLevel)     // (internal)
// Returns item index in XP submenu
{
  var childCnt = pitVar.i.ln();
  if (!childCnt)                                                 // no children
  {
      if (!pitVar.level) return 0;                               // first row in XP submenu
      else
          return dt_getObjectByID(pitVar.id+'TR').rowIndex+1;  // next row after row of root item
  }

  if (itInd==childCnt) return dt_getObjectByID(dt_getLastChild(pitVar).id+'TR').rowIndex+1;

  var itVar = dt_getPrevItem( pitVar.i[itInd] );
  if (itVar.level>=rootLevel) return dt_getObjectByID(itVar.id+'TR').rowIndex+1;
  else
      return 0;
}


function dtreet_ext_insertItem(mInd, pitID, itInd, itParams)
// Adds new item after specified item
{
  var menu = dt_menu[mInd];
  if (pitID)
  {
      var pitVar = dt_getItemVarByID(pitID);
      if (!pitVar) return null;
      var nLevel = pitVar.level+1
  }
  else
  {
      var pitVar = menu;
      var nLevel = 0;
      if (itInd==null) itInd = pitVar.i.ln();
  }


  // Delete '|' symbols from item's text
  itParams[0] = itParams[0].sb(dt_getItemLevel(itParams[0]), itParams[0].ln());
  

  // Check specified index of new item
  if (itInd<0) itInd = 0;
  if (itInd>pitVar.i.ln()) itInd = pitVar.i.ln();



  var tblObj, rowInd;
  
  
  if (menu.isXPStyle)
  {
      if (nLevel!=0)
      {
          if (nLevel==1) tblObj = dt_getObjectByID(pitVar.id+'tbl');                            // get items table directly using XP-title item
          else           tblObj = dt_getObjectByID(pitVar.id+'TR').parentNode.parentNode;

          rowInd = dt_getRowIndex(mInd, pitVar, itInd, tblObj, 1);                              // get index of row of new item in submenu table of items
      }
  }
  else
  {
      if (nLevel==0) tblObj = dt_getObjectByID(menu.id+'tbl')                              // get items table directly using menu ID
      else           tblObj = dt_getObjectByID(pitVar.id+'TR').parentNode.parentNode;      // get parent item's table (TR -> TBODY -> TABLE)

      if (itInd==pitVar.i.ln())
          rowInd = dt_getObjectByID(dt_getLastChild(pitVar).id+'TR').rowIndex+1;   // get index of row of new item in items table, index after last child's index of root item
      else
          rowInd = dt_getObjectByID(pitVar.i[itInd].id+'TR').rowIndex;               // get index of row of new item in items table
  }
  


  // Create params of new item
  dt_insertBlankItem(pitVar, itInd);
  dt_createItemParams(menu, pitVar, itParams, nLevel, itInd);

  var nitVar = tcurMenu.itVar;                                                      // get new item variable
  dt_detectItemVisibility_Expand(menu, nitVar);                                   // detect items visibility and expand-state

  var nextItVar = dt_getNextItem(pitVar.i[itInd]);
  if (menu.hasPoints && nextItVar) nitVar.ptMask = dt_setPointsMask(nitVar.level,
                                                                      menu.maxLevel,
                                                                      nextItVar.ptMask,
                                                                      nextItVar.level);  // fill level mask for new item


  if (menu.isXPStyle && nLevel==0)
  {
      dt_getObjectByID(menu.id+'div').innerHTML += dt_createXPSubmenu(menu, nitVar, '') + dt_closeXPSubmenu();
  }
  else
  {
      var nitRow = tblObj.insertRow(rowInd);                                          // insert new item's row
      with (nitVar) nitRow.style.display = (isVisible && !isHidden) ? '' : 'none';    // determine current visibility of new item
      nitRow.id  = nitVar.id+'TR';

      // Create new objects
      var nitTD = dtdo.createElement('TD');                                            // create TD of new item
      with (nitTD)
      {
          style.padding = '0px';
          style.margin  = '0px';
          innerHTML = dt_createItem(menu, tcurMenu.itVar, '', 0);                   // create item's HTML. Param 0 - do not create TR & TD for item
      }

      nitRow.appendChild(nitTD);
  }

  dt_changeParentExpandButton(menu, nitVar);
}


function dt_changeParentExpandButton(menu, itVar)          // (internal)
// Changes expand-button of item, assign button cursor
{
  var pitVar = itVar.parentIt;
  if (itVar.level>0)
      if (!menu.isXPStyle || (menu.isXPStyle && itVar.level>1))
          with (dt_getObjectByID(pitVar.id+'btn'))
          {
              src = pitVar.hasChild ? (pitVar.expanded ? menu.btns[2] : menu.btns[0]) : tblankImage;
              style.cursor = 'pointer';
          }
}


function dt_increaseItemIndexes(pitVar, itInd_from, inc)   // (internal)
// Increases (decreases) items' indexes in submenu after specified item
{
  with (pitVar)
      for (var j=itInd_from; j<i.ln(); j++) i[j].ind += inc;
}


function dt_insertBlankItem(pitVar, itInd)                 // (internal)
// Inserts blank item into submenu into specified position
{
  dt_increaseItemIndexes(pitVar, itInd, +1);
  with (pitVar)
  {
      var si = i.slice(0, itInd);
      var ei = i.slice(itInd, i.ln());
      i = si.concat([null]).concat(ei);
  }
}


function dtreet_ext_setPressedItem(mInd, itID)
// Sets pressed item
{
  dt_setPressedItem(dt_menu[mInd], itID)
}


function dtreet_ext_changeItem(mInd, itID, itParams)
// Changes item
{
  var menu  = dt_menu[mInd];
  var itVar = dt_getItemVarByID(itID);
  if (!itVar) return null;

  with (itVar)
	{
      text       = itParams[0] ? itParams[0] : text;
      link       = dt_getItemLink(itParams[1]);
      target     = dt_getItemTarget(itParams[6]);
      tip        = dt_getParam(itParams[5], '');
      itStyle    = dt_getItemStyle(menu, itParams[7]);
      icon       = dt_setPathPrefix(dt_getItemIcons(itParams), tpathPrefix_img);
      isDisabled = (target=='_') ? 1 : 0;
      with (dt_getObjectByID(itVar.id+'font'))
      {
          innerHTML = text;
          if (itVar.isDisabled) style.color = menu.fntColorDisabled;
      }

      var itObj = dt_getObjectByID(id);
      itObj.title = tip;

      // Set new appearance of item
      if (menu.isXPStyle && !level) dt_XPTitleChange(itObj, itID, 0);    // XP-title item
      else
          dt_changeItem(itObj, itVar, 0);                                // normal item
  }
}


function dtreet_ext_getItemParams(mInd, itID)
// Returns item parameters
{
  var itVar = dt_getItemVarByID(itID);
  if (!itVar) return null;
  
  with (itVar)
      var iparams = [ id, ind, parentIt.id, level, hasChild, i.ln(), expanded,
                      text, link, target, tip, align, icon,
                      isHidden, isDisabled, isVisible ];
  return iparams;
}


function dtreet_ext_getMenuParams(mInd)
// Returns menu parameters
{
  with (dt_menu[mInd])
      var iparams = [ id, i.ln(), itCount, pressedItemID ];
      
  return iparams;
}




