/**
 * @author cspanring
 */

// global MAPC object
var MAPC = {
	censustracts: {}
};

window.onload = function () {
	
	$("body").css({"background-color" : "#F5F5F5"});

	
	// init paper
	var map = Raphael(0, 0, 386, 386);
	ctSet = map.set(); // censustracts

	bg = map.rect(-1, -1, 387, 387).attr({
		fill: '#F5F5F5',
		'stroke-width': 0
	});
/*
	tl = map.path("M0 0 L 386 0").attr({
		stroke: "#fff",
		"stroke-width": 0.2,
	});
*/

	bg.mousemove(function(e){
		ttSet.hide();
	});

	$.getJSON("ct-min.json", function(data){
	 	jQuery.each(data, function(i, ct){
			MAPC.censustracts[i] = {};
			MAPC.censustracts[i].htc = ct.htc;
			MAPC.censustracts[i].tract = ct.tract;
			MAPC.censustracts[i].town = ct.town;
			attr = {
				fill: ctColor(MAPC.censustracts[i].htc),
				stroke: '#444',
				'stroke-width': 0.2,
				'stroke-linejoin': 'round'
			};
	 		MAPC.censustracts[i].geom = map.path(ct.geom).attr(attr); //create raphael object	 		
			ctSet.push(MAPC.censustracts[i].geom);		
	 		MAPC.censustracts[i].geom.mousemove(function (e) {		
				// info box stays in frame
				(e.clientX > 260) ? bufferX = 115 : bufferX = -12;
				(e.clientY > 320) ? bufferY = 45 : bufferY = -5;
				
				translateX = e.clientX - ttBox.attrs.x - bufferX;
				translateY = e.clientY - ttBox.attrs.y - bufferY;
								
				ttTown.attr({"text" : MAPC.censustracts[i].town});
				ttTract.attr({"text" : "Tract: " + MAPC.censustracts[i].tract});
				ttCat.attr({"text" : ctStatus(MAPC.censustracts[i].htc)});
				
				ttSet.translate(translateX, translateY).toFront().show();
	 			map.safari();
	 		});
	 	});
	 });
	
	// heading
	h = map.text(20, 40, "Census 2010 \n Hard to Count status").attr({
		fill: '#314160',
		'text-anchor': 'start',
		'font-size': '18px'
	});
	
	// legend
	lBox = map.rect(290, 140, 80, 90, 5).attr({
		fill: '#6C88C2',
		stroke: '#fff',
		'stroke-width': 0.2,
		'stroke-linejoin': 'round'
	});
	lCatbox1 = map.rect(300, 150, 16, 10).attr({
		fill: "rgb(238,242,225)",
		stroke: '#fff',
		'stroke-width': 0.2
	});
	lCat1 = map.text(320, 155, "Easier").attr({
		fill: "#fff",
		"text-anchor": "start",
		"font-size": "10px"
	});
	lCatbox2 = map.rect(300, 170, 16, 10).attr({
		fill: "rgb(115,193,95)",
		stroke: '#fff',
		'stroke-width': 0.2
	});
	lCat2 = map.text(320, 175, "Hard").attr({
		fill: "#fff",
		"text-anchor": "start",
		"font-size": "10px"
	});
	lCatbox3 = map.rect(300, 190, 16, 10).attr({
		fill: "rgb(41,119,67)",
		stroke: '#fff',
		'stroke-width': 0.2
	});
	lCat3 = map.text(320, 195, "Harder").attr({
		fill: "#fff",
		"text-anchor": "start",
		"font-size": "10px"
	});
	lCatbox4 = map.rect(300, 210, 16, 10).attr({
		fill: "rgb(38,88,40)",
		stroke: '#fff',
		'stroke-width': 0.2
	});
	lCat4 = map.text(320, 215, "Hardest").attr({
		fill: "#fff",
		"text-anchor": "start",
		"font-size": "10px"
	});
	
	// tooltip
	ttBox = map.rect(0, 0, 110, 44, 5).attr({
		fill: "#6C88C2",
		stroke: "#fff",
		"stroke-width": 0.2
	});
	ttTown = map.text(55, 10, "").attr({
		fill: '#fff',
		'text-anchor': 'middle',
		'font-size': '12px'
	});
	ttTract = map.text(55, 24, "test text").attr({
		fill: '#fff',
		'text-anchor': 'middle',
		'font-size': '10px'
	});
	ttCat = map.text(55, 36, "test text").attr({
		fill: '#fff',
		'text-anchor': 'middle',
		'font-size': '10px'
	});
	
	ttSet = map.set();
	ttSet.push(ttBox, ttTown, ttTract, ttCat);
	ttSet.hide();
	
	// source
	s = map.text(10, 380, "Data source: U.S. Census Bureau, Massachusetts Secretary of State, MAPC Analysis").attr({
		fill: '#314160',
		'text-anchor': 'start',
		'font-size': '9px'
	});
	
	// Classification - 15:Easier to count; 15-39:Hard to count; 39-65:Harder to count; 65-107:Hardest to count	
	// look up color
	ctColor = function(value) {
		if (value <= 15 ) return "rgb(238,242,225)";
		if (value > 15 && value <= 39) return "rgb(115,193,95)";
		if (value > 39 && value <= 65) return "rgb(41,119,67)";
		if (value > 65) return "rgb(38,88,40)";	
	};
	
	// look up category
	ctStatus = function(value) {
		if (value <= 15 ) return "Easier to Count";
		if (value > 15 && value <= 39) return "Hard to Count";
		if (value > 39 && value <= 65) return "Harder to Count";
		if (value > 65) return "Hardest to Count";		
	};
};

