﻿document.observe('dom:loaded', function(ev){

	// only do anything if there are several slides
	slides = $$('div.slides > div.slide');
	numSlides = slides.length;
	if(numSlides > 1) {		
		
		// show all the next/prev links
		// add the prev/next actions
		$$('a.previous', 'a.next').each(function(el){
			
			el.setStyle({
				display: 'inline'
			});
			
			el.observe('click', function(ev){
				ev.stop();
				el = ev.element(ev);
				if(el.tagName != 'A') {
					el = el.up('A');
				}
				
				currentSlide = el.up('.slide');
				
				if(el.hasClassName('previous')) {					
					previousSiblings = currentSlide.previousSiblings();					
					// if there are no previous slides, the next slide is the last slide in the set
					if(previousSiblings.length == 0) {					
						nextSlide = slides[numSlides - 1];						
					} else {
						nextSlide = el.up('.slide').previousSiblings()[0];					
					}					
				} else {				
					nextSiblings = currentSlide.nextSiblings();
					// if there are no next slides, the next slide is the first slide in the set
					if(nextSiblings.length == 0) {
						nextSlide = slides[0];
					} else {
						nextSlide = currentSlide.nextSiblings()[0];					
					}					
				}

				if(Prototype.Browser.IE) {
					currentSlide.hide();
					nextSlide.show();
				} else {
					new Effect.Fade(currentSlide, {duration: 0.5, queue: 'front'});
					new Effect.Appear(nextSlide, {duration: 0.5, queue: 'end'});
				}
				
			});
		});		
	}
});
