var slides, photos, auto, slidepointer = 0, slidecount;
var Slideshow = {
	auto: false,
	start: function(){
		Slideshow.parseSlides();
		if (Slideshow.auto) Slideshow.automate();
	},
	parseSlides: function(){
		slides = $$('#slideshow li');
		photos = $$('#slideshow li div.wrapper div.photo');
		slidecount = slides.length - 1;
		var squeeze_to = 59;
		var max_width = 347;
		var start_widths = new Array();
		var fx = new Fx.Elements(slides, {wait: false, duration: 400, transition:Fx.Transitions.Cubic.easeOut});
		slides.each(function(slide, i){
			start_widths[i] = slide.getStyle('width').toInt();
			slide.addEvent('mouseenter', function(e){
				slide.addClass('hovered');
				var photo = photos[i];
				var obj = {};
				obj[i] = {
					'width' : [slide.getStyle('width').toInt(), max_width],
					'opacity' : [1]
				};
				slides.each(function(other, j){
					if (other != slide){
						other.removeClass('hovered');
						var w = other.getStyle('width').toInt();
						if (w != squeeze_to) obj[j] = {'width': [w,squeeze_to], 'opacity': [.4] };
					}
				});
				fx.start(obj);
			});
		});
		$('slideshow').addEvent('mouseenter', function(e){
			$clear(auto);
		});
		$('slideshow').addEvent('mouseleave', function(e){
			var obj = {};
			slides.each(function(other, j){
				other.removeClass('hovered');
				obj[j] = {'width': [other.getStyle('width').toInt(), start_widths[j]], 'opacity' : [1] };
			});
			fx.start(obj);
			if (Slideshow.auto) Slideshow.automate();
		});
	},
	automate: function()
	{
		auto = setInterval(function(){
			slides[slidepointer].fireEvent('mouseenter');
			slidepointer = (slidepointer == slidecount) ? slidepointer = 0 : slidepointer + 1;
		}, 5000);
	}
};
window.addEvent('domready', Slideshow.start);