// CNavMenu.js
// Define an object for manipulating Webview's navigation menu.
// We define it as an object to avoid naming conflicts with functions.

// Create an instance of the object
window.wvNavMenu = new Object();
// Array of callback functions to call whenever the window is resized
window.wvNavMenu.ResizeFuncs = new Array;

// callResizeFuncs
// Loop over all of the resize functions and call each of them
window.wvNavMenu.callResizeFuncs = function(){
  try{
    for(var i = 0; i < this.ResizeFuncs.length; i++){
      this.ResizeFuncs[i]();
    }
  }catch(e){
    alert("Error: CNavMenu: callResizeFuncs");
  }
  return;
}

// registerResizeFunc
// f - the function
window.wvNavMenu.registerResizeFunc = function(f){
  try{
    if(f instanceof Function){
      this.ResizeFuncs[this.ResizeFuncs.length] = f;
    }
  }catch(e){
    alert("Error: CNavMenu: registerResizeFunc");
  }
  return;
}

// getButtonElement
// RETURN: DOM Node of the button that collapses the menu
window.wvNavMenu.getButtonElement = function(){
  var node = null;
  try{
    node = document.getElementById("btnHideNav");
  }catch(e){
    alert("Error: CNavMenu: getButtonElement");
  }
  return node;
}

// getMenuElement
// RETURN: DOM Node of the menu element
window.wvNavMenu.getMenuElement = function(){
  var node = null;
  try{
    node = document.getElementById("ColNav");
  }catch(e){
    alert("Error: CNavMenu: getMenuElement");
  }
  return node;
}

// isCollapsed
// RETURN: bool, true if menu is collapsed, false if open
window.wvNavMenu.isCollapsed = function(){
  var ret = false;
  try{
    var menu = this.getMenuElement();
    ret = menu.style.display == "none";
  }catch(e){
    alert("Error: CNavMenu: isCollapsed");
  }
  return ret;
}

// collapse
// Collapse the menu
window.wvNavMenu.collapse = function(){
  try{
    var menu = this.getMenuElement();
    menu.style.display = "none";
    var btn = this.getButtonElement();
    btn.value = ">>>";
    this.callResizeFuncs();
  }catch(e){
    alert("Error: CNavMenu: collapse");
  }
  return;
}

// isOpen
// RETURN: bool, true if menu is open, false if collapsed
window.wvNavMenu.isOpen = function(){
  var ret = false;
  try{
    var menu = this.getMenuElement();
    ret = menu.style.display != "none";
  }catch(e){
    alert("Error: CNavMenu: isOpen");
  }
  return ret;
}

// open
// Open the menu
window.wvNavMenu.open = function(){
  try{
    var menu = this.getMenuElement();
    menu.style.display = "";
    var btn = this.getButtonElement();
    btn.value = "<<<";
    this.callResizeFuncs();
  }catch(e){
    alert("Error: CNavMenu: open");
  }
  return;
}

// toggle
// Toggle the menu between its open and closed states
window.wvNavMenu.toggle = function(){
  try{
    if(this.isOpen()){
      // Collapse
      this.collapse();
    }else{
      // Open
      this.open();
    }
  }catch(e){
    alert("Error: CNavMenu: toggle");
  }
  return;
}