/**
 * @author Poly 89490364a@gmail.com
 * @version 0.1
 * @projectDescription 基于文档流的简单走马灯
 * 建议使用强制不换行的inline-block布局代替float以取得最简单的布局。
 * 为了保证chrome和safari的兼容性，请显式设置子元素的宽度。
 * 可传入一个对象作为参数。
 * @param {int} [speed] 速度。
 * @param {int} [len] 启动走马灯的子元素个数，默认为2。
 */
(function($){
	$.fn.simpleScroll=function(o)
	{
		o=o?o:{}
		this.each(function(){
			var that=$(this);
			var e=new $.Event("doScroll");
			e.parent=that;
			var child=that.children();
			if((o.len||2)<=child.length)
			{
				that.bind("resetHead",resetHead).hover(pause,goon);
				child.each(initChildrren).eq(0).trigger(e);
			}
		});
		function initChildrren()
		{
			var that=$(this).bind("doScroll",doScroll);
			this.len=that.outerWidth(true);
			this.parent=that.parent();
		}
		function doScroll(e)
		{
			var that=$(this);
			if(this.len+parseInt(that.css("margin-left"))<0)
			{
				var event=new $.Event("resetHead");
				event.parent=e.parent;
				event.current=that;
				e.parent.trigger(event);
			}
			else
			{
				that.animate({marginLeft:"-=1px"},{duration:0});
				e.parent[0].timer=setTimeout(function(){that.trigger(e)},o.speed||20)
			}
		}
		function resetHead(e)
		{
			e.type="doScroll";
			e.parent.append(e.current.css({marginLeft:"0px"})).children(":first").trigger(e);
		}
		function pause()
		{
			clearTimeout(this.timer)
		}
		function goon()
		{
			var that=$(this);
			var e=$.Event("doScroll");
			e.parent=that;
			that.children(":first").trigger(e);
		}
		return this;
	}
})(jQuery);
