$(function() {
  $('a.fancybox').fancybox({scrolling: 'no'});
  
  // Make external links open in new window
  if(location.href.match('james-pc') === null) {
    $('a').each(function() {
      var href = $(this).attr('href');
      if(href) {
        if( (href.match('http://') !== null) && (href.match('http://www.learning-works.org.uk') === null) ) {
          $(this).attr('target', '_blank');
        }
      }
    });
  }
  
  // Contact popup
  $('a.contactus').fancybox();

  // Set up placeholders
  $(':input').placeholder();

  // Set up tabs
  $('ul.tabs li a').each(function(i, item) {

    // The ID of the panel to display
    var id = $(this).attr('href').split('#')[1];

    // Select first tab
    if(i == 0) {
      $(this).parent('li').addClass('selected');
    }
    // Hide other tab's content
    else {
      $('#' + id).hide();
    }

    $(this).click(function() {

      $(this).parent('li').siblings().each(function() {
        // De-select sibling tabs
        $(this).removeClass('selected');
        // Hide sibling tab content
        var sib_id = $(this).children('a').attr('href').split('#')[1];
        $('#' + sib_id).hide();
      });
      
      // Select tab and show content
      $(this).parent('li').addClass('selected');
      $('#' + id).fadeIn();
      
      // Prevent scrolling
      return false;
    });
  });
});
