Effect.Transitions.slowstop = function(pos) {
  return 1-Math.pow(0.5,20*pos);
}
Element.addMethods({
  wrap: function(element, tag_name) {
    element = $(element);
    var wrapper = document.createElement(tag_name);
    element.parentNode.replaceChild(wrapper, element);
    wrapper.appendChild(element);
    return Element.extend(wrapper);
  }
});
var Slideshow = Class.create();
Slideshow.prototype = {
	version: "0.3",
	duration: 2, 
	padding: 10, 
	slideshow_size: { width:0, height:0 }, 
	slideshow_background: "transparent",
	show_nav: true, 
	show_descriptions: true,
	nav_code: "", 
	description_template: "#{description}",
	auto_play: false, 
	timer_delay:0.9, 
	auto_size: false, 
	auto_center: false,
	auto_resize: true, 
	content_is_images: true,
	timer_id: 0,
	is_playing: false,
	image_list: {}, 
	total: 0,
	the_slide: {}, 
	current_image: 0,
	element_id: "",
	proxy_pos: 0,
	proxy_string: "", 
	ul_id: "", 
	auto_size_dimensions: { width:0, height:0 },
	initialize: function(element) {
		this.proxy_pos = Slideshows.push(this); 
		this.proxy_string = "Slideshows.list["+this.proxy_pos+"]"; 
		this.element_id = element; 
		this.setup(element); 
	},
	setup: function(element) {
		$(element).setStyle({ overflow:'hidden'});
		this.ul_id = $$('#'+element+' ul')[0].id; 
	},
	create: function() {		
		this.image_list = this.generate_image_list(this.element_id); 
		this.description_template = new Template(this.description_template);
		this.resize(this.element_id);
		this.set_nav_code(); 
		this.mark_and_update(0); 
		if ( this.auto_play ) { this.start(); }
	},
	next: function() {	
	
		if ( this.current_image == this.image_list.alts.length-1 ){
			this.slide(0); 
		} else {
			this.slide(this.current_image+1); 
		}
	},
	previous: function() {
		if ( this.current_image == 0 ) {
			this.slide(this.image_list.alts.length-1); 
		} else {
			this.slide(this.current_image-1); 
		}
	},
	slide: function(number) {
		if ( this.current_image == number ) { return false; } 
		if ( this.the_slide.currentFrame ) { this.the_slide.cancel(); }
		distance = this.get_distance(number); 
		this.current_image = number; 
		this.the_slide = new Effect.MoveBy(this.ul_id, 0, distance, { duration: this.duration, transition: Effect.Transitions.slowstop }); 
		return this.mark_and_update(number); 
	},
	mark_and_update: function(number) {
		if ( this.show_descriptions && this.show_nav ) {			
			$('slideshow-description-'+this.proxy_pos).update(this.description_template.evaluate({ number:number+1, description:this.image_list.alts[number], total:this.total, width:this.image_list.widths[number], height:this.image_list.heights[number], filename:this.image_list.filenames[number] }));
		}
		return number;
	},
	resize: function(element) {
		if ( this.slideshow_size.height != 0 ) { $(element).setStyle({ height:this.slideshow_size.height }); }
		if ( this.slideshow_size.width != 0 ) { $(element).setStyle({ width:this.slideshow_size.width }); }
		if ( this.slideshow_size.height == 0 && this.auto_resize ) { 
			new_height = $(element).getHeight() - 20 + 'px';
			$(element).setStyle({ height:new_height });
		}
		if ( this.auto_size ) { 
			$(element).setStyle({ width:this.auto_size_dimensions.width+'px', height:this.auto_size_dimensions.height+'px' });
		}
		if ( this.slideshow_background != "transparent" ) {
			$(element).setStyle({ background:this.slideshow_background })
		}
	},
	generate_image_list: function(element) {
		positions_array = [0];
		alts_array = [];
		widths_array = [];
		heights_array = [];
		filenames_array = [];
		current_position = 0;
		padding = this.padding; 
		big_w = 0; 
		big_h = 0; 
		images = $$('#'+element+' img'); 
		images.each(function(e) {
			dims = e.getDimensions();
			width = dims.width;
			height = dims.height;
			filename = e.src.split('/');
			filename = filename[filename.length-1]; 
			current_position += width + padding;
			if ( width > big_w ) { big_w = width; }
			if ( height > big_h ) { big_h = height; }
			positions_array.push(-current_position);
			alts_array.push(e.alt);
			widths_array.push(width);
			heights_array.push(height);
			filenames_array.push(filename);
		}); 
		if ( this.auto_center ) {
			positions_array = [0];
			current_position = 0;
			images.each(function(e) {
				half_height = Math.floor( ( big_h - e.getDimensions().height ) / 2 ) + 'px';
				e.up('li').setStyle({ width:big_w+'px', "text-align":'center', display:'block' });
				e.setStyle({ 'margin-top':half_height, 'margin-bottom':half_height });
				current_position += big_w + padding;
				positions_array.push(-current_position);
			}); 
		}
		this.total = alts_array.length;
		this.auto_size_dimensions.width = big_w;
		this.auto_size_dimensions.height = big_h;
		return new ImageList(positions_array, alts_array, widths_array, heights_array, filenames_array);;
	}, 
	get_distance: function(number) {
		target_position = this.image_list.positions[number];
		current_position = $(this.ul_id).getStyle('left').split('px')[0];
		distance = target_position - current_position;
		return distance;
	},
	start: function() {
		this.timer_id = setInterval(this.proxy_string+".next()", this.timer_delay*1000);
		if ( this.show_nav && this.auto_play ) {
			$('slideshow-play-stop-'+this.proxy_pos).removeClassName('slideshow-play-only');
			$('slideshow-play-stop-'+this.proxy_pos).addClassName('slideshow-stop-only');
		}
		this.is_playing = true;
	},
	stop: function() {
		clearInterval(this.timer_id);
		if ( this.show_nav && this.auto_play ) {
			$('slideshow-play-stop-'+this.proxy_pos).addClassName('slideshow-play-only');
			$('slideshow-play-stop-'+this.proxy_pos).removeClassName('slideshow-stop-only');
		}
		this.is_playing = false;
	},
	toggle: function() {
		if ( this.is_playing ) {
			this.stop();
		} else {
			this.start();
		}
	},
	next_and_stop: function() {
		this.stop();
		this.next();
	},
	previous_and_stop: function() {
		this.stop();
		this.previous();
	},
	set_nav_code: function() {
	if ( this.nav_code == "" && this.show_nav ) {
			this.nav_code += "<div class=\"slideshow-nav\">";
			if ( this.show_descriptions ) {
				this.nav_code += "<div class=\"slideshow-description\" id=\"slideshow-description-"+this.proxy_pos+"\"></div>";
			}
			if ( this.auto_play ) {
				
				this.nav_code += "<div class=\"slideshow-play-stop\" id=\"slideshow-play-stop-"+this.proxy_pos+"\"><a href=\"#\" class=\"slideshow-stop\" onclick=\""+this.proxy_string+".stop(); return false\"><img src='images/pause.gif' border='0'  > </a><a href=\"#\" class=\"slideshow-play\" onclick=\""+this.proxy_string+".start(); return false\"><img src='images/play.gif' border='0'  ></a></div></div>"
			}
			new Insertion.After(this.element_id, this.nav_code); 
		}
	}
}
var ImageList = Class.create();
ImageList.prototype = {
	widths: [],
	heights: [],
	positions: [],
	alts: [],
	filenames: [],
	initialize: function(positions, alts, widths, heights, filenames) {
		this.positions = positions;
		this.alts = alts;
		this.widths = widths;
		this.heights = heights;
		this.filenames = filenames;
	}
}
var Slideshows = {}
Slideshows = {
	list: [],
	push: function(slideshow) {
		current_pos = Slideshows.list.length
		Slideshows.list.push(slideshow);
		return current_pos;
	}
}
