﻿var siteNav = {

  /*
   * Private:
   */

  /*
   * siteNav.options
   * ---------------
   * This array of objects holds the menu options and submenu options, which
   * are used by methods to generate the main menu navigation on the website.
   *
   * siteNav.options.option  String label of a main menu option.
   * siteNav.options.link    URL destination of a main menu option.
   * siteNav.options.sub     Submenu object for a main menu option.
   * sub.label               String label of a submenu option.
   * sub.link                URL destination of a submenu option.
   */
  options: [
    { option: 'Home', link: 'index.html', sub: null },
    { option: 'Solutions', link: 'solutions.html', sub: null /*[
      { label: 'For Districts', link: 'solutions.html#districts' },
      { label: 'For States', link: 'solutions.html#states' }
    ]*/
    },
    { option: 'About Us', link: 'about.html', sub: [
      { label: 'Contact', link: 'about.html#contact' },
      { label: 'Careers', link: 'about.html#careers' },
      { label: 'Portfolio', link: 'about.html#portfolio' },
      { label: 'Company Info', link: 'about.html' }
    ]
    }
  ],
  
  /*
   * siteNav.subMenuString
   * ---------------------
   * Holds the DOM string of the current page's permanent submenu. Used by events
   * to re-insert the default submenu.
   */
  subMenuString: '',
  
  /*
   * siteNav.initEventsForMenuOption
   * -------------------------------
   * @param o           A menu oject from siteNav.options array.
   * @param isCurrent   Boolean value denoting whether o reflects the current page
   *                    being displayed.
   *
   * initEventsforMenuOption sets up the mouseover events for a main menu
   * option so the visiter can see its submenu on mouseover.
   */
  initEventsForMenuOption: function(o, isCurrent) {
    var menu = $('#site_menu_list');
    var submenu = $('#site_submenu');
    if (isCurrent) {
    // Draw the menu option of THIS page as a non-link, and perma-display its submenu.
      if (o.sub) {
        for (var j in o.sub)
          siteNav.subMenuString += '<li><div><a href="' + o.sub[j].link + '">' + o.sub[j].label + '</a></div></li>';
        submenu.prepend(siteNav.subMenuString);
      }
      menu.prepend($('<li><div class="current">' + o.option + '</div></li>').mouseenter(function() {
        submenu.empty();
        submenu.prepend(siteNav.subMenuString);
      }));
    } else {
      if (o.sub) {
      // Draw other menu options with mouseover events that show their submenus.
        var suboLinkElem = $('<a href="' + o.link + '">' + o.option + '</a>').mouseover(function() {
          var newSubMenuString = '';
          for (var j in o.sub)
            newSubMenuString += '<li><div><a href="' + o.sub[j].link + '">' + o.sub[j].label + '</a></div></li>';
          submenu.empty();
          submenu.prepend(newSubMenuString);
        });
        menu.prepend($('<li></li>').append($('<div></div').append(suboLinkElem)));
      } else {
      // If the menu option has no submenus, just draw the menu option link with no mouseover event.
        menu.prepend($('<li><div><a href="' + o.link + '">' + o.option + '</a></div></li>').mouseenter(function() {
          submenu.empty();
        }));
      }
    }
    
    // Restore the default submenu on mouseleave.
    submenu.mouseleave(function() {
      submenu.empty();
      submenu.prepend(siteNav.subMenuString);
    });
    $('#header').mouseleave(function() {
      submenu.empty();
      submenu.prepend(siteNav.subMenuString);
    });
  },

  /*
   * siteNav.initAnchors
   * -------------------
   * initAnchors sets up all in-page anchors for a scroll-to effect instead of
   * a jump-to effect.
   */
  initAnchors: function() {
    $('.scroll').click(function(event) {
      event.preventDefault();
      var anchor = this.href.split('#')[1];
      var location = $('#' + anchor).offset();
      $('html, body').animate({ scrollTop: location.top }, 500);
    });
    $('.back_to_top a').append('<img src="images/up.png" /> Back to top');
  },
  
  
  /*
   * Public:
   */

  /*
   * siteNav.init
   * ------------
   * @param curOption   Denotes the main menu option that corresponds to the
   *                    current page.
   *
   * siteNav.init initializes the site menu and should be called on page load.
   */
  init: function(curOption) {
    for (var i in siteNav.options)
      siteNav.initEventsForMenuOption(siteNav.options[i], (i == curOption));
    siteNav.initAnchors();
  }
}
