/*
 * This is a javascript file used for putting together a simple image rotator.
 */

$(document).ready(function() {
  function get(i) { return $('#header-images .image:nth-child(' + i  + ')'); }
  function incr(i) {
    var max = $('#header-images .image').length;
    return i + 1 > max ? 1 : i + 1;
  }

  var headerImages = $('#header-images');
  var sleep = $(headerImages).attr('slideshow-timing') * 1000;
  var i = Math.floor(Math.random() * $('#header-images .image').length) + 1;
  setInterval(function() {
    var current = get(i);
    var next = get(incr(i));
    i = incr(i);
    current.fadeOut(400, function() {
      next.fadeIn();
    });
  }, sleep);

  get(i).fadeIn();
});
