
$(document).ready(function() {

		tabContent();

		// IE 6.0 fix
    	$("table thead th").mouseover(function ()
		 {
		        $(this).addClass('sortableHeaderhover');
		 });

		$("table thead th").mouseout(function ()
		{
		    if($(this).hasClass("sortableHeaderhover"))
		    {
		         $(this).removeClass('sortableHeaderhover');
		    }
		});

		//*********************************************************************************************
		// The following code will trigger a sort on all tables.  The source table would automatically
		// get sorted anyway by the 'tablesorter', however it toggles the sort order and we need to
		// take control.
		//*********************************************************************************************

		$("div.scr").find("table").each(function(i){
			$(this).tablesorter({
				widgets: ['zebra'],
				headers: {
					4: {sorter: false},
					5: {sorter: false},
					6: {sorter: false}
				}
			});
		});

		$(".header").click(function () {

		//*********************************************************************************************
		//get the parent DIV name
        //*********************************************************************************************
		var id = $(this).closest("div").attr("id");

		//*********************************************************************************************
		//get the INDEX of the TH that was clicked (relative to the DIV container)
		//*********************************************************************************************
		var index = $("#"+id+" th").index(this);
		var tabIndex;
		if ($("#tablayer1").is(':visible'))
		{
			tabIndex = 1;
		}
		else if ($("#tablayer2").is(':visible'))
		{
			tabIndex = 2;
		}
		else if ($("#tablayer3").is(':visible'))
		{
			tabIndex = 3;
		}
		else if ($("#tablayer4").is(':visible'))
		{
			tabIndex = 4;
		}
		else if ($("#tablayer5").is(':visible'))
		{
			tabIndex = 5;
		}
		else if ($("#tablayer6").is(':visible'))
		{
			tabIndex = 6;
		}
		else if ($("#tablayer7").is(':visible'))
		{
			tabIndex = 7;
		}

		$("#tablayer" + tabIndex).find("th:eq(0)").addClass("sortableHeader");
		$("#tablayer" + tabIndex).find("th:eq(1)").addClass("sortableHeader");
		$("#tablayer" + tabIndex).find("th:eq(2)").addClass("sortableHeader");
		$("#tablayer" + tabIndex).find("th:eq(3)").addClass("sortableHeader");
		$("#tablayer" + tabIndex).find("th:eq("+$("#tablayer" + tabIndex + " th").index(this)+")").removeClass("sortableHeader");



        //*********************************************************************************************
		//Update the style of the column
		//*********************************************************************************************
		$("#tablayer" + tabIndex + " tr").find("td").removeClass("selected");
		$("#tablayer" + tabIndex + " tr").find("td:eq("+index+")").addClass("selected");

		//*********************************************************************************************
		//Update the sorting of the other tables on this table changing
		//*********************************************************************************************
		var thisSortDirection = 0;
		if($(this).hasClass("headerSortDown")){

			thisSortDirection = 1;
		}

		// Ascending
		if (thisSortDirection == 0)
		{
			$("#tablayer" + tabIndex).find("th:eq("+$("#tablayer" + tabIndex + " th").index(this)+")").removeClass("headerSortUp" + index);
			$("#tablayer" + tabIndex).find("th:eq("+$("#tablayer" + tabIndex + " th").index(this)+")").addClass("headerSortDown" + index);
		}
		// Descending
		else
		{
			$("#tablayer" + tabIndex).find("th:eq("+$("#tablayer" + tabIndex + " th").index(this)+")").removeClass("headerSortDown" + index);
			$("#tablayer" + tabIndex).find("th:eq("+$("#tablayer" + tabIndex + " th").index(this)+")").addClass("headerSortUp" + index);
		}
		var i=0;
		for (i=0;i<=3;i++)
		{
			if (! (i==index)) {
				$("#tablayer" + tabIndex).find("th:eq(i)").removeClass("headerSortUp"+i);
				$("#tablayer" + tabIndex).find("th:eq(i)").removeClass("headerSortDown"+i);
			}
		}

		//*********************************************************************************************
		//If this is clicked to remove the mouse hover style
		//*********************************************************************************************
        if($(this).hasClass("sortableHeaderhover")){
	    $(this).removeClass('sortableHeaderhover');
	    }

		var sorting = [[index,thisSortDirection]];

		return false;
	});
});
