_slist = null;
_slides = null;
_play = null;
_steps = [];

var _curr = 0;

var duration = 1000;
var step_duration = 7000;
var timer = null;
var cycle = false;

function initSlideShow()
{
	_slist = $('slide-list');
	_slides = $('slides');
	_play = $('btn-go');
	
	if (_slist && _slides)
	{
		var links = _slist.getElements("a");
		links.each(function(link, i){
			var c = document.getElementById(link.href.substr(link.href.indexOf("#") + 1)); 
			if (c)
			{
				link._tab = c;
				link._tab.fx = new Fx.Style(link._tab, 'opacity', {duration: duration, wait: true});

				_steps.push(link);
				link._index = _steps.length-1;
				if (i == 0)
				{
					link.parentNode.className += " active";
					link._tab.fx.set(1);
				}
				else
				{
					link._tab.fx.set(0);
				}
				link.onclick = function() {
					changeBtn(false);
					change(this._index);
					return false;
				}
			}
		});
		if (_play) {
			_play.onclick = function() {
				if (cycle)
				{
					changeBtn(false);
					clearTimeout(timer);
				}
				else
				{
					changeBtn(true);
					change();
				}
			}
		}
		changeBtn(true);
		timer = setTimeout('change()', step_duration);
	}
}

function change(_index) {
	if (typeof(_index) != "undefined")
	{
		changeBtn(false);
		_next = _index;
	}
	else
	{
		_next = _curr+1;
		if (!_steps[_next])
		{
			_next = 0;
		}
	}
	_steps[_curr]._tab.fx.start(0);
	_steps[_curr].parentNode.className = _steps[_curr].parentNode.className.replace("active", "");
	
	_steps[_next]._tab.fx.start(1);
	_steps[_next].parentNode.className += " active";
	_curr = _next;
	clearTimeout(timer);
	if (cycle)
	{
		timer = setTimeout('change()', step_duration);
	}
}

function changeBtn(_action) {
	if (_action) {
		_play.className = "pause";
		cycle = true;
	}
	else
	{
		_play.className = "play";
		cycle = false;
	}
}

if (window.addEventListener)
	window.addEventListener("load", initSlideShow, false);
else if (window.attachEvent && !window.opera)
	window.attachEvent("onload", initSlideShow);
