﻿
jQuery.fn.addTableMarkup = function(){
	return this.each(function(){
		var table = $(this);
		
		table.find("thead,tbody").each(function(){
			var rows = $(this).find("> tr");
			
			rows.eq(0).addClass("first");
			rows.eq(-1).addClass("last");
			
			rows.each(function(){
				var cells = $(this).find("> td,> th");
				
				cells.eq(0).addClass("first");
				cells.eq(-1).addClass("last");
			});
		})
	});
};

jQuery.fn.adjustPaddingForSuffixesInCells = function() {
	return this.each(function(){
		var table = $(this).filter("table");
		
		var headerCells = table.find("> thead > tr > th");
		
		var rows = table.find("> tbody > tr");
		
		headerCells.each(function(){
			var headerCell = $(this);
			
			if(headerCell.is(".numerical")){
				var indexOfHeaderCell = headerCell.index();
				
				var cellsWithSuffix = $();
				var cellsWithoutSuffix = $();
				
				rows.each(function(){
					var row = $(this);
					
					var cell = row.children().eq(indexOfHeaderCell);
					
					var cellHtml = cell.html();
					
					if(cellHtml.match(/(\d+)([^\d<]+)/)){
						cellsWithSuffix = cellsWithSuffix.add(cell);
						
						cell.html(cellHtml.replace(/(\d+)([^\d<]+)/, "$1<span class=\"suffix\">$2</span>"));
					} else {
						cellsWithoutSuffix = cellsWithoutSuffix.add(cell);
					}
				});
				
				var widestSuffixWidth = 0;
				
				cellsWithSuffix.find("span.suffix").each(function(){
					var suffixWidth = $(this).outerWidth();
					
					if(widestSuffixWidth < suffixWidth){
						widestSuffixWidth = suffixWidth;
					}
				});
				
				cellsWithoutSuffix.add(headerCell).append("<span style=\"padding-right:" + widestSuffixWidth + "px" + "\"></span>");
			}
		});
	});
};

jQuery.fn.enableNumericalCellsInTable = function() {
	return this.each(function(){
		var table = $(this);
		
		var indexesWhereContentIsNumerical = [];
		var maxIndex = -1;
		
		var dubiousCells = [];
		
		table.find("tr").each(function(){
			var row = $(this);
			
			row.find("td").each(function(){
				var cell = $(this);
				
				var html = cell.html();
				
				if(!html){
					return;
				}
				
				
				html = html.replace("–","-");
				html = html.replace(/&[^;]+;/g,"");
				html = html.replace(/<[^>]+>/g,"");
				html = html.replace(/[,.\(\)]/g,"");
				html = html.replace(/\s/g,"");
				
				if(html.match(/\d\d\d\d\d\d[^\d]\d\d\d\d/)){ // org nr
					return;
				}
				
				if(html.match(/\d\d\d\d[^\d]\d\d[^\d]\d\d/)){ // datum
					return;
				}
				
				if(html.length > 20){
					return;
				}
				
				if(html == "-"){
					dubiousCells[dubiousCells.length] = cell;
				}
				
				if(html == "*"){
					dubiousCells[dubiousCells.length] = cell;
				}
				
				var cellContentIsNumerical = !isNaN(parseInt(html));
				
				if(cellContentIsNumerical){
					cell.addClass("numerical");
					
					var index = cell.index();
					
					indexesWhereContentIsNumerical[index] = true;
					
					if(index > maxIndex){
						maxIndex = index;
					}
				}
			});
		});
		
		for(var i = 0; i < maxIndex + 1; i++){
			if(indexesWhereContentIsNumerical[i] == true){
				table.find("thead tr:last").each(function(){
					$(this).find("th:nth-child(" + (i+1) + ")").not(".heading").addClass("numerical");
				});
			}
		}
		
		for(var i = 0; i < dubiousCells.length; i++){
			var cell = dubiousCells[i];
			if(indexesWhereContentIsNumerical[cell.index()] == true){
				cell.addClass("numerical");
			}
		}
	});
};



$(function(){
    $("#main table").addTableMarkup().adjustPaddingForSuffixesInCells().enableNumericalCellsInTable();
});
