var ImageRotator = new Class({
	Implements	: [Events,Options],
	options		: {
		'rollingTimeout' : 5000, // msec per il rolling delle immagini
		'rollingObject' : []
	},
	
	initialize	: function(opzioni)	{
		this.setOptions(opzioni);
		if((this.options['rolloverContainer']) && (this.options['rollingObject']) && (this.options['rollingObject'].length >= 3)) {
			this.rolloverContainer = $(this.options['rolloverContainer']);

			this.largeThumb = $('grande_a').setStyle("display", "block");
			this.largeThumb.img = this.largeThumb.getElement('img');
			this.largeThumb.activate = ImageRotator.activate.bind(this.largeThumb);
			
			
			this.smallThumb1 = $('piccola1').setStyle("display", "block");
			this.smallThumb1.img = this.smallThumb1.getElement('img');
			this.smallThumb1.activate = ImageRotator.activate.bind(this.smallThumb1);

			this.smallThumb2 = $('piccola2').setStyle("display", "block");
			this.smallThumb2.img = this.smallThumb2.getElement('img');
			this.smallThumb2.activate = ImageRotator.activate.bind(this.smallThumb2);

			this.currentItem = 1;

			this.start();			
		}
	}
});

ImageRotator.implement({
	"getNextRollingObject" : function(step) {
		var retVal = 0;

		if((this.currentItem + step) < this.options['rollingObject'].length) {
			retVal = this.currentItem + step;

		} else {
			retVal = step - 1;
		}

		return retVal;
	},
	
	"rollImages" : function () {
		var currObj = this.options['rollingObject'][this.currentItem];
		var nextObj1 = this.options['rollingObject'][this.getNextRollingObject(1)];
		var nextObj2 = this.options['rollingObject'][this.getNextRollingObject(2)];
		
		this.largeThumb.activate(currObj['href'], currObj['fnamel']);
		this.smallThumb1.activate(nextObj1['href'], nextObj1['fnamel']);;
		this.smallThumb2.activate(nextObj2['href'], nextObj2['fnamel']);;

		this.largeThumb.fade('in');
		this.smallThumb1.fade('in');
		this.smallThumb2.fade('in');

		this.currentItem++;

		if(this.currentItem >= this.options['rollingObject'].length) {
			this.currentItem = 0;
		}
	},

	"start" : function () {
		this.rollID = this.rollImages.periodical(this.options['rollingTimeout'], this);
		return this
	},

	"stop" : function () {
		$clear(this.rollID);
		return this
	}
});

ImageRotator.activate = function (href, imgSrc) {
	this.setStyle('opacity', '0.0');
	this.setAttribute('href', href);
	this.img.src = imgSrc;
};

