/*
-----------------------------------------------
vna-vermont.org
Script: vdwSlideshow.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 05 September 2007
----------------------------------------------- */

vdwSlideshow = {
	container : 'slideshow', // id of slideshow container
	delay : 10000, // delay in milliseconds
	speed : 0.5, // speed of transition in seconds
	current : 0, // current slide
	pauseText : 'Pause', // Link text, title text and class (lowercased) for pause button
	playText : 'Play', // Link text, title text and class (lowercased) for play button
	autoplay : true, // Automatically start the slideshow, boolean
	transitioning : false, // Is there a transition currently taking place
	init : function() {
		// Get the slides
		vdwSlideshow.slides = $(vdwSlideshow.container).immediateDescendants();
		// Set URL using hash to enable bookmarking
		if (window.location.hash.indexOf('slide') != -1) {
			vdwSlideshow.current = window.location.hash.substr(window.location.hash.indexOf('slide') + 5) - 1;
		}
		// Prepare the slideshow
		vdwSlideshow.prepare();
		// Create the navigation
		vdwSlideshow.buildNav();
	},
	buildNav : function() {
		// Navigation Title
		var ttlLI = Builder.node('li', { id : 'nav_title' }, [Builder.node('strong', 'Slideshow Navigation')]);
		// Pause and Play link
		var togglePlayLI = Builder.node('li', { id : 'play_pause', className : 'pause' });
		var o = (vdwSlideshow.autoplay) ? vdwSlideshow.pauseText : vdwSlideshow.playText;
		var togglePlayA = Builder.node('a', { href : '#', title : o }, [Builder.node('span', o)]);
		togglePlayLI.appendChild(togglePlayA);
		// Previous Link
		var prevLI = Builder.node('li', { id : 'nav_prev' });
		var prevA = Builder.node('a', { href : '#', title : 'Previous' }, [Builder.node('span', 'Previous')]);
		prevLI.appendChild(prevA);
		// Next Link
		var nextLI = Builder.node('li', { id : 'nav_next' });
		var nextA = Builder.node('a', { href : '#', title : 'Next' }, [Builder.node('span', 'Next')]);
		nextLI.appendChild(nextA);
		// Build container div and ul and append navigation
		var div = Builder.node('div', { id : 'slideshow_navigation' });
		var ul = Builder.node('ul');
		ul.appendChild(ttlLI);
		ul.appendChild(togglePlayLI);
		ul.appendChild(prevLI);
		ul.appendChild(nextLI);
		div.appendChild(ul);
		$('container').appendChild(div);
		// Set up link functions
		Event.observe(prevA, 'click', vdwSlideshow.prev, false);
		Event.observe(nextA, 'click', vdwSlideshow.next, false);
		Event.observe(togglePlayA, 'click', vdwSlideshow.togglePlay, false);
	},
	prepare : function() {
		// Add class for styling
		$(vdwSlideshow.container).addClassName('hasJS');
		// Hide all but the current slide
		for (var i = vdwSlideshow.slides.length - 1; i > -1; i--) {
			vdwSlideshow.slides[i].hide();
		}
		vdwSlideshow.slides[vdwSlideshow.current].show();
		// Start the slideshow
		if (vdwSlideshow.autoplay) {
			vdwSlideshow.timeout = setTimeout("vdwSlideshow.transition('next')", vdwSlideshow.delay);
		}
	},
	transition : function(dir) {
		// Bail out if there is a transition already in progress
		if (vdwSlideshow.transitioning) { return; }
		vdwSlideshow.transitioning = true;
		// Determine the number of the next slide in the queue
		if (dir == 'next') {
			var queue = (vdwSlideshow.current < vdwSlideshow.slides.length - 1) ? (vdwSlideshow.current + 1) : 0;
		} else if (dir = 'prev') {
			var queue = (vdwSlideshow.current > 0) ? (vdwSlideshow.current - 1) : (vdwSlideshow.slides.length - 1);
		}
		// Fade down the current slide
		Effect.Fade(vdwSlideshow.slides[vdwSlideshow.current], { duration: vdwSlideshow.speed, afterFinish : function() {
			// Fade up the next slide
			Effect.Appear(vdwSlideshow.slides[queue], { duration: vdwSlideshow.speed, afterFinish : function() {			
				// If autoplay is on, start the next transition, otherwise clear the queue
				if (vdwSlideshow.autoplay) {
					vdwSlideshow.timeout = setTimeout("vdwSlideshow.transition('next')", vdwSlideshow.delay);
				} else {
					clearTimeout(vdwSlideshow.timeout);
				}
				// Update the current slide, url and end the transition
				vdwSlideshow.current = queue;
				window.location.hash = 'slide' + (vdwSlideshow.current + 1);
				vdwSlideshow.transitioning = false;
			}});
		}});
	},
	prev : function(e) {
		// Stop autoplaying and go to the previous slide
		vdwSlideshow.pause();
		vdwSlideshow.transition('prev');
		Event.stop(e);
	},
	next : function(e) {
		// Stop autoplaying and go to the next slide
		vdwSlideshow.pause();
		vdwSlideshow.transition('next');
		Event.stop(e);
	},
	pause : function() {
		// Toggle the class, link title and link text
		$('play_pause').className = vdwSlideshow.playText.toLowerCase();
		$('play_pause').down().title = vdwSlideshow.playText;
		$('play_pause').down().down().firstChild.nodeValue = vdwSlideshow.playText;
		// Stop autoplaying
		vdwSlideshow.autoplay = false;
		clearTimeout(vdwSlideshow.timeout);
	},
	play : function() {
		// Toggle the class, link title and link text
		$('play_pause').className = vdwSlideshow.pauseText.toLowerCase();
		$('play_pause').down().title = vdwSlideshow.pauseText;
		$('play_pause').down().down().firstChild.nodeValue = vdwSlideshow.pauseText;
		// Start autoplaying
		vdwSlideshow.autoplay = true;
		vdwSlideshow.transition('next');	
	},
	togglePlay : function(e) {
		if (vdwSlideshow.autoplay) {
			vdwSlideshow.pause();
		} else {
			vdwSlideshow.play();
		}
		Event.stop(e);
	}
}

vdwDOM.addEvent(window, 'load', vdwSlideshow.init, false);
