// 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.

// 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(list_id, order, sortby){
	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\.]+$/)) sortfn = ts_sort_numeric;
	
	switch (order) {
		case "asc":
			array.sort(sortfn);
		break;
		case "desc":
			array.sort(sortfn);
			array.reverse();
		break;
		case "len":
			array.sort(sortByLength);
		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;
}

function sortByLength(a,b)
{
	return a[1].length - b[1].length;
}
