// JavaScript Document
// Author: Andy Asberry, Ciphertek Systems, LLC
// Date: June 2010



var CTS_PHOTO_ROTATOR = {

  selector: '.cts_photo_rotator',
  interval_id: null,
  delay: 5,
  current: 0,
  
  init: function(selector, interval) {
    var this_self = CTS_PHOTO_ROTATOR;
    this_self.selector = selector;
    this_self.num_pics = $(selector + ' img').length;
    this_self.delay = interval;
    this_self.getFirstIndex();
    this_self.startRotation();
  },
  
  startRotation: function() {
    var this_self = CTS_PHOTO_ROTATOR;
    if (this_self.num_pics > 1) {
      this_self.interval_id = setInterval(this_self.rotateImage, this_self.delay*1000);
    }
  },
  
  rotateImage: function() {
    var this_self = CTS_PHOTO_ROTATOR;
    $(this_self.selector + ' img').eq(this_self.current).css('display', 'none');
    this_self.current++;
    this_self.current = this_self.current % this_self.num_pics;
    $(this_self.selector + ' img').eq(this_self.current).css('display', 'block');
  },
  
  getFirstIndex: function() {
    var this_self = CTS_PHOTO_ROTATOR;
    var first = Math.floor(Math.random()*(this_self.num_pics));
    $(this_self.selector + ' img').css('display', 'none');
    $(this_self.selector + ' img').eq(first).css('display', 'block');
    this_self.current = first;
  },

  stopRotation: function() {
    var self = CTS_PHOTO_ROTATOR;
    clearInterval(this_self.interval_id);
  }

};