$(function(){
	$.fn.simple_gallery = function(options){
		options = $.extend({vertical:false} /* default options */, options);
		if(options.vertical){
			$(this).each(function(){
				var th=$(this);
				var ul=th.find('ul');

				if(th.height()<ul.height()){
					var animated=false;
					var el_height=ul.children('li').outerHeight(true);

					th.find('.nav_l').click(function(){
						if(animated) return;
						if(ul.position().top>=0) return;
						animated=true;
						ul.animate({
							top: '+='+el_height
							}, 500, 'swing', function(){
							animated=false;
							}
						);
					});
					th.find('.nav_r').click(function(){
						if(animated) return;
						if(ul.position().top<=th.height()-ul.height()) return;
						animated=true;
						ul.animate({
							top: '-='+el_height
							}, 500, 'swing', function(){
							animated=false;
							}
						);
					});
				}
			});
		}
		else {
			$(this).each(function(){
				var th=$(this);
				var ul=th.find('ul');
				if(th.width()<ul.width()){
					var animated=false;
					var el_width=ul.children('li').outerWidth(true);
					th.find('.nav_l').click(function(e){
						e.preventDefault();
						if(animated) return;
						if(ul.position().left>=0) return;
						animated=true;
						ul.animate({
							left: '+='+el_width
							}, 500, 'swing', function(){
							animated=false;
							}
						);
					});
					th.find('.nav_r').click(function(e){
						e.preventDefault();
						if(animated) return;
						if(ul.position().left<=th.width()-ul.width()) return;
						animated=true;
						ul.animate({
							left: '-='+el_width
							}, 500, 'swing', function(){
							animated=false;
							}
						);
					});
				}
			});
		}
		return this;
	}
	$('.simple_gallery').simple_gallery();
	$('.simple_gallery_vert').simple_gallery({vertical: true});
});
