/*
Script: SimpleSlides.js
	Simple slide show that pulls creates labels for each slide dynamically and injecting them into a navigation element.  It can loop through each slide, or go directly to one from any other.

	License:
		MIT-style license.

	Author:
		Ryan Florence <rpflorence@gmail.com>

*/

var SimpleSlides = new Class({

	Implements: [Options, Events, Loop], 

		options: {
			/*
			onShow: $empty
			onLabelEvent: $empty,
			*/
			labelStorageAttribute: 'alt',
			slideSelector: 'img',
			attributePrefix: 'simple_slides_',
			labelEvent: 'click',
			delay: 4000
		},

	initialize: function(container,navigation,options){
		this.setOptions(options);
		this.setLoop(this.loop, this.options.delay);
		this.container = document.id(container);
		this.navigation = document.id(navigation);
		this.labels = [];
		this.slides = this.container.getElements(this.options.slideSelector);
		this.currentIndex = 0;
		this.build();
	},

	build: function(){

		if(this.container.getStyle('position') != 'absolute') this.container.setStyle('position','relative');

		this.slides.each(function(slide,index){
	
			slide.setStyles({
				'position': 'absolute',
				'top':0,
				'left':0
			});
	
			this.labels[index] = new Element('div',{
				'id': this.options.attributePrefix + 'label_'+index,
				'class': this.options.attributePrefix + 'label',
				'html': slide.get(this.options.labelStorageAttribute)
			})
			.addEvent(this.options.labelEvent,this.labelEvent.bind(this, index))
			.inject(this.navigation);
	
			if(index != 0) slide.fade('hide');
			else this.labels[index].addClass(this.options.attributePrefix + 'label_current');
	
		}.bind(this));

		return false;
	},

	show: function(index){

		this.fireEvent('show', [this.currentIndex, index]);
		this.slides[this.currentIndex].fade('out');
		this.labels[this.currentIndex].removeClass(this.options.attributePrefix + 'label_current');

		this.currentIndex = index;
		this.slides[this.currentIndex].fade('in');
		this.labels[this.currentIndex].addClass(this.options.attributePrefix + 'label_current');

		return this;
	},

	loop: function(){
		var nextIndex = (this.currentIndex == this.slides.length-1) ? 0 : this.currentIndex + 1;
		this.show(nextIndex);
		return this;
	},

	labelEvent: function(index){
		this.fireEvent('labelEvent', index);
		if(this.isLooping) this.stopLoop();
		this.show(index);
		return this;
	}


});
