window.addEvent('domready', function() {
  var showDuration = 4000;
  var container = $('c-slides');
  
  if (container) {
	  var images = container.getElements('img');
	  var currentIndex = 0;
	  var interval;
	  var toc = [];
	  var li;
	  var tocActive = 'toc-active';
	  
	  var start = function() { interval = show.periodical(showDuration); };
	  var stop = function() { $clear(interval); };
	  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);
	  };
	  
	  images.each(function(img,i){
		li = new Element('li').inject($('pagination'));
		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);
			}
		  }
		}).inject(li));
		if(i > 0) { img.set('opacity',0); }
	  });
		
	  container.addEvents({
		mouseenter: function() { stop(); },
		mouseleave: function() { start(); }
	  });
	  
	  window.addEvent('load',function(){
		start();
	  });
  }
});
