jQuery.fn.asset = function(options){
	settings = jQuery.extend({images: []}, options);
}
//
jQuery.fn.gallery = function(options){
	settings = jQuery.extend({
								images: [],
								legend: [],
								gallery_id:0,
								galleryWidth:200,
								galleryHeight:200
	}, options);
	var ref = $(this);
	$.data(ref,'index',0);
	//
	//
	var handleWidthRatio = 6;
	var handleWidth = settings.galleryWidth/handleWidthRatio;
	ref.css({'position':'relative','width':settings.galleryWidth+'px','height':settings.galleryHeight+'px','overflow':'hidden'});
	var cont = $('<span class="gallery-cont"></span>');
	var handleLeft = $('<span class="gallery-handle-left"></span>');
	var handleRight = $('<span class="gallery-handle-right"></span>');
	var legend = $('<span class="gallery-legend"></span>');
	handleLeft.css({
					'position':'absolute',
					'display':'block',
					'left':0,
					'width':handleWidth+'px',
					'height':settings.galleryHeight+'px',
					'opacity':0
				});
	handleRight.css({
					'position':'absolute',
					'display':'block',
					'left':(handleWidth*(handleWidthRatio-1))+'px',
					'width':handleWidth+'px',
					'height':settings.galleryHeight+'px',
					'opacity':0
				});
	legend.css({
					'position':'absolute',
					'padding':'5px',
					'background':'#FFF',
					'opacity':'0.8',
					'display':'block',
					'bottom':'0',
					'width':settings.galleryWidth+'px'
				});
	//
	cont.css({'display':'block','position':'absolute','width':(settings.galleryWidth*settings.images.length)+'px'});
	ref.append(cont);
	ref.append(handleLeft);
	ref.append(handleRight);
	ref.append(legend);
	legend.html((settings.legend[0]?settings.legend[0]:'&nbsp;'));
	var i = 0;
	var pos 					= [];
	var posOverLeft 			= [];
	var posOverRight 			= [];
	$(settings.images).each(function(){
		var path 				= this.path+this.image+'/gal_'+settings.gallery_id+'_image.jpg';
		var img 				= $('<img src="'+path+'"/>');
		img.css({'float':'left','width':settings.galleryWidth+'px'});
		pos[i] 					= (-(settings.galleryWidth*i))+'px';
		posOverLeft[i] 			= (i==0)?0:(-(settings.galleryWidth*i)+handleWidth)+'px';
		posOverRight[i] 		= (i==settings.images.length-1)?0:(-(settings.galleryWidth*i)-handleWidth)+'px';
		cont.append(img);
		i++;
		});
		var overRight = function(){
					var index = $.data(ref,'index');
					if (index+1 < settings.images.length){
						$(this).stop().animate({'opacity':1});
						cont.stop().animate({'left':$.data(ref,'position').posOverRight[index]});
					}
				};
		var outRight = function(){
					var index = $.data(ref,'index');
					$(this).stop().animate({'opacity':0});
					cont.stop().animate({'left':$.data(ref,'position').pos[index]});
				};
		var outDurringAnimate = function(){$(this).stop().animate({'opacity':0});};
		
		var overLeft = function(){
					var index = $.data(ref,'index');
					if (index > 0){
						$(this).stop().animate({'opacity':1});
						cont.stop().animate({'left':$.data(ref,'position').posOverLeft[index]});
					}
				};
		var outLeft = function(){
					var index = $.data(ref,'index');
					$(this).stop().animate({'opacity':0});
					cont.stop().animate({'left':$.data(ref,'position').pos[index]});
				};
		var clickRight = function(){
					legend.html('&nbsp;');
					var index = $.data(ref,'index');
					if (index+1 < settings.images.length){
						handleRight.unbind('mouseover');
						handleRight.unbind('mouseout');
						handleLeft.unbind('mouseover');
						handleLeft.unbind('mouseout');
						handleRight.mouseout(outDurringAnimate);
						//$(this).stop().animate({'opacity':0});
						$.data(ref,'index',index+1);
						cont.stop().animate({	'left':$.data(ref,'position').pos[$.data(ref,'index')]},
												500,
												function(){
															handleRight.unbind('mouseout');
															handleLeft.mouseover(overLeft);handleLeft.mouseout(outLeft);
															handleRight.mouseover(overRight);handleRight.mouseout(outRight);
															legend.html(settings.legend[$.data(ref,'index')]?settings.legend[$.data(ref,'index')]:'&nbsp;');
															}
											);
					}
		}
		var clickLeft = function(){
					legend.html('&nbsp;');
					var index = $.data(ref,'index');
					if (index > 0){
						handleRight.unbind('mouseover');
						handleRight.unbind('mouseout');
						handleLeft.unbind('mouseover');
						handleLeft.unbind('mouseout');
						handleLeft.mouseout(outDurringAnimate);
						//$(this).stop().animate({'opacity':0});
						$.data(ref,'index',index-1);
						cont.stop().animate({	'left':$.data(ref,'position').pos[$.data(ref,'index')]},
												500,
												function(){
															handleLeft.unbind('mouseout');
															handleLeft.mouseover(overLeft);handleLeft.mouseout(outLeft);
															handleRight.mouseover(overRight);handleRight.mouseout(outRight);
															legend.html(settings.legend[$.data(ref,'index')]?settings.legend[$.data(ref,'index')]:'&nbsp;');
															}
											);
					}
				};
		handleRight.mouseover(overRight);
		handleRight.mouseout(outRight);
		handleLeft.mouseover(overLeft);
		handleLeft.mouseout(outLeft);
		handleRight.click(clickRight);
		handleLeft.click(clickLeft);
		
	$.data(ref,'position',{'pos':pos,'posOverLeft':posOverLeft,'posOverRight':posOverRight});
};
