/* Image rotator by Illumi A/S Steen F. Tøttrup */
var Rotator = new Class({
	Implements: Options, options: {
		containerId: '',
		textId: ''
	},
	initialize: function(options) {
		this.setOptions(options);
		this.currentImageIndex = 0;
		if ($(this.options.containerId)) {
			this.container = $(this.options.containerId);
		}
		if ($(this.options.textId)) {
			this.textContainer = $(this.options.textId);
		}

		this.items = this.container.getElements('img');
		//this.items.combine(this.container.getChildren('a').getChildren('img'));
		this.imageCount = this.items.length;
		this.items.each(function(item, index) {
			if (index > 0) {
				//item.fade();
				//item.set('morph', { duration: 'long' });
				item.set('style', 'display: none');
			}
		}, this);
		this.ShowText((this.currentImageIndex + 1) + '/' + this.imageCount);
		if (this.items.length > 0) {
			this.items[0].set('style', 'display: block');
		}
	},
	ShowText: function(text) {
		this.textContainer.set('text', 'Image: ' + text);
	},
	Next: function() {
		if (this.currentImageIndex < this.imageCount - 1) {
			this.items[this.currentImageIndex].set('style', 'display: none');
			this.currentImageIndex++;
			this.items[this.currentImageIndex].set('style', 'display: block');
			this.ShowText((this.currentImageIndex + 1) + '/' + this.imageCount);
		}
		else {
			this.items[this.currentImageIndex].set('style', 'display: none');
			this.currentImageIndex = 0;
			this.items[this.currentImageIndex].set('style', 'display: block');
			this.ShowText((this.currentImageIndex + 1) + '/' + this.imageCount);
		}
	},
	Previous: function() {
		if (this.currentImageIndex > 0) {
			this.items[this.currentImageIndex].set('style', 'display: none');
			this.currentImageIndex--;
			this.items[this.currentImageIndex].set('style', 'display: block');
			this.ShowText((this.currentImageIndex + 1) + '/' + this.imageCount);
		}
		else {
			this.items[this.currentImageIndex].set('style', 'display: none');
			this.currentImageIndex = this.imageCount - 1;
			this.items[this.currentImageIndex].set('style', 'display: block');
			this.ShowText((this.currentImageIndex + 1) + '/' + this.imageCount);
		}
	},
	Count: function() {
		return this.imageCount;
	}

});

window.addEvent('domready', function() {
	if ($('topImageRotator')) {
		var slide = new Rotator({ containerId: 'topImageRotator', textId: 'RotatorText' });
		if (slide.Count() > 0) {
			$('RotatorNext').addEvent('click', function() {slide.Next();return false;}, slide);
			$('RotatorPrev').addEvent('click', function() {slide.Previous();return false;}, slide);
		}
		else {
			$('RotatorNext').set('style', {'display': 'none'});
			$('RotatorPrev').set('style', {'display': 'none'});
			$('RotatorText').set('style', {'display': 'none'});
		}
	}
});
