// JavaScript Document
/*
 *
 *
 *
 *
 *
 */




    

var konaRoll = function( selector, options, controlOptions ) {
    
    var _this = {};
    
    var _selector = $( selector ),
        
        _container, _innerContainer,
        
        _containerWidth, _containerHeight,
        
        _minItemNum,
        
        _item, _itemWidth, _itemHeight, _itemLength,
        
        _axis, _direction,//_axis:{ 0: horizontal, 1: vertical }, _direction:{ 0: prev, 1: next }
        
        _rollInterval, _distance, _scroll=0,
        
        _scrollMode, _distanceMode, _itemDistanceMode,
        
        _options = {
            
            direction: 'left',// shortcut: d
            
            auto: 1,// shortcut: a
            
            item: 'auto',
            
            itemDisplay: 'block',//未使用
            
            minItemNum: 1,
            
            speed: 1000,// shortcut: s
            
            delay: 4000,// shortcut: de
            
            row: 'auto',// shortcut: r
            
            containerWidth: 'auto',
            
            step: 1,
            
            px: 0,
            
            easing: 'swing',
            
            mouseOverStop: 1
            
        },
        _controlOptions = {
            
            prevId: null,
            
            nextId: null,
            
            speed: 1000,
            
            delay: 4000,
            
            step: 1,
            
            event: 'mouseup',
            
            px: 0,
            
            easing: 'swing',
            
            directionFollow: 0
            
        };
    
    
    
    
    _options = $.extend( true, {}, _options, options );
    
    initOptionsShortcut();
    

    _item = getItem();
    
    _itemWidth = _item.outerWidth(1);
    
    _itemHeight = _item.outerHeight(1);
    
    _itemLength = _item.size();
    
    //container宽度高度初始化
    _containerWidth = _selector.width();
    
    _containerHeight = _selector.height();
    
    _minItemNum = getMinItemNum();
    
    if ( _itemLength < _minItemNum ) {
        return;
    };
    
    initInfo();
    
    buildDom();
    
    autoRoll();
    
    mouseOverStop();
    
    
    initControlOptions();
    
    initRollControl();
    
    
    function initOptionsShortcut() {
        
        _options.auto = _options.a != null ? _options.a : _options.auto;
        
        _options.direction = _options.d != null ? _options.d : _options.direction;
        
        _options.speed = _options.s != null ? _options.s : _options.speed;
        
        _options.delay = _options.de != null ? _options.de : _options.delay;
        
        _options.row = _options.r != null ? _options.r : _options.row;
        
    }
    
    function getItem() {
        
        var itemId, item;
        
        itemId = _options.item;
        
        item = _selector.children();
        
        if ( itemId == 'auto' && item[0] && ( item[0].nodeName == 'UL' || item[0].nodeName == 'DL' ) ) {
            
            item = item.children();
            
            
        }
        
        return item;
        
    }
    
    function getMinItemNum() {
        
        return _options.minItemNum;
        
    }//wait
    
    function initInfo() {
        
        var direction = _options.direction;
        
        if ( direction == 'left' || direction == '3' ) {
            
            _axis = 0;
            
            _direction = 1;
            
        } else if ( direction == 'up' || direction == '0' ) {
            
            _axis = 1;
            
            _direction = 1;
            
        } else if ( direction == 'right' || direction == '1' ) {
            
            _axis = 0;
            
            _direction = 0;
            
        } else if ( direction == 'down' || direction == '2' ) {
            
            _axis = 1;
            
            _direction = 0;
            
        } else {
            
            _axis = 0;
            
            _direction = 1;
            
        };
        
        
        
        if ( _axis ) {
            
            _scrollMode = 'scrollTop';
            
            _distanceMode = 'height';
            
            _itemDistanceMode = _itemHeight;
            
        } else {
            
            _scrollMode = 'scrollLeft';
            
            _distanceMode = 'width';
            
            _itemDistanceMode = _itemWidth;
            
        }
        
    }
    
    function buildDom() {
        
        _selector.wrapInner('<div></div>').wrapInner('<div></div>');
        
        _container = _selector.children('div');
        
        _innerContainer = _container.children('div');
        
        _container.css({
            width: _containerWidth,
            height: _containerHeight,
            overflow: 'hidden'
        });
        
        setInnerContainerCss();
        initInnerContainerContext();
        
        _container.scrollTop(0).scrollLeft(0);
        
        function setInnerContainerCss() {
            
            var row = _options.row, width;
            
            
            if ( _options.containerWidth != 'auto' ) {
                
                width = parseInt( _options.containerWidth );
                
            } else if ( _axis ) {
                
                if ( row == 'auto' ) {
                    
                    row = Math.ceil( _containerWidth / _itemWidth );
                    
                };
                
                width = _itemWidth * row;
                
                
            } else {
                
                if ( row == 'auto' ) {
                    
                    row = Math.ceil( _containerHeight / _itemHeight );
                    
                };
                
                width = Math.ceil( _itemLength / row ) * _itemWidth;
                
            };
            
            _innerContainer.css({
                overflow: 'hidden',
                width: width,
                height: function(){ return $(this).height(); }
            });
            
            
            
            
        }
        
        function initInnerContainerContext() {
            
            var display = _options.itemDisplay,
                itemId = _options.item,
                itemSelector;
            
            if ( display == 'block' ) {
                
                _innerContainer.children().wrapAll($('<div></div>'));
                
                _innerContainer.children().css({
                    width: _innerContainer.width(),
                    height: _innerContainer.height(),
                    float: 'left',
                    overflow: 'hidden'
                })
            };
            
            if ( _axis ) {
                _innerContainer.height( _innerContainer.height() * 3 );
            } else {
                _innerContainer.width( _innerContainer.width() * 3 );
            };
            
            itemSelector = _innerContainer.children();
            
            if ( itemId.toUpperCase() != 'UL' && itemId.toUpperCase() != 'DL' && ( itemSelector[0].nodeName == 'UL' || itemSelector[0].nodeName == 'DL' ) ) {
                itemSelector = itemSelector.children();
            }
            
            itemSelector.clone().appendTo(itemSelector.parent()).clone().appendTo(itemSelector.parent());
            
        }
        
    }
    
    function autoRoll() {
        
        if ( _options.auto ) {
            
            clearInterval( _rollInterval );
            
            _rollInterval = setInterval( function() {
                
                roll();
                
            }, _options.speed + _options.delay );
            
        }
        
    }
    
    function mouseOverStop() {
        
        if ( _options.mouseOverStop ) {
            
            _selector.mouseover( function() {
                
                clearInterval( _rollInterval );
                
            } ).mouseout( function() {
                
                autoRoll();
                
            } );
            
        }
        
    }
    
    function roll(d, i) {
        
        var params = {}, speed, easing;
        
        speed = _options.speed;
        
        easing = _options.easing;
        
        callback = noop;
        
        
        
        if (typeof i == 'number' && i == 1) {
            
            speed = _controlOptions.speed;
            
            easing = _controlOptions.easing;
            
            params[_scrollMode] = countScroll(d, i);
            
            if (_controlOptions.event == 'mouseup') {
                callback = autoRoll;
            }
            
        } else {
            
            params[_scrollMode] = countScroll();
            
        }
        
        _container.stop().animate( params, speed, easing, callback );
        
    }
    
    function countScroll(d, i) {
        
        var step = _options.step,
            distance,
            px = _options.px,
            direction = _direction;
        
        if (typeof i == 'number' && i == 1) {
            
            direction = d;
            step = _controlOptions.step;
            px = _controlOptions.px;
            
        }
        
        distance = _itemDistanceMode * step;
        
        if ( px != 0 && typeof px == 'number' ) {
            
            distance = px;
            
        }
        
        
        if ( direction ) {
            
            _scroll += distance;
            
            if ( _scroll > _innerContainer[_distanceMode]() -  _container[_distanceMode]() ) {
                
                _container[_scrollMode]( _container[_scrollMode]() - _innerContainer[_distanceMode]() / 3 );
                
                _scroll = _scroll - _innerContainer[_distanceMode]() / 3;
                
            }
            
        } else {
            
            _scroll -= distance;
            
            if ( _scroll < 0 ) {
                
                _container[_scrollMode]( _container[_scrollMode]() + _innerContainer[_distanceMode]() / 3 );
                
                _scroll = _scroll + _innerContainer[_distanceMode]() / 3;
                
            }
            
        }
        
        return _scroll;
        
    }
    
    function initControlOptions() {
        
        _controlOptions = $.extend( true, {}, _controlOptions, controlOptions );
        
        if (_controlOptions.speed == null) {
            _controlOptions.speed = _options.speed;
        }
        
        if (_controlOptions.delay == null) {
            _controlOptions.delay = _options.delay;
        }
        
    }
    
    function initRollControl() {
        
        if ( _controlOptions.prevId == null && _controlOptions.nextId == null ) {
            return;
        }
        
        var startEvent, endEvent;
        
        startEvent = _controlOptions.event;
        
        if (startEvent == 'mousedown') {
            endEvent = 'mouseup';
        }
        
        if (startEvent == 'mouseover') {
            endEvent = 'mouseout';
        }
        
        
        if (_controlOptions.prevId != null ) {
            $(_controlOptions.prevId).bind(startEvent, function(){
                clearInterval( _rollInterval );
                if (_controlOptions.directionFollow == 1) {
                    _direction = 0;
                }
                if (startEvent == 'mousedown' || startEvent == 'mouseover') {
                    _rollInterval = setInterval(function(){
                        roll(0, 1);
                    }, _controlOptions.speed + _controlOptions.delay);
                } else {
                    roll(0, 1);
                }
            });
            
            if (endEvent) {
                $(_controlOptions.prevId).bind(endEvent, function(){
                    autoRoll();
                });
                if (endEvent == 'mouseup') {
                    $(_controlOptions.prevId).bind('mouseout', function(){
                        autoRoll();
                    });
                }
            }
        }
        
        if (_controlOptions.nextId != null ) {
            $(_controlOptions.nextId).bind(startEvent, function(){
                clearInterval( _rollInterval );
                if (_controlOptions.directionFollow == 1) {
                    _direction = 1;
                }
                if (startEvent == 'mousedown' || startEvent == 'mouseover') {
                    _rollInterval = setInterval(function(){
                        roll(1, 1);
                    }, _controlOptions.speed + _controlOptions.delay);
                } else {
                    roll(1, 1);
                }
            });
            
            if (endEvent) {
                $(_controlOptions.nextId).bind(endEvent, function(){
                    autoRoll();
                });
                if (endEvent == 'mouseup') {
                    $(_controlOptions.nextId).bind('mouseout', function(){
                        autoRoll();
                    });
                }
            }
        }
        
    }
    
    function noop()
    {
        
    }
    
    
}



