/***************************************************************
*  Gallery Slide Show
*  jpl 11/10/09
*
***************************************************************/

var GallerySlideShow = new Class({

	Implements: [Options, Events],
	
	options: {
		singleDuration:				7000,
		slideShowFadeDuration:		1000,
		jumpFadeDuration:			250,
		singleImageSelector:		'img',
		thumbnailMenuContainer:		null,
		menuItemSelector:			'img',
		currentMenuItemClass:		'current',
		startAutomatically:			true
	},
	
	initialize: function(imageSetContainer, options) {
		
		this.setOptions(options);
		
		this.imageSetContainer = $(imageSetContainer);
		if(!this.imageSetContainer) {
			throw('Non-existent imageSetContainer passed to GallerySlideShow constructor: ' + imageSetContainer);
			return;
		}
		this.imageSet = this.imageSetContainer.getElements(this.options.singleImageSelector);
		this.imageSet.each(function(theImage, index) {
			theImage.setStyle('display', (index==0 ? 'block' : 'none'));
		}, this);
		
		this.imageCount =this.imageSet.length;
		this.currentImage = 0;
		
		this.prepThumbnailMenuContainer();
		
		this.slideshowAction = null;
		if(this.options.startAutomatically)
			this.start();
	},
	
	prepThumbnailMenuContainer: function() {
		var thumbnailMenuContainer = $(this.options.thumbnailMenuContainer);
		if(!thumbnailMenuContainer)
			return;
		
		this.thumbnailSet = thumbnailMenuContainer.getElements(this.options.menuItemSelector  ||  'img');
		
		this.thumbnailSet.each(function(theThumbnail, index) {
			theThumbnail.addEvent('click', this.menuJumpToImage.pass([index], this));
		}, this);
		this.menuJumpToImage(0);
	},
	
	menuJumpToImage: function(index) {
		if(this.thumbnailSet && this.thumbnailSet[index]) {
			this.thumbnailSet[this.currentImage].removeClass(this.options.currentMenuItemClass);
			this.thumbnailSet[index].addClass(this.options.currentMenuItemClass);
			this.jumpToImage(index, true);
		}
	},
	
	jumpToImage: function(index, stopSlideShow, duration) {
		if(!$(this.imageSet[index])) {
			this.stop();
			return;
		}
		
		this.swapSections(this.imageSet[this.currentImage], this.imageSet[index], 
						  duration  ||  this.options.jumpFadeDuration);
		this.currentImage = index;
	},
	
	advanceSlideShow: function() {
		this.jumpToImage((this.currentImage+1) % this.imageCount, false, this.options.slideShowFadeDuration);
	},
	
	start: function() {
		this.slideshowAction = this.advanceSlideShow.periodical(this.options.singleDuration, this);
	},
	
	stop: function() {
		clearInterval(this.slideshowAction);
	},
	
	
	swapSections: function(oldSection, newSection, duration) {
		
		oldSection = $(oldSection);
		newSection = $(newSection);
		
		if(!duration)
			duration = 200;
		
		if(newSection.style.display == 'none') { // only swap if not already swapped!
			var oldSectionFx = oldSection.get('tween', { property : 'opacity', duration : duration });
			var newSectionFx = newSection.get('tween', { property : 'opacity', duration : duration });
			oldSectionFx.start(1,0).chain(function() {
				newSection.style.visibility = 'hidden';
				oldSection.style.display = 'none';
				newSection.style.display = '';
				newSectionFx.start(0, 1);
			});
		}
	}
	
	
});