var hrefized = false,
	ie = getIEVersion(),
	px, py,
	yappend="";
if (typeof year == "number") yappend="&year="+year;
else var year="";

// Returns the version of Internet Explorer or -1 indicating the use of another browser.
function getIEVersion() {
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function capitalize(string) { return string.charAt(0).toUpperCase() + string.slice(1); };

//Yeah, it invalidates our SVG, but it's necessary for NyroModal until it updates to support SVG.
function hrefize() {
	if (!hrefized) {
		if (ie > 0 && ie < 9)
			$('area').each(function() { $(this).attr('href',$(this).attr('href')+year) }).nyroModal();
		else {
			$('svg a').each(function() { $(this).attr('href',$(this).attr('xlink:href')+yappend).attr('title',$(this).children("polygon, path").attr('id')+" County") }).nyroModal();
			$('svg polygon, svg path').css("fill","#58B");
		}
		hrefized = true;
	}
}

$(document).ready(function() {
	$("div.legslist").hide();
	$(".mapview").addClass("current");
	
	$(".mapview").click(function() {
		if (!$(this).hasClass("current")) {
			$("#tabs a").removeClass('current');
			$(this).addClass('current');
			$("div.legslist").hide();
			$(".map").show();
		}
		return false;
	});
	$(".legview").click(function() {
		if (!$(this).hasClass("current")) {
			$("#tabs a").removeClass('current');
			$(this).addClass('current');
			$(".map").hide();
			$("div.legslist").show();
		}
		return false;
	});
	
	//If our browser supports SVG, load the interactive map. Otherwise load the image map.
	if (ie > 0 && ie < 9) {
		$('#mappo').load('/includes/counties.php',function() { hrefize(); });
		$('.map').append("<p style='text-align:center; font-style:italic;'>You are viewing the basic map. To use the interactive map, please upgrade your browser to the latest version of <a href='http://www.mozilla.org/en-US/firefox/new/'>Firefox</a>, <a href='http://www.google.com/chrome'>Chrome</a>, or <a href='http://windows.microsoft.com/en-us/internet-explorer/products/ie/home'>Internet Explorer</a>.</p>");
	} else {
		$('#mappo').load('/images/counties.svg',function() { hrefize(); });
		$('#mappo').on("mouseover","polygon,path",function(e) {
			defcolor = $(this).css('fill');
			$(this).css('fill','#8BE');
			$('body').append('<div id="countyhover">'+$(this).attr('id').replace('_',' ')+'</div>');
			$('#countyhover').css('top',(getPosition(e).y - 20) + 'px').css('left',(getPosition(e).x - 50) + 'px');
		}).on("mouseout","polygon,path",function() {
			$(this).css('fill','#58B ');
			$("#countyhover").remove();
		});
		
	}
});

var data = {},
	curyear=2010,
	county = "<?php echo $id; ?>",
	branches = {"n":"Senate", "h":"House"};

$(document).ready(function() {
	getdata(curyear);
});

function getdata(sendyear) {
	curyear=sendyear;
	if (sendyear in data) {
		buildlist(data[sendyear]);
	} else {
		$.post(
			"/find-your-legislator-popup.php",
			{ json: "true", year: sendyear, id: county },
			function(newdata) {
				buildlist(newdata);
				data[sendyear] = data;
			},
			"json"
		);
	}
}

function buildlist(newdata) {
	var html;
	for (var branch in newdata) {
		html = '<tr><td colspan="2" class="thead"><h2>'+branches[branch]+'</h2></td></tr>';
		for (var leg in newdata[branch]) if (rank in newdata[branch][leg]) {
			html += '<tr><td><a href="/'+branches[branch]+'/'+newdata[branch][leg]['name']+'/'+curyear+'/"><span class="dot '+newdata[branch][leg]['victor']+'"></span> '+newdata[branch][leg]['name']+'</a>';
			if (newdata[branch][leg]['datein']) html += '<em>Appointed on '+newdata[branch][leg]['datein']+' to replace '+newdata[branch][newdata[branch][leg]['district']]['name']+'</em>';
			html += '</td><td class="right">';
			if (branch == 'n') html += "S"; else if (branch == 'h') html += "H";
			html += "D-"+newdata[branch][leg]['district']+'</td></tr>';
		}
	}
}

function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}
