//sparkMarquee平滑滚动插件
//author: atozwordpress.com@gmail.com
//url: http://atozwordpress.com
//AtoZ WordPress , all about WordPress
//插件使用:
//在需要幻灯的DIV上设定形如:
//<div id="indexMarquee" class="sparkMarquee" direction="right" speed="30" step="1">
//需要保持滚动所在DIV的格式如下:
/*
<div>
	<ul>
		<li></li>
	</ul>
</div>
*/

jQuery.fn.extend({
	sparkMarquee:function(o){
		o=jQuery.extend({
			width:jQuery(this).width() || 500,//滚动整体宽度
			height:jQuery(this).height() || 300,//滚动整体高度
			direction: jQuery(this).attr('direction') || 'up',//滚动方向: up,down,left,right
			speed: parseInt(jQuery(this).attr('speed')) || 30,//滚动速度
			step: parseInt(jQuery(this).attr('step')) || 1 //滚动步长;
		}, o || {});
		var goid;
		var direction=o.direction;
		var step=o.step;
		var liW=0,liH=0;
		var w=o.width;
		var h=o.height;
		var div=jQuery(this);
		var ul=jQuery('ul:first',div);
		var li=jQuery(ul).children('li');
		var html=ul.html();
		var ulW,ulH;
		if(direction=='right'){
			step=-step;
			direction='left';
		}	
		if(direction=='down'){
			step=-step;
			direction='up';
		}
		if(direction=='left'){
			li.each(function(){
				liW+=jQuery(this).outerWidth(true);
			});
			ul.width(liW);
			if(ul.width<=w){
				return;
			}
		}
		if(direction=='up'){
			li.each(function(){
				liH+=jQuery(this).outerHeight(true);
			});
			ul.height(liH);
			if(ul.height<=h){
				return;
			}
		}
		ul.append(html);
		li=jQuery(ul).children('li');
		_init();
		goid=setInterval(go,o.speed);
		div.hover(function(){
			clearInterval(goid);
		},function(){
			goid=setInterval(go,o.speed);
		});
		function go(){
			switch(o.direction){
				case "left":
					return _marqueeLeft();
					break;
				case "right":
					return _marqueeRight();
					break;
				case "up":
					return _marqueeUp();
					break;
				case "down":
					return _marqueeDown();
					break;
				defaut:
					break;
			}
		}
		function _marqueeLeft(){
			var l=div.scrollLeft();
			if(Math.abs(l)>=ulW/2){
				l=0;
			}
			var sl=l+step;
			div.scrollLeft(sl);
		}
		function _marqueeRight(){
			var l=div.scrollLeft();
			if(Math.abs(l)<=ulW/2-w){
				l=ulW-w;
			}
			var sl=l+step;
			div.scrollLeft(sl);
		}
		function _marqueeUp(){
			var t=div.scrollTop();
			if(Math.abs(t)>=ulH/2){
				t=0
			}
			var st=t+step;
			div.scrollTop(st);
		}
		function _marqueeDown(){
			var t=div.scrollTop();
			if(Math.abs(t)<=ulH/2-h){
				t=ulH-h;
			}
			var st=t+step;
			div.scrollTop(st);
		}
		function _init(){
			if(direction=='left'){
				li.css({
					display:'inline',
					float:'left',
					position:'relative'
				});
				liW=0;
				li.each(function(){
					liW+=jQuery(this).outerWidth();
				});
				ul.width(liW);
				ul.append('<div class="fn"></div>');
			}
			if(direction=='up'){
				liH=0;
				li.each(function(){
					liH+=jQuery(this).outerHeight();
				});
				ul.height(liH);
			}
			ulW=ul.width();
			ulH=ul.height();
			if(o.direction=="right"){
				li.css({
					float:'right'
				});
				div.scrollLeft(ulW-w);
			}
			if(o.direction=="down"){
				li.each(function(){
					ul.prepend(jQuery(this));
				});
				div.scrollTop(ulH-h);
			}
		}
	}
});

//使用class='sparkMarquee'即可自动套用;
jQuery(document).ready(function(){
	jQuery('.sparkMarquee').each(function(){
		jQuery(this).sparkMarquee();
	});
});
