var Tab=function(t){var e=function(t){this.conf=this._extends({bonds:[],selected:"tab_selected",auto:false,startOn:0,trigger:"mouseover",touchDir:"h",customSelect:null,onRender:function(){},onBefore:function(){},onAfter:function(){}},t);this._initDoms();this._initStates();this._initEvents();this._launch()};e.prototype={constructor:e,_id:function(t){return document.getElementById(t)},_class:function(t,e,s){var a=[],n,o,r;if(e==null)e=document.body;if(s==null)s="*";if(e.getElementsByClassName){return e.getElementsByClassName(t)}if(e.querySelectorAll){return e.querySelectorAll("."+t)}n=e.getElementsByTagName(s);o=n.length;r=new RegExp("(^|\\s)"+t+"(\\s|$)");for(i=0,j=0;i<o;i++){if(r.test(n[i].className)){a[j]=n[i];j++}}return a},_extends:function(t,e){for(property in e){t[property]=e[property]}return t},_addEvent:function(t,e,s){t.addEventListener?t.addEventListener(e,s,false):t.attachEvent("on"+e,s)},_hasClass:function(t,e){var s=new RegExp("(^| )"+e+"( |$)");return s.test(t.className)},_removeClass:function(t,e){var s=new RegExp("(^| )"+e+"( |$)");if(e){t.className=t.className.replace(s," ").replace(/^\s+|\s+$/g,"")}else{t.className=""}},_addClass:function(t,e){if(!this._hasClass(t,e)){t.className+=" "+e}},_initDoms:function(){var t=this,e=this.conf,s=e.bonds.length,a=[],n=[];for(var i=0;i<s;i++){var o=e.bonds[i];if(o.length<2){throw new Error("参数长度不匹配")}a.push(typeof o[0]==="string"?this._id(o[0]):o[0]);n.push(typeof o[1]==="string"?this._id(o[1]):o[1])}this.doms={tabs:a,conts:n}},_initStates:function(){var t=this,e=this.conf,s=this.doms,a,n,i,o,r,l;n=s.tabs.length;o="ontouchstart"in window?e.touchDir:false;this.states={curIdx:0,total:n,auto:e.auto,order:null,enable:true,touchDir:o}},_initEvents:function(){var t=this,e=this.conf,s=this.doms,a=this.states,n,i,o=false,r=false,l,c,u;for(var h=0;h<a.total;h++){if(s.tabs[h].getAttribute("data-tabed")){continue}this._addEvent(s.tabs[h],e.trigger,function(e){return function(s){if(!a.enable){return}t.select(e)}}(h));if(a.touchDir){n=a.touchDir=="v"?"pageY":"pageX";i=a.touchDir=="v"?"pageX":"pageY";this._addEvent(s.conts[h],"touchstart",f);this._addEvent(s.conts[h],"touchmove",d);this._addEvent(s.conts[h],"touchend",v)}s.tabs[h].setAttribute("data-tabed",h)}function f(e){if(e.touches.length!==1){return}o=true;t._autoPlay&&clearInterval(t._autoPlay);startPos=e.touches[0][n];c=e.touches[0][i]}function d(e){var s,o,l;if(e.touches.length!==1||r){return}s=e.touches[0][n]-startPos;if(!u){o=e.touches[0][i]-c;if(Math.abs(s)>Math.abs(o)*10){u=true}}u&&e.preventDefault();if(Math.abs(s)>50){if(s>0){l=a.curIdx-1<0?a.total-1:a.curIdx-1}else{l=a.curIdx+1==a.total?0:a.curIdx+1}t.select(l);r=true}}function v(e){startPos=c=null;u=false;r=o=false;a.auto&&t.autoPlay(a.auto)}},_launch:function(){var t=this.conf,e=this.doms,s=this.states;if(!isNaN(t.startOn)){this.select(t.startOn)}t.onRender&&t.onRender.call(this);if(t.auto){this.autoPlay(t.auto)}},add:function(t,e){var s=this.conf,a=this.doms,n=this.states;if(typeof t=="string"){t=this._id(t)}if(typeof e=="string"){e=this._id(e)}a.tabs.push(t);a.conts.push(e);n.total++;this._initEvents()},order:function(t){},prev:function(){var t=this,e=this.conf,s=this.doms,a=this.states;if(a.curIdx==0){this.select(a.total-1)}else{this.select(a.curIdx-1)}},next:function(){var t=this,e=this.conf,s=this.doms,a=this.states;if(a.curIdx==a.total-1){this.select(0)}else{this.select(a.curIdx+1)}},select:function(t,e){var s=this,a=this.conf,n=this.doms,i=this.states;if(t==i.curIdx){return}if(!i.enable){return}if(t<0||t>i.total-1){throw new Error("所切换的选项卡不存在");return}this._selectTimeout&&clearTimeout(this._selectTimeout);this._selectTimeout=setTimeout(function(){a.onBefore&&a.onBefore.call(s,t);s._autoPlay&&clearInterval(s._autoPlay);if(a.customSelect&&typeof a.customSelect==="function"){for(var o=0;o<i.total;o++){if(t==o){s._addClass(n.tabs[t],a.selected)}else{s._removeClass(n.tabs[o],a.selected)}}a.customSelect.call(s,t)}else{for(var o=0;o<i.total;o++){if(t==o){s._addClass(n.tabs[t],a.selected);n.conts[t].style.display=""}else{s._removeClass(n.tabs[o],a.selected);n.conts[o].style.display="none"}}}i.curIdx=t;i.auto&&s.autoPlay(i.auto);a.onAfter&&a.onAfter.call(s,t);e&&e.call(s)},50)},autoPlay:function(t){var e=this,s=this.conf,a=this.doms,n=this.states,i=t?isNaN(t)?5e3:t*1e3:false;this._autoPlay&&clearInterval(this._autoPlay);if(i){this._autoPlay=setInterval(function(){if(n.curIdx==n.total-1){e.select(0)}else{e.select(n.curIdx+1)}},i)}n.auto=t},enable:function(t){this.states.enable=!!t}};return e}();