(function(){"use strict";u8.Base.TabsModel=function(e){if(u8.tools.isInstanceOfClass(e,"TabsModel")){return e}$.extend(this,{_selectedIndex:-1},e);if(!this.items){this.items=[]}this.bind(e);var t=this.selectedIndex;if(typeof t=="undefined"){t=this.indexWhere(u8._.attr("selected"));if(t>=0){this.setSelectedIndex(t)}else if(this.autoSelect&&this.items.length){this.setSelectedItem("$first")}}else{this.setSelectedIndex(t)}};u8.Base.TabsModel.makeClass({name:"TabsModel",events:["Update","PreRemove","Remove","PreSelectionChange","SelectionChange"]});$.extend(u8.Base.TabsModel.prototype,{dispose:function(){if(this._isDisposed){return}this.unbind();this._isDisposed=true},add:function(e,t){if(!e){return this}if(typeof t=="undefined"||t>=this.items.length){t=this.items.length;this.items.push(e)}else{this.items.splice(t,0,e)}if(this._selectedIndex>=t){this._selectedIndex++}this._onUpdate({reason:"add",item:e,index:t});if(e.selected){this.setSelectedIndex(t)}else if(this.getSelectedIndex()>=t){this.setSelectedIndex(this.getSelectedIndex())}return this},removeAt:function(t,i){var s=this.at(t);if(!s){return this}this.raiseAsync("PreRemove",{index:t,item:s,cancel:false},function(n,d){if(d.cancel){return i&&i(this,d)}this.items.splice(t,1);d.item=s;this._onUpdate({reason:"remove",item:s});this.raise("Remove",d);var r=this.getSelectedIndex();if(t==r){if(this.autoSelect){if(t>this.indexWhere(e)){t=this.lastIndexWhere(e,t-1)}else{t=this.indexWhere(e,t)}}else{t=-1}this.setSelectedIndex(t,{force:true,allowCancel:false})}else if(t<r){this.setSelectedIndex(r-1)}if(i){i(this,{})}}.bind(this));return this},moveTo:function(e,t){if(t<0||t>=this.items.length){t=this.items.length-1}var i=this.indexOf(e);if(i<0){return}if(t==i){return}var s=this._selectedIndex==i;this.items.splice(i,1);this.items.splice(t,0,e);if(s){this._selectedIndex=t}this.raise("Update",{reason:"move",item:e,oldIndex:i,newIndex:t})},isEmpty:function(){return!(this.items&&this.item.length)},getSelectedItem:function(){return this.at(this.getSelectedIndex())},setSelectedItem:function(t,i){var s;if(t=="$last"){s=this.lastIndexWhere(e);return s>=0?this.setSelectedIndex(s,i):this}if(t=="$first"){s=this.indexWhere(e);return s>=0?this.setSelectedIndex(s,i):this}if(t=="$prev"){if(this._selectedIndex<=0){return this}s=this.lastIndexWhere(e,this._selectedIndex-1);if(s>=0){return this.setSelectedIndex(s,i)}return this}if(t=="$next"){s=this.indexWhere(e,this._selectedIndex+1);if(s>=0){return this.setSelectedIndex(s,i)}return this}if(t=="$nearest"){var n=Math.max(this._selectedIndex,-1);s=this.indexWhere(e,n+1);if(s>=0){return this.setSelectedIndex(s,i)}s=this.lastIndexWhere(e,n);if(s>=0){return this.setSelectedIndex(s,i)}this.setSelectedIndex(-1);return this}s=this.items.indexOf(t);return this.setSelectedIndex(s,i)},getSelectedIndex:function(){return this._selectedIndex},setSelectedIndex:function(e,t){if(typeof e=="function"){e=this.indexWhere(e)}else{e=Math.max(Number(e),-1)}if(isNaN(e)||e>=this.items.length){this.setSelectedItem("$first");u8.log.warn("Cannot select tab with index: "+e.toString()+"!");return undefined}var i=t&&t.force;if(!i&&this._selectedIndex===e){return this}var s=this.getSelectedItem();var n=$.extend({},t&&t.args,{index:e,item:s,cancel:false});this.raise("PreSelectionChange",n);if(n.cancel&&(!t||t.allowCancel!==false)){return undefined}if(s){s.selected=false}var d={reason:"selection",deselected:s};s=this.at(e);if(s){s.selected=true;d.selected=s}this._selectedIndex=e;this.raise("Update",d).raise("SelectionChange",n);return this},at:function(e){return this.items[e]},indexOf:function(e){return this.items.indexOf(e)},clear:function(){this.items=[];this._onUpdate()},update:function(e){this._onUpdate({index:e})},lastIndexWhere:function(e,t){if(typeof t=="undefined"){t=this.items.length-1}for(var i=t;i>=0;i--){var s=this.items[i];if(e(s)){return i}}return-1},indexWhere:function(e,t){if(typeof t=="undefined"){t=0}for(var i=t;i<this.items.length;i++){var s=this.items[i];if(e(s)){return i}}return-1},findWhere:function(e,t){if(typeof t=="undefined"){t=0}for(var i=t;i<this.items.length;i++){var s=this.items[i];if(e(s)){return s}}return undefined},_onUpdate:function(e){this.raise("Update",e||{})}});function e(e){return e.visible!==false}})();