//SETTINGS

var timeout_length = 100;


// VARIABLES

var current_nav_item = null;
var mm_active = false;
var ddm_active = false;
var timeout = null;

jQuery(function($) {
  $("#nav ul li.ddm").each(function (i) {
    $(this).prepend('<span class="left_corner"></span><span class="center_repeat"></span><span class="right_corner"></span>');
  });
  
  
  ////////////////////////////////////////////////////////
  // EVENT: Primary nav events
  ////////////////////////////////////////////////////////
  
  $("#nav li.ddm").mouseover(function () {
    deactivate_mm();
                                       
    current_nav_item = $(this);
    mm_active = true;
                                       
    $(this).addClass("hover");

    show_dropdown_menu();
  });
  
  $("#nav li.ddm").mouseout(function () {
    mm_active = false;
    timeout = setTimeout(deactivate_ddm, timeout_length);
  });
  
  
  ////////////////////////////////////////////////////////
  // EVENT: Menu events
  ////////////////////////////////////////////////////////
  
  $("#dd_menu").mouseover(function () {
    ddm_active = true;
  });
  
  $("#dd_menu").mouseout(function () {
    ddm_active = false;
    timeout = setTimeout(deactivate_ddm, timeout_length);
  });
  
}); //close: jQuery(function($) {


////////////////////////////////////////////////////////
// FUNCTION: Deactivate dropdown menu
////////////////////////////////////////////////////////

function deactivate_ddm(){
  if(mm_active == false && ddm_active == false){
    hide_dropdown_menu();
    deactivate_mm();
  }
}


////////////////////////////////////////////////////////
// FUNCTION: Deactivate main menu
////////////////////////////////////////////////////////

function deactivate_mm(){
  if(current_nav_item != null){
    $(current_nav_item).removeClass("hover");
    current_nav_item = null;
  }
}


////////////////////////////////////////////////////////
// FUNCTION: Resize dropdown menu
////////////////////////////////////////////////////////

function resize_dropdown_menu(mm_width, mm_height) {  
  $("#dd_menu_content").css("width", mm_width);
  $("#dd_menu_bottom_center").css("width", mm_width - 10);

  $("#dd_menu_side_right").height($("#dd_menu_content").height() + 25);
  $("#dd_menu_side_left").height($("#dd_menu_content").height() + 25);
  
  //adjust width
  $("#dd_menu_content").css("width", mm_width);
  $("#dd_menu_bottom_center").css("width", mm_width - 10);
}


////////////////////////////////////////////////////////
//FUNCTION: Resize dropdown menu
////////////////////////////////////////////////////////

function resize_position_dropdown_menu() {
	var current_nav_id = $(current_nav_item).attr("id");
	switch(true){
    case current_nav_id == "nav_hotel":
      resize_dropdown_menu(515, 300);
      position_dropdown_menu(20);
      break;
      
    case current_nav_id == "nav_dining":
      resize_dropdown_menu(485, 310);
      position_dropdown_menu(20);
      break;
      
    case current_nav_id == "nav_bars":
      resize_dropdown_menu(375, 310);
      position_dropdown_menu(20);
      break;
        
    case current_nav_id == "nav_amenities":
      resize_dropdown_menu(595, 310);
      position_dropdown_menu(20);
      break;
        
    case current_nav_id == "nav_entertainment":
      resize_dropdown_menu(405, 310);
      position_dropdown_menu(20);
      break;
        
    case current_nav_id == "nav_casino":
      resize_dropdown_menu(430, 310);
      position_dropdown_menu(20);
      break;
  
    case current_nav_id == "nav_conventions":
      resize_dropdown_menu(595, 310);
      position_dropdown_menu(260);
      break;
      
    case current_nav_id == "nav_activities":
      resize_dropdown_menu(520, 310);
      position_dropdown_menu(380);
      break;
    
    case current_nav_id == "nav_specials":
      resize_dropdown_menu(470, 310);
      position_dropdown_menu(20);
      break;
  }
}


////////////////////////////////////////////////////////
// FUNCTION: Position dropdown menu
////////////////////////////////////////////////////////

function position_dropdown_menu(mm_offset) {
  var position = $(current_nav_item).position();
  $("#dd_menu").css("left", position.left - mm_offset);
}


////////////////////////////////////////////////////////
// FUNCTION: Show dropdown
////////////////////////////////////////////////////////

function show_dropdown_menu() {
  $("#dd_menu").show();
  
  $(".nav_menu").hide();
  
  var nav_name = $(current_nav_item).attr("id");
  $("#" + nav_name + "_menu").show();
  resize_position_dropdown_menu();
}


////////////////////////////////////////////////////////
// FUNCTION: Hide dropdown
////////////////////////////////////////////////////////

function hide_dropdown_menu() {
  $("#dd_menu").hide();
}

////////////////////////////////////////////////////////
// BUILD DROPDOWN MENUS
////////////////////////////////////////////////////////

jQuery(function($) {
  $("#nav li.ddm").each(function (i) {
    $("#dd_menu_content").prepend('<div id="' + $(this).attr("id") + '_menu" class="nav_menu"><ul>' + $("#site_map a[href*='" + $(this).find("a").attr("href") + "']:first").parent().find("ul").clone().html() + '</ul></div');
  });
  
  $(".nav_menu ul ul ul").remove();
		
		$(".nav_menu a").click(function() {
		  pageTracker._trackEvent('Navigation', 'Click', $(this).text());
		});
  //$(".nav_menu ul li").attr("class", "");	
  
});
