(function($){
		
		$.fn.photo = function(index,arg,list_callback,show_callback){
			
			if(typeof index !== 'undefined')
				$index = index;
			else
				$index = 0;
				
			if(typeof $index == 'string')
				$selected_type = 1;
			else if(typeof $index == 'number')
				$selected_type = 0;
				
			if(typeof arg !== 'undefined'){
				$.each(arg,function(i,n){
					eval('$.fn.photo.Default.'+i+'= n;');				
				});
			}
			
			$list_callback = list_callback;
			$show_callback = show_callback;
			$el = this;
			$_ = $(this);
			$el.opts = $.fn.photo.Default;
			$.fn.photo.CreateList();
			
		}
		
		
		//创建列表
		$.fn.photo.CreateList = function(){
			
			$l = $('ul',$_);
			$img = $('img',$l);
			
			
			$ld = $('.photo_list_area',$_);
			$sd = $('.photo_show_area',$_);
			$td = $('.photo_text_area',$_);
			
			$uw = parseInt($el.opts.l_width) + parseInt( $el.opts.area_append_w );
			$uh = parseInt($el.opts.l_height) + parseInt( $el.opts.area_append_h );
			
			$l  .css('width' 	, $uw * $img.length + 'px' );
			$l	.css('left'  	, 0);
			
			$ld .css('width' 	, ($uw * $el.opts.show) + 'px' )
				.css('height'	, $uh + 'px' )
				.css('overflow'	,'hidden')
				.css('position'	,'relative');
				
			
			$img.bind('click'	,function(){$.fn.photo.BtnClick($(this));return false;})
				.css('width'	,$el.opts.l_width + 'px')
				.css('height'	,$el.opts.l_height + 'px')
				.css('opacity'	,$el.opts.n_selected_opacity);
			
			$.fn.photo.ArrowButton();
			$.fn.photo.MoveCenter();
			
			
			$OverHandler = function(){
				$.fn.photo.MouseOverHandler($(this));
			}
			
			$OutHandler = function(){
				$.fn.photo.MouseOutHandler($(this));
			}
			
			$.fn.photo.BindImgMouse();
		}
		
		//绑定鼠标移入移出事件
		$.fn.photo.BindImgMouse = function(){
			
			if($el.opts.is_mouse_event){
				
				$img.unbind('mouseover',$OverHandler);
				$img.unbind('mouseout',$OutHandler);
				
				for(i=0;i<$img.length;i++){
					
					if(i != $index){
						$($img[i]).bind('mouseover',$OverHandler)
								   .bind('mouseout',$OutHandler);
					}
					
				}
				
			}
			
		}
		
		//鼠标移入触发事件
		$.fn.photo.MouseOverHandler = function(img){
			
			img.animate(
				{'opacity':1},$el.opts.mouse_over_speed			
			);
			
		}
		
		//鼠标移除触发事件
		$.fn.photo.MouseOutHandler = function(imgz){
			imgz.animate({'opacity':$el.opts.n_selected_opacity},$el.opts.mouse_over_speed);
		}
		
		
		//列表图触发事件
		$.fn.photo.BtnClick = function(obj){
			
			//回调函数
			if(typeof $list_callback != 'undefined' && typeof $list_callback == 'function'){
				$list_callback(obj);						
			}	  
			
			$img.each(function(i){
				
				if(obj.attr('src') == $(this).attr('src')){
					if($index != i){
						$index = i;
						$.fn.photo.MoveCenter();
						$.fn.photo.BindImgMouse();
					}
					return;
				}
				
			});	
			
		}
		
		//填充ALT标签内容
		$.fn.photo.ShowAlt = function(){
			
			var text = $($img[$index]).attr('alt');
			$td.html(text);
			
		}
		
		//绑定左右按钮事件
		$.fn.photo.ArrowButton = function(){
			
			$('#'+$el.opts.left_button	,$_).bind('click',function(){$.fn.photo.ArrowButton_Handler.left()})
											.css('cursor','pointer');
			$('#'+$el.opts.right_button	,$_).bind('click',function(){$.fn.photo.ArrowButton_Handler.right()})
											.css('cursor','pointer');
			
		}
		
		$.fn.photo.ArrowButton_Handler = {
			'left' : function(){
				if($index > 0)
					$index -= 1;
				
				$.fn.photo.BindImgMouse();
				$.fn.photo.MoveCenter();
			},
			'right' : function(){
				if($index < $img.length - 1)
					$index += 1;
				
				$.fn.photo.BindImgMouse();
				$.fn.photo.MoveCenter();
			}
		}
		
		$.fn.photo.CreateShow = function(){
		
			var href = $($img[$index]).parent().attr('href');
			
			var showimg = $('<img src="'+href+'" />');
			
			if($.fn.photo.Default.b_width > 0){
				showimg.attr('width',$.fn.photo.Default.b_width);	
			}
			
			if($.fn.photo.Default.b_height > 0){
				showimg.attr('height',$.fn.photo.Default.b_height);
			}
			
			//回调函数
			if(typeof $show_callback != 'undefined' && typeof $show_callback == 'function'){
				showimg.bind('click',function(){
					$show_callback($($img[$index]),$index);						  
				});
			}	 
			
			
			$.fn.photo.loader();
			
			showimg.bind('load',function(){
										 
				$sd.html($(this));
				$sd.css('opacity',0);
				$sd.animate({
					opacity : 1	
				},$el.opts.show_create_speed);
				
			});
			
			
		}
		
		$.fn.photo.loader = function(){
			
			$sd.html($('<img src="'+$el.opts.load_gif+'">'));
			
		}
		
		$.fn.photo.MoveCenter = function(){
			
			$img.css('opacity',$el.opts.n_selected_opacity);
			
			$img.each(function(i){
							   
				if($selected_type == 1){
					if($(this).attr('src') == $index){
						$(this).css('opacity',1);
						$index = i;
						$selected_type = 0;
					}
				}else{
					if(i == $index)
						$(this).css('opacity',1);
				}
				
			});	
			
			var MaxPoint = 0;
			var MinPoint = -(($img.length - ( parseInt($el.opts.show / 2) + 1 )) * $uw);
			
			var point = 0;
			
			if($index > ( parseInt($el.opts.show / 2) ) )
				point = -( ($index - parseInt($el.opts.show / 2)) * $uw );
			
			
			if($index > ($img.length - ( parseInt($el.opts.show / 2) )) - 1 )
				point = -( ( ( ( $img.length - ( parseInt($el.opts.show / 2 ) ) ) - 1 ) - ( parseInt($el.opts.show / 2) ) ) * $uw );
			
		
			if( point <= MaxPoint && point >= MinPoint ){
				
				$l.animate({
					left : point 
				},$el.opts.list_move_speed);
				
			}
			
			$.fn.photo.CreateShow();
			$.fn.photo.ShowAlt();
			
		}
		
		$.fn.photo.Default = {
			
			l_width 			: '200',			//列表图的宽 
			
			
			
			l_height 			: '150',			//列表图的高
			
			
			b_width				: 0,
			
			b_height			: 0,
			
			
			
			area_append_w 		: '10',				//附加区域的宽，如果你的LI的样式表使用了BORDER,MARGIN属性，在这里添上，否则将计算不准
			
			
			
			area_append_h 		: '2',				//附加区域的高，同样道理
			
			
			
			show 				: '3',				//一页显示图片的个数
			
			
			
			n_selected_opacity 	: 0.3,				//未选择图片的透明度
			
			
			
			show_create_speed 	: 500,				//显示大图片区域渐变速度
			
			
			
			list_move_speed 	: 500,				//列表移动速度
			
			
			
			mouse_over_speed	: 300,				//鼠标移过时图片变化速度
			
			
			
			is_mouse_event		: true,				//鼠标移动时是否有效果
			
			
			
			load_gif 			: 'templates/grl/images/loading.gif',	//载入图片时显示的图标
			
			
			left_button			: 'leftbutton',		//向左移动按钮的ID
			
			
			
			right_button		: 'rightbutton'		//向右移动按钮的ID
			
		}
			  
	})(jQuery);
