$(document).ready(function(){
	var _gall = $('#gallery');
	if(_gall.length){
		var _img = $('#gall-img');
		var btn_prev = _gall.find('.link-prev');
		var btn_next = _gall.find('.link-next');
		var _list = _gall.find('div > ul');
		var list_w = _list.height();
		var hold_w = _list.parent().height();
		if(list_w > hold_w){
			var _el = _list.children();
			_list.append(_el.clone()).append(_el.clone()).css('margin-top', - list_w);
			btn_prev.mouseenter(function(){
				_list.stop();
				moveTop();
			}).mouseleave(function(){
				_list.stop();
			}).click(function(){
				return false;
			});;
			btn_next.mouseenter(function(){
				_list.stop();
				moveBottom();
			}).mouseleave(function(){
				_list.stop();
			}).click(function(){
				return false;
			});
		}
		var _a = -1;
		var _btn = _list.find('a');
		_btn.click(function(){
			if(_btn.index(this) != _a){
				_img.attr('src', $(this).attr('href')).attr('alt', $(this).attr('rel'));
				_a = _btn.index(this);
			}
			return false;
		});
		var t_duration;
		var _k = 5;
		function moveBottom(){
			t_duration = (list_w*2 + parseInt(_list.css('margin-top')))*_k;
			_list.animate({ marginTop: - list_w*2}, t_duration, 'linear', function(){
				$(this).css('margin-top', -list_w);
				moveBottom();
			});
		}
		function moveTop(){
			t_duration = - parseInt(_list.css('margin-top'))*_k;
			_list.animate({ marginTop: 0}, t_duration, 'linear', function(){
				$(this).css('margin-top', -list_w*2);
				moveTop();
			});
		}
	}
});