/*
 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(Y){var YgetClassName=Y.ClassNameManager.getClassName,DATATABLE="datatable",COLUMN="column",ASC="asc",DESC="desc",TEMPLATE='<a class="{link_class}" title="{link_title}" href="{link_href}">{value}</a>';function DataTableSort(){DataTableSort.superclass.constructor.apply(this,arguments);}
Y.mix(DataTableSort,{NS:"sort",NAME:"dataTableSort",ATTRS:{trigger:{value:{event:"click",selector:"th"},writeOnce:"initOnly"},lastSortedBy:{setter:"_setLastSortedBy",lazyAdd:false},template:{value:TEMPLATE}}});Y.extend(DataTableSort,Y.Plugin.Base,{initializer:function(config){var dt=this.get("host"),trigger=this.get("trigger");dt.get("recordset").plug(Y.Plugin.RecordsetSort,{dt:dt});dt.get("recordset").sort.addTarget(dt);this.doBefore("_createTheadThNode",this._beforeCreateTheadThNode);this.doBefore("_attachTheadThNode",this._beforeAttachTheadThNode);this.doBefore("_attachTbodyTdNode",this._beforeAttachTbodyTdNode);dt.delegate(trigger.event,Y.bind(this._onEventSortColumn,this),trigger.selector);dt.after("recordsetSort:sort",function(){this._uiSetRecordset(this.get("recordset"));});this.on("lastSortedByChange",function(e){this._uiSetLastSortedBy(e.prevVal,e.newVal,dt);});if(dt.get("rendered")){dt._uiSetColumnset(dt.get("columnset"));this._uiSetLastSortedBy(null,this.get("lastSortedBy"),dt);}},_setLastSortedBy:function(val){if(Y.Lang.isString(val)){return{key:val,dir:"asc",notdir:"desc"};}
else if(val&&val.key){if(val.dir==="desc"){return{key:val.key,dir:"desc",notdir:"asc"};}
else{return{key:val.key,dir:"asc",notdir:"desc"};}}
else{return null;}},_uiSetLastSortedBy:function(prevVal,newVal,dt){var prevKey=prevVal&&prevVal.key,prevDir=prevVal&&prevVal.dir,newKey=newVal&&newVal.key,newDir=newVal&&newVal.dir,cs=dt.get("columnset"),prevColumn=cs.keyHash[prevKey],newColumn=cs.keyHash[newKey],tbodyNode=dt._tbodyNode,prevRowList,newRowList;if(prevColumn){prevColumn.thNode.removeClass(YgetClassName(DATATABLE,prevDir));prevRowList=tbodyNode.all("."+YgetClassName(COLUMN,prevColumn.get("id")));prevRowList.removeClass(YgetClassName(DATATABLE,prevDir));}
if(newColumn){newColumn.thNode.addClass(YgetClassName(DATATABLE,newDir));newRowList=tbodyNode.all("."+YgetClassName(COLUMN,newColumn.get("id")));newRowList.addClass(YgetClassName(DATATABLE,newDir));}},_beforeCreateTheadThNode:function(o){if(o.column.get("sortable")){o.value=Y.substitute(this.get("template"),{link_class:o.link_class||"",link_title:"title",link_href:"#",value:o.value});}},_beforeAttachTheadThNode:function(o){var lastSortedBy=this.get("lastSortedBy"),key=lastSortedBy&&lastSortedBy.key,dir=lastSortedBy&&lastSortedBy.dir,notdir=lastSortedBy&&lastSortedBy.notdir;if(o.column.get("sortable")){o.th.addClass(YgetClassName(DATATABLE,"sortable"));}
if(key&&(key===o.column.get("key"))){o.th.replaceClass(YgetClassName(DATATABLE,notdir),YgetClassName(DATATABLE,dir));}},_beforeAttachTbodyTdNode:function(o){var lastSortedBy=this.get("lastSortedBy"),key=lastSortedBy&&lastSortedBy.key,dir=lastSortedBy&&lastSortedBy.dir,notdir=lastSortedBy&&lastSortedBy.notdir;if(o.column.get("sortable")){o.td.addClass(YgetClassName(DATATABLE,"sortable"));}
if(key&&(key===o.column.get("key"))){o.td.replaceClass(YgetClassName(DATATABLE,notdir),YgetClassName(DATATABLE,dir));}},_onEventSortColumn:function(e){e.halt();var dt=this.get("host"),column=dt.get("columnset").idHash[e.currentTarget.get("id")],key=column.get("key"),field=column.get("field"),lastSortedBy=this.get("lastSortedBy"),dir=(lastSortedBy&&lastSortedBy.key===key&&lastSortedBy.dir===ASC)?DESC:ASC,sorter=column.get("sortFn");if(column.get("sortable")){dt.get("recordset").sort.sort(field,dir===DESC,sorter);this.set("lastSortedBy",{key:key,dir:dir});}}});Y.namespace("Plugin").DataTableSort=DataTableSort;},'3.3.0',{lang:['en'],requires:['datatable-base','plugin','recordset-sort']});
