//##############################################################################
// CROSS-FRAME FUNCTIONS
//##############################################################################

function dm_getObjectInFrame(id, frameInd)
{
  if (!dm_isFrameAccessible(id, frameInd)) return null;
  return parent.frames[frameInd].document.getElementById(id);
}


function dm_writeCrossSubmenuHTML(menu, smVar, id)
{
    var smObj = null;

    // If subframe's page wasn't loaded from alien domain, create submenus in the frame with top-menu
    if (!(smObj=dm_createCrossHTML(menu, id))) smObj = dm_writeSubmenuHTML(smVar);

    // Toggle mode is on and some item was pressed -> highlight this item
    if (menu.toggleMode>=0 && menu.curPressedIt!=-1) dm_setCrossPressedItem(menu);

    return smObj;
}


function dm_createCrossHTML(menu, id)
// Copies submenu data to subframe
{
  if (!dm_isFrameAccessible(id, menu.cfSFInd)) return null;

  var obj=dm_getObjectInFrame(id, menu.cfSFInd);
  if (obj) return obj;                                                        // submenus are in a subframe already

  var frame = parent.frames[menu.cfSFInd].document;
  if (!frame) return null;

  // If IE defines compatMode as "CSS1Compat", dm_getFrameDocElement() returns document.documentElement instead of document.body,
  // so submenus aren't saved into subframe -> use body element instead
  fdocElement = frame.body;
  var s = dm_getSubmenusHTML(menu);

  with (fdocElement)
  {
      // Try to write submenus' HTML into special <DIV> if it exists
      // It's quite good to create this <DIV> manually in subframe's page for Netscape 6
      // because without it this <DIV> browser damages page appearance
      var crossDIV = frame.getElementById('dmDIV');
      if (b_NS && b_VER<7 && crossDIV) crossDIV.innerHTML = s;
      else
          if (b_IEComp) insertAdjacentHTML('afterBegin', s);
          else          innerHTML += s;
  }

  return dm_getObjectInFrame(id, menu.cfSFInd);                               // return the submenu object in frame
}


function dm_getSubmenusHTML(menu)
// Returns HTML code for all submenus
{
  var s='';
  with (menu)
      for (var ii=1; ii<m.ln(); ii++)
          with (m[ii])
          {
              if (!smHTML) dm_createSubmenuHTML(menu, m[ii], '', '', dmZOrder, 1);
              s += smHTML;   // run all submenus excluding a top-menu
          }

  return s;
}


function dm_extractFramesetSizes(sizesStr, frameInd)
// Returns 3 strings from frameset sizes (e.g. "40%,50,100,*" + frameInd=1 => [ "40%,", "50", ",100,*" ])
{
  var s1='', s2='';
  var sizesArr = sizesStr.split(',');
  for (var i=0; i<frameInd; i++) s1 += sizesArr[i]+',';
  for (var i=frameInd+1; i<sizesArr.length; i++) s2 += ','+sizesArr[i];
  return [s1, sizesArr[frameInd], s2];
}


function dm_setCrossPressedItem(menuVar)
// Sets current pressed item in subframe
{
  var m = menuVar.curPressedSm;
  var i = menuVar.curPressedIt;
  with (toggleRec)
  {
      pressedSelf     = true;
      changeStyleOnly = true;
  }
  dm_ext_setPressedItem(menuVar.ind, m, i, true);
}


function dm_isFrameAccessible(id, frameInd)
{
  try
      {
          var obj = parent.frames[frameInd].document.getElementById(id);
          cfType = 1;
          return 1;
      }
  catch (e)
      {
          cfType = 3;
          return 0;
      }
}


function dm_setCrossSubmenuPos(menu, smVar, smObj, smXY)
// Corrects submenu position in subframe
{
    var mfSize = dm_getClientSize(null);                                      // scroll offset of a main frame
    var sfSize = dm_getClientSize(menu);                                      // scroll offset of a subframe
    var l=0, t=0;
    var smSize = dm_getObjectSize(dm_getObjectByID(smVar.id+'tbl'));
    var w=smSize[2], h=smSize[3];
    var frame = parent.frames[menu.cfSFInd];

    if (menu.cfOrient)                                                        // vertical frame orientation
    {
        var dy = mfSize[1];
        if (b_IE || b_OP) dy += frame.window.screenTop - window.screenTop;

        switch (menu.smView)
        {
            case 0: case 1: l = sfSize[0]-smVar.offx;
            break;
            case 2: case 3: l = sfSize[2]+sfSize[0]-smSize[2]-smVar.offx;
            break;
        }
        t = smXY[1] + sfSize[1] - dy;
    }
    else                                                                      // horizontal frame orientation
    {
        var dx = mfSize[0];
        if (b_IE || b_OP) dx += frame.window.screenLeft - window.screenLeft;

        l = smXY[0] + sfSize[0] - dx;
        switch (menu.smView)
        {
            case 0: case 2: t = sfSize[1] + smVar.offy;
            break;
            case 1: case 3: t = sfSize[3]+sfSize[1]-smSize[3] - smVar.offy;
            break;
        }
    }

    // Check submenu position with page bounds
    l = dm_correctSubmenuCoord(l, w, sfSize[0], sfSize[0]+sfSize[2], 0);
    t = dm_correctSubmenuCoord(t, h, sfSize[1], sfSize[1]+sfSize[3], 0);

    with (smObj.style)
    {
        left = l+'px';
        top  = t+'px';
    }

    // Change x,y coords for first-level submenu in cross-frame mode
    smXY[0] = l;
    smXY[1] = t;

    return smXY;
}


function dm_frameResize(menu, id)
// Resizes top frame
{
    var fset = parent.document.getElementById(menu.cfFSID);
    with (fset)                                                               // get frame's sizes from frameset COLS/ROWS attribute
        var fsSizes = menu.cfOrient ? cols : rows;

    if (!oldFsetSizes) oldFsetSizes = fsSizes;
    var fsSizes3 = dm_extractFramesetSizes(fsSizes, menu.cfMFInd);   // divide the string with sizes (extract the size of a top frame)
    var docSize  = dm_getClientSize(menu);                           // get document size
    var smSize   = dm_getObjectSize(dm_getObjectByID(id+'tbl'));    // get submenu size

    // Resize top frame
    with (fset)
        switch (menu.cfOrient)
        {
          // left-to-right frame orientation
          case 0:  if (smSize[1]+smSize[3] > docSize[3]) rows = fsSizes3[0] + (smSize[1]+smSize[3]+2) + fsSizes3[2];
          break;
          // top-to-bottom frame orientation
          case 1:  if (smSize[0]+smSize[2] > docSize[2]) cols = fsSizes3[0] + (smSize[0]+smSize[2]+2) + fsSizes3[2];
          break;
          // right-to-left frame orientation
          //case 3:
          //break;
          // bottom-to-top frame orientation
          //case 4:
          //break;
        }

}

// Set flag that dmenu_cf.js file with cross-frame support is loaded
var dmCF=1;

