function debug(msg){
    //console.debug('++++++++++  '+msg+'  ++++++++++');
}

;(function($) {

    $.fn.extend({
        dsTab : function(opts){
            _this = this;
            this.each(function(index,El){
                new dsTab(opts,_this.eq(index));    
            });

        }
    });

    function dsTab(opts,target){
        debug('new');
        this.opts = {};
        //默认参数
        this.defaults = {
            itemEl          : '.content a',
            btnElName       : 'btns',
            btnItem         : 'li a',
            currentClass    : 'current',        //按钮当前样式
            autoCreateTab   : true,
            btnItemHtml     : '<li class="btn@index"><a href="javascript:void(0)">@index</a></li>',
            index           : 0,
            size            : 3,
            maxSize         : 5,                //最大个数
            action          : 'mouseover',      //默认切换动作
            overStop        : true,
            change          : true,
            changeTime      : 5000              //自动切换时间
        };
        this.tab = target;                  
        this.contentItem = null;            //内容Item
        this.btnItem = null;                //按钮Item
        this.intervalProcess = null;
        this.init(opts);
    }
    //初始化
    dsTab.prototype.init = function(opts){
        debug('init');
        //覆盖默认参数
        this.opts = $.extend({}, this.defaults, opts || {});
        this.contentItem = this.tab.find(this.opts.itemEl);
        //铰接为空时，无点击动作
        this.contentItem.each(function(){
            _this = this.tagName == 'A'?$(this):$(this).parents('a'); 
            if(_this.length){
                var href = _this.attr('href');
                if(typeof(href) == 'undefined' || href == '#'){
                    _this.css('cursor','default').click(function(){return false});
                }    
            }
        });
        //获取个数
        this.opts.size = this.contentItem.size();
        if(this.opts.size <= 1){
            return; //如果个数不大于1个时,不显示按钮
        }
        if(this.opts.maxSize != -1 && this.opts.size > this.opts.maxSize ){
            this.opts.size = this.opts.maxSize;
        }
        //自动生成tab按钮
        if(this.opts.autoCreateTab == true){
            var btnsHtml = '';
            for(var i=1;i<=this.opts.size;i++){
                var li = this.opts.btnItemHtml;
                li = li.replace(/@index/g,i);
                btnsHtml += li;    
            }
            var btns = this.tab.find(' .' +this.opts.btnElName);
            if(btns.length >= 1){
                btns.eq(0).html('<ul>'+btnsHtml+'<ul>');   
            }else{
                this.tab.append('<div class="'+this.opts.btnElName+'"><ul>'+btnsHtml+'</ul></div>');    
            }          
        }
        this.btnItem = this.tab.find('.'+this.opts.btnElName+' '+this.opts.btnItem);
        this.bind(); //绑定操作    
        this.change_handle();
        if(this.opts.change == true){
            this.start();
        }
    }
    //绑定操作            
    dsTab.prototype.bind = function(){
        debug('bind');
        var _this = this;
        //鼠标移上后停止切换动作
        if(this.opts.change == true && this.opts.overStop == true){
            this.tab.hover(function(){
                _this.stop();
            },function(){
                _this.start();
            });
        }
        //切换动作
        this.btnItem.bind(this.opts.action,function(){
            var index = _this.btnItem.index(this);
            //console.debug(index);
            _this.opts.index = parseInt(index);
            _this.change_handle();
            this.blur();
        });
    }
    dsTab.prototype.start = function(){
        var _this = this;
        clearInterval(this.intervalProcess);
        this.intervalProcess = setInterval(function(){
            _this.change_handle();
        },this.opts.changeTime);
        debug('start'+this.intervalProcess);
    }
    dsTab.prototype.stop = function(){
        debug('stop'+this.intervalProcess);
        clearInterval(this.intervalProcess);
    }
    dsTab.prototype.change_handle = function(){
        if (this.opts.index > this.opts.size-1) {
            this.opts.index = 0;
        }
        debug('change_handle__'+this.opts.index);
        this.change(this.opts.index)
        this.opts.index += 1;
    }
    dsTab.prototype.change = function(index){
        this.btnItem.removeClass(this.opts.currentClass);
        this.btnItem.eq(index).addClass(this.opts.currentClass);
        this.contentItem.hide();
        this.contentItem.eq(index).show();
    }
})(jQuery)
