/* Author: Fabrice Delaneau */


$(document).ready(function() {

  $('a.fancybox').fancybox();

  // Setup main navigation dropdown
  var $nav = $('#nav');
  $nav.find('> li').each(function(index) {
    var $will_hide = false;
    var $was_on_subnav = false;
    if ($(this).find('ul').length) {
      var
        $el      = $(this),
        $subnav  = $el.find('.subnav')
      ;


      $el.hover(function(e) {
        $subnav.css("padding-left", $el.offset().left-220);
        $subnav.show().trigger("subnav-show");
      }, function(e) {
        if(e.pageY < 176){
          $subnav.hide().trigger("subnav-hide");
        } else {
          $will_hide = setTimeout(function() {
            $('.subnav:visible').hide().trigger("subnav-hide");
          }
          , 500);
        }
      });

      $('.subnav').hover(function(e) {
        if($will_hide){
          clearTimeout($will_hide);
        }
      }, function(e) {
        if($will_hide){
          clearTimeout($will_hide);
        }
        $('.subnav:visible').hide().trigger("subnav-hide");
        return false;
      });

    }
  });


  // Menu link class handler
  (function(){

    $nav.bind('subnav-show', function(e){
      $(e.target).parents("li").addClass("current");
    });

    $nav.bind('subnav-hide', function(e){
      $(e.target).parents("li").removeClass("current");
    });

  })();

  // Hide other subnav
  (function(){
    $nav.bind('subnav-show', function(e){
      $nav.find(".subnav").not(e.target).each(function(){
        var $subnav = $(this);
        if ($subnav.is(":visible"))
        {
          $subnav.hide();
          $subnav.trigger("subnav-hide");
        }
      });
    });
  })();

  // Click outside of an open menu hides it
  (function(){
    $('body:first').click(function(e){
      // Clicks inside account menu are ignored
      if ($(e.target).parents("#nav").length)
      {
        return;
      }
      $nav.find('.subnav:visible').each(function(){
        $(this).hide().trigger("subnav-hide");
      });
    });
  })();









  // Setup home slider
  var
    $slider = $('#loopedslider'),
    $pagination,
    paginationWidth,
    containerWidth = 945,
    paginationPosition
  ;

  // setup loopSlider plugin
  $slider.loopedSlider({
    containerClick: false,
    autoStart: 5000,
    hoverPause: true,
    addPagination: false
  });

  // center loopSlider pagination
  $pagination = $slider.find('.pagination');
  paginationWidth = $pagination.width();
  if ($slider.hasClass('small-slider'))
  {
    containerWidth = 305;
    paginationPosition = - Math.round((containerWidth / 2) + (paginationWidth / 2));
  }
  else
  {
    paginationPosition = Math.round((containerWidth / 2) - (paginationWidth / 2));
  }
  $pagination.css('left', paginationPosition + 'px');

});












