Fx.Style.implement({
	fadeOut: function(){
		this.start(1,0);
	},
	fadeIn: function(){
		this.start(0,1);
	}
});

Rotator = new Class({

	options : {
		fade : 2500,
		timer : 5000,
		imgbox : 'slides-home',
		facet : 0,
		delay : 0,
		class : 'rotator_'
	},

	initialize: function(options){
		// this is a Rotator
		var self = this;

		this.idx = 1;
		this.setOptions(this.options, options);

		this.images = this.options.images;
		this.options.topImg = this.options.class+options.facet+'0';

		this.tips = this.options.tips;
		this.targets = this.options.targets;

		// this preloads all images
		new Asset.images(this.images, {
			onComplete : function(){
				// context = browser - must use self
				self.loaded(options.facet);
			}
		});
	},

	loaded : function(facet){
		// invoked by Asset (=this)
		// 'this' is an image
		// called once by each image after it's loaded
		// create image
		var self = this;
		this.botImg = new Element('img').setProperties({
			'class':this.options.class+facet+'bg',
			'src' : this.images[0]
		}).injectInside(this.options.imgbox);
		this.topImg = new Element('img').setProperties({
			'class':this.options.class+facet+' rotator_top',
			'id':this.options.class+this.options.facet+'0',
			'src' : this.images[0],
			'title': this.tips[0]
		}).injectInside(this.options.imgbox);
		this.topImg.addEvent('click', function(){
			// context = browser - must use self
			document.location.href = self.targets[self.idx];
		});
		this.topImg.tips = new Tips([this.options.class+this.options.facet+'0']);

		// create our effect
		this.fx = new Fx.Style(this.options.topImg, 'opacity', {
			duration:this.options.fade
		}).addEvent('onComplete',function(){
			//self.schedule.bind(self); // context = browser - must use self - doesn't fire
			//self.schedule(); // context = browser - must use self - fires, but wrong context
			self.schedule(self); // context = browser - must use self - works!
		});
		this.fade.delay(this.options.delay,this);
	},

	fade : function(){
		this.botImg.setProperty('src',this.images[this.idx]);
		this.fx.fadeOut();
	},

	schedule : function(self){
		self.topImg.src = self.images[self.idx];
		self.topImg.setStyles({'opacity': 1, 'visibility' : 'visible'});
		self.changeImage();
		self.waitID = self.fade.delay(self.options.timer,self);
	},

	changeImage : function(){
		this.idx = ( this.idx == this.images.length-1 ) ? 0 : ++this.idx;
	}
});
Rotator.implement( new Options );
