﻿jQuery.fn.slideView = function(settings) {
    settings = jQuery.extend({
        freshInterval: 500,
        direction: 0,
        duration: 500,
        random: 0
    }, settings);
    return this.each(function() {
        var _variable = settings.variable;
        var _width = 0;
        var _lis = [];
        _lis.push(0);
        var _isLeft = settings.direction == 0;
        var _isRandom = settings.random != 0;
        var _width_max = 0;
        var _cursor = 0;
        var ul = jQuery(this);
        ul.index_li = 0;
        ul.find('li.rc').each(function(i) {
            var me = jQuery(this);
            var _width_me = me.width();
            if (_width_me > _width_max) _width_max = _width_me;
            _cursor++;
            _width = _width + me.width();
            var _height = me.height();
            _lis.push(_width);
            ul.append(me.clone());
        });
        //ul.width = _width;
        ul.width = _width_max * (_cursor)
        ul.css('width', _width_max * _cursor * 2 + 'px');
        ul.find('li.rc').each(function(j) {
            var me = jQuery(this);
            me.css('width', _width_max + 'px');
        });
        jQuery('div.div-rc_' + _variable).css('width', _width_max + 'px');
        ul._lis = _lis;
        var _current = 0;
        var _helper = {
            _index0: [],
            _index1: [],
            getNext: function() {
                var _ret = false;
                if (_isRandom) _ret = _helper._getRandom();
                else _ret = _helper._getOrdered();
                return _ret;
            },
            _getOrdered: function() {
                if (typeof ul.cursor_li == 'undefined') ul.cursor_li = 0;
                else ul.cursor_li++;
                if (ul.cursor_li > ul._lis.length - 1) ul.cursor_li = 1;
                //alert(ul.cursor_li);
                return ul.cursor_li;
                //return ul._lis[ul.cursor_li];
            },
            _getRandom: function() {
                if (_helper._index1.length == 0) {
                    for (var i = 0; i < ul._lis.length; i++) {
                        _helper._index1.push(ul._lis[i]);
                    }
                    _helper._index0 = [];
                }
                var _max = _helper._index1.length - 1;
                var _random = Math.round(_max * Math.random());
                _helper._index0.push(_helper._index1[_random]);
                var _ret = _helper._index1[_random];
                _helper._index1.splice(_random, 1);
                return _random;
                //return _ret;
            },
            animate: function() {
                var _step = -_current;
                if (!_isLeft) _step = _current - ul.width;
                //alert(_step);
                ul.animate(
                    { 'left': _step + 'px',
                        'opacity': '1'
                    },
                    settings.duration,
                    false,
                    function() {
                        if (_current >= ul.width) {
                            if (_isLeft) {
                                ul.css('left', (ul.width - _current) + 'px');
                                _current = _current - ul.width;
                            } else {
                                ul.css('left', -_current + 'px');
                                _current = _current - ul.width;
                            }
                        }
                        var _next = _width_max * _helper.getNext();
                        if (_next < _current) {//if left, must next>curent so -next<-current
                            _next = ul.width + _next;
                        }
                        _current = _next;
                    }
                );
            }
        };

        if (settings.random != 2) _current = _width_max * _helper.getNext();
        if (_isLeft) ul.css('left', -_current + 'px');
        else ul.css('left', -(ul.width + _current) + 'px');
        jQuery(this).at_intervals(function() {
            _helper.animate();
        }, { name: 'slideview', delay: settings.freshInterval });
    });
};