/*
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.com/yui/license.html
version: 3.3.0
build: 3167
*/
YUI.add("datatable-sort",function(G){var F=G.ClassNameManager.getClassName,H="datatable",B="column",D="asc",C="desc",A='<a class="{link_class}" title="{link_title}" href="{link_href}">{value}</a>';function E(){E.superclass.constructor.apply(this,arguments);}G.mix(E,{NS:"sort",NAME:"dataTableSort",ATTRS:{trigger:{value:{event:"click",selector:"th"},writeOnce:"initOnly"},lastSortedBy:{setter:"_setLastSortedBy",lazyAdd:false},template:{value:A}}});G.extend(E,G.Plugin.Base,{initializer:function(J){var K=this.get("host"),I=this.get("trigger");K.get("recordset").plug(G.Plugin.RecordsetSort,{dt:K});K.get("recordset").sort.addTarget(K);this.doBefore("_createTheadThNode",this._beforeCreateTheadThNode);this.doBefore("_attachTheadThNode",this._beforeAttachTheadThNode);this.doBefore("_attachTbodyTdNode",this._beforeAttachTbodyTdNode);K.delegate(I.event,G.bind(this._onEventSortColumn,this),I.selector);K.after("recordsetSort:sort",function(){this._uiSetRecordset(this.get("recordset"));});this.on("lastSortedByChange",function(L){this._uiSetLastSortedBy(L.prevVal,L.newVal,K);});if(K.get("rendered")){K._uiSetColumnset(K.get("columnset"));this._uiSetLastSortedBy(null,this.get("lastSortedBy"),K);}},_setLastSortedBy:function(I){if(G.Lang.isString(I)){return{key:I,dir:"asc",notdir:"desc"};}else{if(I&&I.key){if(I.dir==="desc"){return{key:I.key,dir:"desc",notdir:"asc"};}else{return{key:I.key,dir:"asc",notdir:"desc"};}}else{return null;}}},_uiSetLastSortedBy:function(M,J,I){var U=M&&M.key,N=M&&M.dir,T=J&&J.key,K=J&&J.dir,Q=I.get("columnset"),S=Q.keyHash[U],O=Q.keyHash[T],R=I._tbodyNode,L,P;if(S){S.thNode.removeClass(F(H,N));L=R.all("."+F(B,S.get("id")));L.removeClass(F(H,N));}if(O){O.thNode.addClass(F(H,K));P=R.all("."+F(B,O.get("id")));P.addClass(F(H,K));}},_beforeCreateTheadThNode:function(I){if(I.column.get("sortable")){I.value=G.substitute(this.get("template"),{link_class:I.link_class||"",link_title:"title",link_href:"#",value:I.value});}},_beforeAttachTheadThNode:function(M){var L=this.get("lastSortedBy"),K=L&&L.key,I=L&&L.dir,J=L&&L.notdir;if(M.column.get("sortable")){M.th.addClass(F(H,"sortable"));}if(K&&(K===M.column.get("key"))){M.th.replaceClass(F(H,J),F(H,I));}},_beforeAttachTbodyTdNode:function(M){var L=this.get("lastSortedBy"),K=L&&L.key,I=L&&L.dir,J=L&&L.notdir;if(M.column.get("sortable")){M.td.addClass(F(H,"sortable"));}if(K&&(K===M.column.get("key"))){M.td.replaceClass(F(H,J),F(H,I));}},_onEventSortColumn:function(N){N.halt();var L=this.get("host"),K=L.get("columnset").idHash[N.currentTarget.get("id")],J=K.get("key"),M=K.get("field"),O=this.get("lastSortedBy"),I=(O&&O.key===J&&O.dir===D)?C:D,P=K.get("sortFn");if(K.get("sortable")){L.get("recordset").sort.sort(M,I===C,P);this.set("lastSortedBy",{key:J,dir:I});}}});G.namespace("Plugin").DataTableSort=E;},"3.3.0",{lang:["en"],requires:["datatable-base","plugin","recordset-sort"]});