
var SlideShow = Class.create({	
	
	initialize: function(container, options){

		// Ensure container exists
		if (!$(container)) { return false; }

		// Override default options
		this.options = Object.extend({
			delay: 5000
		}, options || {});
		this.container = container;
		
		// Get array of slides
		this.slides = $$('#'+this.container+' li');	
		
		// Stop this slideshow if there is only one slide
		if (this.slides.length<2) return false;
		
		// Set start and end frame
		this.startSlide = 0;
		this.endSlide = this.slides.length-1;
		
		// Hide extra slides
		this.hideSlides();
		
		// Start the slide show
		setTimeout(this.fadeInOut.bind(this,this.startSlide),this.options.delay);		
	},
	
	// Hide all but the first slide
	hideSlides: function() {
		var firstSlide = true;
		this.slides.each( function(slide) {
			if (!firstSlide) { slide.setStyle({ display:'none' }); }
			firstSlide = false;
		});		
	},
	
	// Fade from one frame to the next
	fadeInOut: function(slide) {
		Effect.Fade(this.slides[slide]);
		if (slide == this.endSlide) { slide = this.startSlide; } else { slide++; }
		Effect.Appear(this.slides[slide]);
		setTimeout(this.fadeInOut.bind(this,slide), this.options.delay + 1850);
	}		
});




// Global DOM onload
document.observe("dom:loaded", function() {
	new SlideShow('slideshow',{ delay:10000 });	// Start slide on homepage
	
	$$('a.popup').each(function(a){
		a.observe('click',function(event){
			window.open(a.href,'popup','width=400,height=495,scrollbars=1,resizable=1');
			event.stop()
		}.bind(this));
	});
	
});
