﻿var HIDE_LEFT_ATTRIB = 'nu_orig_left';
var HIDE_TOP_ATTRIB = 'nu_orig_top';
var HIDE_POSITION_ATTRIB = 'nu_orig_position';
var HIDE_HEIGHT_ATTRIB = 'nu_orig_height';
var GROUP_ATTRIB = 'nu_orig_group';
var HIDE_POSITION_TOP = '-300000em';

function toggle_the_div(div,fast,with_fade,customFunction)
{
    div.each( function() {
        var item = $(this);
        if (HIDE_POSITION_TOP==item.css('top'))
            show_the_div(item,fast,with_fade,customFunction);
        else
            hide_the_div(item,fast,with_fade,customFunction);
    });
}

function hide_the_div(div,fast,with_fade,customFunction)
{
    div.each( function() {
        var item = $(this);
        if (HIDE_POSITION_TOP == item.css('top'))
            return;
        
        item.attr( HIDE_TOP_ATTRIB, item.css('top') );
		item.attr( HIDE_LEFT_ATTRIB, item.css('left') );
        item.attr( HIDE_POSITION_ATTRIB, item.css('position') );
        if (item.height()!='100%')
            item.attr( HIDE_HEIGHT_ATTRIB, item.height() );
        if (fast) {
            item.css('height','0px').css('position','absolute').css('top',HIDE_POSITION_TOP).css('left',0);
        }
        else {
            var settings = null;
            if (with_fade)
                settings = {height:'0px',opacity:0.0};
            else
                settings = {height:'0px'};
            // Now it's been stored we can animate and hide the div
            item.animate( settings, 'normal', function() {
                $(item).css('position','absolute').css('top',HIDE_POSITION_TOP).css('left',0);
				customFunction;
            });
        }
    } );
}

function show_the_div(div,fast,with_fade,customFunction)
{
    div.each( function() {
		var item = $(this);	 
		
		if (item.attr(HIDE_TOP_ATTRIB) == item.css('top'))
            return;			   
					   
        if (undefined == item.attr(HIDE_TOP_ATTRIB))
            return;
		if (undefined == item.attr(HIDE_LEFT_ATTRIB))
            return;
        if (with_fade)
            item.css('opacity',0.0);
        item.css('top', item.attr(HIDE_TOP_ATTRIB) );
		item.css('left', item.attr(HIDE_LEFT_ATTRIB) );
        item.css('position', item.attr(HIDE_POSITION_ATTRIB) );
    
        if (undefined == item.attr(HIDE_HEIGHT_ATTRIB))
            var new_height = '';
        else
            var new_height = item.attr(HIDE_HEIGHT_ATTRIB);
                
        if (with_fade) {
            item.animate( {opacity:1.0,height:new_height}, 'slow', function() { $(this).height(''); customFunction; }  );
        }
        else {            
            item.css('opacity',1.0).animate( {height:new_height}, function() { $(this).height(''); customFunction; } );
        }
    } );
}

//Table Scripts
$(document).ready(function(){
	//Show Hide
	$("table.showHide tbody th").prepend("<a href='#' class='moreLink'>More</a>");
	hide_the_div($("table.showHide div.moreContent"),true,false);
	$("table.showHide div.moreContent").parents("td").css("padding","0 6px");
	$("table.showHide tbody th,td").toggleClass("closed");
	
	$("table.showHide tbody th").click(function () {
	  $(this).parents('tr').children().toggleClass("closed");
	  toggle_the_div($(this).parents('tr').next().children().children(),false,false);
	  $(this).children("a").toggleClass("close")
	  
	  if ($(this).children("a").text()==("Less")){
		  $(this).children("a").text("More");
		  $(this).children("a").parents("th").css({fontWeight:"normal"});}
	  else{
		  $(this).children("a").text("Less");
		  $(this).children("a").parents("th").css({fontWeight:"bold"});
		  $(this).children("a").css("font-weight","normal");}
	});
	
	//Auto Open
	var urlRegExp = new RegExp('\#[a-zA-Z0-9\/\?\%\-]+');
	
	$('a[href*="#"]').each( function() {
		if (location.pathname == this.pathname && location.host == this.host) {
			var href = $(this).attr('href').match(urlRegExp);
			var dt = $("table.showHide tbody th"+href);

			if((href != null) && (dt != null)) {
				$(this).click( function() {
					var href = $(this).attr('href').match(urlRegExp);
					var dt = $("table.showHide tbody th"+href);
					
					$(dt).parents('tr').children().removeClass("closed");
					show_the_div($(dt).parents('tr').next().children().children(),false,false);
					
					$(dt).children("a").addClass("close")
					$(dt).children("a").text("Less");
					$(dt).children("a").parents("th").css({fontWeight:"bold", color:"#000"});
		  			$(dt).children("a").css("font-weight","normal");
				});
			}
		}
	});
	
	var pageUrl = window.location.href;
	var href = pageUrl.match(urlRegExp);
	if (href != null) {
		var dt = $("table.showHide tbody th"+href);
		$(dt).parents('tr').children().removeClass("closed");
		show_the_div($(dt).parents('tr').next().children().children(),false,false);
		
		$(dt).children("a").addClass("close")
		$(dt).children("a").text("Less");
		$(dt).children("a").parents("th").css({fontWeight:"bold", color:"#000"});
		$(dt).children("a").css("font-weight","normal");
	}
	
	//Striping
	$("table").each(function () {
	if ($(this).hasClass("showHide")){
	  $(this).find("tbody tr:nth-child(4n),tbody tr:nth-child(4n-1)").addClass("alt");}
	  else{
	  $(this).find("tbody tr:even").addClass("alt");}
	 });

 	//Link Fix
		var regexS = "(#.+)";
		var regex = new RegExp(regexS);
		var results = regex.exec( window.location.href );
		if(results != null)
		window.location=results[0];
});

//Row Highlighting
// Add indexable values too all the cells in the table in the format col[number] row[number] as a class
function index_table(table) {
	// loop through each table individually
	table.each( function() {
		// set the row count to 1 to start and create a new array to store any cells that are not in the html due to colspans and rowspans
		var rowCount = 1;
		var skipArray = new Array();
		
		// loop through all the table rows incrementing the row count each time
		$(this).children().children('tr').each( function() {
			// set the column count for the row to 1 to start
			var colCount = 1;
			
			// loop through all the cells in the row
			$(this).children().each( function() {

				// First check the skip array to see if they cell selected has currently got the same col and row value as one that is part of a col and/or rowspan
				// If the id is the same then increment the column count by one until it isnt
				for(var i=0; i<skipArray.length; i++){
					var cellID = colCount + ',' + rowCount;
					if(skipArray[i] == cellID){
						colCount++;
					}
				}
			
				// Add the row and column classes to the cell
				$(this).addClass('col' + colCount);
				$(this).addClass('row' + rowCount);

				if (($(this).attr('colspan') > 1) && ($(this).attr('rowspan') > 1)) {
					// If there is a column and row span set on the cell then loop through all the cell cooridinates it covers and add them to the skipArray
					// also add the column and row ids to the cell so it is selected when any of the columns or rows are selected
				
					for(var i=0; i<$(this).attr('rowspan'); i++) {
						var row = rowCount+i;
						for(var j=0; j<$(this).attr('colspan'); j++) {
							var column = colCount+j;
							$(this).addClass('col' + column);
							$(this).addClass('row' + row);
							skipArray.push(column+','+row);
						}
					}
					// increase the column count by the number of columns spanned
					colCount = colCount + parseInt($(this).attr('colspan'));
				}
				else if($(this).attr('colspan') > 1){
					// If there is only a colspan add the column id to the cell and increment column count by one for every column the cell spans
					for(var i=0; i<$(this).attr('colspan'); i++) {
						$(this).addClass('col' + colCount);
						colCount++;
					}
				}
				else if($(this).attr('rowspan') > 1){
					// If there is only a rowspan add the row id to the cell and loop through the other rows spanned doing the same
					for(var i=1; i<$(this).attr('rowspan'); i++) {
						var row = rowCount+i;
						skipArray.push(colCount+','+row);
						$(this).addClass('row' + row);
					}
					colCount++;
				}
				else {
					// If there is no row or colspans just increment colCount
					colCount++;
				}
			});
			rowCount++;
		});
	});
}

// hightlight the columns, within a particular table, that contain the particular cell
function highlight_columns(table,cell) {
	// Get a list of all the column ids assosiated with the tables cell
	var columns = cell.attr('class').match(/(?:^|\s+)(col\d+)/g);

	// As long as the array of column ids is not empty hightlight the columns in the table
	if (columns != null) {
		// Loop through all the items in the column array
		for(var i=0; i<columns.length;i++){
			// Build a sting that uses the id and makes it into a class
			var columnToHightlight = '.' + columns[i];
			// Remove any spaces from the class name
			columnToHightlight = columnToHightlight.replace(/\s/,'');
			// find all the cells within the table that have the same class name as the cell selected and highlight them
			table.find(columnToHightlight).addClass('highlight');
		}
	}
}

// hightlight the rows, within a particular table, that contain the particular cell
function highlight_rows(table,cell) {
	// Get a list of all the row ids assosiated with the tables cell
	var rows = cell.attr('class').match(/(?:^|\s+)(row\d+)/g);
	
	// As long as the array of row ids is not empty hightlight the rows in the table
	if (rows != null) {
		// Loop through all the items in the row array
		for(var i=0; i<rows.length;i++){
			// Build a sting that uses the id and makes it into a class
			var rowToHightlight = '.' + rows[i];
			// Remove any spaces from the class name
			rowToHightlight = rowToHightlight.replace(/\s/,'');
			// find all the cells within the table that have the same class name as the cell selected and highlight them
			table.find(rowToHightlight).addClass('highlight');
		}
	}
}


$(document).ready(function() {
	
	// Apply the class highlightable to any cell in a table with the class name highlightTable so that the mouse pointer changes to a hand when hovered over
	$('.highlightTable td, .highlightTable th').addClass('highlightable');
	// Index the highlighttables so that rows and columns can be highlighted
	index_table ($('.highlightTable'));
 
	// When a table cell within the table body is clicked clear all the cells in the table of the highlight class then highlight both columns and rows with cross the cell
	$('.highlightTable tbody td').click( function() {
		var thisTable = $(this).parent('tr').parent().parent();
		thisTable.find('tr, td, th').removeClass('highlight');
		
		highlight_columns(thisTable,$(this));
		highlight_rows(thisTable,$(this));
	});
	
	// If a table header within the body of the table is clicked clear the highlight classes on any cells in the table then hightlight the row the header is in
	$('.highlightTable tbody th').click( function() {
		var thisTable = $(this).parent('tr').parent().parent();
		thisTable.find('tr, td, th').removeClass('highlight');
		
		highlight_rows(thisTable,$(this));
	});
	
	// If a table header within the head of the table is clicked clear the highlight classes on any cells in the table then hightlight the column the header is in
	$('.highlightTable thead th').click( function() {
		var thisTable = $(this).parent('tr').parent().parent();
		thisTable.find('tr, td, th').removeClass('highlight');
		
		highlight_columns(thisTable,$(this));
	});
});
