
//Rotator
Event.observe(window, 'load', function() {
	activeFeature = 1;
	totalFeatures = $('features_list').childElements().size();
	interval = 5000;
	offset = 656;
	position = 0;
	timer = null;
	startTimer();
	Event.observe('features','mouseover', stopTimer);
	Event.observe('features','mouseout', startTimer);
	listWidth = offset * totalFeatures;
	
	// Set the styles for the sliding list
	$('features_list').setStyle({
		width: listWidth+'px'
	});
	
	list = new Element('ul', { 'id': 'feature_buttons'});
	$('features').appendChild(list);
	
	for(i=1;i <= totalFeatures;i++){
		var item = new Element('li', { 'id': 'feature'+i+'_btn'}).update('<a href="#" onclick="slideTo('+i+')">'+i+'</a>');
		$('feature_buttons').appendChild(item);
	}
});
function switchFeature(nextElementIndex){
	if(nextElementIndex != activeFeature){
		currentElement = 'feature'+activeFeature;
		nextElement = 'feature'+nextElementIndex;
		topPosition = $(currentElement).getStyle('z-index');
		$(nextElement).setStyle('z-index:'+(topPosition-1));
		$(nextElement).show();
		$(currentElement).fade({afterFinish: function(){
			$(nextElement).setStyle('z-index:'+(topPosition));
		} });
		activeFeature = nextElementIndex;
	}
	
	// Uncomment the following line for debugging purposes only.
	// debug();
	
}
function slideTo(id){
	feature = 'feature'+id;
	feature_array = $('features_list').childElements();
	position = 1;
	i = 1;
	if (feature == feature_array.first().identify()){}
	else{
		feature_array.each(
			function(s){
				if(s.identify() == feature) position = i;
				i++;
			}
		);
		
	/*
		for(i=0;i<position;i++){
			slideFeature();
		}
		*/
	}
			destination = offset * (position-1) * -1;
			new Effect.Move('features_list', { 
				x: destination, 
				y: 0, 
				mode: 'absolute',
				duration: .3,
				transition: Effect.Transitions.linear,
				afterFinish: function (){
					//listItem = $('features_list').firstDescendant().remove();
					//$('features_list').appendChild(listItem);
					//$('features_list').style.left = 0;
				}
			});
}
function slideFeature(){
	position = offset * -1;
	new Effect.Move('features_list', { 
		x: position, 
		y: 0, 
		mode: 'absolute',
		duration: .3,
		transition: Effect.Transitions.linear,
		afterFinish: function (){
			listItem = $('features_list').firstDescendant().remove();
			//button = listItem.identify() + '_btn';
			//button.addClassName('active');
			$('features_list').appendChild(listItem);
			$('features_list').style.left = 0;
		}
	});
	
}

function switchNext(){
// 2. Set next feature to active feature
		if(activeFeature<totalFeatures) slideFeature(0);
		else slideFeature(1);
}

function debug(){
	/*$('debug').update(
		$('feature1').getStyle('z-index') + '<br />' +
		$('feature2').getStyle('z-index') + '<br />'
	);*/
	
}

function stopTimer(){
	timer=window.clearInterval(timer)
}
function startTimer(){
	timer = setInterval("switchNext()",interval);
}
