jQuery.fn.extend({
  wowoj_clickScroll: function() {
    var imgs = {
        unl : 'http://www.wowoj.com/service/fengyeye_zjgm_index2/images/btn_scroll_uleft.gif',
        unr : 'http://www.wowoj.com/service/fengyeye_zjgm_index2/images/btn_scroll_uright.gif',
        l : 'http://www.wowoj.com/service/fengyeye_zjgm_index2/images/btn_scroll_left.gif',
        r : 'http://www.wowoj.com/service/fengyeye_zjgm_index2/images/btn_scroll_right.gif'
    };
    
    var contentCount = 5;
    var speed = 8;
    var pause = false;
    var scrolling = false;
    var delayTime = 3000/50;
    var delay = delayTime;
    
    $('.btn-scroll-left',this).attr('src',imgs.l);
    $('.btn-scroll-right',this).attr('src',imgs.r);
    
    var items = $('ul li',this);
    var ulWidth = 0;
    var liWidth = 184;
    items.each(function(i,li){
        ulWidth += liWidth;
    })
    $('ul',this).css('width',ulWidth);
    
    
    
    //滚动项
    $('.btn-scroll-left',this).click(function(e){
        if(!scrolling){scrollLeft()};
    });
    $('.btn-scroll-right',this).click(function(e){
        if(!scrolling){scrollRight()};
    });
    $('ul',this).hover(function(e){
        pause = true;
    },function(){
        pause = false;
    });
        
    var wrapper = this;
    var scrollWrapper = $('.scrollWrapper',this)[0];
    var maxScorll = ulWidth-($(scrollWrapper).width());
    var scrollTimeHandle;
    
    
    window.setInterval(function(){
        if(pause || scrolling){return;}
        
        if(delay > 0){
            delay--;
        }else{
            scrollRight();
        }
    },50);
    
    
    function scrollLeft(callback){
        if(scrollWrapper.scrollLeft === 0){
            $('ul li',wrapper).last().prependTo($('ul',wrapper));
            scrollWrapper.scrollLeft = liWidth;
        }
        
        scrolling = true;
        var scrollWidth = liWidth;
        
        var handle = window.setInterval(function(){
            scrollWrapper.scrollLeft -= speed;
            scrollWidth -= speed;
            if(scrollWidth <= 0){
                window.clearInterval(handle);
                scrolling = false;
                delay = delayTime;
                if(callback){callback();}
            }
        },50);
    }
    
    function scrollRight(callback){
        if(scrollWrapper.scrollLeft >= maxScorll){
            $('ul li',wrapper).first().appendTo($('ul',wrapper));
            scrollWrapper.scrollLeft -= liWidth;
        }
        
        scrolling = true;
        var scrollWidth = liWidth;
        
        var handle = window.setInterval(function(){
            scrollWrapper.scrollLeft += speed;
            scrollWidth -= speed;
            if(scrollWidth <= 0){
                window.clearInterval(handle);
                scrolling = false;
                delay = delayTime;
                if(callback){callback();}
            }
        },50);
    }
  }
});

