var tabulate = function tabulate(index, data, caption, columns) {
	var top = d3.select("#" + index);
    
    var table = d3.select("#" + index).append("table"),
    	caption = table.append("caption")
    		.text(caption),
        thead = table.append("thead"),
        tbody = table.append("tbody");
  
 
    
    // append the header row
    thead.append("tr")
        .selectAll("th")
        .data(columns)
        .enter()
        .append("th")
        .text(function(column) { return column; });

    // create a row for each object in the data
    var rows = tbody.selectAll("tr")
        .data(data)
        .enter()
        .append("tr");

    // create a cell in each row for each column
    var cells = rows.selectAll("td")
        .data(function(row) {
            return columns.map(function(column) {
                return {column: column, value: row[column]};
            });
        })
        .enter()
        .append("td")
        .attr("class", function(d,i) { var text = columns[i], leadChar = text[0]; 
        	if (leadChar >= 0 && leadChar <= 9){
        		text = "X" + text;									// as class names starting with a digit don't work - insert an x
        	}
        	return text.toLowerCase().replace(/\W+/g, "-"); })	// replace non-alpha chars with a -
        .text(function(d) { return d.value; });
        
//    var link1 = top.append("div")
//    	.append("a")
//   	.attr("href", "/sites/poverty/files/poverty/data/csv/" + index + ".csv")
//    	.text("download table data as .csv file");
//
//    var link2 = top.append("div")
//    	.append("a")
//    	.attr("href", "/sites/poverty/files/poverty/data/" + index + ".json")
//    	.text("download table data as .json file");
    	
    return table;
}

function povertyHeatmap(num) {
    var red_value = 0,
        green_value = 0,
        blue_value = 0,
        
        high_red = 166,
        mid_red = 241,
        low_red = 0,
        
        high_green = 0,
        mid_green = 201,
        low_green = 104,
        
        high_blue = 42,
        mid_blue = 52,
        low_blue = 55,
         
        high_point = 100,
        mid_point = 50,
        low_point = 0,


        top_interp = (num - mid_point) / (high_point - mid_point),
        low_interp = (mid_point - num) / (mid_point - low_point);
    
    top_interp = top_interp * top_interp;
    low_interp = low_interp * low_interp;

    if (num > mid_point) {
        red_value = (high_red - mid_red) * top_interp + mid_red;
        green_value = (high_green - mid_green) * top_interp + mid_green;
        blue_value = (high_blue - mid_blue) * top_interp + mid_blue;
    } else {
        red_value = mid_red - low_interp * (mid_red - low_red);
        green_value = mid_green - low_interp * (mid_green - low_green);
        blue_value = mid_blue - low_interp * (mid_blue - low_blue);
    }

    return "rgb(" + d3.round(red_value, 0) + ", " + d3.round(green_value, 0) + ", " + d3.round(blue_value, 0) + ")";
}
function heatMapColor(num) {
    return isNumber(num) ? povertyHeatmap(num) : "#e5e5e5";
}

