function invert(val_to_invert) {
	return ((val_to_invert<0) ? val_to_invert * -1 : val_to_invert * -1);
}

$(document).ready(function() {
	var _img_width = $('#gallery_items').find('img').eq(0).attr('width');
	var _img_number = $('#gallery_items').find('img').length;
	var _gallery = $('#gallery_items');
	var _max_left = invert((_img_width * _img_number) - _img_width);
	$('#prev').hide();
	
	_gallery.attr('width', (_img_width * _img_number) + 'px');
	
	$('#next').click(function() {
		var cur_left = parseInt(_gallery.css('left'));
		
		if(!_gallery.is(':animated')) {
			if(cur_left > _max_left) {
				var next_left = cur_left - _img_width;
				_gallery.animate({ left: next_left + 'px' }, 500);
			} else {
				//_gallery.animate({ left: '0px' }, 500);
			};
		};
		
		if((cur_left - parseInt(_img_width)) == _max_left) {
			$('#next').fadeOut();
		};
		
		if(next_left && (next_left < '0')) {
			$('#prev').fadeIn();
		};
		return false;
	});
	
	$('#prev').click(function() {
		var cur_left = parseInt(_gallery.css('left'));
		
		if(!_gallery.is(':animated')) {
			if(cur_left < 0) {
				var next_left = cur_left + _img_width;
				_gallery.animate({ left: next_left + 'px' }, 500);
			};
		};
		
		if(cur_left < '0') {
			$('#next').fadeIn();
		};
		
		if(next_left == '0') {
			$('#prev').fadeOut();
		};
		
		return false;
	});
	
	
});
