// Declared from in C#

//string var defaultPointerID // the current page's pointer 
//string[] var pointerIDs     // all menu item id's in the menu

var intervalID;

// Activates the menu pointer (the red arrow) with the given ID
function highlightPointer(pointerID)
{
	clearInterval(intervalID);

  for (i = 0; i < pointerIDs.length; i++)
  {
    var visible = 'hidden';

    if (pointerIDs[i] == pointerID)
      visible = 'visible';

      var element = $get(pointerIDs[i]);
      if (element != null) {
          element.style.visibility = visible;

          if (visible == 'visible') {
              var subMenuContainerElement = FindParentInStackByPartlyID(element, 'SubMenuContainerDiv');
              if (subMenuContainerElement != null) {
                  if (subMenuContainerElement.className == 'collapsed')
                      subMenuContainerElement.className = 'expanded';
              }
          }
      }
  }   
}

// Searches through the parentNode stack of the specified element. The first 
// node that has an id containing the specified part is returned.
function FindParentInStackByPartlyID(element, partlyIdMatchString)
{
  var searchedElement = element;
  var hasParent = true;

  while(hasParent) {

    // See if id matches partly on the specified string
    if (searchedElement.id !=null && searchedElement.id.indexOf(partlyIdMatchString) > -1)
      return searchedElement;

    // Stop next iteration if there are no parents
    if(searchedElement.parentNode == null)
      hasParent = false;
    else
      searchedElement = searchedElement.parentNode;
  }

  return null;
}

// Delay return to top for a bit, to prevent screen flicker
function highlightDefaultPointer()
{
	intervalID = setInterval('highlightPointer(defaultPointerID)', 400);
}

// set startup event handler
$addHandler(window, "load", init);
//addLoadHandler();

// Add Load Handler
function addLoadHandler()
{
  window.onload = init();
}

// Startup event handler
function init()
{
  //window.alert("test");
  highlightPointer(defaultPointerID);
}

/* expand collapse functions */

function expand(ioNode) {
	if ($get(ioNode) != null) {
		$get(ioNode).className='expanded';
	}
}

function collapse(ioNode) {
	if ($get(ioNode) != null) {
		$get(ioNode).className='collapsed';
	}
}

// Toggles the given node from closed to open
function ioSwitch(ioNode) {

  var nodeState;
  
	if ($get(ioNode) != null) {
		nodeState = $get(ioNode).className;
		
	  if (nodeState == 'collapsed') {
		  expand(ioNode);
	  } else {
		  collapse(ioNode);
	  }
	}
}
