(function($){
  var $headerLinks = $("#header ul.navigation > li > a"),
      $productsNav = $("#nav-categories").css({"opacity":0, "top": 35}).hide(),
      isShow = false;
  
  function showCategories() {
    isShow = true;
    $productsNav.show().animate({
      "top": 30,
      "opacity": .95
    }, 150);
    $productsNav.parent().addClass("active");
  };
  
  function hideCategories() {
    isShow = false;
    $productsNav.animate({
      "top": 35,
      "opacity": 0
    }, 150, function(){ $productsNav.hide(); });
    $productsNav.parent().removeClass("active");
  };
  
  $productsNav.click(function(e){
      e.stopPropagation();
    })
    .prev().click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      if (isShow) {
        hideCategories();
        $("body").unbind("click", hideCategories);
      } else {
        showCategories();
        $("body").one("click", hideCategories);
      }
    });
  
  $("ul.classes-danger li").hover(
    function(){
      var $this = $(this),
          $thisTitle = $this.data("title-elt");
      
      if (!$thisTitle) {
        $thisTitle = $('<span class="title"><span>' + $this.find("img").attr("title") + '</span></span>').appendTo($this);
        $thisTitle.css({
          left: -$thisTitle.outerWidth()/2+$this.width()/2 + "px",
          top: $this.height() + "px"
        });
        
        $this.data("title-elt", $thisTitle);
      }
      $this.data("title-elt").show();
      
    },
    function(){
      $(this).data("title-elt").hide();
    });
  
  $.e3cortex_productsNav = function(miniHeight, txtMore){
    var $productsNav = $("#footer .products-nav"),
        $productsNavs = $productsNav.find("div"),
        $moreLinks = $('<a class="more" tabindex="0">'+txtMore+'</a>').appendTo($productsNavs),
        totalHeight = $productsNav.height();

    $productsNavs.find("dl").css({
      "height": miniHeight,
      "overflow": "hidden"
    });

    $moreLinks.click(function(){
      $productsNavs.find("dl").animate({
        "height": totalHeight
      }, 150, function(){
        $moreLinks.remove();
      });
    });
  };
})(jQuery);
