menuHash = {"games-menu-item-button": "games-submenu", "news-menu-item-button": "news-submenu", "company-menu-item-button": "company-submenu", "careers-menu-item-button": "careers-submenu", "contact-menu-item-button": "contact-submenu" };
submenuHash = {"games-submenu": "games-menu-item-button", "news-submenu":"news-menu-item-button", "company-submenu": "company-menu-item-button", "careers-submenu": "careers-menu-item-button", "contact-submenu": "contact-menu-item-button" };
var openMenuId = false;
var menuOriginalTop;
var menuAnimating = false;
  
function setupMenu() {
  var menuOpen = function(menu, callback) {
    menuAnimating = menu;
    openMenuId = menu;
    if (typeof callback === 'undefined') {
      callback = function() { return false; };
    }
    $("#" + menu).stop().animate({"top":"70px"},"500",function() {     
      menuAnimating = false;
      callback();
    });
//     console.log("menuOpen() complete.");
  };
  
  var menuClose = function(callback) {
//     console.log("menuClose() called with openMenuId = " + openMenuId);
    if (typeof callback === 'undefined') {
      callback = function() { return false; };
    }
    if (openMenuId) {
      var thisOpenMenuId = openMenuId;
      openMenuId = false;
      var top = (80 - $("#" + thisOpenMenuId).outerHeight()) + "px";
      $("#" + thisOpenMenuId).stop().animate({"top":top},"400",function() {
// 	console.log("Menu " + thisOpenMenuId + " closed.");
	
	callback();
      });
    } else {
//       console.log(openMenuId + " isn't a real menu.");
    }
//     console.log("menuClose() complete.");
  }
    
//   console.log("List of dropdowns:");
//Detect which menu item should be selected, based on the current url and the contents of the menu.  Apply the "selected" class appropriately.
  $(".menu-item").each(function() {
    var thisId = $(this).children("a").attr("id");
    if ($(this).hasClass("dropdown")) {
      
//       console.log(thisId);
//       console.log(menuHash[thisId]);
      $("#" + menuHash[thisId]).find("a").each(function() {
	if (document.location.href.indexOf($(this).attr("href")) > 0) {
	  console.log(document.location.href.indexOf($(this).attr("href")));
	  console.log("Hey!  " + thisId + " should be highlighted!");
	  $("#" + thisId).addClass("selected");
	}
// 	console.log($(this).attr("href"));
      });
    } else {
//       console.log($(this).find("a").attr("href"));
      if (document.location.href.indexOf($("#" + thisId).attr("href")) > 0) {
	console.log("Hey!  " + thisId + " should be highlighted!");
	$("#" + thisId).addClass("selected");
      } else {
	console.log("Looking for " + $("#" + thisId).attr("href") + " in " + document.location.href);
      }
    }
  });
  
  $(".dropdown a, .dropdown a .chevron").bind("mouseover", function() {
    var newOpenMenuId = menuHash[this.id];
    if ($(this).hasClass("chevron")) {
//       console.log("Chevron detected: ");
//       console.log($(this).parent().attr("id"));
      newOpenMenuId = menuHash[$(this).parent().attr("id")];
    }
    var thisOpenMenuId = openMenuId;
    if (newOpenMenuId != openMenuId) {
      menuClose();
      menuOpen(newOpenMenuId);
    }
    return(false);
  });
  
  $(".submenu-item").add("#menu").add("#menu .chevron").bind("mouseover", function(e) {
    e.stopPropagation();
  });
  
  $("body").bind("mouseover", function() {
    menuClose();
  });
}

