// slider.js update:2011.10.10

if(typeof jQuery != 'undefined') {
	jQuery(function($) {
									
		
		$.fn.extend({ 
				slider:function(options) {        
				var settings = $.extend({}, $.fn.slider.defaults, options);  
				var slWrap=$(this).find(settings.slidersWrapper);
				var sl=$(this).find(settings.slider);
				var pgWrap=$(this).find(settings.paginationWrapper);
				var pg=$(this).find(settings.pagination);
				var prev=$(this).find(settings.prev);
				var next=$(this).find(settings.next);
				var width=settings.width;
				var height=settings.height;
				var direction=settings.direction;
				var pgactive=settings.pgactive;
				var step=settings.step;
				var autoplay=settings.autoplay;
				var autotime=settings.autotime;
				var it;
				var et={duration:settings.time,easing: 'easeOutQuint'};
				var n=0;
				var tempLeft=0;
				var tempTop=0;
				var i=0;
				if(direction=='horizontal')
				{
					horiAnimate();
				}
				else if(direction=='vertical')
				{
					verticalAnimate();
				}
				
				function horiAnimate(){
						slWrap.css({position:'absolute',width:sl.length*width+'px',height:height+'px'});
						sl.each(function(){
									 $(this).css({float:'left',width:width+'px',display:'block'});
								});
						if(pg!=null)
						{
							pg.bind('click',function(){
												if(pgactive)
												{
													pg.filter('.active').removeClass('active');
													$(this).addClass('active');
												}
												slideTo($(this).index(),direction,width);
											 });
						}
						if(autoplay==true)
						{
							it=setInterval(autoPlay,autotime);
							sl.hover(function(){clearInterval(it);},function(){it=setInterval(autoPlay,autotime);});
							prev.hover(function(){clearInterval(it);},function(){it=setInterval(autoPlay,autotime);});
							next.hover(function(){clearInterval(it);},function(){it=setInterval(autoPlay,autotime);});

						}
						if(prev!=null)
						{
							prev.bind('click',function(){pnMotion('prev',width);});
						}
						if(next!=null)
						{
							next.bind('click',function(){pnMotion('next',width)});
						}
					}//横向滑动
				
				function verticalAnimate(){
						slWrap.css({position:'absolute',height:sl.length*height+'px',width:width+'px'});
						sl.each(function(){
									 $(this).css({height:height+'px',display:'block'});
								});
						if(pg!=null)
						{
							pg.bind('click',function(){
												if(pgactive)
												{
													pg.filter('.active').removeClass('active');
													$(this).addClass('active');
												}
												slideTo($(this).index(),direction,height);
											 });
						}
						if(prev!=null)
						{
							prev.bind('click',function(){pnMotion('prev',height);});
						}
						if(next!=null)
						{
							next.bind('click',function(){pnMotion('next',height)});
						}
						if(autoplay==true)
						{
							it=setInterval(autoPlay,autotime);
							sl.hover(function(){clearInterval(it);},function(){it=setInterval(autoPlay,autotime);});
						}
					}//纵向滑动
					
				function autoPlay(){
							if(direction=='horizontal')
							{
								if(i<sl.length-1)
								{
									slideTo(i,direction,width);
									if(pgactive)
									{
										pg.filter('.active').removeClass('active');
										pg.eq(i).addClass('active');
									}
									i++;
								}
								else if(i>=sl.length-1)
								{
									slideTo(sl.length-1,direction,width);
									if(pgactive)
									{
										pg.filter('.active').removeClass('active');
										pg.eq(i).addClass('active');
									}
									i=0;
								}
							}
							else if(direction=='vertical')
							{
								if(i<sl.length-1)
								{
									slideTo(i,direction,height);
									if(pgactive)
									{
										pg.filter('.active').removeClass('active');
										pg.eq(i).addClass('active');
									}
									i++;
								}
								else if(i>=sl.length-1)
								{
									slideTo(sl.length-1,direction,height);
									if(pgactive)
									{
										pg.filter('.active').removeClass('active');
										pg.eq(i).addClass('active');
									}
									i=0;
								}
							}
						
				}//自动播放
				
				
				function pnMotion(pn,dist){
						switch (direction)
						{
							case 'horizontal': hPnMotion(pn,dist)
							break
							case 'vertical': vPnMotion(pn,dist)
							default:
							break
						}						
					}
				
				function hPnMotion(pn,dist){
						if(pn=='prev')
						{
									
									if(tempLeft==0)
									{
										pnTo(direction,-(slWrap.width()-step*dist));
										tempLeft=-(slWrap.width()-step*dist);
									}
									else
									{
										if(Math.abs(tempLeft)<=step*dist)
										{
											pnTo(direction,0);
											tempLeft=0;
										}
										else
										{		
											pnTo(direction,tempLeft+step*dist);
										}
									}
									
						};
						if(pn=='next')
						{
								if(Math.abs(tempLeft)==slWrap.width()-step*dist)
								{
									pnTo(direction,0);
									tempLeft=0;
								}
								else
								{
									if(slWrap.width()-Math.abs(tempLeft)<=2*step*dist)
									{
										pnTo(direction,-(slWrap.width()-step*dist));
										tempLeft=-(slWrap.width()-step*dist);
									}
									else
									{
										
											pnTo(direction,tempLeft-step*dist);
									}
								}
						};
					}//横向
					
					function vPnMotion(pn,dist){
						if(pn=='prev')
						{
									
									if(tempTop==0)
									{
										pnTo(direction,-(slWrap.height()-step*dist));
										tempTop=-(slWrap.height()-step*dist);
									}
									else
									{
										if(Math.abs(tempTop)<=step*dist)
										{
											pnTo(direction,0);
											tempTop=0;
										}
										else
										{
												pnTo(direction,slWrap.position().top+step*dist);
										}
									}
									
						};
						if(pn=='next')
						{
								if(Math.abs(tempTop)==slWrap.height()-step*dist)
								{
									pnTo(direction,0);
									tempTop=0;
								}
								else
								{
									if(slWrap.height()-Math.abs(tempTop)<=2*step*dist)
									{
										pnTo(direction,-(slWrap.height()-step*dist));
										tempTop=-(slWrap.height()-step*dist);
									}
									else
									{
											
											pnTo(direction,slWrap.position().top-step*dist);
									}
								}
						};
					}//纵向
				
					
				function slideTo(n,direct,dist){
						
						i=n;
						slWrap.stop();
						switch (direct)
						{
							case 'horizontal': tempLeft=-dist*n;slWrap.animate({left:'-'+dist*n+'px'},et) 
							break
							case 'vertical': tempTop=-dist*n;slWrap.animate({top:'-'+dist*n+'px'},et)
							default:
							break
						}
						
					}//
				function pnTo(direct,pos){
						slWrap.stop();
						switch (direct)
						{
							case 'horizontal': tempLeft=pos;i=Math.abs(tempLeft/width);slWrap.animate({left:pos+'px'},et); 
							break
							case 'vertical': tempTop=pos;i=tempTop/height;  slWrap.animate({top:pos+'px'},et);
							default:
							break
						}
						
					}//
				
				
				}           
		});
		
	   $.fn.slider.defaults = {
		  slidersWrapper:'.J-sliders-wrapper',//幻灯父容器 必填项
		  slider:'.J-slider',//the sliders 幻灯 必填项
		  paginationWrapper:'.J-pagination-wrapper',//the pagination wrapper 分页按扭父容器
		  pagination:'.J-pagination', //the pagination button 分页按扭
		  prev:'.prev',//pre-slider button 上一张幻灯按扭 可选
		  next:'.next',//next-slider button 下一张按扭 可选
		  direction:'horizontal',//direction of the slider 幻灯方向，可选为:horizontal 或者 vertical
		  time:2000,//滑动时间
		  width:960,//幻灯宽度
		  height:500,//幻灯高度
		  pgactive:true,
		  step:1,
		  autoplay:false,
		  autotime:5000
		  };
	});
}
