window.addEvent('domready',function() {


	//var slides = new Array('adventure_1.jpg', 'adventure_2.jpg', 'adventure_3.jpg');
	//slides.sort(function() {return 0.5 - Math.random()})
	//new Slideshow('slideshowContainer', slides, { controller: false, hu: '/zukes/images/social/contests/adventure/', duration: 2000, width: 350, height: 425 , href: '/social/contests/voting-results/adventure-photo-finalists/'});

	var featured_url = '/zukes/images/homepage/homepage_featured.jpg';
	var featured = new Asset.image(featured_url, {
			id: 'featured', 
			title: 'Zukes Difference', 
			onload: function() {
				$('featured').setStyle('background-image', 'url('+featured_url+')');
				$('featured_hide').setStyle('display', 'none');
			}
		});
		
	// var life_container = $('slideshowContainer');
	// var life_url = '/zukes/images/homepage/slides/life_'+Math.floor(Math.random()*6)+'.jpg';
	// var life = new Asset.image(life_url, {
	// 		id: 'lifestyle_slide', 
	// 		title: 'Lifestyle Slide', 
	// 		onload: function() {
	// 			life_container.setStyle('background-image', 'url('+life_url+')');
	// 		}
	// 	});
	// 
	/* settings */
	var showDuration = 6000;
	var container = $('social_container');
	var images = container.getElements('div');
	var currentIndex = 0;
	var interval;
	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].set('tween',{ duration: 1000 }).fade(0.0);
		toc[currentIndex].removeClass(tocActive);
		images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].set('tween',{ duration: 1000 }).fade(1.0);
		toc[currentIndex].addClass(tocActive);
	};
	
	/* new: control: table of contents */
	images.each(function(img,i){
		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: ((i + 1) * (tocWidth + 10))
			}
		}).inject(container));
		if(i > 0) { img.set('opacity',0); }
	});
	
	/* new: control: next and previous */
	var next = new Element('a',{
		href: '#',
		id: 'next',
		text: '>>',
		events: {
			click: function(e) {
				if(e) e.stop();
				stop(); show();
			}
		}
	}).inject(container);
	var previous = new Element('a',{
		href: '#',
		id: 'previous',
		text: '<<',
		events: {
			click: function(e) {
				if(e) e.stop();
				stop(); show(currentIndex != 0 ? currentIndex -1 : images.length-1);
			}
		}
	}).inject(container);
	
	/* 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();
	});
});
