﻿function applyTableSort() {
	jQuery("th.sort").addClass('default'); // default all sortable columns to displaying "down" option initially

	jQuery("th.sort").mouseenter(function() {
		if (jQuery(this).hasClass("active")) {
			if (jQuery(this).hasClass("down")) {
				jQuery(this).children(".sort_arrow").css("backgroundImage","url(/certification/images/sortselectedhover-whtbg.png)");
			} else if (jQuery(this).hasClass("up")) {
				jQuery(this).children(".sort_arrow").css("backgroundImage","url(/certification/images/sorthover-whtbg.png)");
			}
		} else if (jQuery(this).hasClass("default")) {
			jQuery(this).children(".sort_arrow").css("backgroundImage","url(/certification/images/sortselectedhover-whtbg.png)");
		}
	});
	
	jQuery("th.sort").mouseleave(function() {
		if (jQuery(this).hasClass("active")) {
			if (jQuery(this).hasClass("down")) {
				jQuery(this).children(".sort_arrow").css("backgroundImage","url(/certification/images/sortselected-whtbg.png)");
			} else if (jQuery(this).hasClass("up")) {
				jQuery(this).children(".sort_arrow").css("backgroundImage","url(/certification/images/sort-whtbg.png)");
			}
		} else if (jQuery(this).hasClass("default")) {
			jQuery(this).children(".sort_arrow").css("backgroundImage","url(/certification/images/sortwhtbg.png)");
		}
	});
	
	jQuery("th.sort").click(function() {
		setActive(this); // make active

		if (jQuery(this).hasClass('down')) {
			jQuery(this).removeClass('down').addClass('up');
			jQuery(this).children(".sort_arrow").css("backgroundImage","url(/certification/images/sorthover-whtbg.png)");
			sortColumn(this, 'desc');
		} else if (jQuery(this).hasClass('up')) {
			jQuery(this).removeClass('up').addClass('down');
			jQuery(this).children(".sort_arrow").css("backgroundImage","url(/certification/images/sortselectedhover-whtbg.png)");
			sortColumn(this, 'asc');
		} else {
			jQuery(this).addClass('down'); // if no up/down class assigned, assign 'down'
			sortColumn(this, 'asc');
		}
	});
	
	function getColumnIndex(column) {
		var colCount = -1;
		var cols = jQuery(column).parent().children();
		jQuery(cols).each(function() {
			colCount++;
			if (this == column) {
				return false;
			}
		});
		return colCount;
	}
	
	function sortColumn(columnHeader, order) {
		function sortByCertification(rows, order) {
			var certs = [];
			var hpCerts = [];
			for (var i = 0; i < rows.length; i++) {
				var certName = jQuery(rows[i]).children('td').get(1);
				certName = jQuery(certName).text();
				if (certName.trim().split(' ')[0] == 'HP') {
					hpCerts.push(rows[i]);
				} else {
					certs.push(rows[i]);
				}
			}
			
			// sort arrays
			sortRows(hpCerts, 1, order);
			sortRows(certs, 1, order);
			
			rows = hpCerts.concat(certs);
			return rows;
		}
		
		// get column's position in table
		var colNum = getColumnIndex(columnHeader);
		
		// get reference to tbody, to detach and remove tr elements
		var tbody = jQuery(columnHeader).closest('table').children('tbody');
		
		// get collection of rows
		var rows = jQuery(tbody).children('tr').detach();
		jQuery(rows).removeClass('last').removeClass('dark'); // remove style classes
		
		if (colNum == 0) {
			// sort by certification (HP certs at top of each section), then by skill level
			rows = sortByCertification(rows, order);
			sortRows(rows, 0, order);
		} else if (colNum == 1) { // sort by certification (HP certs at top)
			rows = sortByCertification(rows, order);
		}
		setRowStyleClasses(rows);
		jQuery(rows).appendTo(tbody);
	}
	
	function sortRows(rows, column, order) {
		rows.sort(function(a, b) {
			var aPart = jQuery(a).children('td').get(column);
			var bPart = jQuery(b).children('td').get(column);
			if (jQuery(aPart).text().toLowerCase() > jQuery(bPart).text().toLowerCase()) return order == 'asc' ? -1 : 1;
			if (jQuery(aPart).text().toLowerCase() < jQuery(bPart).text().toLowerCase()) return order == 'asc' ? 1 :  -1;
			return 0;
		});
	}
	
	function setActive(clickable) {
		// reset all siblings to 'default' state
		jQuery(clickable).siblings('.sort')
			.removeClass('up')
			.removeClass('down')
			.removeClass('active')
			.addClass('default')
			.children(".sort_arrow")
			.css("backgroundImage","url(/certification/images/sortwhtbg.png)");
		jQuery(clickable).removeClass('default').addClass('active');
	}
	
	function setRowStyleClasses(rows) {
		jQuery(rows).removeClass('last').removeClass('dark'); // remove style classes
	    jQuery.each(rows, function(i) {
			if (i % 2 == 1) {
				jQuery(this).addClass('dark');
			}
			if (i == rows.length - 1) {
				jQuery(this).addClass('last');
			}
	    });			
	}

};

