var as = [];
var tabsets = [];
var curr_index = 0;

var duration = 700;
var stop_duration = 5000;

var start_o = 0;
var end_o = 1;
var _slideshowTimeout = false;

function initSlideShow()
{
	
	as = [];
	curr_index = 0;
	
	var _box = document.getElementById('slides');
	if(_box)
	{	

		var _as = _box.getElementsByTagName('div');
		var cnt = 0;
		var total_cnt = 0;

		for(i = 0; i < _as.length; i++)
		{
			if(_as[i].className == "box")
			{
				total_cnt++;
			}
		}
		
		for(i = 0; i < _as.length; i++)
		{
			if(_as[i].className == "box")
			{
				as[cnt] = _as[i];
				as[cnt]._index = cnt;
				
				if(cnt == 0)
				{
					as[cnt].style.display = 'block';
				}
				else
				{
					as[cnt].style.display = 'none';					
				}
				cnt++;
			}
		}

		var aa = _box.parentNode.parentNode.getElementsByTagName('a');
		for(i = 0; i < aa.length; i++)
		{
			if(aa[i].parentNode.className.indexOf('next') != -1)
			{
				if(!(total_cnt > 1))
				{
					aa[i].style.visibility = 'hidden';
				}
				else
				{
					aa[i].href = 'javascript:;';
					aa[i].onclick = function()
					{
						var next_index = curr_index+1;
						if(next_index >= tabsets.length)
						{
							next_index = 0;
						}
						tabsets[next_index].onclick();
					}
				}
			}
			if(aa[i].parentNode.className.indexOf('prev') != -1)
			{
				if(!(total_cnt > 1))
				{
					aa[i].style.visibility = 'hidden';
				}
				else
				{
					aa[i].href = 'javascript:;';
					aa[i].onclick = function()
					{
						var next_index = curr_index-1;
						if(next_index < 0)
						{
							next_index = tabsets.length - 1;
						}
						tabsets[next_index].onclick();
					}
				}
			}
		}

		var _tabset = document.getElementById('tabset');
		if(_tabset)
		{
			tabsets = _tabset.getElementsByTagName('a');
			for(i = 0; i < tabsets.length; i++)
			{
				tabsets[i]._index = i;
				if(i == curr_index) tabsets[i].parentNode.className = "active";
				tabsets[i].onclick = function()
				{
					tabsets[curr_index].parentNode.className = "";
					_switch(false,this._index);
					this.parentNode.className = "active";
				}
			}
		}		
	}

}

function _switch(_start,_next_index)
{
	clearTimeout(_slideshowTimeout);
	var as_cnt = as.length;
	if(as_cnt > 1)
	{
		if(!_start)
		{

			var next_index = curr_index + 1;

			if(typeof(_next_index) != 'undefined')
			{
				var next_index = _next_index;
			}

			if(next_index >= as_cnt)
			{
				next_index = 0;
			}
			else if(next_index < 0)
			{
				next_index = as_cnt - 1;
			}
		
			if(next_index != curr_index)
			{

				var _opacity1 = new Fx.Styles( as[curr_index], {duration: duration,
					
					onStart: function(){
						this.element.style.display = 'block';
					},
					onComplete: function(){this.element.style.display = 'none';}	
					
					});
			
				_opacity1.custom({'opacity': [end_o,start_o]});
		
				var _opacity2 = new Fx.Styles( as[next_index], {duration: duration,
					
					onStart: function(){
						this.element.style.display = 'block';
					},
					onComplete: function(){this.element.style.display = 'block';}		
					
					});
			
				_opacity2.set({'opacity': start_o});
				_opacity2.custom({'opacity': [start_o,end_o]});
		
				 curr_index = next_index;

			}
		}
		
	}
}

if (window.attachEvent){
	window.attachEvent("onload", initSlideShow);
	}
else if (window.addEventListener){
	window.addEventListener("load", initSlideShow, false);
	}
