(function($){
    $.fn.kxbdMarquee = function(options){
        var opts = $.extend({},$.fn.kxbdMarquee.defaults, options);
        
        return this.each(function(){
            var $marquee = $(this);//滚动元素容器
            var _scrollObj = $marquee.get(0);//滚动元素容器DOM
            var scrollW = $marquee.width();//滚动元素容器的宽度
            var scrollH = $marquee.height();//滚动元素容器的高度
            var $element = $marquee.find("div.jq_marquee:eq(0)"); //滚动元素
            var $kids = $marquee.find("div.jq_marquee:eq(1)");//滚动子元素
			
            var _type = (opts.direction == 'left' || opts.direction == 'right') ? 1:0;//滚动类型，1左右，0上下
			$kids.html($element.html());
			var tab=$marquee[0];
			var tab1=$element[0];
			var tab2=$kids[0];
            function scrollFunc(){
				if(opts.direction == 'up')
				{
					if(tab2.offsetTop-tab.scrollTop<=0)
					{
						tab.scrollTop-=tab1.offsetHeight;
					}
					else
					{
						tab.scrollTop+=opts.scrollAmount;
					}
				}
				else if(opts.direction == 'left')
				{
					if(tab2.offsetWidth-tab.scrollLeft<=0)
					{
						tab.scrollLeft-=tab1.offsetWidth;
					}
					else
					{
						tab.scrollLeft+=opts.scrollAmount;
					}
				}
				else if(opts.direction == 'down')
				{
					if(tab.scrollTop==0)
					{
						tab.scrollTop+=tab1.offsetHeight;
					}
					else
					{
						tab.scrollTop-=opts.scrollAmount;
					}
					//alert(tab.scrollTop);
				}
				else if(opts.direction == 'right')
				{
					if(tab.scrollLeft==0)
					{
						tab.scrollLeft+=tab1.offsetWidth;
					}
					else
					{
						tab.scrollLeft-=opts.scrollAmount;
					}
				}
            }
            //滚动开始
            var moveId = setInterval(scrollFunc, opts.scrollDelay);
            //鼠标划过停止滚动
            $marquee.hover(
                function(){
                    clearInterval(moveId);
                },
                function(){
                    clearInterval(moveId);
                    moveId = setInterval(scrollFunc, opts.scrollDelay);
                }
            );
            
        });
    };
    $.fn.kxbdMarquee.defaults = {
        direction: 'right',//滚动方向，'left','right','up','down'
        scrollAmount:1,//步长
        scrollDelay:20//时长
    };
    $.fn.kxbdMarquee.setDefaults = function(settings) {
        $.extend( $.fn.kxbdMarquee.defaults, settings );
    };
})(jQuery);
