/**
 * @fileoverview This is an HTML nav bar widget that allows users to navigate
 * through the content on webpresence website.
 * @requires div with id 'menu'
 */

/**
 * Creates a new Navigation Bar.
 *
 * @param {string} menuDiv
 *      name of menu div.
 * @param {number} noOfLevels
 *      No of navigation leves 2/3.
 * @constructor
 */
NavigationBar = function(menuDiv, noOfLevels) {
  this.menuDiv_ = menuDiv;
  this.levels_ = noOfLevels;
};

/**
 * Initializes the state of the sidebar. It defines the onClick method for
 * sidebar. This function basically performs appropriate changes in the
 * sidebar to open and close appropriate elements when a click happens.
 */
NavigationBar.prototype.initState = function() {
  $('#menu ul').hide();
  $("ul[level = 'level2']").slideUp('normal');
  $("ul[level = 'level3']").slideUp('normal');
  $("ul[level = 'level1']").slideDown('normal');
  $('#menu li a').click(
    /**
     * Adjusts sidebar as a response to the click on any one of the sidebar
     * links. Sidebar has levels_ no of levels. When a click is performed on a
     * link at level i, all the other open elements at level i are closed and
     * the element, which is clicked in opened, so that all the links at level
     * i+1 for the clicked element are displayed.
     * @return {boolean} False when an element having further links down
     * the tree is clicked.
     */
    function() {
      var checkElement = $(this).next();
      var levelAttr = checkElement.attr('level');
      if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        if (levelAttr != 'level1') {
          return false;
        }
      }
      if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        if (levelAttr == 'level2') {
          $("ul[level = 'level2']").slideUp('normal');
          $("ul[level = 'level3']").slideUp('normal');
          checkElement.slideDown('normal');
          return false;
        }
        if (levelAttr == 'level3') {
          $("ul[level = 'level3']").slideUp('normal');
          checkElement.slideDown('normal');
          return false;
        }
      }
      return true;
    }  //End of function
  );
};

/**
 * Adjusts the navigation bar by opening appropriate level1 and level2 elements
 * of the nav bar.
 *
 * @param {string} val1
 *      value of level1 elements.
 * @param {string} val2
 *      value of level2 elements.
 */
NavigationBar.prototype.adjust = function(val1, val2) {
  var condition1 = "a[title = '" + val1 + "']";
  var condition2 = "a[title = '" + val2 + "']";
  $(condition1).next().slideDown('normal');
  $(condition1).next().children().children(condition2).next().slideDown('normal');
};

/**
 * Creates navigation html elements for the specified json node.
 *
 * @param {JSON} jsonNode
 *      json Node for the element to be rendered.
 * @param {Element} parentUlNode
 *      ul element. Current element to be rendered should be the child of this
 *      ul node.
 * @param {number} level
 *      current level of the element in the navigation hierarchy.
 * @return {Element} liNode
 *      li element which will contain the data for the current json node.
 */
NavigationBar.prototype.createNavTree = function(jsonNode, parentUlNode, level)
{
  var liNode = document.createElement('li');
  var aNode = document.createElement('a');
  var ulNode = null;
  //configure nodes
  aNode.innerHTML = jsonNode.name;
  aNode.title = jsonNode.name;
  aNode.href = jsonNode.url;
  liNode.appendChild(aNode);
  if (level < this.levels_) {
    if (level != 0) {
      aNode.href = 'javascript: (void); ';
      parentUlNode.appendChild(liNode);
    }
    ulNode = document.createElement('ul');
    var nextLevel = level + 1;
    ulNode.setAttribute('level', 'level' + nextLevel);
    liNode.appendChild(ulNode);
    for (index in jsonNode.childNodes) {
      childNode = jsonNode.childNodes[index];
      this.createNavTree(childNode, ulNode, level + 1);
    }
  } else {
    parentUlNode.appendChild(liNode);
  }
  return liNode;
};

/**
 * Renders HTML elements of navigation bar for the specified json object.
 *
 * @param {JSON} jsonObj
 *      json object containing the hierarchical metadata required to render the
 *      navigation bar.
 */
NavigationBar.prototype.renderNavigationBar = function(jsonObj) {
  var menu_element = document.getElementById(this.menuDiv_);
  var liNode = this.createNavTree(jsonObj[0], null, 0);
  menu_element.appendChild(liNode);
};

/**
 * Initializes the sidebar for the specified json objects. It includes
 * rendering the navigation bar and initializing its state.
 *
 * @param {JSON} jsonObj
 *      json object containing the hierarchical metadata required to render the
 *      navigation bar.
 */
NavigationBar.prototype.initialize = function(jsonObj) {
  this.renderNavigationBar(jsonObj);
  this.initState();
};

