/**
* @classDescription 模拟Marquee，无间断滚动内容
* @author Aken Li(www.kxbd.com)
* @DOM
*      <div id="marquee">
*          <ul>
*               <li></li>
*               <li></li>
*          </ul>
*      </div>
* @CSS
*      #marquee {width:200px;height:50px;overflow:hidden;}
* @Usage
*      $('#marquee').kxbdMarquee(options);
* @options
*    isEqual:true,//所有滚动的元素长宽是否相等,true,false
*      loop: 0,//循环滚动次数，0时无限
*    direction: 'left',//滚动方向，'left','right','up','down'
*    scrollAmount:1,//步长
*    scrollDelay:20//时长
*/
var moveId1;
var $marquee1;
var opts1;
var _scrollObj1;
var scrollSize1;

function scrollFunc1(){
    var _dir = (opts1.direction == 'left' || opts1.direction == 'right') ? 'scrollLeft':'scrollTop';
    if (opts1.loop > 0) {
        numMoved+=opts1.scrollAmount;
        if(numMoved>scrollSize1*opts1.loop){
            _scrollObj1[_dir] = 0;
            return clearInterval(moveId1);
        } 
    }
    if(opts1.direction == 'left' || opts1.direction == 'up'){
        _scrollObj1[_dir] +=opts1.scrollAmount;
        if(_scrollObj1[_dir]>=scrollSize1){
            _scrollObj1[_dir] = 0;
        }
    }else{
        _scrollObj1[_dir] -=opts1.scrollAmount;
        if(_scrollObj1[_dir]<=0){
            _scrollObj1[_dir] = scrollSize1;
        }
    }
}
(function($){

    $.fn.kxbdMarquee1 = function(options){
        opts1 = $.extend({},$.fn.kxbdMarquee1.defaults, options);
        
        return this.each(function(){
            $marquee1 = $(this);//滚动元素容器
            _scrollObj1 = $marquee1.get(0);//滚动元素容器DOM
            var scrollW = $marquee1.width();//滚动元素容器的宽度
            var scrollH = $marquee1.height();//滚动元素容器的高度
            var $element = $marquee1.children(); //滚动元素
            var $kids = $element.children();//滚动子元素
            scrollSize1=0;//滚动元素尺寸
            var _type = (opts1.direction == 'left' || opts1.direction == 'right') ? 1:0;//滚动类型，1左右，0上下
            
            //防止滚动子元素比滚动元素宽而取不到实际滚动子元素宽度
            $element.css(_type?'width':'height',10000);
            //获取滚动元素的尺寸
            if (opts1.isEqual) {
                scrollSize1 = $kids[_type?'outerWidth':'outerHeight']() * $kids.length;
            }else{
                $kids.each(function(){
                    scrollSize1 += $(this)[_type?'outerWidth':'outerHeight']();
                });
            }
            //滚动元素总尺寸小于容器尺寸，不滚动
            if (scrollSize1<(_type?scrollW:scrollH)) return; 
            //克隆滚动子元素将其插入到滚动元素后，并设定滚动元素宽度
            $element.append($kids.clone()).css(_type?'width':'height',scrollSize1*2);
            
            var numMoved = 0;

            //滚动开始
            moveId1 = setInterval(scrollFunc1, opts1.scrollDelay);   
 
            //鼠标划过停止滚动
            $marquee1.hover(        
                function(){                       
                    clearInterval(moveId);
                    clearInterval(moveId1);
                },
                function(){
                    clearInterval(moveId);
                    clearInterval(moveId1);
                    moveId = setInterval(scrollFunc, opts.scrollDelay);
                    moveId1 = setInterval(scrollFunc1, opts.scrollDelay);
                }
            );
            
        });
    };
    $.fn.kxbdMarquee1.defaults = {
        isEqual:true,//所有滚动的元素长宽是否相等,true,false
        loop: 0,//循环滚动次数，0时无限
        direction: 'left',//滚动方向，'left','right','up','down'
        scrollAmount:1,//步长
        scrollDelay:20//时长

    };
    $.fn.kxbdMarquee1.setDefaults = function(settings) {
        $.extend( $.fn.kxbdMarquee1.defaults, settings );
    };
})(jQuery);


