// by Bruno Bornsztein - www.feedmarker.com or blog.feedmarker.com
// You're free to use this however you want. You can even take this attribution out if you like.
//
// modified by Alex Simeonov for the needs of Barix AG, Switzerland

// Make sure the list you want to sort has a unique id
// Then create a link to sort the list in the following format:
// <a href="javascript:void(0);" onclick="sort(this)" list_id="the id of the list you want to sort" order="asc or desc">Sort</a>
// that will just just the list by it list item values

// if you want to sort by an attribute you've included within each list item (i.e. <LI size="10">), just do this:
// // <a href="javascript:void(0);" onclick="sort(this)" list_id="the id of the list you want to sort" order="asc or desc" sortby="your attribute">Sort</a>

function ts_getInnerText(el) {
//Thanks to http://www.kryogenix.org/code/browser/sorttable/ for this function
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}


function ts_getInnerText(el) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}

function parse_list_to_array(list_id, attribute){
	var list = document.getElementById(list_id);
	var cs = list.childNodes;
	var list_array = new Array();

	var l = cs.length;
	for (var i = 0; i < l; i++) {
		node = cs[i];
		if (node.nodeName == "LI"){
			if(!attribute){
				var value = ts_getInnerText(node);
				list_array.push([node, value]);
			} else{
				list_array.push([node, node.getAttribute(attribute)]);
			}
		}
	}
	return list_array; //returns an array with the node in [0] and the attribute in [1]
}


function sort(link){

	var list_id = link.getAttribute('list_id');
	var order = link.getAttribute('order');
	var sortby = link.getAttribute('sortby');
	var link_text = link.childNodes[0].nodeValue;

	if (order == 'desc'){
 		order = 'asc';
		link.setAttribute('order','asc');
		if (link_text == 'A-Z'){
			link.childNodes[0].nodeValue='Z-A';
		}
		if (link.childNodes[0].nodeValue.substr(0,8) == 'Filesize'){
			document.getElementById('sortarrow').src='arrowdown.gif';
		}

	} else {
		order = 'desc';
		link.setAttribute('order','desc');
		if (link_text == 'Z-A'){
			link.childNodes[0].nodeValue='A-Z';
		}
		if (link.childNodes[0].nodeValue.substr(0,8) == 'Filesize'){
			document.getElementById('sortarrow').src='arrowup.gif';
		}
	}

	if (list_id=="player"){list_id=document.getElementById('musicdir').value;}

	var array = parse_list_to_array(list_id, sortby);

// Work out a type to sort by
	var itm = array[1][1];
	sortfn = mysortfn_by_attribute;
/*
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
*/
	if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;

	array.sort(sortfn);

	switch (order){
		case "desc":
		array.reverse();
		break;
	}

	var list = document.getElementById(list_id);

	for (var k = 0; k < array.length; k++){
		list.appendChild(array[k][0]);
	}

	return;
}

function mysortfn_by_attribute(a,b) {
  // Note that each thing we are passed is an array, so we don't compare the things
  // we're passed; instead, we compare their second column
  if (a[1]<b[1]) return -1;
  if (a[1]>b[1]) return 1;
  return 0;
}

function ts_sort_numeric(a,b) { 
    aa = a[1]
    if (isNaN(aa)) aa = 0;
    bb = b[1]
    if (isNaN(bb)) bb = 0;
    return bb-aa;
}

// Functions for deleting files
// ASI, 25.11.2009
function delInit(){
     var textEntry, newTextEntry;
     var checkBoxId;
     var i;
     var ulID=document.getElementById('musicdir').value;
     var ul=document.getElementById(ulID);
     var numOfEntries=ul.getElementsByTagName('li').length;

     // add the checkboxes
     for (i=0; i<numOfEntries;i++){
         textEntry=ul.getElementsByTagName('li')[i].innerHTML;
	 checkBoxId=textEntry.substr(0,textEntry.indexOf("<"));
	 newTextEntry='<input name=del_list type=checkbox ID="'+checkBoxId+'">&nbsp;'+textEntry;
	 ul.getElementsByTagName('li')[i].innerHTML=newTextEntry;
     }
}

function delSelected(){
     var baseUrl='basic.cgi?delsel';
     var tempUrl='';
     var delUrl=''
     var selFilesCount=0;
     var totalElements;
     var curChkboxId='';
     var chkboxes=document.files.del_list;
     var i;
     
     // parse all checkboxes
     totalElements=chkboxes.length;
     
     for (i=0;i<totalElements;i++){
        if (chkboxes[i].checked == true){
	   curChkboxId=chkboxes[i].id;
	   tempUrl=tempUrl+'&'+curChkboxId;
	   selFilesCount++;
	}
     }
     delUrl=baseUrl+tempUrl;

     if (selFilesCount>0){
        delUrl=baseUrl+tempUrl;
     	window.location=delUrl;
     }else{
        alert('Please select some files first!');
     }
}

