/*
The Rotator class rotates thru all elements (with an applied class of "rotate") in a fading* slideshow with optional links.

Note the following options.  The only required option is "collection":
	collection: the selector path to your .rotate elements (i.e. '#container div.rotate')
	linksCollection: the selector path to your .rotate link elements (i.e. a.rotate). default is no links.
	autoPlay: self explanatory.  default is true.
	duration: duration each element is shown.  default is 4000ms.
	fadeDuration: duration of the fade transitions.  default is 400ms.
	activeLink: name of class for links. default is 'active'.

(*remember you can't fade PNG's in IE6!!)
*/

var Rotator = new Class({
	Implements: [Options],
	options: {
		duration:4000,
		fadeDuration:400,
		autoPlay:true,
		activeLink: 'active'
	},
	initialize: function(options){
		this.setOptions(options);
		this.collection = $$(this.options.collection);
		this.linksCollection = $$(this.options.linksCollection);
		this.fades = this.options.fadeDuration;
		//the following loop is required to aaddress a transistion bug
		this.collection.each(function(item){
			item.fade('out');
		});
		//if there are links, set their events to fire the singleShot() method
		//which stops the slideshow and shows the corresponding element
		if(this.linksCollection){
			this.linksCollection[0].addClass(this.options.activeLink);
			this.linksCollection.each(function(litem,x){				
				litem.addEvent('click',function(e){
					e.preventDefault();
					this.clearSlideshow();
					this.singleShot(x);
					this.linkClass(x)
				}.bind(this));
			},this);
		}
		this.collection[0].show();
		this.collection[0].fade('in');
		if(this.options.autoPlay == 'true'){
			this.play();
		}
	},
	play: function(){
		//runs the slideshow() method every n number of milliseconds
		this.next = 0;
		this.prev;
		this.periodical = this.slideshow.bind(this).periodical(this.options.duration); 
	},
	hideAll: function(){
		//fades all active elements
		this.collection.each(function(item){
			item.fade('out');
			(function(){item.hide()}).delay(this.fades);
		},this);
	},
	singleShot: function(x){
		//shows one element -- for use with optional navigation.  
		//accepts array key integer corresponding to clicked element to be shown		
		this.hideAll();
		(function(){this.collection[x].show()}.bind(this)).delay(this.fades);
		(function(){this.collection[x].fade('in')}.bind(this)).delay(this.fades);
	},
	slideshow: function(){
		//slideshow method, to be called ONLY by the periodical in the play() method
		if(this.next == this.collection.length-1){
			this.next = 0
		}
		else{
			this.next = this.next+1
		};
		if(this.next == 0){
			this.prev = this.collection.length-1;
		}
		else{
			this.prev = this.next-1;
		}
		this.singleShot(this.next);
		this.linkClass(this.next);
	},
	linkClass: function(x){		
		this.linksCollection.removeClass(this.options.activeLink);
		var aLnk = this.linksCollection[x];
		aLnk.addClass(this.options.activeLink)		
	},
	clearSlideshow: function(){
		$clear(this.periodical);
	}
});
