$(document).ready(function() {

    // Font replacement
    Cufon.replace('#main h2', { fontFamily: 'HelveticaNeue LT 63 MdEx', fontWeight: '500' });
    Cufon.replace('#side-menu li a', { fontFamily: 'HelveticaNeue LT 53 Ex', hover: true });
    Cufon.replace('.product-list h4 a', {
        fontFamily: 'HelveticaNeue LT 63 MdEx',
        fontWeight: '500',
        hover: true
    });
    Cufon.replace('h3', { fontFamily: 'HelveticaNeue LT 53 Ex' });

    // Product hover
    var $product_list = $("#main").find(".product-list");
    $("li", $product_list).each(function() {
        $(this).hover(
      function() {
          $("h4", this).addClass("hover");
          Cufon.refresh('.product-list h4 a');
      },
      function() {
          $("h4", this).removeClass("hover");
          Cufon.refresh('.product-list h4 a');
      }
    );
        $(this).click(function() {
            window.location.href = $("a", this).attr("href");
        });
    });


    // Adjust position on bottom links
    function adjustBottomPosition() {
        var $main = $("#main");
        var rightHeight = $(".right", $main).innerHeight();
        if ($main.hasClass("home")) {
            var topPadding = $(".left", $main).css('paddingTop');
            $(".left", $main).height(rightHeight - topPadding.replace("px", ""))
      .find("#side-menu").css('position', 'absolute').css('bottom', 0);
            var $intro = $(".intro", $main);
            var introPadding = $intro.css('paddingBottom').replace("px", "");
            $intro.height($("#side-menu").position().top - 36).css('overflow', 'auto').css('position', 'absolute');
            $intro.jScrollPane();
        }
    }
    if (jQuery.browser.webkit) {
        $("img").load(function() {
            setTimeout(adjustBottomPosition(), 200);
        });
    }
    else {
        adjustBottomPosition();
    }
    // Top Nav Dropdown
    var $nav = $("#nav");
    $(".sub-nav", $nav).each(function() {
        if ($(this).parent().attr("id") != "products") {
            var $sub = $(this);
            var origHeight = $sub.height();
            $sub.css({ 'height': 0, 'opacity': 0.93 });
            if (jQuery.browser.msie) {
                $(this).get(0).style.removeAttribute('filter');
            }
            $(this).parent()
      .hover(function() {
          $sub.stop().animate({
              height: origHeight
          });
      },
      function() {
          $sub.stop().animate({
              height: 0
          }, 200, function() {
              $sub.prev().css({ 'background-color': 'transparent', 'color': '#003e67' });
          });
      }
      );
        }
        else {
            var $megaProducts = $(this).parent();
            $megaProducts.children(".sub-nav").remove();
            $megaProducts.append("<div id='mega'><img src='/img/loading.gif' alt='Loading...' class='loading' /><div id='mega-content'></div></div>");
            $megaProducts.hover(
        function() {
            $(this).find("#mega").slideDown("normal", function() {
                $("#mega-content").load("/product-dropdown.aspx", function() {
                    $("#menu-categories", this).show();
                    $(".loading", "#mega").stop(true, true).hide();
                })
            });
        },
        function() {
            $(this).find("#mega").stop(true, true).slideUp("fast");
        }
      );
        }
    });
    if (jQuery.browser.msie) {
        $(".sub-nav li a", $nav).hover(
      function() {
          $(this).parent().css('background', '#fff');
      },
      function() {
          $(this).parent().css('background', 'transparent');
      }
    );
    }
    $nav.children("li").hover(
    function() {
        $(this).css('background', '#003e67 url(/img/sidenav_bg_o.jpg) repeat-x top');
        $(this).children("a").css({ 'color': '#fff' });
    },
    function() {
        $(this).not(".selected").css('background', 'transparent');
        $(this).not(".selected").children("a").css({ 'color': '#003e67' });
    }
  );

    // Top Nav Product drop-down
    var $products = $("#products");
    var $menuProducts = $("#menu-products");
    var width = $menuProducts.width();
    var height = $menuProducts.height();
    $menuProducts.css({
        'width': 0,
        'height': 0,
        'top': $products.height(),
        'opacity': 0,
        'display': 'block'
    });
    $products.hoverIntent(
  function() {
      $menuProducts
    .stop()
    .animate({
        width: width,
        height: height,
        opacity: 0.5
    }, 600)
    .animate({
        opacity: 0.8
    }, 200);
  },
  function() {
      $menuProducts
    .stop()
    .animate({
        width: 0,
        height: 0,
        opacity: 0
    }, 200);
  });
});
