function ListSort(list_id, order)
{
	var list_obj = document.getElementById(list_id);
	var list_featured_products = document.getElementById('list_featured_products');
	var list_more_products = document.getElementById('list_more_products');
	if(list_featured_products)
	{
		list_obj.removeChild(list_featured_products)
	}
	if(list_more_products)
	{
		list_obj.removeChild(list_more_products)
	}
	var list_items = list_obj.getElementsByTagName('li');
	var num_list_items = list_items.length;
    var alpha = [], numeric = [];
    var aIdx = 0, nIdx = 0;

//console.log(order.options[order.selectedIndex].value);

  for (var i=0; i<num_list_items; i++) 
	{

		if(list_items[i].parentNode.id == list_id)
		{
			var paras = list_items[i].getElementsByTagName('p');
			var num_paras = paras.length;
			for (k = 0; k < num_paras; k++) 
			{
			  if(paras[k].className=='price')
				{
					var price = paras[k].innerHTML;
					var sale_pattern = '/<span class=\'price\'>.*/g';
					//if(price.match(sale_pattern)) alert('sale price');
				}

        if(paras[k].className=='fabric_weight')
				{
					var fabric_weight = paras[k].innerHTML;
					//then strip the grams g
					fabric_weight = fabric_weight.replace("g", "");
					//console.log(fabric_weight);
				}
			}

			var links = list_items[i].getElementsByTagName('a');
			var num_links = links.length;
      var best_seller = 0;
      var double_cuff = 0;
			for (k = 0; k < num_links; k++)
			{
        if(links[k].className=='best_seller')
				{
					best_seller = 1;
				}

        if(links[k].className=='double_cuff')
				{
					double_cuff = 1;
				}

			}

			var content = list_items[i].innerHTML;

			/*
			 * Split data into two separate arrays, one for numeric content and 
			 * one for everything else (alphabetic). Store both the actual data
			 * that will be used for comparison by the sort algorithm (thus the need
			 * to parseFloat() the numeric data) as well as a reference to the 
			 * element's parent row. The row reference will be used after the new
			 * order of content is determined in order to actually reorder the HTML
			 * table's rows.
			 */

			if(order.options[order.selectedIndex].value == 'default')
			{	
				//console.log("default sort");
				var num = list_items[i].id.replace("item_", "");
				 numeric[nIdx++] = { 
				value: Number(num),
				original_content: content,
				item_id: list_items[i].id
				 }
			
			}else if(order.options[order.selectedIndex].value == 'asc' || order.options[order.selectedIndex].value == 'desc')
			{
				//console.log("price sort");
				var num = price.replace(/([^0-9\.])/g, "");
				  if (parseFloat(num) == num) { 
					numeric[nIdx++] = {
						value: Number(num),
						original_content: content,
						item_id: list_items[i].id
					}
				} else {
					alpha[aIdx++] = {
						value: price,
						original_content: content,
						item_id: list_items[i].id
					}
				}
			}else if(order.options[order.selectedIndex].value == 'asc_fabric' || order.options[order.selectedIndex].value == 'desc_fabric')
			{
				//console.log("fabric sort");
				var num = fabric_weight.replace(/([^0-9\.])/g, "");
				  if (parseFloat(num) == num) { 
					numeric[nIdx++] = {
						value: Number(num),
						original_content: content,
						item_id: list_items[i].id
					}
				} else {
					alpha[aIdx++] = {
						value: fabric_weight,
						original_content: content,
						item_id: list_items[i].id
					}
				}
			}else if(order.options[order.selectedIndex].value == 'asc_best' || order.options[order.selectedIndex].value == 'desc_best')
			{
				//console.log("fabric sort");
				var num = best_seller;
				  if (parseFloat(num) == num) {
					numeric[nIdx++] = {
						value: Number(num),
						original_content: content,
						item_id: list_items[i].id
					}
				} else {
					alpha[aIdx++] = {
						value: best_seller,
						original_content: content,
						item_id: list_items[i].id
					}
				}
			}else if(order.options[order.selectedIndex].value == 'asc_cuff' || order.options[order.selectedIndex].value == 'desc_cuff')
			{
				//console.log("fabric sort");
				var num = double_cuff;
				  if (parseFloat(num) == num) {
					numeric[nIdx++] = {
						value: Number(num),
						original_content: content,
						item_id: list_items[i].id
					}
				} else {
					alpha[aIdx++] = {
						value: double_cuff,
						original_content: content,
						item_id: list_items[i].id
					}
				}
			}

		}
    }
	list_obj.innerHTML = '';

    /*
     * Sort according to direction (ascending or descending)
     */
	if(order.options[order.selectedIndex].value == 'asc'
|| order.options[order.selectedIndex].value == 'asc_fabric'
|| order.options[order.selectedIndex].value == 'asc_best'
|| order.options[order.selectedIndex].value == 'asc_cuff'
)
	{
		var ordered_list = [], top, bottom;
        if(alpha) top = bubbleSort(numeric, 1);
        if(numeric) bottom = bubbleSort(alpha, 1);
	}else if(order.options[order.selectedIndex].value == 'desc'
|| order.options[order.selectedIndex].value == 'desc_fabric'
|| order.options[order.selectedIndex].value == 'desc_best'
|| order.options[order.selectedIndex].value == 'desc_cuff'
)
	{
		var ordered_list = [], top, bottom;
		if(alpha) top = bubbleSort(alpha, -1);
		if(numeric) bottom = bubbleSort(numeric, -1);
	}else
	{
		var ordered_list = [], top, bottom;
		if(alpha) top = bubbleSort(alpha, 1);
		if(numeric) bottom = bubbleSort(numeric, 1);
	}


	ordered_list = top.concat(bottom);
    for (var i=0; i<ordered_list.length; i++) {

		var new_li = document.createElement("li");
		new_li.innerHTML = ordered_list[i].original_content;
		new_li.setAttribute("id",ordered_list[i].item_id);
        list_obj.appendChild(new_li);
    }

}

function bubbleSort(arr, dir) {
    // Pre-calculate directional information
    var start, end;
    if (dir === 1) {
        start = 0;
        end = arr.length;
    } else if (dir === -1) {
        start = arr.length-1;
        end = -1;
    }
    
    // Bubble sort: http://en.wikipedia.org/wiki/Bubble_sort
    var unsorted = true;
    while (unsorted) {
        unsorted = false;
        for (var i=start; i!=end; i=i+dir) {
            if (arr[i+dir] && arr[i].value > arr[i+dir].value) {
                var a = arr[i];
                var b = arr[i+dir];
                var c = a;
                arr[i] = b;
                arr[i+dir] = c;
                unsorted = true;
            }
        }
    }
    return arr;
}


