var current_nav;


function buildLeftNav_MenuOptions(nav_options, level, html) {
   html += '<ul>';
   for (var option in nav_options) {
      html += '<li>';

      // class is a JavaScript reserved word, hence _class
      var _class = '';
      if (option == current_nav[level] && level == current_nav.length-1) {
         _class += 'current ';
      }

      // consume href, url, title, tooltip, class
      html += '<a ' +
         'href="' + (nav_options[option].url || nav_options[option].href || '') + '" ' +
         'title="' + (nav_options[option].tooltip || nav_options[option].title || '') + '" ' +
         'class="' + _class + ' ' + (nav_options[option]['class'] || '') + '" ';

      // convert arbitrary arrNAV attributes into HTML attributes
      for (var attrib in nav_options[option]) {
         // exclude the following because they've been consumed or are leftnav specific
         if (['href', 'url', 'title', 'tooltip', 'class', 'options', 'prevnext'].join().search(attrib) > -1) { continue; }
         html += attrib + '="' + nav_options[option][attrib] + '" ';
      }

      html += '>' + option + '</a>';

      if (option == current_nav[level]) {
         html = buildLeftNav_MenuOptions(nav_options[option].options, level+1, html);
      }

      html += '</li>';
   }
   html += '</ul>';

   return html;

}  // buildLeftNav_MenuOptions()


function showLeftNavMenu() {
   var section_name = current_nav[0];
   
   // mgic.com doesn't have a mgic_content, so to be backwards compatible, check to see if it exists
   var mgic_content = document.getElementById('mgic_content');
   if (mgic_content) {
      // websites without leftnav won't call showLeftNavMenu; conditional indenting achieved
      mgic_content.className += ' leftnav_indent';
   }

   var section_name_div = document.getElementById('section_name');
   section_name_div.innerHTML = '<a href="' + (arrNAV[section_name].url || arrNAV[section_name].href || '') + '">' + section_name + '</a>';

   var content_div = document.getElementById('leftnav_content');
   content_div.innerHTML = buildLeftNav_MenuOptions(arrNAV[section_name].options, 1, "");

}  // showLeftNavMenu()


function changeSection(section_name) {
   //current_nav = [section_name];

   //showLeftNavMenu();
   
   window.location.href = arrNAV[section_name].url;

}  // changeSection()


// this could be refactored to support arbitrary attributes like buildLeftNav_MenuOptions
function buildTabs() {
   var html = "";
   var count = 0;
   var dropdown_events;

   for (var section_name in arrNAV) {
      dropdown_events = '';
      
      if (!document.getElementById('leftnav_content') && arrNAV[section_name].options) {
         var tab_dropdown = document.createElement('div');
         tab_dropdown.id = section_name.replace(/[^a-zA-Z]/g, '') + "_dropdown";
         tab_dropdown.className = 'tabs_dropdown';
         
         for (var option_name in arrNAV[section_name].options) {
            var url = (arrNAV[section_name].options[option_name].url || arrNAV[section_name].options[option_name].href || '');
            tab_dropdown.innerHTML += '<a href="' + url + '">'+ option_name + '</a>';
         }
         document.getElementById('mgic_container').appendChild(tab_dropdown);

         dropdown_events = ' onmouseover="mDropDownTabs(event, \'' + tab_dropdown.id + '\');"';
      }
      
      var url = (arrNAV[section_name].url || arrNAV[section_name].href || '');
      if (section_name == current_nav[0]) {
         html += '<span class="on' + (count == 0 ? ' first' : '') + (url == '' ? ' disabled' : '') + '">' + (url == '' ? '' : '<a href="' + url + '"' + dropdown_events + '>') + section_name + '</a></span>';
      }
      else {
         html += '<span class="' + (count == 0 ? ' first' : '') + (url == '' ? ' disabled' : '') + '">' + (url == '' ? '' : '<a href="' + url + '"' + dropdown_events + '>') + section_name + '</a></span>';
      }
      
      count++;
   }
   return html;

}  // buildTabs()


function showTabs() {
   var tabs_div = document.getElementById('tabs');

   tabs_div.innerHTML = buildTabs();

}  // showTabs()

