var ImageCache = Class.create({
  initialize: function() {
    window.IMAGE_CACHE = [];
  },
  add: function(img) {
    window.IMAGE_CACHE.push(img);
  },
  addWithIndex: function(idx, img) {
    window.IMAGE_CACHE[idx] = img;
  },
  retrieveWithIndex: function(idx) {
    return window.IMAGE_CACHE[idx];
  },
  cacheSize: function() {
    return window.IMAGE_CACHE.size();
  },
  clearCache: function() {
    window.IMAGE_CACHE.clear();
  }
});
var PhotoRotator = Class.create({
  initialize: function(params) {
    this._container   = $(params.imageContainer) || null;
    this._base        = $(params.baseImage)      || null;
    this._controls    = $(params.controlBox)     || null;
    this._images      = $A(params.imageList)     || [];
    this._links       = $A(params.linkList)      || [];
    this._rotateSpeed = params.speed             || 6;
    this._imgCache    = new ImageCache();
    this._ptr         = 0;
    this._timer       = null;
    if (this._images.size() > 1) {
      this._init();
    }
  },
  previousPhoto: function(evt) {
    this._ptr = (this._ptr == 0) ? this._imgCache.cacheSize() - 1 : this._ptr - 1;
    this._swapOut();
    if (evt) {
      this._setupTimer();
    }
  },
  nextPhoto: function(evt) {
    this._ptr = (this._ptr == this._imgCache.cacheSize() - 1) ? 0 : this._ptr + 1;
    this._swapOut();
    if (evt) {
      this._setupTimer();
    }
  },
  showPointer: function() {
    this.style.cursor = 'pointer';
  },
  hidePointer: function() {
    this.style.cursor = 'default';
  },
  _init: function() {
    this._initImageCache();
    this._setupHTML();
    this._setupTimer();
  },
  _initImageCache: function() {
    for (var i = 0; i < this._images.size(); i++) {
      var imgEl = new Element('img', { src: this._images[i] });
      this._imgCache.add(imgEl);
    }
    this._images = null;
  },
  _setupHTML: function() {
    var prev = (new Element('span')).update('Previous');
    var nxt = (new Element('span')).update('Next');
    prev.addClassName('fltlft padlft');
    nxt.addClassName('fltrt padrt txtrt');
    prev.observe('click', this.previousPhoto.bindAsEventListener(this));
    prev.observe('mouseover', this.showPointer.bind(prev));
    nxt.observe('click', this.nextPhoto.bindAsEventListener(this));
    nxt.observe('mouseover', this.showPointer.bind(nxt));
    this._controls.insert({top:prev, bottom:nxt});
  },
  _setupTimer: function() {
    if (this._timer != null) {
      this._timer.stop();
      this._timer = null;
    }
    this._timer = new PeriodicalExecuter(function() {
      this.nextPhoto();
    }.bind(this), this._rotateSpeed * 2);
  },
  _swapOut: function() {
    this._base.src = this._imgCache.retrieveWithIndex(this._ptr).src;
    this._base.stopObserving('click');
    this._base.stopObserving('mouseover');
    this.hidePointer.bind(this._base).call();
    if (this._links[this._ptr]) {
      this._base.observe('click', this._imgAct.bindAsEventListener(this._links[this._ptr]));
      this._base.observe('mouseover', this.showPointer.bind(this._base));
    }
  },
  _imgAct: function(evt) {
    window.open(this);
    evt.stop();
    return false;
  }
});