//animateGo幻灯插件
//author: atozwordpress.com@gmail.com
//url: http://atozwordpress.com
//AtoZ WordPress , all about WordPress
//插件使用:
//在需要幻灯的DIV上设定形如:
//<div id="indexShow" class="show" direction="right" speed="1000" pause="5000" showNumber="true">
//需要保持幻灯所在DIV的格式如下:
/*
<div>
	<ul>
		<li></li>
	</ul>
</div>
*/
//此插件目前需要结合spark-function.js,有调用其中elemResize(),runOnLoad();

jQuery.fn.extend({	
	animateGo:function(o){
		o=jQuery.extend({
			width:jQuery(this).width() || 500,//动画整体宽度
			height:jQuery(this).height() || 300,//动画整体高度
			direction: jQuery(this).siblings('.direction:last').html() || 'right',//动画方向: up,down,left,right,fade
			speed: parseInt(jQuery(this).siblings('.speed:last').html()) || 1000,//动画速度
			pause: parseInt(jQuery(this).siblings('.pause:last').html()) || 5000,//停顿时长
			extra: jQuery(this).siblings('.extra:last').html() || 'none' //slide extra type: none, number
		}, o || {});
		var step;
		var goid;
		var showNumberUl;
		var showNumberLi;
		var html=jQuery(this).html();
		jQuery(this).html('<div class="show_div">'+html+'</div>');
		var w=o.width;
		var h=o.height;
		var div=jQuery(this).children('.show_div:first');
		var ul=jQuery('ul',div);
		var li=jQuery('li',ul);
		if(li.index(li.filter(':last'))==0){//判断假如只有一个Li就退出不进行动画;
			return;
		}	
		if(o.extra=='number'){
			var showNumber_html='<ul class="show_num">';
			li.each(function(){
				var index=li.index(jQuery(this));
				showNumber_html+='<li><a href="'+jQuery(this).children('a').attr('href')+'">'+(index+1)+'</a></li>';
			});
			showNumber_html+='</ul>';
			jQuery(this).append(showNumber_html);
			showNumberUl=jQuery('.show_num',jQuery(this));
			showNumberLi=jQuery('li',showNumberUl);
			showNumberLi.bind('click',function(){
				jQuery(this).addClass('current');
				var index=showNumberLi.index(jQuery(this));
				li.filter(':eq('+index+')').addClass('ready');
				go();
				return false;
			});
		}	
		_init();
		goid=setInterval(go,o.pause);
		jQuery(this).hover(function(){
			clearInterval(goid);
		},function(){
			goid=setInterval(go,o.pause);
		});
		function go(){
			_classInit();
			switch(o.direction){
				case "left":
					step=w;
					return _animateLeft();
					break;
				case "right":
					step=w;
					return _animateRight();
					break;
				case "up":
					step=h;
					return _animateUp();
					break;
				case "down":
					step=h;
					return _animateDown();
					break;
				case "fade":
					return _animateFade();
				defaut:
					step=h;
					return _animateDown();
					break;
			}
		}
		function _animateUp(){
			li.filter('.ready').css({
				left:0,
				top:h
			});
			li.filter('.current').animate({
				top:-step+'px'
			},o.speed);
			li.filter('.ready').animate({
				left:0,
				top:0
			},o.speed);
		}
		function _animateDown(){
			li.filter('.ready').css({
				left:0,
				top:-h
			});
			li.filter('.current').animate({
				top:step+'px'
			},o.speed);
			li.filter('.ready').animate({
				left:0,
				top:0
			},o.speed);
		}
		function _animateLeft(){
			li.filter('.ready').css({
				left:w,
				top:0
			});
			li.filter('.current').animate({
				left:-step+'px'
			},o.speed);
			li.filter('.ready').animate({
				left:0,
				top:0
			},o.speed);
		}
		function _animateRight(){
			li.filter('.ready').css({
				left:-w,
				top:0
			});
			li.filter('.current').animate({
				left:step+'px'
			},o.speed);
			li.filter('.ready').animate({
				left:0,
				top:0
			},o.speed);
		}
		function _animateFade(){
			li.css({
				'z-index':90
			});
			li.filter('.ready').css({
				left:0,
				top:0,
				'z-index':100,
				opacity:0
			});
			li.filter('.current').css({
				left:0,
				top:0,
				'z-index':99,
				opacity:1
			});
			li.filter('.current').animate({
				opacity:0
			},o.speed);
			li.filter('.ready').animate({
				opacity:1
			},o.speed);
		}
		function _classInit(){
			li.stop(true,true);
			if(!li.hasClass('ready')){
				li.filter('.current:first').next('li').addClass('ready');
			}else{
				li.removeClass('current');
				li.filter('.ready').each(function(){
					var pos=jQuery(this).position();
					if(pos.left==0 && pos.top==0 && jQuery(this).css('z-index')==100 && jQuery(this).css('opacity')==1){
						jQuery(this).addClass('current');
						jQuery(this).removeClass('ready');
					}
				});
			}
			if(!li.hasClass('ready')){
			li.filter('.current:first').next('li').addClass('ready');
			}	
			if(!li.hasClass('ready')){
				li.filter(":first").addClass('ready');
			}
			if(o.extra=='number'){
				var index=li.index(li.filter('.ready'));
				showNumberLi.removeClass('current');
				showNumberLi.filter(':eq('+index+')').addClass('current');
			}	
		}
		function _init(){
			li.css({
				left:w,
				top:h,
				display:'block',
				position:'absolute',
				width:w,
				height:h,
				overflow:'hidden',
				'z-index':100,
				opacity:1
			});
			li.filter(':first').addClass('current');
			li.find('img.showimg').each(function (){
				jQuery(this).elemResize(w,h);
			});
			li.filter('.current').css({
				left:0,
				top:0
			});
			if(o.extra=='number'){
				showNumberLi.filter(':first').addClass('current');
			}
		}
	}
});

//使用class='show'即可自动幻灯;
runOnLoad(function(){
	jQuery('.show').each(function(){
		jQuery(this).animateGo();
	});
});
