(function( $ ){

  $(document).ready(function() {

    $('#nav ul').superfish(); // navigation drop down menu

    if ( $('.switcher').length > 0 ) {

      PreloadImgs();
      SetTimer(secs);

      // only show the first 5 items
      $('.switcher li:lt(5)').css('display','inline');
      //var startAt = Math.floor(Math.random()*($('.switcher li:lt(5)').length));
      //$('.switcher li').eq(startAt).attr('class', 'selected'); // set a random LI to selected and swap the BG
      var link = 'webjet';
      $('.switcher li:first-child').attr('class','selected');
      $.fn.fullscreenr({
        width: 1000,
        height: 735,
        bgID: '#bgimg'
      });
      $('#bgimg').fadeIn(1000, function(){
        $('#'+link+'-msg').fadeIn(1000);
      });

      $('.switcher a').bind('click', function(e){
        e.preventDefault();
        clearTimeout(switcherTimer); // restart the timer
        if ( $(this).parent('li').attr("class") != 'selected') { // check it is not already selected
          $('.switcher li').attr('class',''); // clear old selected items
          $(this).parent('li').attr('class', 'selected').find('a').swapBg(); // set to selected and swap the BG
        }
        SetTimer(secs); // start the time again
      });

      $('.switcher a').tooltip({ // add a tooltip to each of the switcher buttons
        track: false,
        delay: 0,
        showURL: false,
        fade: 250,
        top: -35,
        left: -25,
        bodyHandler: function() {
          return $(this).html(); // set the link text to be the title
        }
      });

    }

    // fading hover overlay on client logos
    if ( $('.client-logos').length ) {
      $('.client-logos a').bind('mouseover', function(){
        var linkSrc = $(this).attr('href');
        $(this).parent('li').css({
          position:'relative'
        });
        var img = $(this).children('img');
        $('<a />')
        .attr('href', linkSrc)
        .text(' ')
        .css({
          'height': img.height(),
          'width': img.width(),
          'background-color': '#fff',
          'position': 'absolute',
          'top': 0,
          'left': 0,
          'cursor' : 'pointer',
          'opacity': 0.0
        })
        .bind('mouseout', function(){
          $(this).fadeOut(500, function(){
            $(this).remove();
          });
        })
        .insertAfter(this).animate({
          'opacity': 0.5
        }, 500);
      });

    }

  });

  // load the bg image
  $.fn.swapBg = function(){
    clearTimeout(switcherTimer); // restart the timer
    var linkParts = this.attr('href').split('/');
    var link = linkParts[2];
    $('#header-msg div').fadeOut(500);
    $('#bgimg').fadeOut(500, function(){
      if ( $('#'+link+'-img').length ) {
        $('#bgimg').attr('src', $('#'+link+'-img').attr('src'));
        $.fn.fullscreenr({
          width: 1000,
          height: 735,
          bgID: '#bgimg'
        });
        $('#bgimg').fadeIn(1000, function(){
          $('#'+link+'-msg').fadeIn(1000);
        });
      }
    });
    SetTimer(secs); // start the time again
  }

  // simulate a click on the switcher buttons
  function Switcher() {
    if ( $('.switcher li.selected').length ) {
      if ($('.switcher li.selected').index() >= $('.switcher li:lt(4)').length ){
        $('.switcher li').attr('class','');
        $('.switcher li:first-child').attr('class','selected').find('a').swapBg();
      } else {
        $('.switcher li.selected').next('li').attr('class','next');
        $('.switcher li.selected').attr('class','');
        $('.switcher li.next').attr('class','selected');
        $('.switcher li.selected').find('a').swapBg();
      }
    } else {
      $('.switcher li:first-child').attr('class','selected').find('a').swapBg();
    }
  }

  var switcherTimer;
  var secs = 10000;
  function SetTimer(secs) {
    if ( $('#homepage').length ) {
      switcherTimer = setTimeout(Switcher, secs);
    }
  }

  function PreloadImgs() {
    // only load the first 5 items
    $('.switcher li:lt(5)').each(function(){
      var linkParts = $(this).find('a').attr('href').split('/');
      var link = linkParts[2];
      $('<img id="'+link+'-img" />').load(function(){
        $('#preload').append( $(this) );
      })
      .attr('src','/site/uploads/'+link+'-bg.jpg');
    });
  }

})( jQuery );

