$(document).ready(function() {
  /*
   * Hide header photo
   *
   */
  $(".header_background").hide();
  

  /*
   * Check if submenu exists
   *
   */
  if ($(".submenu").size()) {
    /*
     * Align submenu with current menuitem
     *
     */
    if ($(".submenu").offset().left > $(".menuitem_active").offset().left) {
      $(".submenu").offset({ left: $(".menuitem_active").offset().left });
    }


    /*
     * Add first child and last child to submenu
     *
     */
    $(".submenu a:first-child, .submenu_disabled:first-child").addClass("first_child");
    $(".submenu a:last-child, .submenu_disabled:last-child").addClass("last_child");


    /*
     * Align submenu's arrow
     *
     */
    var left = Math.round($(".menuitem_active").position().left - ($(".menu").width() - $(".submenu").width()));
    $(".submenu_center").css("background-position", left + "px 0px");


    /*
     * Hide submenu, show it when window loads
     *
     */
    if (jQuery.support.leadingWhitespace) {
      $(".submenu").hide();
      $(".submenu").css("top", "200px");
    }
  }


  /*
   * Align menuitem's reflection to menuitem
   *
   */
  if ($(".menuitem_active").offset()) $(".menu_reflection").offset({ left: Math.round($(".menuitem_active").offset().left) });


  /*
   * Make menuitem's reflection same width as menuitem
   *
   */
  $(".menu_reflection_center").width($(".menuitem_active a").width());


  /*
   * Align menuitem's glow to menuitem
   *
   */
  if ($(".menuitem_active").offset()) $(".menu_glow").offset({ left: Math.round($(".menuitem_active").offset().left - ($(".menu_glow").width() / 2) + ($(".menuitem_active").width() / 2)) });


  /*
   * Make menuitem's bubble same width as menuitem
   *
   */
  $(".menu_bubble_center").width($(".menuitem_active a").width());

  if ($(".menuitem_active").offset()) $(".menu_bubble").offset({ left: Math.round($(".menuitem_active").offset().left) });
  $(".menu_bubble").css({ "bottom": "0px" });
  $(".menu_bubble").hide();

  if (jQuery.support.leadingWhitespace) {
    $(".menu_reflection").css("top", "200px");
    $(".menu_reflection").hide();
  }


  /*
   * Align topic's glow to topic
   *
   */
  $(".h3_glow").each(function() {
    $(this).offset({ left: Math.round($(this).parent().offset().left - ($(this).width() / 2) + ($(this).parent().find(".h3_center").width() / 2)) });
  });


  /*
   * Top Menu
   *
   */
  $(".topmenuitem").hover(
    function() {
      $(this).animate({ "color": "#ffffff" }, { duration: 700, queue: false });
    },
    function() {
      $(this).animate({ "color": "#848e98" }, { duration: 700, queue: false });
    }
  );


  /*
   * Menu
   *
   */
  $(".menuitem:not(.menuitem_active)").hover(
    function() {
      $(this).find("a").animate({ "color": "#ffffff" }, { duration: 700, queue: false });
    },
    function() {
      $(this).find("a").animate({ "color": "#848e98" }, { duration: 700, queue: false });
    }
  );


  /*
   * Make menuitem's bubble, glow and reflection follow mouse
   *
   */
  $(".menuitem").hover(
    function() {
      var menuitem = $(this);
      var left = Math.round($(".menu").position().left + $(this).position().left);
      var width = $(this).find("a").width();
      var easing;


      if ($(this).index() == 0 || $(this).index() == $(".menuitem").size() - 1) {
        easing = "easeOutQuint";
      } else {
        easing = "easeOutBack";
      }


      /*
       * Align and resize bubble and reflection
       *
       */
      $(".menu_bubble, .menu_reflection").animate({ "left": left }, { duration: 300, queue: false, easing: easing });
      $(".menu_bubble_center, .menu_reflection_center").animate({ "width": width }, { duration: 300, queue: false, easing: easing });


      /*
       * Align glow
       *
       */
      $(".menu_glow").animate({ "left": left - ($(".menu_glow").width() / 2) + (menuitem.width() / 2) }, { duration: 300, queue: false, easing: easing });
    },
    function() {
      var menuitem = $(this);
      var left = Math.round($(".menu").position().left + $(".menuitem_active").position().left);
      var width = $(".menuitem_active a").width();


      if ($(".menuitem_active").index(".menuitem") == 0 || $(".menuitem_active").index(".menuitem") == $(".menuitem").size() - 1) {
        easing = "easeOutQuint";
      } else {
        easing = "easeOutBack";
      }


      /*
       * Align bubble and reflection
       *
       */
      $(".menu_bubble, .menu_reflection").animate({ "left": left }, { duration: 300, queue: false, easing: easing });
      $(".menu_bubble_center, .menu_reflection_center").animate({ "width": width }, { duration: 300, queue: false, easing: easing });


      /*
       * Align glow
       *
       */
      $(".menu_glow").animate({ "left": Math.round(left - ($(".menu_glow").width() / 2) + ($(".menuitem_active").width() / 2)) }, { duration: 300, queue: false, easing: easing });
    }
  );

  $(".submenu a:not(.submenu_active)").hover(
    function() {
      $(this).animate({ "color": "#ffffff" }, { duration: 700, queue: false });
    },
    function() {
      $(this).animate({ "color": "#848e98" }, { duration: 700, queue: false });
    }
  );
});

$(window).load(function() {
  /*
   * Show header photo
   *
   */
  $(".header_background").fadeIn(700);


  /*
   * Show menu bubble and reflection
   *
   */
  $(".menu_bubble").fadeIn(700);
  $(".menu_bubble").animate({ bottom: 10 }, { duration: 700, queue: false });

  if (jQuery.support.leadingWhitespace) {
    $(".menu_reflection").fadeIn(700);
    $(".menu_reflection").animate({ top: 210 }, { duration: 700, queue: false });
  }

  // Show submenu
  if (jQuery.support.leadingWhitespace) {
    $(".submenu").fadeIn(700);
    $(".submenu").animate({ top: 210 }, { duration: 700, queue: false });
  }
});
