$.fn.gallery=function(o){
	o = $.extend({
        amount:9,				   
		easing:'easeOutCirc',
		speed:600,
		shift_left:260,
		visible:3,
		center:{left:380, top:0},
		gallery_left:function(pos, n){
			return pos+n*260
		},
		gallery_top:function(now){
			return 1600 - Math.sqrt(2560000-(((now-200)/3.5-50)*((now-200)/3.5-50)*12.25))
		},
		angle:function(t,l){
			if (Math.abs(l-position[1].left)<2) {return 0} else {return Math.tan((t-position[1].top)/(l-position[1].left))*180/Math.PI*1.7;}
		},
		gal_scroll:'.scroll_gallery',
		drag:'.drag',
		drag_w:350,
		drag_top:function(now){
			return 1390 - Math.sqrt(1960000-((now/3.5-50)*(now/3.5-50)*12.25))
		}
    }, o || {});
	
	
	var gal=this;
	position=[];
	gal.data({o:o});
	var num=0;
	o=gal.data('o')
	
	position[1] = o.center;
	position[0] = {
		left:o.gallery_left(position[1].left,-1),
		top:o.gallery_top(o.gallery_left(position[1].left,-1))
	};
	
	
	window.show_gallery = function (gal){
	$.when($('.gallery li')).then(function(){
		o=gal.data('o');
		$(o.drag).css({top:0, left:0})
		$('.gallery').css({display:'block'});
		gal.css({display:'block'});
		
		
		gal.find('li').eq(0).stop().animate(position[0],o.spee2,o.easing);
		gal.find('li').eq(0).find('a span').stop().animate({rotate:o.angle(position[0].top, position[0].left)},o.speed,o.easing);
		
		gal.find('li').eq(1).stop().animate(position[1],o.speed,o.easing);
		gal.find('li').eq(1).find('a span').stop().animate({rotate:0},o.speed,o.easing);
		
		for (i=o.visible-o.amount;i<0;i++) {
			position[i] = {
				left:o.gallery_left(position[0].left,i),
				top:o.gallery_top(o.gallery_left(position[0].left,i))
			};	
		}
		for (var i=2; i<o.amount; i++) {
			position[i] = {
				left:o.gallery_left(position[i-1].left, 1),
				top:o.gallery_top(o.gallery_left(position[i-1].left, 1))
			};
			
			gal.find('li').eq(i).stop().animate({left:position[i].left},{duration:o.speed, step:function(now){
				$(this).css({top:o.gallery_top(now)});
				$(this).find('a span').css({rotate:o.angle(o.gallery_top(now), now)+'Deg'})
			}, easing:o.easing});
		}
	})
	}
	
	
	window.hide_gallery = function (gal){
		gal.o=gal.data('o')
		for (var i=0; i<o.amount; i++) {
			gal.find('li').eq(i).stop().animate({left:position[1].left},{duration:o.speed, step:function(now){
				$(this).css({top:o.gallery_top(now)});
				$(this).find('a span').css({rotate:o.angle(o.gallery_top(now), now)+'Deg'})
			}, easing:o.easing});
		}
		$.when(gal.find('li')).then(function(){
			$('.gallery').css({display:'none'});
			$(gal).css({display:'none'});
		});
	}
	
	gal.find(o.gal_scroll).slider({
		 min: 0,
		 max: o.drag_w,
		 handle: o.drag,
		 stop: function (event, ui) {
			num=Math.round(ui.value/o.drag_w*(o.amount-o.visible));
			gallery_anim(num);
		 },
		 slide: function (event, ui) {
			if (num!=Math.round(ui.value/o.drag_w*(o.amount-o.visible))) {
				num=Math.round(ui.value/o.drag_w*(o.amount-o.visible));
				if ((0<=num)&&(num<o.amount)) {
					gallery_anim(num);
				}
			}
		 }
	});
	
	function gallery_anim(num){
			pos_left=(num)*o.drag_w/(o.amount-o.visible);
			$(o.drag).stop().animate({left:pos_left}, {duration:o.speed, step:function(now){
				$(this).css({top:o.drag_top(now)})
			}, easing:o.easing});
			
			n=-num;
			for (var i=0; i<o.amount; i++) {
				gal.find('li').eq(i).stop().animate({left:position[i+n].left},{duration:o.speed, step:function(now){
					$(this).css({top:o.gallery_top(now)});
					$(this).find('a span').css({rotate:o.angle(o.gallery_top(now), now)+'Deg'})
				}, easing:o.easing});
			}
	}
	
	gal.parent().mousewheel(function(e, d){
		if (num-d>=0 && num-d <= o.amount-o.visible) {
			gallery_anim(num-d);
			num=num-d;
			return false
		}
	})
	return gal;
}
