// $Id$

/**
 * @author Henri Médot
 * @version 2009-11-24
 */

$(function() {

  // config
  var domain = 'didiersacodesign.fr';
  var threshold = 20;

  // initialise les liens externes
  var hostname = window.location.hostname;
  $('a[href]').each(function(i) {
    if (this.href.indexOf(hostname) == -1) {
      $(this).addClass('target-blank');
    }
  });
  $('a.target-blank').attr('target', '_blank');

  // initialise les liens mailto
  $('.mailto').each(function(i) {
    var html = $(this).html();
    $(this).html('<a href="mailto:' + html + '@' + domain + '">' + html + '@' + domain +'</a>');
  });

  // initialise le menu latéral
  var scroll = 0;
  var menu = $('#menu');
  var sideMenu = $('#side-menu');
  var sideMenuInner = $('#side-menu-inner');
  $('li.active', menu).parent().parent().addClass('active');
  sideMenuInner.html(menu.html());
  $('.top-item > a', sideMenuInner).prepend('[ ').append(' ]');
  $('li:not(.top-item) > a > span', sideMenuInner).append('<span class="bullet">&lt;</span>');
  $('li li:not(.active) a', sideMenuInner).mouseenter(function(event) {
    $('> span', this).stop(true).animate({paddingRight: '10px'}, 100);
  }).mouseleave(function(event) {
    $('> span', this).stop(true).animate({paddingRight: '0'}, 100);
  });
  $('.top-item:not(.active)', sideMenuInner).mouseenter(function(event) {
    $('.top-item.active', sideMenuInner).addClass('leave');
  }).mouseleave(function(event) {
    $('.top-item.active', sideMenuInner).removeClass('leave');
  });

  var scrollTop = function(scroll, animate) {
    if (!animate) {
      sideMenu.scrollTop(scroll);
    }
    else {
      sideMenu.stop(true).animate({scrollTop: scroll}, 250);
    }
  };

  var scrollTopEvent = function(event, animate) {
    var h = sideMenu.height();
    var innerH = sideMenuInner.outerHeight();
    if ((h >= innerH) || (event == null)) {
      scroll = 0;
    }
    else {
      scroll = -threshold + Math.max(threshold, Math.min(h - threshold, event.pageY)) * (innerH - h + 2 * threshold) / h;
    }
    scrollTop(scroll, animate);
  };

  var scrollTopItem = function(item, animate) {
    if (item.length == 0) return;
    var h = sideMenu.height();
    scrollTop(Math.round(item.position().top + item.outerHeight() / 2 - h / 2 + 20), animate);
  };

  sideMenu.mousemove(function(event) {
    scrollTopEvent(event);
  });

  $(window).resize(scrollTopEvent);
  scrollTopItem($('.top-item.active', sideMenuInner));

  // initialise le menu principal
  $('.top-item', menu).each(function(i) {
    $(this).data('index', i);
  }).mouseenter(function(event) {
    var item = $('.top-item-' + $(this).data('index'), sideMenuInner);
    if (!item.hasClass('active')) {
      $('.top-item.active', sideMenuInner).addClass('leave');
      item.addClass('hover');
    }
    scrollTopItem(item, true);
  }).mouseleave(function(event) {
    $('.top-item', sideMenuInner).removeClass('hover leave');
  });

  // initialise le menu secondaire
  $('#secondary-menu ul ul').hide();
  $('#secondary-menu > ul > li > a').click(function(event) {
    $('+ ul', this).toggle('fast');
  });

  // initialise le pager
  var title = $('#main h1').html();
  if (title) {
    $('#content .tab .caption').prepend(title + '<br />');
  }
  if ($('.pager').length > 0) {
    var selectTab = function(a) {
      a = $(a);
      $('.pager a').removeClass('active');
      a.addClass('active');
      $('#content .tab').hide();
      $(a.attr('href')).show();
    };

    $('.pager a').click(function(e) {
      selectTab(this);
    });
    var hash = window.location.hash;
    var hasSelected = false;
    if (hash != '') {
      var a = $('.pager a[href=' + hash + ']');
      if (a.length == 1) {
        selectTab(a);
        window.location = hash;
        hasSelected = true;
      }
    }
    if (!hasSelected) {
      selectTab('.pager a:first');
    }
  }
});
