_slist = null;
_slides = null;
_steps = [];
_play = false;

var _curr = 0;
var step_time = 8000;
var duration = 500;
var links = [];
var _timer;
function initSlideShow()
{
	_slist = $('tabset');
	_slides = $('tabholder');
	
	if (_slist && _slides)
	{
		_slides.className = _slides.className.replace("loader", "");
		links = _slist.getElements("a");
		links.each(function(link, i) {
			var c = document.getElementById(link.rel.substr(link.rel.indexOf("#") + 1)); 
			if (c)
			{
				link._tab = c;
				link._tab.fx = new Fx.Style(link._tab, 'opacity', {duration: duration, wait: true,onStart: function(){_play = true;},onComplete: function(){_play = false;}});

				_steps.push(link);
				link._index = _steps.length-1;
				
				link._tab.style.visibillity = "visible";
				if (i == 0)
				{
					link.parentNode.className += " active";
					link._tab.fx.set(1);
				}
				else
				{
					link._tab.fx.set(0);
				}
				var t_a = link._tab.getElement('a');
				if(t_a) {
						t_a.onmouseover = function() {
							if (_timer) clearTimeout(_timer);
						}
						t_a.onmouseout = function() {
							_timer = setTimeout(function(){autoChange()}, step_time);
						}
				}
				link.onmouseover = function() {
					if (_timer) clearTimeout(_timer);
					if (_curr != this._index) {
						links.each(function(lnk) {
							lnk.className = lnk.className.replace("active", "");
						});
						this.className += " active";
						change(this._index);
					}
					return false;
				}
				link.onmouseout = function() {
					_timer = setTimeout(function(){autoChange()}, step_time);
					return false;
				}
			}
		});
		_links = links;
		function autoChange() {
				if (_timer) clearTimeout(_timer);
				var _index = findIndex(links, $$(links).filter(function(item,index){return item.className.indexOf('active') != -1})[0]);
				var _summ = _links.length - 1;
				links.removeClass('active');
				if (_summ >= _index + 1) {
					_links[_index+1].addClass('active');
					change(_index+1);
				} else {
					_links[0].addClass('active');
					change(0);
				}
				_timer = setTimeout(function(){autoChange()},step_time);
		}
		_timer = setTimeout(function(){autoChange();},step_time);
	}
}
function findIndex(_obg, _this) {
		var _i;
		_obg.each(function(_el, i){
				if(_el == _this) _i = i;
		});
		return _i;
}

function change(_index) {
	if (typeof(_index) != "undefined")
	{
		_next = _index;
	}
	else
	{
		_next = _curr+1;
		if (!_steps[_next])
		{
			_next = 0;
		}
	}
	_steps[_curr]._tab.fx.stop();
	_steps[_curr]._tab.fx.start(0);
	_steps[_curr].parentNode.className = _steps[_curr].parentNode.className.replace("active", "");
	
	_steps[_next]._tab.fx.stop();
	_steps[_next]._tab.fx.start(1);
	_steps[_next].parentNode.className += " active";
	_curr = _next;
}

if (window.addEventListener)
	window.addEventListener("load", initSlideShow, false);
else if (window.attachEvent && !window.opera)
	window.attachEvent("onload", initSlideShow);
