var Tabify = {
	currentTab: 1,
	tabCount: 1,
	slideshowTimer: 9000,
	slideshow: null,

	init: function() {
		// hide all tabs that aren't the first
		var featureList = $('featureList');
		if(featureList)
		{
			// set the initial tab count
			this.tabCount = featureList.immediateDescendants().length;
			// set a good class name
			featureList.addClassName('scripted');
			featureList.immediateDescendants().each(function(s) {
				if(!s.hasClassName('1'))
				{
					s.hide();
				} else {
					Tabify.showImage(null, 1);
				}
			});

			$('rightList').immediateDescendants().each(function(s, i) {
				var href = s.down('a');
				var i = i+1;				
				Event.observe(href, 'mouseover', Tabify.showImage.bindAsEventListener(Tabify, i));
			});

			Tabify.slideshow = setInterval("Tabify.nextImage()", Tabify.slideshowTimer);
		}
	},

	previousImage: function(event) {
		if(event != null)
		{
			Event.stop(event);
			clearInterval(this.slideshow);
			this.slideshow = setInterval("Tabify.nextImage()", this.slideshowTimer);
		}

		var imgToShow = (this.currentTab == 1) ? this.tabCount : this.currentTab - 1;
		this.showImage(null, imgToShow);
	},

	nextImage: function(event) {
		if(event != null)
		{
			Event.stop(event);
			clearInterval(this.slideshow);
			this.slideshow = setInterval("Tabify.nextImage()", this.slideshowTimer);
		}

		var imgToShow = (this.currentTab == this.tabCount) ? 1 : this.currentTab + 1;
		this.showImage(null, imgToShow);
	},

	showImage: function(event, imageNumber) {
	
		if(event != null)
		{
			Event.stop(event);
			clearInterval(this.slideshow);
			this.slideshow = setInterval("Tabify.nextImage()", this.slideshowTimer);
		}

		if((imageNumber >= 1) && (imageNumber <= this.tabCount))
		{
			var featureList = $('featureList');

			var toHide = null; var toShow = null;
			featureList.immediateDescendants().each(function(s, i) {
				if(s.hasClassName(Tabify.currentTab))
				{
					toHide = s;
				}
				else if(s.hasClassName(imageNumber))
				{
					toShow = s;
				}
			});

			if((toHide != null) && (toShow != null))
			{
				Effect.Fade(toHide, {
					duration: 0.8,
					beforeStart: function() { Effect.Appear(toShow, { duration: 0.8 }); }
				});
			}

			this.currentTab = imageNumber;

			$('rightList').immediateDescendants().each(function(s) {
				if(s.hasClassName(imageNumber))
				{
					s.id = 'on';
				}
				else
				{
					s.id = '';
				}

				if(s.hasClassName('imageOfTotal'))
				{
					s.innerHTML = imageNumber + " of " + Tabify.tabCount;
				}
			});
		}
	}
};
