SQ.Lottery = new SQ.Class();

SQ.Lottery.include({
    init: function (options) {
        this.lottery_items = $(options.elem + ' ' + options.item);
        $.extend(this,{
            lowerTimer:1,
            lowerSpeed:1,
            speed:1
        },options,{
            stopItem:-1
        });
    },
    run:function(){
        var that = this;
        if(that.currentItem.index() === that.stopItem){
            setTimeout(function(){
                that.stopCallback();
            },200);
            that.speed = 1;
            clearTimeout(that.t);
            clearInterval(that.lowerSpeedInterval);
            that.stopItem = -1;
            return;
        }
        that.t = setTimeout(function () {
            if (that.prevItem && that.prevItem.size() > 0) {
                $(that.highLight, that.prevItem).removeClass(that.focus);
            }
            $(that.highLight, that.currentItem).addClass(that.focus);
            that.prevItem = that.currentItem;
            if(that.currentItem.next().size()>0){
                that.currentItem = that.currentItem.next();
            }else{
                that.currentItem = that.lottery_items.eq(0);
            }
            that.t = that.run();
        }, that.interval/that.speed);
    },
    start: function () {
        var that = this;
        var itemLength = that.lottery_items.size();
        if(that.playSecond){
            that.run();
            return
        }else{
            that.playSecond = true;
        }
        if(that.prevItem&&that.prevItem.size()>0){
            $(that.highLight, that.prevItem).removeClass(that.focus);
        }
        that.currentItem = that.lottery_items.eq(0);
        that.prevItem = null;
        that.run();
    },
    _continue:function(){
        var that = this;
        var itemLength = that.lottery_items.size();
        that.run();
    },
    setStopItem:function(index){
        var that = this;
        /*设置停止时间*/
        setTimeout(function(){
            that.stopItem = index;
        },that.stopTimer*1000);

        /*设置降速时间*/
        setTimeout(function(){
            that.lowerSpeedInterval = setInterval(function(){
                that.speed =that.speed - (that.speed-that.lowerSpeed)/((that.stopTimer-that.lowerTimer)*100);
            },10);
        },that.lowerTimer*1000);
    },
    stopCallback:function(){
    },
    //底层初始化函数,自动在init之前执行
    initializer: function () {

    }
});