
var th = [];
var th_height = 0;

_tstep = 279;
_tstep_current = 0;

var _ths = null;
var _th_link = null;
var _th_image = null;
var _th_text = null;
var _th_play = null;
var _th_counter = null;

var th_fx = null;
var th_opacity = null;
var th_opacity2 = null;
var _th_loop = false;

var _th_active = 0;

var th_duration = 700;
var th_stop_duration = 5000;

var _thTimeout = false;


function initThumbnails()
{
	_ths = document.getElementById('thumbnails');
	_th_link = document.getElementById('photo-link');
	_th_image = document.getElementById('photo-image');
	_th_text = document.getElementById('photo-text');
	_th_play = document.getElementById('th-play');
	_th_counter = document.getElementById('photo-counter');

	if(_ths && _th_link && _th_image && _th_text && _th_play)
	{	
		
		th_opacity = new Fx.Styles( _th_image, {duration: 1000,wait: false,
				onStart: function(){},
				onComplete: function(){}		
			});
		th_fx = new Fx.Styles(_ths,{
				duration:1000,
				wait: false,
				onStart: function(){
				},
				onComplete: function(){
				}
		});
		
		th_height = _ths.offsetHeight;
		
		th = _ths.getElementsByTagName("a");
		for (i =0; i < th.length; i ++)
		{
			th[i]._index = i;
			th[i].onclick = function()
			{
				var _index = this._index
					var _opacity1 = new Fx.Styles( _th_image, {duration: th_duration,
					onStart: function(){},
					onComplete: function(){changeImage(_index);}
					});
				_opacity1.custom({'opacity': [1,0]});
				stopSlide();
				
				return false;
			}
		}
		
		var _th_next = document.getElementById("th-next");
		if (_th_next)
		{
			_th_next.onclick = function	() {
				if (th[_th_active+1])
				{
					stopSlide();
					_th_switch();
				}
				return false;
			}
		}
		
		var _th_prev = document.getElementById("th-prev");
		if (_th_prev)
		{
			_th_prev.onclick = function	() {
				stopSlide();
				if (_th_active == "0")
				{
					return false;
				}
				else if (_th_active == "1")
				{
					_th_active = th.length;
				}
				else {
					_th_active = _th_active - 2;
				}
				_th_switch();
				return false;
			}
		}
		
		_th_play.onclick = function(){
			if (_th_loop) {
				stopSlide();
			}
			else {
				startSlide();
				//_th_switch();
			}
			return false;
		}
		
		var _links = _ths.parentNode.parentNode.getElementsByTagName("a");
		for (i = 0; i < _links.length; i ++)
		{
			if (_links[i].className == "up")
			{
				_links[i].onclick = function() {
					_slide(false);
					return false;
				}
			}
			if (_links[i].className == "down")
			{
				_links[i].onclick = function() {
					_slide(true);
					return false;
				}
			}
		}
		
	}
}

function changeImage(_obj) {
	var _link = th[_obj];
	
	_th_active = _link._index;
	if (_link.rel) {
		_th_link.href = _link.rel;
		}
		if (_link.parentNode.getElementsByTagName("div")[0]) {
			_th_text.innerHTML = _link.parentNode.getElementsByTagName("div")[0].innerHTML;
		}
		_th_image.src = _link.href;

		if (_th_counter) {
			_th_counter.innerHTML = _th_active+1 + " of " + th.length;
		}
		th_opacity.custom({'opacity': [0,1]});
}

function stopSlide() {
	clearTimeout(_thTimeout);
	_th_loop = false;
	_th_play.innerHTML = "Play slideshow";
}

function startSlide() {
	clearTimeout(_thTimeout);
	_th_loop = true;
	_thTimeout = setTimeout('_th_switch()',th_duration + th_stop_duration);
	_th_play.innerHTML = "Stop slideshow";
}

function _slide(_offset)
{
	if (_offset)
	{
		if (th_height - _tstep_current > _tstep)
		{
			_tstep_current = _tstep_current + _tstep;
			th_fx.start({
				'top': 0 - _tstep_current + "px"
			});
		}
	}
	else
	{
		if (_tstep_current > 0)
		{
			_tstep_current = _tstep_current - _tstep;
			th_fx.start({
				'top': 0 - _tstep_current + "px"
			});
		}
	}
}


function _th_switch()
{
	clearTimeout(_thTimeout);
	var as_cnt = th.length;
	var _next = null;
	if(as_cnt > 1)
	{
		if (th[_th_active +1])
		{
			_next = _th_active+1;
		}
		else
		{
			_next = 0;
		}
		
			var _opacity1 = new Fx.Styles( _th_image, {duration: th_duration,
			onStart: function(){},
			onComplete: function(){changeImage(_next);}
			});
			_opacity1.custom({'opacity': [1,0]});
			_th_active = _next;
		

		if (_th_loop)
		{
			_thTimeout = setTimeout('_th_switch()',th_duration + th_stop_duration);
		}
	}
}


if (window.attachEvent){
	window.attachEvent("onload", initThumbnails);
}
else if (window.addEventListener){
	window.addEventListener("load", initThumbnails, false);
}
