

function sortAlpha(column, rows, direction) {
	rows.sort(function(a, b) {
		var keyA = $(a).children('td').eq(column).text().toUpperCase();
		var keyB = $(b).children('td').eq(column).text().toUpperCase();
		if (direction == 'ascending') {
			if (keyA < keyB) return -1;
			if (keyA > keyB) return 1;
		} else {
			if (keyA > keyB) return -1;
			if (keyA < keyB) return 1;
		}
		return 0;
	});
}

function sortDate(column, rows, direction) {
	rows.sort(function(a, b) {
		var keyA = parseFloat($(a).children('td').eq(column).text().replace(/[^0-9]/g, ''));
		var keyB = parseFloat($(b).children('td').eq(column).text().replace(/[^0-9]/g, ''));
		if (direction == 'ascending') {
			if (keyA < keyB) return -1;
			if (keyA > keyB) return 1;
		} else {
			if (keyA > keyB) return -1;
			if (keyA < keyB) return 1;
		}
		return 0;
	});
}

function sortNumeric(column, rows, direction) {
	rows.sort(function(a, b) {
		var keyA = parseFloat($(a).children('td').eq(column).text().replace(/[^0-9]/g, ''));
		var keyB = parseFloat($(b).children('td').eq(column).text().replace(/[^0-9]/g, ''));
		if (direction == 'ascending') {
			if (keyA < keyB) return -1;
			if (keyA > keyB) return 1;
		} else {
			if (keyA > keyB) return -1;
			if (keyA < keyB) return 1;
		}
		return 0;
	});
}

function sortStar(column, rows, direction) {
	rows.sort(function(a, b) {
		var keyA = parseFloat($(a).children('td').eq(column).children('input').val());
		var keyB = parseFloat($(b).children('td').eq(column).children('input').val());
		if (direction == 'ascending') {
			if (keyA < keyB) return -1;
			if (keyA > keyB) return 1;
		} else {
			if (keyA > keyB) return -1;
			if (keyA < keyB) return 1;
		}
		return 0;
	});
}

function triggerResort() {
	$('table.sortable').each(function() {
		var table = $(this);
		var cookieCol = $.cookie(table.attr('id') + '_column');
		var cookieDir = $.cookie(table.attr('id') + '_direction');

		$('th', table).each(function(column) {

			$(this).attr('sort:col', column);

			if (cookieCol) {
				if (column == cookieCol) {
					$(this).addClass('default');
					if (cookieDir && cookieDir == 'descending') {
						$(this).addClass('ascending');
					}
				} else {
					$(this).removeClass('default');
				}
			}

			if ($(this).is('.sortable')) {
				$(this).width(($(this).width() + 18) + 'px');
				$(this).hover(function() { 
					$(this).addClass('sorthover');
				}, function() { 
					$(this).removeClass('sorthover');
				});

				$(this).click(function() {
					var dirsort = 'ascending';
					if ($(this).is('.ascending')) {
						dirsort = 'descending';
					}

					$.cookie(table.attr('id') + '_column', $(this).attr('sort:col'));
					$.cookie(table.attr('id') + '_direction', dirsort);

					$('th', table).each(function(column) {
						$(this).removeClass('descending');
						$(this).removeClass('ascending');
					});

					$(this).addClass(dirsort);

					var rows = table.find('tbody > tr').get();
					if ($(this).is('.alpha-sort')) {
						sortAlpha(column, rows, dirsort);
					} else if ($(this).is('.numeric-sort')) {
						sortNumeric(column, rows, dirsort);
					} else if ($(this).is('.date-sort')) {
						sortDate(column, rows, dirsort);
					} else if ($(this).is('.star-sort')) {
						sortStar(column, rows, dirsort);
					}
					$.each(rows, function(index, row) {
						table.children('tbody').append(row);
					});
				});
			}
		});

		var defaultSort = $('th.default', table);
		if (defaultSort) {
			defaultSort.trigger('click');
		}

	});
}

$(document).ready(function() {
	triggerResort();
});

