window.addEvent('domready',function() {
	/* settings */
	var showDuration = 4000;
	var container = $('slideshow-container');
	var images = container.getElements('img');
	var currentIndex = 0;
	var interval;
	var visibileAan;
	var toc = [];
	var tocWidth = 20;
	var tocActive = 'toc-active';
	
	/* new: starts the show */
	var start = function() { interval = show.periodical(showDuration); };
	var stop = function() { $clear(interval); };
	/* worker */
	var show = function(to) {
		images[currentIndex].fade('out');
		toc[currentIndex].removeClass(tocActive);
		images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].fade('in');
		toc[currentIndex].addClass(tocActive);
	};
	
	/* new: control: table of contents */
	images.each(function(img,i){
			if(images.length == 1){
				visibileAan= "hidden";	
			}else{
				visibileAan= "visible";	
			}
			toc.push(new Element('a',{
				text: i+1,
				href: '#',
				'class': 'toc' + (i == 0 ? ' ' + tocActive : ''),
				events: {
					click: function(e) {
						if(e) e.stop();
						stop();
						show(i);
					}
				},
				styles: {
					left: 260+((i + 1) * (tocWidth + 10)),
					top: 435,
					height: 20,
					visibility: visibileAan
				}
			}).inject(container));
		
			if(i > 0) { img.set('opacity',0); }	
	});
	
	/* new: control: start/stop on mouseover/mouseout */
	container.addEvents({
		mouseenter: function() { stop(); },
		mouseleave: function() { start(); }
	});
	
	/* start once the page is finished loading */
	window.addEvent('load',function(){
			start();
	});
});

