// Preloader
(function() {
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        cache.push(cacheImage);
    };
  };
})();

window.onload = function() {
  if ($.preLoadImages) {
    $.preLoadImages("/images/backgrounds/about.jpg", "/images/backgrounds/contact.jpg", "/images/backgrounds/fresh-food-local.jpg", "/images/backgrounds/fresh-food-promise.jpg", "/images/backgrounds/fresh-food.jpg", "/images/backgrounds/hospitality-commercial.jpg", "/images/backgrounds/hospitality-school.jpg", "/images/backgrounds/hospitality.jpg", "/images/backgrounds/parents.jpg", "/images/backgrounds/people.jpg", "/images/backgrounds/together-talk.jpg", "/images/backgrounds/training-chef.jpg", "/images/backgrounds/training.jpg", "/images/backgrounds/vacancies.jpg", "/images/backgrounds/who-pre.jpg", "/images/backgrounds/who-preparatory.jpg", "/images/backgrounds/who-senior.jpg", "/images/backgrounds/who-sixth.jpg", "/images/backgrounds/who.jpg", "/images/backgrounds/work-financial.jpg", "/images/backgrounds/work-wsh.jpg", "/images/backgrounds/work.jpg", "/images/backgrounds/homepage1.jpg", "/images/backgrounds/homepage2.jpg", "/images/backgrounds/homepage3.jpg");
  }
}


$(function() {

  // Background Switching
  $('#backgrounds img:gt(0)').hide();
  setInterval(function(){
    $('#backgrounds :first-child').fadeOut(1000)
      .next('img').fadeIn(1000)
      .end().appendTo('#backgrounds');}, 
  5000);
  
  // Team Members
  $('#team-members a').on('mouseenter', function(e) {
    e.preventDefault();
    
    var index = $("#team-members li a").index($(this));
    $("#team li").removeClass('active').eq(index).addClass('active');
    $('#team-members li').removeClass('active');
    $(this).closest('li').addClass('active');
  });
  
  $('#team li').on('mouseenter', function() {
    var index = $("#team li").index($(this));
    $("#team-members li").removeClass('active').eq(index).addClass('active');
    $('#team li').removeClass('active');
  });
  
  $('#team li').on('mouseleave', function() {
    $('#team-members li').removeClass('active');
  });
  
  // Individuals
  $('#individuals li').on('mouseenter', function() {
    var index = $("#individuals li").index($(this));
    $(".individual").removeClass('active').eq(index).addClass('active');
    $('#individuals li').removeClass('active');
    $(this).addClass('active');
  });
  
  // Vacancies
  $('#category li').on('click', function() {
    var index = $("#category li").index($(this));
    var location = $('#location ul').eq(index);
    
    if ((!$(this).hasClass('active'))) {
      $('#role li').removeClass('active');
    }
    
    $('#category li').not($(this)).removeClass('active');
    $(this).addClass('active');
    
    $('#location ul').not(location).removeClass('active');
    location.addClass('active');
    
    $("#location").jScrollPane({showArrows: true, maintainPosition: false});
    
  });
  
  $('#location li').on('click', function() {
    var location_index = $('#location ul').index(($(this).parent()));
    var index = $(this).parent().children().index($(this));
    var role = $('#role ul.role').eq(location_index).children('li.job-description').eq(index);
    $('#role li').not(role).removeClass('active');
    role.addClass('active');
    
    $("#role").jScrollPane({showArrows: true, maintainPosition: false});
  });
  
  // Custom Scrolling
  $("#category").jScrollPane({showArrows: true, maintainPosition: false});
  
  
  // Contact Addresses
  $('#offices li').on('mouseenter', function() {
    var index = $('#offices li').index($(this));
    var address = $('#address li').eq(index);
    
    $('#offices li').not($(this)).removeClass('active');
    $(this).addClass('active');
    
    $('#address li').not(address).removeClass('active');
    address.addClass('active');
  
    if (index == 0) {
      $("#map").removeClass();
    }
    else if (index == 1) {
      $("#map").removeClass().addClass('leeds');
    }
    else if (index == 2) {
      $("#map").removeClass().addClass('edinburgh');
    }
    else {
      $("#map").removeClass().addClass('london');
    }
  });
  
  // Nav
  var nav_items = $('#masthead nav ul ul > li');
  nav_items.each(function(i, elem) {
    if ($(elem).hasClass('active')) {
      $(elem).parentsUntil('nav', 'li').addClass('show-subnav');
    };
  });
  
  
});
