/*
jQWidgets v3.7.1 (2015-Feb)
Copyright (c) 2011-2015 jQWidgets.
License: http://jqwidgets.com/license/
*/
(function(n){n.jqx.dataview.sort=function(){this.sortby=function(t,i,r){var b=Object.prototype.toString,f,u,c,l,y,h,p,v,o,s,w;if(i==null){this.sortdata=null;this.refresh();return}i==undefined&&(i=!0);i=i=="a"||i=="asc"||i=="ascending"||i==!0?!0:!1;f=t;this.sortfield=t;this.sortfielddirection=i?"asc":"desc";this.sortcache==undefined&&(this.sortcache={});this.sortdata=[];u=[];c=!1;f=="constructor"&&(f="");this.virtualmode||this.sortcache[f]==null||(l=this.sortcache[f],u=l._sortdata,l.direction==i?u.reverse():(!l.direction&&i&&u.reverse(),c=!0),u.length<this.totalrecords&&(this.sortcache={},c=!1,u=[]));Object.prototype.toString=typeof t=="function"?t:function(){return this[t]};var e=this.records,k=this.that,a="";if(this.source.datafields&&n.each(this.source.datafields,function(){if(this.name==t)return this.type&&(a=this.type),!1}),u.length==0)if(e.length)for(y=e.length,h=0;h<y;h++)o=e[h],o!=null&&(s=o,p=s.toString(),u.push({sortkey:p,value:s,index:h}));else{v=!1;for(obj in e){if(o=e[obj],o==undefined){v=!0;break}s=o;u.push({sortkey:s.toString(),value:s,index:obj})}v&&n.each(e,function(n,t){u.push({sortkey:t.toString(),value:t,index:n})})}c||(r==null?(this._sortcolumntype=a,w=this,u.sort(function(n,t){return w._compare(n,t,a)})):u.sort(r));i||u.reverse();Object.prototype.toString=b;this.sortdata=u;this.sortcache[f]={_sortdata:u,direction:i};this.reload(this.records,this.rows,this.filters,this.updated,!0)};this.clearsortdata=function(){this.sortcache={};this.sortdata=null};this._compare=function(t,i,r){var t=t.sortkey,i=i.sortkey,f;if(t===undefined&&(t=null),i===undefined&&(i=null),t===null&&i===null)return 0;if(t===null&&i!==null)return-1;if(t!==null&&i===null)return 1;if(n.jqx.dataFormat)if(r&&r!="")switch(r){case"number":case"int":case"float":return t<i?-1:t>i?1:0;case"date":case"time":return t<i?-1:t>i?1:0;case"string":case"text":t=String(t).toLowerCase();i=String(i).toLowerCase()}else{if(n.jqx.dataFormat.isNumber(t)&&n.jqx.dataFormat.isNumber(i)||n.jqx.dataFormat.isDate(t)&&n.jqx.dataFormat.isDate(i))return t<i?-1:t>i?1:0;n.jqx.dataFormat.isNumber(t)||n.jqx.dataFormat.isNumber(i)||(t=String(t).toLowerCase(),i=String(i).toLowerCase())}try{if(t<i)return-1;if(t>i)return 1}catch(u){f=u}return 0};this._equals=function(n,t){return this._compare(n,t)===0}};n.extend(n.jqx._jqxGrid.prototype,{_rendersortcolumn:function(){var r=this.that,t=this.getsortcolumn(),i;this.sortdirection&&(i=function(t,i){var u=r.getcolumn(t);u&&(i.ascending?n.jqx.aria(u.element,"aria-sort","ascending"):i.descending?n.jqx.aria(u.element,"aria-sort","descending"):n.jqx.aria(u.element,"aria-sort","none"))},this._oldsortinfo&&this._oldsortinfo.column&&i(this._oldsortinfo.column,{ascending:!1,descending:!1}),i(t,this.sortdirection));this._oldsortinfo={column:t,direction:this.sortdirection};this.sortdirection&&n.each(this.columns.records,function(){var i=n.data(document.body,"groupsortelements"+this.displayfield);t==null||this.displayfield!=t?(n(this.sortasc).hide(),n(this.sortdesc).hide(),i!=null&&(i.sortasc.hide(),i.sortdesc.hide())):r.sortdirection.ascending?(n(this.sortasc).show(),n(this.sortdesc).hide(),i!=null&&(i.sortasc.show(),i.sortdesc.hide())):(n(this.sortasc).hide(),n(this.sortdesc).show(),i!=null&&(i.sortasc.hide(),i.sortdesc.show()))})},getsortcolumn:function(){return this.sortcolumn!=undefined?this.sortcolumn:null},removesort:function(){this.sortby(null)},sortby:function(n,t,i,r,u){if(this._loading&&u!==!1)throw new Error("jqxGrid: "+this.loadingerrormessage);if(n==null&&(t=null,n=this.sortcolumn),n!=undefined){var f=this.that;if(i==undefined&&f.source.sortcomparer!=null&&(i=f.source.sortcomparer),ascending=t=="a"||t=="asc"||t=="ascending"||t==!0?!0:!1,f.sortdirection=t!=null?{ascending:ascending,descending:!ascending}:{ascending:!1,descending:!1},f.sortcolumn=t!=null?n:null,f.source.sort||f.virtualmode){if(f.dataview.sortfield=n,f.dataview.sortfielddirection=t==null?"":ascending?"asc":"desc",f.source.sort&&!this._loading){f.source.sort(n,t);f._raiseEvent(6,{sortinformation:f.getsortinformation()});return}}else f.dataview.sortby(n,t,i);if(r===!1)return;f.groupable&&f.groups.length>0?(f._render(!0,!1,!1),f._updategroupheadersbounds&&f.showgroupsheader&&f._updategroupheadersbounds()):(f.pageable&&f.dataview.updateview(),f._updaterowsproperties(),f.rendergridcontent(!0));f._raiseEvent(6,{sortinformation:f.getsortinformation()})}},_togglesort:function(n){var i=this.that,r,t;this.disabled||n.sortable&&i.sortable&&(r=i.getsortinformation(),t=null,r.sortcolumn!=null&&r.sortcolumn==n.displayfield?(t=r.sortdirection.ascending,t=i.sorttogglestates>1?t==!0?!1:null:!t):t=!0,i.sortby(n.displayfield,t,null))}})})(jqxBaseFramework)