//-----------------------------------------------------------
//		Sorting Function for Yahoo! Store - jquery plugin
//		Author: FastPivot (http://www.fastpivot.com)
//      Requires : jQuery & Basic Pagination
//
//		Version: 1.0 (1.19.2010)
//-----------------------------------------------------------


function sortMe(how, rows, cols, lb, ctrls) {

	
	// Working Load Info
	//jQuery('#contents-table').css("opacity", "0.2").css("filter", "alpha(opacity = 20)");
	jQuery('.wkSpn').show();
	
 	// Set Main variables
	var mylist = jQuery('#contents-table');
	var listitems = mylist.find('td.item').get();
	
	// Run a Switch on how to sort (price, name - high, low)
	switch(how){
	
		case "price1":
			jQuery('.sbPrice').removeClass('arrow2');
			jQuery('.sbPrice').addClass('arrow3');
			jQuery('.sbName').removeClass('arrow2');
			jQuery('.sbName').removeClass('arrow3');
			listitems.sort(function(a, b) {
				var compA = jQuery(a).find('div.fPrice').text();
				var compAA = compA.replace("jQuery", "");
				var compB = jQuery(b).find('div.fPrice').text();
				var compBB = compB.replace("jQuery", "");
				return compAA - compBB;			
			})								
		break;
		
		case "name1":	
			jQuery('.sbName').removeClass('arrow2');
			jQuery('.sbName').addClass('arrow3');
			jQuery('.sbPrice').removeClass('arrow2');
			jQuery('.sbPrice').removeClass('arrow3');					
			listitems.sort(function(a, b) {
				var compA = jQuery(a).find('div.name a').text();
				var compB = jQuery(b).find('div.name a').text();
				return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;		
			})
		break;

		case "price2":
			jQuery('.sbPrice').removeClass('arrow3');
			jQuery('.sbPrice').addClass('arrow2');
			jQuery('.sbName').removeClass('arrow2');
			jQuery('.sbName').removeClass('arrow3');
			listitems.sort(function(a, b) {
				var compA = jQuery(a).find('div.fPrice').text();
				var compAA = compA.replace("jQuery", "");
				var compB = jQuery(b).find('div.fPrice').text();
				var compBB = compB.replace("jQuery", "");
				return compBB - compAA;			
			})				
	
		break;
		
		case "name2":
			jQuery('.sbName').removeClass('arrow3');
			jQuery('.sbName').addClass('arrow2');
			jQuery('.sbPrice').removeClass('arrow2');
			jQuery('.sbPrice').removeClass('arrow3');		
			listitems.sort(function(a, b) {
				var compA = jQuery(a).find('div.name a').text();
				var compB = jQuery(b).find('div.name a').text();
				return (compB < compA) ? -1 : (compB > compA) ? 1 : 0;
			})
		break;
	
	}
	
	// Setting Time Out
	setTimeout(function(){

		// Stop Ajax Load
		//jQuery('#contents-table').css("opacity", "1").css("filter", "alpha(opacity = 100)");
		jQuery('.paginationControls').remove();
		jQuery('.wkSpn').hide();
		
		// Re-Write all of the contents ... as quickly as possible.
		var nr = parseInt(listitems.length / cols);
		var nr1 = (listitems.length / cols);
		var itc = listitems.length;
		if(nr == nr1) {
			var numRows = nr;
		} else {
			var numRows = nr + 1;
		}
		jQuery("#contents-table").empty();
		
		for(i=0; i<numRows; i++){
		
			var nRow = "<tr>";
			
				for(x=0; x<cols; x++){
				
					var pos = (i * cols) + x; 
					
					if(pos<itc){
						nRow += "<td class=\"item\">";
						nRow += jQuery(listitems[pos]).html();
						nRow += "</td>";				
					}
					
					if(x < (cols-1)){
						nRow += "<td width='1' bgcolor='' class='verticalDivider'><img height='1' border='0' width='1' src='http://ep.yimg.com/ca/Img/trans_1x1.gif'/></td>"
					}
	
				}
	
			nRow += "</tr>";
	
					if(i < (numRows-1)){
						nRow += "<td bgcolor='' colspan='8' class='horizontalDivider'><img height='1' border='0' width='1' src='http://ep.yimg.com/ca/Img/trans_1x1.gif'/></td>"
					}
	
			jQuery(nRow).appendTo("#contents-table");
	
		
		}
		// Reset's the Paging		
		jQuery("#contents-table tbody:first").basicPagination( {rowsPerPage: rows, colsPerPage: cols, itemCount: itc, linkBreak: lb, paginationControlsLocation: ctrls, currentPage: 1});
	
	}, 1000);
		

}
