var items;
var handles;
var btnplay;
var interval = 5000;
var currentid = 0;
var itotal;
var timer;
var isPlaying = true;

function sFinit(pitems, phandles, pinterval, pbtnplay)
{
	items = pitems;
	btnplay = pbtnplay;
	itotal = items.length;
	handles = phandles;
	interval = pinterval;
	
	items.fade('hide');
	items[0].fade('in');
		handles[0].set('morph', {
						duration: 300,
						transition:  Fx.Transitions.Sine.easeOut
					}).morph({
				'color':'#3c8636',
				'background-position':'0px -31px' 
			});

	for (i=0;i<handles.length;i++) {
		handles[i].store('item',i);
		handles[i].addEvent('mouseover', function(e){
			pause();
			tofade(e.target.retrieve('item'))
		});
		handles[i].addEvent('mouseleave', function(e){
			play();
		});
	}
	play();
	
	items.addEvent('mouseover', function(e){
		pause();
	});
	items.addEvent('mouseleave', function(e){
		play();
	});
	
	btnplay.addEvent('click', function(e){
		if (this.retrieve("toggle") == null)
	  	 	{
	  	 		btnplaystate('pause');
	  	 		pause();
	  	 		this.store("toggle","pause");
	  	 	}
	 		else if (this.retrieve("toggle") == 'play')
	  	 	{
	  	 		btnplaystate('pause');
	  	 		pause();
	  	 		this.store("toggle","pause");
	  	 	}
	  		else if (this.retrieve("toggle") == 'pause')
	  	 	{
	  	 		btnplaystate('play');
	  	 		play();
	  	 		this.store("toggle","play");
  	 	}
		});
}

function tofade(num)
{
	currentid = num;
	items.fade('out');
	items[num].fade('in');
	handles.setStyles({
				'color':'#347094',
				'background-position':'0px 0px' 
			});
	handles[num].setStyles({
				'color':'#3c8636',
				'background-position':'0px -31px' 
			});
}

function next()
{
	currentid++;
	if (currentid == itotal){
		currentid = 0;
	}
	tofade(currentid);
}

function play()
{
	if (isPlaying) {
		timer = next.periodical(interval);
	}
}

function pause()
{
	$clear(timer);
}


function btnplaystate(s)
{
	if (s == "play") {
		isPlaying = true;
		btnplay.set('morph', {
				duration: 200,
				transition:  Fx.Transitions.Sine.easeOut
			}).morph({
		'background-position':'0px 0px' 
		});
	} else if (s == "pause") {
		isPlaying = false;
		btnplay.set('morph', {
				duration: 200,
				transition:  Fx.Transitions.Sine.easeOut
			}).morph({
		'background-position':'0px -41px' 
		});
	}
}
