// image rotator script
//
// Built by Lowell Hamilton (lowell@broken-bit.com) for Robin Reece Web Design
//
// 

var RR = {};

RR.Rotator = Class.create();
RR.DefaultOptions = {};

Object.extend(RR.Rotator.prototype, {
  initialize: function(options) {
	
	var me = this;  
	this.options = options;
	if (!$(options.destDiv)) {
		alert("Container div not found");
	} else {
	    	this.slideContainer = $(options.destDiv);
	};



	this.slides = options.images;
    	this.currentSlide = 0;
    	this.paused = false;
    	var self = this;
	this.slides = [];

	// Put the slides into the div
	var slideContainer = Builder.node('div', {id: 'RRrotatorContainer', style: 'display: block'});

	var slideControl = Builder.node('div', {id: 'slideshowcontrol_box'});
 
	var backBtn = Builder.node('span', {className: 'slideshowcontrol_btn'}, "<<");
	slideControl.appendChild(backBtn);
	backBtn.observe('click', this.backward.bind(this, idx, idx));

	this.currentimg = 0;
	var idx = 0;
	// Chop off the last array element if it is null
	options.images = options.images.compact();

	options.images.each( function(image) {
		// Add each slide to the container
		if (!image) {
			alert("Rotator image is not complete!" + image.caption + " " + image.url);
		};
		var theimage = Builder.node('img', {id: 'RRrotatorImg' + idx, src: image.src, alt: image.caption});

		theimage.setStyle({width: options.width + 'px', height: options.height + 'px', position: 'absolute'});
		if (idx !== 0) {
			// don't hide the first one
			theimage.setStyle({display: 'none'});
		}

		clicker = function(url) {window.location = url};

		if (image.url) {
			theimage.observe('click', clicker.bind(undefined, image.url));
		};

		slideContainer.appendChild(theimage);

		  jumpToSlide = function(obj, idx) {
		    rotator.stop();
		    rotator.transition(idx);
		  };

		var controlBtn = Builder.node('span', {className: 'slideshowcontrol_btn'}, idx + 1);
		slideControl.appendChild(controlBtn);
		controlBtn.observe('click', this.jumpToSlide.bind(this, idx, idx));
		idx = idx + 1;
	});

	var forwardBtn = Builder.node('span', {className: 'slideshowcontrol_btn'}, ">>");
	slideControl.appendChild(forwardBtn);
	forwardBtn.observe('click', this.forward.bind(this, idx, idx));



	this.slideContainer.appendChild(slideContainer);
	$('slideshowcontrol_box').appendChild(slideControl);


//	if (options.images[this.currentimg].caption) {
//		$('RRrotatorCaption').show().update(options.images[this.currentimg].caption);
//	};


	this.start();
  },
  

  forward: function(speed) {
    	var newSlideIdx = this.currentSlide+1;
    	if (newSlideIdx >= this.options.images.length) {
		newSlideIdx = 0;
	}
    	this.transition(newSlideIdx, speed);
  },
  
  backward: function() {
    var newSlideIdx = this.currentSlide-1;
    if (newSlideIdx < 0) newSlideIdx = this.options.images.length-1;
	
	var currNum = newSlideIdx+1;
    this.transition(newSlideIdx, .001);
  },
  
  
  start: function() {
    if (this.interval) return;
    this.interval = setInterval(this.forward.bind(this, 0), this.options.seconds_per_slide * 1000);
  },
  
  stop: function() {
    if (!this.interval) return;
    clearInterval(this.interval);
    this.interval = null;
  },
  
  pausePlay: function() {
    if (this.interval)  this.stop();
    else                this.start();
    },
	
  rndm : function(min, max){
		return Math.floor(Math.random() * (max - min + 1) + min);
	},			
	
  /* ========== */
  
  transition: function(newSlideIdx, speed) {
    var oldSlideIdx = this.currentSlide;
    if (newSlideIdx == oldSlideIdx) return;

    if (this.fadeIn && this.fadeIn.state != 'finished') this.fadeIn.cancel();
    if (this.fadeOut && this.fadeOut.state != 'finished') {
      this.fadeOut.cancel(); Element.hide(this.fadeOut.element);
    }
	if (speed > 0) {
	    this.fadeOut = new Effect.Fade($('RRrotatorImg' + oldSlideIdx), {duration: speed});
	    this.fadeIn = new Effect.Appear($('RRrotatorImg'+ newSlideIdx), {duration: speed});
	} else {
	    this.fadeOut = new Effect.Fade($('RRrotatorImg' + oldSlideIdx), {duration: this.options.duration});
	    this.fadeIn = new Effect.Appear($('RRrotatorImg'+ newSlideIdx), {duration: this.options.duration});
	}
    this.currentSlide = newSlideIdx;

//	if (this.options.images[this.currentSlide].caption) {
//		$('RRrotatorCaption').show().update(this.options.images[this.currentimg].caption);
//	} else {
//		$('RRrotatorCaption').hide();
//	};;

	}	
});


  

