var strListContainerName = "";
var fMutexMode = false;
var fTitleExpands = false;
var xEventCallback = null;
var xExpanderClickCallback = null;

function SetActiveListContainerName(strName)
{
  strListContainerName = strName;
}

function SetMutexMode(fEnabled)
{
  fMutexMode = fEnabled;
}

function SetTitleExpandMode(fEnabled)
{
  fTitleExpands = fEnabled;
}

function SetEventCallbackFunction(func)
{
  xEventCallback = func;
}

function SetExpanderClickCallback(func)
{
  xExpanderClickCallback = func;
}

function ClearListContainer()
{
  ClearContainerByName(strListContainerName);
}

function AddListItem(strTitle, strURL, fJSURL)
{
  var cont = GetDOMObject(strListContainerName);
  if (!cont)
    return null;
  var newItemDiv = document.createElement("div");
  
  //Create the expander
  var expander    = document.createElement("span");
  var expanderImg = document.createElement("img");
  expander.setAttribute("class", "listItem_expander");
  expander.className = "listItem_expander";
  expander.style.cursor = "default";
  expander.onclick = _expander_toggleItems;
  expanderImg.setAttribute("src", "/i/plus.gif");
  expander.appendChild(expanderImg);
  
  //Create the item title
  var titleDiv  = document.createElement("div");
  var titleLink = document.createElement("a");
  titleDiv.setAttribute("class", "listItem_title");
  titleDiv.className = "listItem_title";
  titleDiv.appendChild(expander);
  if (fJSURL)
  {
    if (fTitleExpands)
    {
      titleLink.onclick  = function() {
        var func = _title_expandItems;
        func.apply(this);
        eval(strURL);
      };
    }
    else
      titleLink.onclick = new Function(strURL);
    titleLink.setAttribute("href", "javascript:;");
  }
  else
  {
    if (fTitleExpands)
      titleLink.onclick = _title_expandItems;
    titleLink.setAttribute("href", strURL);
  }
  titleLink.appendChild(document.createTextNode(strTitle));
  titleDiv.appendChild(titleLink);
  
  //Create the subitem container
  var itemsDiv = document.createElement("div");
  itemsDiv.setAttribute("name",  "listItem_subItemsList");
  itemsDiv.setAttribute("id",    "listItem_subItemsList");
  itemsDiv.setAttribute("class", "listItem_subItemsList");
  itemsDiv.className = "listItem_subItemsList";
  itemsDiv.style.display = "none";
  
  //Add the elements to the new item
  newItemDiv.appendChild(titleDiv);
  newItemDiv.appendChild(itemsDiv);
  
  //Add the new item to the list
  cont.appendChild(newItemDiv);
  
  if (xEventCallback)
    xEventCallback("ADD_ITEM", newItemDiv);
  
  return newItemDiv;
}

function AddSubItem(itemNode, itemName, itemURL, fJSURL)
{
  var cont = GetDOMObject(strListContainerName);
  if (!cont || !itemNode)
    return;
  var itemsCont = GetDOMObject("listItem_subItemsList", itemNode);
  if (!itemsCont)
    return;
  var newLink = document.createElement("a");
  if (fJSURL)
  {
    newLink.onclick = new Function(itemURL);
    newLink.setAttribute("href", "javascript:;");
  }
  else
    newLink.setAttribute("href", itemURL);
  newLink.appendChild(document.createTextNode(itemName));
  itemsCont.appendChild(newLink);
  itemsCont.appendChild(document.createElement("br"));
  
  if (xEventCallback)
    xEventCallback("ADD_SUBITEM", itemName);
}

function AddSubItemByName(strListItemName, strSubItemTitle, strSubItemURL)
{
  var item = GetItemByName(strListItemName);
  if (item)
    AddSubItem(item, strSubItemTitle, strSubItemURL);
}

function ExpandItem(itemNode)
{
  if (!itemNode)
    return;
  var itemsCont = GetDOMObject("listItem_subItemsList", itemNode);
  if (!itemsCont)
    return;
  if (itemsCont.style.display != "block")
  {
    itemsCont.style.display = "block";
    var expanderImg = itemNode.firstChild.firstChild.firstChild;
    if (expanderImg)
      expanderImg.setAttribute("src", "/i/minus.gif");
    if (xEventCallback)
      xEventCallback("EXPAND", itemNode);
    if (fMutexMode)
      _collapseOtherItems(itemNode);
  }
}

function ExpandItemByName(strItemName)
{
  var item = GetItemByName(strItemName);
  if (item)
    ExpandItem(item);
}

function CollapseItem(itemNode)
{
  if (!itemNode)
    return;
  var itemsCont = GetDOMObject("listItem_subItemsList", itemNode);
  if (!itemsCont)
    return;
  if (itemsCont.style.display != "none")
  {
    itemsCont.style.display = "none";
    var expanderImg = itemNode.firstChild.firstChild.firstChild;
    if (expanderImg)
      expanderImg.setAttribute("src", "/i/plus.gif");
    if (xEventCallback)
      xEventCallback("COLLAPSE", itemNode);
  }
}

function CollapseItemByName(strItemName)
{
  var item = GetItemByName(strItemName);
  if (item)
    CollapseItem(item);
}

function GetItemByName(strName)
{
  var cont = GetDOMObject(strListContainerName);
  if (!cont)
    return null;
  for (var i = 0; i < cont.childNodes.length; i++)
  {
    var anchorTag = cont.childNodes[i].firstChild.childNodes[1];
    for (var j = 0; j < anchorTag.childNodes.length; j++)
    {
      if ((anchorTag.childNodes[j].nodeType == 3) && (anchorTag.childNodes[j].nodeValue.toUpperCase() == strName.toUpperCase()))
        return cont.childNodes[i];
    }
  }
  return null;
}

function GetItemStates()
{
  var cont = GetDOMObject(strListContainerName);
  if (!cont)
    return null;
  var itemStates = "";
  for (var i = 0; i < cont.childNodes.length; i++)
    itemStates += (cont.childNodes[i].childNodes[1].style.display == "block") ? "1" : "0";
  return itemStates;
}

function SetItemStates(itemStates)
{
  var cont = GetDOMObject(strListContainerName);
  if (!cont)
    return;
  if (itemStates.length != cont.childNodes.length)
    return;
  for (var i = 0; i < itemStates.length; i++)
  {
    if (itemStates.substring(i, i + 1) == "1")
      ExpandItem(cont.childNodes[i]);
    else
      CollapseItem(cont.childNodes[i]);
  }
}

function _expander_toggleItems()
{
  var itemsDiv = GetDOMObject("listItem_subItemsList", this.parentNode.parentNode);
  if (!itemsDiv)
    return;
  var displayValue = itemsDiv.style.display;
  if (!displayValue)
    displayValue = "block";
  itemsDiv.style.display = (displayValue == "block") ? "none" : "block";
  var expanderImg = this.firstChild;
  if (expanderImg)
    expanderImg.setAttribute("src", (displayValue == "block") ? "/i/plus.gif" : "/i/minus.gif");
  if (xExpanderClickCallback)
    xExpanderClickCallback((displayValue == "block") ? "COLLAPSE" : "EXPAND", itemsDiv.parentNode);
  if (xEventCallback)
    xEventCallback((displayValue == "block") ? "COLLAPSE" : "EXPAND", itemsDiv.parentNode);
  if (fMutexMode)
    _collapseOtherItems(itemsDiv.parentNode);
}

function _title_expandItems()
{
  var itemsDiv = GetDOMObject("listItem_subItemsList", this.parentNode.parentNode);
  if (!itemsDiv)
    return;
  itemsDiv.style.display = "block";
  var expanderImg = this.parentNode.firstChild.firstChild;
  if (expanderImg)
    expanderImg.setAttribute("src", "/i/minus.gif");
  if (xExpanderClickCallback)
    xExpanderClickCallback("COLLAPSE", itemsDiv.parentNode);
  if (xEventCallback)
    xEventCallback("COLLAPSE", itemsDiv.parentNode);
  if (fMutexMode)
    _collapseOtherItems(itemsDiv.parentNode);
}

function _collapseOtherItems(expandedItem)
{
  var cont = GetDOMObject(strListContainerName);
  if (!cont)
    return;
  for (var i = 0; i < cont.childNodes.length; i++)
  {
    if (cont.childNodes[i] != expandedItem)
    {
      var subItemsDiv = cont.childNodes[i].childNodes[1];
      var expanderImg = cont.childNodes[i].firstChild.firstChild.firstChild;
      if (subItemsDiv && expanderImg)
      {
        if (subItemsDiv.style.display != "none")
        {
          subItemsDiv.style.display = "none";
          expanderImg.setAttribute("src", "/i/plus.gif");
          if (xEventCallback)
            xEventCallback("COLLAPSE", cont.childNodes[i]);
        }
      }
    }
  }
}

