/* 	slideshow script 
	written by Mark Whitcher of AnIdea, LLC
	see us at www.anideaweb.com
	copyright ©2007 by Mark Whitcher
*/

// // determines how long each image will appear
// var length_of_time_each_image_will_appear = 8.0;
// 
// // when set to true mooslide will randomly choose which image to start with
// // set to false if you always want to start with the first image
// var choose_random_starting_image = true;
// 
// // when set to true, mooslide will stagger the tansitions of multiple slide shows
// // set to false if you want them all to transition together
// var stagger_the_slide_shows = true; 
// 
// 
// 
// /* DO NOT CHANGE ANYTHING BELOW THIS POINT */
// var settings = new Object;
// settings.show_length = length_of_time_each_image_will_appear * 1000;
// settings.randomize = choose_random_starting_image;
// settings.stagger = stagger_the_slide_shows;
// var slideshows;
// 
var settings = new Object;

function startShow(options) {
	
	options = typeof(options) == 'undefined' ? {} : options
	
	settings.show_length = (options["length"] || 4) * 1000
	settings.randomize = options["random"]
	settings.stagger = options["stagger"]
	settings.transition_duration = (options['transition'] || 1) * 1000

	/* get the slideshows */
	slideshows = $$('.slideshow');
	
	if (settings.stagger) {
		// add functions to the slideshow so we can transition through them
		settings.show_length = settings.show_length / slideshows.length
		slideshows.current_position = 0;
		slideshows.current_slideshow = function() {
			return this[this.current_position];
		}
		slideshows.set_next = function() {
			var next_position = this.current_position + 1;
			if (next_position >= this.length) {
				next_position = 0;
			}
			this.current_position = next_position;
			return this.current_position;
		}
	}
	
	slideshows.each(function(item,index) {
		
		// tell the show what it's index is
		item.showIndex = index;
		
		// set the length of the show based on the presence of a <var> tag
		if (item.getElement('var')) {
			var variable = item.getElement('var')
			item.showLength = variable.innerHTML * 1000
			variable.destroy()
		} else {
			item.showLength = settings.show_length * 1000
		}
						
		// find out if the slideshow is supposed to do a slow fade or not
		item.slowfade = item.hasClass('slowfade');
		
		// find out if the slideshow is to play through only once
		item.onlyonce = item.hasClass("once");
		
		// set up the fade function to call
		item.fadeNow = function() {
			if (this.slowfade) {
				return transitionSlowFade(this);
			} else {
				return transitionNormalFade(this);
			}
		}
		
		// set the images attribute to the children of the slideshow
		item.images = item.getChildren();
		
		// set the play counter to the number of images
		item.counter = item.images.length;
		
		// sets the starting position for the slideshow
		if(settings.randomize) {
			item.position = Math.floor(Math.random() * item.images.length)
		} else {
			item.position = 0;
		}
		
		// hides the images that are not the first one
		item.images.each(function(image,index) {
			image.setStyle('position','absolute')
			image.setStyle('z-index',1)
			if(index != item.position) {image.fade('hide');}
		})
		
		
		item.next_position = function() {
			var position = this.position + 1;
			if (position >= this.images.length) {
				position = 0;
			}
			return position;
		}
		item.current_image = function() {return this.images[this.position]}
		item.next_image = function() {return this.images[this.next_position()]}
		item.set_next = function() {this.position = this.next_position();}
	})
	
	setTimeout('transition()',(settings.show_length));  /* then we schedule the next transition*/
}

function transition() {
	var duration;
	if (settings.stagger) {
		// transition the current slide show and set the next one
		slideshow = slideshows.current_slideshow();
		duration = slideshow.fadeNow();
		slideshows.set_next();
	} else {
		slideshows.each(function(slideshow) {
			if (!(slideshow.onlyonce && slideshow.counter <= 1)) {
				slideshow.fadeNow();
				slideshow.counter -= 1;
			}
		})
		duration = settings.transition_duration;
	}
	alert
	setTimeout('transition()',(settings.show_length + duration))
}

function transitionNormalFade(slideshow) {
	var current_image = slideshow.current_image();
	var next_image = slideshow.next_image();
	current_image.setStyle('z-index',1);
	next_image.setStyle('z-index',2);
		
	new Fx.Tween(next_image,{property: 'opacity', duration: settings.transition_duration}).start(1).chain(function(){current_image.setStyle('opacity',0)})
	
	slideshow.set_next()
	return settings.transition_duration;
}

function transitionSlowFade(slideshow) {
	var current_image = slideshow.current_image();
	var next_image = slideshow.next_image();
	current_image.setStyle('z-index',1);
	next_image.setStyle('z-index',2);
	
	var tween_out = new Fx.Tween(current_image,{property: 'opacity',duration:settings.transition_duration})
	var tween_in = new Fx.Tween(next_image,{property: 'opacity',duration:settings.transition_duration})
	
	tween_out.start(0).chain(function(){tween_in.start(1)})
		
	slideshow.set_next()
	
	return (settings.transition_duration * 2)
}



