
// Change the image used on the home page
function changeSplash(num) {
  if (!num) {
    if (activeSplash < splashes) {
      num = activeSplash + 1;
    }
    else {
      num = 1;
    }
  }

  if (num != activeSplash) {
    activeSplash = num;
    $('.toggler-active').removeClass('toggler-active');
    $('#toggler'+activeSplash).addClass('toggler-active');
    $('.splash').fadeOut();
    $('.alt'+activeSplash).fadeIn();
  }
}

function slideSwitch() {
  // Credit to: http://jonraasch.com/blog/a-simple-jquery-slideshow
  var $active = $('#slideshow img.active');

  if ( $active.length == 0 ) $active = $('#slideshow img:last');

  var $next = $active.next().length ? $active.next() : $('#slideshow img:first');

  $active.addClass('last-active');

  $next.css({opacity: 0.0})
      .addClass('active')
      .animate({opacity: 1.0}, 500, function() {
          $active.removeClass('active last-active');
      });
}

$(function() {



  if ($('.splash').length > 1) {
    // We have more than one splashes, so let's set up a timer etc

    activeSplash = 1;
    splashes = $('.splash').size();

    // Dynamically add the splash togglers
    extra = '<ul id="splash-togglers">';
    extra += '<li id="toggler1" class="toggler-active">1</li>';
    for (i=1; i<=splashes; i++) {
      $('.splash:eq('+(i-1)+')').addClass('alt'+i);
      if (i>1) extra += '<li id="toggler'+i+'">'+i+'</li>';
    }
    $(extra).insertAfter('#splash-canvas');

    // Toggle images on the home page when we mouse over the togglers
    $('#splash-togglers li').hover(
      function() {
        $("#splash-togglers").stopTime('splash-toggling');
        changeSplash($(this).attr('id').substr(7,1));
      },
      function() {
      }
    );


    // Auto roate through the images on the home page
    $("#splash-togglers").everyTime(5000, "splash-toggling", function() {
      changeSplash();
    }, 100);

  }


});
