/**
 * @author Poly 89490364a@gmail.com
 * @version 0.3
 * @projectDescription  简单的分标签
 * 默认会在对象元素内寻找类tBtn与tCon通过点击设置类为current进行显示或隐藏。
 * 也可传入一个对象作为参数进行设置。
 * @param {jQuery Selector} tBtn 作为标签按钮的选择符。
 * @param {jQuery Selector} tCon 作为标签内容的选择符。
 * @param {String} method 作为触发标签选择行为的事件名称,默认为click事件,理论上支持自定义事件。
 * @param {boolean function} reset 在鼠标移出后是否恢复默认状态。如果值是一个函数，则将此函数作为方法调用。
 * @param {function} btnInit 初始化按钮
 * @param {function} conInit 初始化内容块
 * @param {function} btnOn 设置按钮激活行为
 * @param {function} btnOff 设置按钮隐藏行为
 * @param {function} conOn 设置内容激活行为
 * @param {function} conOff 设置内容隐藏行为
 * @param {String function} [filter] 分组函数,通过e.i将按钮和内容区本别分组。
 * @param {int} circle 设置自动循环间隔。
 * @param {boolean} activeDefault 设置是否执行默认动作。
 */

(function($){
	$.fn.tab168=function(o)
	{
		o=o?o:{};
		o.btnOn=typeof(o.btnOn)=="function"?o.btnOn:stdOn;
		o.btnOff=typeof(o.btnOff)=="function"?o.btnOff:stdOff;
		o.conOn=typeof(o.conOn)=="function"?o.conOn:stdOn;
		o.conOff=typeof(o.conOff)=="function"?o.conOff:stdOff;
		o.btnInit=typeof(o.btnInit)=="function"?o.btnInit:null;
		o.conInit=typeof(o.conInit)=="function"?o.conInit:null;
        o.freq=parseInt(o.circle)?parseInt(o.circle):0;
		if(typeof(o.filter)=="string" )
		{eval("o.filter="+o.filter);}
		if(!o.filter)
		{o.filter=single;}
		
		this.each(function(){
            var that=$(this);
			var def;
			that.data("tBtn",$(o.tBtn||".tBtn",this));
			that.data("tCon",$(o.tCon||".tCon",this));

            if(o.freq)
            {
                that.hover(stopTimer,beginTimer)
                    .bind('circle',setCurrent)
                    .trigger('mouseleave');
            }
						
			that.bind("selectChange",changeSelect).bind("reset",reset);

			if(!that.data("tBtn").length)
			{return true;}

			that.data("tBtn").each(function(i){
				var that=$(this);
				that.data("i",i);
				that.bind(o.method||"click",function(e){
					var event=new jQuery.Event("selectChange");
					event.i=i;
					that.trigger(event);
                    if(!o.activeDefault)
                    {
                        e.preventDefault();
                    }
				});
				if(that.hasClass("current"))
				{def=i;}
				if(o.btnInit)
				{
					o.btnInit.apply(this);
				}
			});

			that.data("tBtn").bind("itemOn",o.btnOn).bind("itemOff",o.btnOff);
			that.data("tBtn").bind("itemOff",stopPropagation);
			that.data("tBtn").bind("itemOn",stopPropagation);

			if(that.data("tCon").length)
			{

                that.data("tCon").each(function(i){
                    var that=$(this);
                    that.data("i",i);
                    if(o.conInit)
                    {
                        o.conInit.apply(this);
                    }
                });


                that.data("tCon").bind("itemOn",o.conOn).bind("itemOff",o.conOff);
                that.data("tCon").bind("itemOff",stopPropagation);
                that.data("tCon").bind("itemOn",stopPropagation);
            }

			that.data("def",def?def:0);
			that.trigger("reset");
			if(o.reset)
			{
				that.mouseleave(function(){that.trigger("reset");})
			}
		});
		return this;

		/**
		 * 默认的激活处理程序，设置类为current。
		 * @param {event} e
		 */
		function stdOn(e)
		{
			$(this).addClass("current");
		}
		
		/**
		 * 默认的隐藏处理程序，移除类current。
		 * @param {event} e
		 */
		function stdOff(e)
		{
			$(this).removeClass("current");
		}
		
		/**
		 * 在当前唯一的tab和con上触发itemOn,其余的触发itemOff;
		 * @param {event} e
		 * @return {object} 包含两组对象，itemOn和itemOff
		 */
		function single(e)
		{
			var that=$(this);
			var onBtn=that.data("tBtn").eq(e.i);
			var offBtn=that.data("tBtn").not(onBtn[0]);
            if(that.data("tCon").length)
            {
                var onCon=that.data("tCon").eq(e.i);
                var offCon=that.data("tCon").not(onCon[0]);
                onBtn=onBtn.add(onCon);
                offBtn=offBtn.add(offCon);
            }
			return {itemOn:onBtn,itemOff:offBtn};
		}
		
		/**
		 * 选择改变时的处理函数
		 * @param {event} e
		 */
		function changeSelect(e)
		{
            var that=$(this);
            if(this != e.target)
            {
                that.data('current',$(e.target));
            }
			var filter=o.filter.call(this,e);
			filter.itemOff.trigger("itemOff");
			filter.itemOn.trigger("itemOn");
			e.stopPropagation();
		}
		
		/**
		 * 设置为默认状态
		 */
		function reset(e)
		{
			var that=$(this);
			var event=new jQuery.Event("selectChange");
			event.i=that.data("def");
			$(this).trigger(event);
			if(typeof(o.reset)=="function")
			{
				o.reset.apply(this);
			}
			e.stopPropagation();
		}
		
		/**
		 * 阻止事件冒泡。
		 */
		function stopPropagation(e)
		{
			var that=$(this);
			e.stopPropagation();
		}
            
        //终止计时器
        function stopTimer()
        {
            var that=$(this);
            var timer=that.data("timer");
            if(timer)
            {
                clearTimeout(timer);
            }
        }
        //启动计时器
        function beginTimer()
        {
            var that=$(this);
            var timer=setTimeout(function(){that.trigger("circle")},o.freq||5000);
            that.data("timer",timer);
        }
        //设置当前项
        function setCurrent()
        {
            var that=$(this);
            var tBtn=that.data('tBtn');
            var current=that.data('current')||tBtn.eq(0);//tBtn.filter('.current');//$('.tBtn .current',this);
            var next=current.next();
            next=next.length?next:tBtn.eq(0);
            next.trigger(o.method||'click');
            beginTimer.apply(this);
        }

	}
})(jQuery);

