
/**
 * Extends the clientcide SimpleCarousel to animate content
 */



var SimpleCarouselWithHtmlContentOverlay = new Class({
	Extends: SimpleCarousel,
	options: {
		contentContainer: ''
	},
	initialize: function(container, slides, buttons, contentContainer, nextPreviousButtons, options){
		this.setOptions(options);
		this.contentContainer = $$(contentContainer);
		this.slides = $$(slides);
		this.nextPreviousButtons = $$(nextPreviousButtons);
		slides.each(function(slide){
		   slide.setStyle('opacity',0);
		   slide.setStyle('visibility','hidden');
		});
		if ($chk(buttons)) {
			this.buttons = $$(buttons);
		} else {
			this.buttons = null;
		}
		
		this.parent(container, slides, buttons, options);
	},

	showSlide: function(slideIndex){
		var action = {};
		if (this.buttons.length != 0) {
				this.slides.each(function(slide, index) {
				if (index == slideIndex && index != this.currentSlide){ //show
					$(this.buttons[index]).swapClass(this.options.buttonOffClass, this.options.buttonOnClass);
					action[index.toString()] = {
						opacity: 1
					};
					this.contentContainer[index].swapClass('hidden', '');
				} else {
					$(this.buttons[index]).swapClass(this.options.buttonOnClass, this.options.buttonOffClass);
					
					this.contentContainer[index].swapClass('', 'hidden');
				}
			}, this);
		} else {
			this.slides.each(function(slide, index) {
				if (index == slideIndex && index != this.currentSlide){ //show
					action[index.toString()] = {
						opacity: 1
					};
					this.contentContainer[index].swapClass('hidden', '');
				} else {
					action[index.toString()] = {
						opacity:0
					};
					this.contentContainer[index].swapClass('', 'hidden');
				}
			}, this);
		}
		
		this.fireEvent('onShowSlide', slideIndex);
		this.currentSlide = slideIndex.toInt();
		this.slideFx.start(action);
		return this;
	},
	setupAction: function(action) {
		if ($chk(this.buttons)) {
				this.buttons.each(function(el, idx){
				$(el).addEvent(action, function(ev) {
					/**
					 * custom by Luke Hoggett <luke@fatpublisher.com.au>
					 * ensures that links aren't triggered
					 */
					ev.stop();
					this.slideFx.setOptions(this.slideFx.options, {duration: this.options.rotateActionDuration});
					if (this.currentSlide != idx) this.showSlide(idx);
					this.stop();
				}.bind(this));
			}, this);
		}

		this.nextPreviousButtons.each(function(el) {
			if (el.get('class').contains('next')) {
				$(el).addEvent(action, this.onNext.bind(this));
			}
			if (el.get('class').contains('previous')) {
				$(el).addEvent(action, this.onPrevious.bind(this));
			}
		}, this);
	},
	onNext: function(ev) {
		ev.stop();
		this.slideFx.setOptions(this.slideFx.options, {duration: this.options.rotateActionDuration});

		var showNextIdx = this.currentSlide + 1;
		if (showNextIdx >= this.slides.length) {
			showNextIdx = 0;
		}
		this.showSlide(showNextIdx);
		this.stop();
	},
	onPrevious: function(ev) {
		ev.stop();
		this.slideFx.setOptions(this.slideFx.options, {duration: this.options.rotateActionDuration});
		var showPrevious = this.currentSlide - 1;
		if (showPrevious < 0) {
			showPrevious = this.slides.length - 1;
		}
		this.showSlide(showPrevious);
		this.stop();
	},
	autoplay: function(){
		this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);
		this.fireEvent('onAutoPlay');
		return this;
	},
	rotate: function(){
		var current = this.currentSlide;
		var next = (current+1 >= this.slides.length) ? 0 : current+1;
		this.showSlide(next);
		this.fireEvent('onRotate', next);
		return this;
	}
});