
$(document).ready(function(){

  // Keeping spammers at bay
  $('.nospam').each(function(){
    var protected_email = $(this).html();
    protected_email = protected_email.replace(" (arobase) ","@");
    $(this)
      .html(protected_email)
      .replaceWith("<a href=\"mailto:"+$(this).text()+"\">"+$(this).text()+"</a>");
  });

  // Using an image for the company name
  //$('strong.name').each(function(){
  //  var text = $(this).text();
  //  $(this).html('<img src="http://media.imagiervagabond.fr/meta/img/imagier-name.png" alt="' + text +'" />')
  //})

  // Tabs on the exposition page
  // Preparing the stage
  $('#expo-tabs').prepend('<ul class="nav"></ul>');
  if ($('#expo-tabs > #galerie').length != 0) {
    var max_tab_height = 800;
  } else {
    var max_tab_height = 0;
  }
  // Doing a bit of work with each future tab, building the tabs navigation
  $('#expo-tabs > .tab').each(function(){
    var text = $(this).find('h2').text();
    var html = '<li><a href="#' + $(this).attr('id') + '">' + text + '</a></li>';
    $('#expo-tabs > .nav').append(html);
    this.setAttribute('tabIndex', '-1');
    $(this).find('h2').addClass('hidden');
    var height = $(this).height();
    if(height > max_tab_height) {
      max_tab_height = height;
    };
  });
  // And then we hide every tab but the first one
  $('#expo-tabs > .tab + .tab').hide();
  $('#expo-tabs > .nav a:eq(0)').addClass('current');
  // Min-height on the container will stop the page from jumping up and down
  $('#expo-tabs').css('min-height', max_tab_height + 50);
  // Finally we take care of clicks on the tabs navigation
  $('#expo-tabs > .nav a').click(function(){
    $('#expo-tabs > .nav a.current').removeClass('current');
    $(this).addClass('current');
    var target_id = this.href.split('#')[1];
    $('#expo-tabs > .tab:visible').hide();
    $('#'+target_id).show();
    return false;
  });

  // Image gallery
  $('#galerie .links a').click(function(){
    var bigpic = $(this).attr('href');
    var legend = $(this).find('img').attr('alt');
    $('#galerie .placeholder img').attr('src', bigpic);
    $('#galerie .placeholder span').text(legend);
    return false;
  })

});
