/* Frontpage highlights */
if ($('preview'))
{
	// Collect
	images = $$('#preview img');
	pagers = $$('#preview li');
	pagers[0].addClass('active');
	numImages = images.length;
	imgCounter = { current: 0, previous: 0 };

	// Set first image	
	images.fade('hide');	
	images[0].fade('in');
	
	/* Swap images */
	function swapImage(state)
	{
		pagers.removeClass('active');
		pagers[state.current].addClass('active');
		
		images[state.previous].fade('out');
		
		images[state.current].addClass('active');
		images[state.current].fade('in');
		images[state.current].removeClass('active');
		
		state.previous = state.current;
	}
	
	// Loop images - bound with imgCounter
	addCount = function()
	{ 
		this.current++;
		
		if (this.current > numImages-1)
				this.current = 0;
		
		swapImage(this);
	};
	
	// Start timer if more than one image
	if (images.length > 1)
		var timer = addCount.periodical(10000, imgCounter);
	
	/* Set pager events */
	pagers.addEvent('click', function()
	{
		if (pagers.indexOf(this) == imgCounter.current)
			return;
		
		clearInterval(timer);
		imgCounter.current = pagers.indexOf(this);
		swapImage(imgCounter);
		timer = addCount.periodical(10000, imgCounter);
	});
}

