var ImageRotator = {
  // Arguments:
  //   images - A list of the ids of elements to rotate
  //   duration - the length of time an element to should show for
  initialize: function(args) {
  
    this.images = args['images'];
    this.duration = args['duration'] || 5000
  },
  
  start: function() {
    this.show(ImageRotator.images[0]);
    setTimeout("ImageRotator.crossFade(0)", this.duration);
  },

  show: function(image) {
    $(image).fadeIn();
  },

  hide: function(image) {
    $(image).fadeOut();
  },

  crossFade: function(current_index) {

    if(current_index+1 == ImageRotator.images.length) {
      next_index = 0
    }
    else {
      next_index = current_index + 1
    }
var current = ImageRotator.images[current_index];
    var next = ImageRotator.images[next_index];

    this.hide(current);
    this.show(next);
    setTimeout("ImageRotator.crossFade("+next_index+")", this.duration);
  }
}

$(document).ready(function() {
  // note that this code will only work for 1 image rotator currently...
  if($(".rotate").length) {
    var images =  $(".rotate").find("img");
    ImageRotator.initialize({images: images, duration: 5000});
    ImageRotator.start();
  }
});

