// Reusable rotator. 
// Used for home page banner images and news ticker.
var Rotator = new Class({
	initialize: function(container, tagname, options) {
		this.options = Object.extend({
			interval: 10000,
			duration: 1000,
			crossfade: false,
			reverse: false,
			fadeup: false
		}, options || {});
	
		this.list = [];	
		$each($(container).getElementsByTagName(tagname), function(el){
			$(el).setStyles({display:'none', opacity:0});
			this.list.push(el);
		}, this);
		if (this.options.reverse) this.list.reverse();			
		if (!this.options.fadeup) this.list[0].setStyles({display:'block', opacity:1});				
		
		this.total = this.list.length;
		if(this.total == 0) return false;
		
		this.pos = 0;
		this.prev = null;
		
		this.update();
		this.update.periodical(this.options.interval, this);
    },
	fade: function() {
		var cduration = (this.options.crossfade) ? this.options.duration*2 : this.options.duration/2;
		new Fx.Style(this.list[this.prev],'opacity',{onComplete:function(el){el.setStyle('display','none');}, duration: cduration}).start(1,0);
	},
	show: function() {
		new Fx.Style(this.list[this.pos],'opacity',{onStart:function(el){el.setStyle('display','block');}, duration: this.options.duration}).start(0,1);	
	},
    update: function() {				
		if (this.prev == null) {
			if (this.options.fadeup == true) this.show();
		} else {
			this.show();
		}	
		if (this.options.crossfade == true) {
			this.fade();	
		} else {
			cduration = this.options.duration/2;
			this.fade.delay(this.options.interval-cduration, this);
		}			
		if(this.pos < this.total-1) {
			this.prev = this.pos;
			this.pos = this.pos+1;
		} else {
			this.prev = this.total-1;
			this.pos = 0;
		}		
    }
});

window.addEvent('domready', (function() {new Rotator('pic', 'img', {interval: 6000, duration: 500, crossfade: true, reverse: true});}));
