/* Contao Open Source CMS, (c) 2005-2015 Leo Feyer, LGPL license */
var SORT_INDEX,THOUSANDS_SEPARATOR=",",DECIMAL_SEPARATOR=".",TableSort=new Class({initialize:function(e,t,n){t&&(THOUSANDS_SEPARATOR=t),n&&(DECIMAL_SEPARATOR=n);if(!e.rows||e.rows.length<1||!e.tHead||e.tHead.rows.length<1)return;var r=null,i=Cookie.read("TS_"+e.get("id").toUpperCase());i!==null&&(r=i.split("|"));var s=e.tHead.rows[e.tHead.rows.length-1];for(var o=0;o<s.cells.length;o++){if(s.cells[o].className.indexOf("unsortable")!=-1)continue;var u=s.cells[o],a=(new Element("a")).addClass("pointer");a.innerHTML=u.innerHTML,u.innerHTML="",a.addEvent("click",function(e,t){this.resort(e,t)}.pass([o,u],this)).inject(u),r!==null&&r[0]==o&&($(u).addClass(r[1]=="desc"?"asc":"desc"),this.resort(r[0],u))}},resort:function(e,t){var n=$(t);if(n==null)return;var r=n.getParent("tr"),i=r.getParent("table");if(i==null||i.tBodies[0].rows.length<2)return;SORT_INDEX=e;var s=0,o="",u;while(o==""&&i.tBodies[0].rows[s])o=i.tBodies[0].rows[s].cells[e].innerHTML.replace(/<[^>]+>/g,"").clean(),s++;var a=[];for(s=0;s<i.tBodies[0].rows.length;s++)a[s]=i.tBodies[0].rows[s];t.className.indexOf("date")!=-1||o.match(/^\d{1,4}[\/\. -]\d{1,2}[\/\. -]\d{1,4}$/)?a.sort(this.sortDate):t.className.indexOf("currency")!=-1||o.match(/^[£$€Û¢´]/)||o.match(/^-?[\d\.,]+[£$€]$/)?a.sort(this.sortNumeric):t.className.indexOf("numeric")!=-1||o.match(/^-?[\d\.,]+(E[-+][\d]+)?$/)||o.match(/^-?[\d\.,]+%?$/)?a.sort(this.sortNumeric):a.sort(this.sortCaseInsensitive);var f=$$("base").get("href"),l=f[0].replace(window.location.protocol+"//","").replace(window.location.host,"").replace(/\/$/,"")||"/",c=r.getChildren(),h;if(t.className.indexOf("asc")==-1){for(s=0;s<c.length;s++)c[s].removeClass("asc"),c[s].removeClass("desc");t.addClass("asc"),Cookie.write("TS_"+i.id.toUpperCase(),e+"|asc",{path:l})}else{for(s=0;s<c.length;s++)c[s].removeClass("asc"),c[s].removeClass("desc");t.addClass("desc"),Cookie.write("TS_"+i.id.toUpperCase(),e+"|desc",{path:l}),a.reverse()}for(s=0;s<a.length;s++){h=a[s].className,h=h.replace(/row_\d+/,"").replace(/odd|even|row_first|row_last/g,"").clean(),h+=" row_"+s,s==0&&(h+=" row_first"),s>=a.length-1&&(h+=" row_last"),h+=s%2==0?" even":" odd",a[s].className=h.trim();for(u=0;u<a[s].cells.length;u++)h=a[s].cells[u].className,h=h.replace(/col_\d+/,"").replace(/odd|even|col_first|col_last/g,"").clean(),h+=" col_"+u,u==0&&(h+=" col_first"),u>=a[s].cells.length-1&&(h+=" col_last"),a[s].cells[u].className=h.trim();i.tBodies[0].appendChild(a[s])}},sortDate:function(e,t){var n=e.cells[SORT_INDEX].innerHTML.replace(/<[^>]+>/g,"").clean(),r=t.cells[SORT_INDEX].innerHTML.replace(/<[^>]+>/g,"").clean(),i=n.replace(/[\/\.-]/g," ").split(" "),s=r.replace(/[\/\.-]/g," ").split(" "),o,u;return n.match(/^\d{1,2}[\/\. -]\d{1,2}[\/\. -]\d{2,4}$/)&&(o=(i[2].length==4?i[2]:"19"+i[2])+(i[1].length==2?i[1]:"0"+i[1])+(i[0].length==2?i[0]:"0"+i[0]),u=(s[2].length==4?s[2]:"19"+s[2])+(s[1].length==2?s[1]:"0"+s[1])+(s[0].length==2?s[0]:"0"+s[0])),n.match(/^\d{2,4}[\/\. -]\d{1,2}[\/\. -]\d{1,2}$/)&&(o=(i[0].length==4?i[0]:"19"+i[0])+(i[1].length==2?i[1]:"0"+i[1])+(i[2].length==2?i[2]:"0"+i[2]),u=(s[0].length==4?s[0]:"19"+s[0])+(s[1].length==2?s[1]:"0"+s[1])+(s[2].length==2?s[2]:"0"+s[2])),o==u?0:o<u?-1:1},sortNumeric:function(e,t){var n=new RegExp("\\"+THOUSANDS_SEPARATOR,"g"),r=e.cells[SORT_INDEX].innerHTML.replace(n,""),i=t.cells[SORT_INDEX].innerHTML.replace(n,"");return DECIMAL_SEPARATOR!="."&&(r=r.replace(DECIMAL_SEPARATOR,"."),i=i.replace(DECIMAL_SEPARATOR,".")),r=r.replace(/<[^>]+>/).replace(/[^0-9\.,-]/g,"").clean(),i=i.replace(/<[^>]+>/).replace(/[^0-9\.,-]/g,"").clean(),r=parseFloat(r),isNaN(r)&&(r=0),i=parseFloat(i),isNaN(i)&&(i=0),r-i},sortCaseInsensitive:function(e,t){var n=e.cells[SORT_INDEX].innerHTML.replace(/<[^>]+>/g,"").clean().toLowerCase(),r=t.cells[SORT_INDEX].innerHTML.replace(/<[^>]+>/g,"").clean().toLowerCase();return n==r?0:n<r?-1:1}});