var _curr = 0;
var duration = 800;

function initGallery(_target) {

	if (!_target._slides && !_target._links)
	{
		_target._curr = 0;
		_target._slist = _target.getElements('div.sub ul')[0];
		_target._slider = _target.getElements('div.icon')[0]
		
		if (_target._slist && _target._slider)
		{
			var _ul = document.createElement("ul");
			_target._slider.appendChild(_ul);
			
			_target._links = _target._slist.getElements("a");
			
			_target._links.each(function(link, i) {
				if (link.rel)
				{
					var _li = document.createElement("li");
					var _img = document.createElement("img");
					_img.src = link.rel;
					_li.appendChild(_img);
					_ul.appendChild(_li);
				}
				link._t = _target;
				link._index = i;
				link.addEvent("click", function(){
					change(this._index, this._t);
					return false;
				});
			});
			
			_target._slides = _target._slider.getElements("img");
			_target._slider.removeClass("loader");
		
			_target._slides.each(function(el, i){
				el.fx = new Fx.Tween(el, {duration: duration, wait:false}).set('opacity', 0);
				el.style.visibility="visible";
			
				if (i == 0)
				{
					el.fx.set('opacity', 1);
				}
			});
		}
	}
}

function change(_index, _target) {
	if (_target._slides[_index])
	{
		_target._slides[_target._curr].fx.start('opacity', 0);
		_target._curr = _index;
		_target._slides[_target._curr].fx.start('opacity', 1);
	
		$(_target._links[_target._curr].parentNode.parentNode).getElements("li").each(function(el){
			el.removeClass("active");
		});
		$(_target._links[_target._curr].parentNode).addClass("active");
	}
}

