// xCollapsible, Copyright (C) 2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser.com Javascript Library, Distributed under the terms of the GNU LGPL


 var mnu = new Array(); // array for xCollapsible Left side XP style open/close boxes    
 var groups = new Array(); // array for xCollapsible open/close groups in editors    
 
function xCollapsible(outerEle,bShow,iSrc,iSrcUp) // object prototype
{
  // Constructor

  var container = xGetElementById(outerEle);
  if (!container) {return null;}
    
  var bS = bShow; // PW: Some new editor mods to bShow to disable hidden group if Gecko if group contains RichContent. 
  
  var isUL = container.nodeName.toUpperCase() == 'UL';
  var i, trg, aTgt = xGetElementsByTagName(isUL ? 'UL':'DIV', container);
  for (i = 0; i < aTgt.length; ++i) {
    trg = xPrevSib(aTgt[i]);
    if (trg && trg.parentNode) //PW new
    {
      var hasRichContent = findFCKDescendentByName(trg.parentNode, 'fckrcid'); //PW new
      if (!document.all && !bS && hasRichContent) {bS = true;}
    }
    if (trg && (isUL || trg.nodeName.charAt(0).toUpperCase() == 'H')) {
      aTgt[i].style.display = bS ? 'block' : 'none';
      trg.style.cursor = 'pointer';
      trg.xClpsTgt = aTgt[i];
      trg.onclick = trg_onClick;
    }  
  }
  
  // Private

  function trg_onClick()  
  {
    var tgt = this.xClpsTgt.style;
    var fc = xFirstChild(this);  //  PW: customized to swap img
    
    var fcSrc = iSrc ? iSrc:'/system_images/menubox/chevronUp.gif';
    var fcSrcUp = iSrcUp ? iSrcUp:'/system_images/menubox/chevronDown.gif';
    
    if (tgt.display == 'none') {
      tgt.display = 'block';
      if (fc) {fc.src = fcSrc;}
    }  
    else {
      tgt.display = 'none';
       if (fc) {fc.src = fcSrcUp;}
    }
    
    // PW: This is a hack for Gecko... it stops editing when the editor is hidden. 
    if ( !document.all ) 
    { 
    var fckEditorId = findFCKDescendentByName(this.parentNode, 'fckrcid');
      if (fckEditorId) 
      {
        var oEditor = FCKeditorAPI.GetInstance(fckEditorId.name);       
        oEditor.EditorDocument.designMode = "off" ; 
        oEditor.EditorDocument.designMode = "on" ; // This spits out FCK error but also fixes things.
        oEditor.Focus() ; 
        oEditor.ToolbarSet.RefreshItemsState() ;       
      }
    } 

  }

  // Public
  this.onUnload = function()
  {
    if (!container || !aTgt) {return;}
    for (i = 0; i < aTgt.length; ++i) {
      trg = xPrevSib(aTgt[i]);
      if (trg && (isUL || trg.nodeName.charAt(0).toUpperCase == 'H')) {
//        aTgt[i] = null;
        trg.xClpsTgt = null;
        trg.onclick = null;
      }
    }
  }
  
   
  //  PW: For the hack for Gecko...
  function findFCKDescendentByName(current, name) {
    if (current.getAttribute) {
      if (current.getAttribute('fckrcid') == name) {
        return current;
      }
    }
                             
    var children = current.childNodes;
    var i;
    for (i = 0; i < children.length; i++) {
      var result = findFCKDescendentByName(children[i], name);
      if (result != null) return result;
    }
    return null;
  }

  
} // end xCollapsible














