// Init
$(function(){

	// Hide Teasers
	$('.view-members').hide();
	
	// Add marker
	$('#group-photo').append('<div class="marker">&rsaquo;</div>');
	
	// Setup click handler
	$('#groupmap area').click(
		function(){
			var bid = this.getAttribute('id').replace(/[^0-9]/g, '');
			return loadBioInfo(bid, this.getAttribute('coords'), false);
		}
	).dblclick(
		function(){
			var bid = this.getAttribute('id').replace(/[^0-9]/g, '');
			return loadBioInfo(bid, this.getAttribute('coords'), $(this).attr('href'));
		}
	);

});

// Load bio info on click
function loadBioInfo(bid, coordinates, url) {
	bid = '#bio-'+ bid;
	
	// Check double click
	if($(bid +' a.more').size() && url) {
		location.href = url;
		return;
	}
	
	// Class the marker
	if($(bid +' a.more').size()) {
		$('#group-photo div.marker').addClass('bio-marker');
	}
	else {
		$('#group-photo div.marker').removeClass('bio-marker');
	}
	
	// Set name
	var name = $(bid +' h2 a').html();
	
	// Set text
	var text = $(bid +' .content').html();
	if (!text) {
		text = '<p>Select a person from the group photo to learn more.</p>';
	}
	$('.short-bio').html('<h3>'+ name +'</h3>'+ text).fadeIn();
	
	// get x
	var coords = coordinates.split(',');
	var c = 0;
	var left = new Array();
	for (var i=0; i<coords.length; i = i+2) {
		left[c] = parseInt(coords[i]);
		c++;
	}
	var x = Math.min.apply(Math, left);
	// get y
	var y = 0;
	var coords = coordinates.split(',');
	for (i=0; i<coords.length; i=i+2) {
		if (coords[i+1]) {
			y = y + parseInt(coords[i+1]);
		}
	}
	y = Math.ceil(y/left.length); // left coords should equal count of top coords
	
	// fix coordinates
	fixed = coordFix(x, y);
	x = fixed[0];
	y = fixed[1];
	
	// move marker
	$('#group-photo .marker').css({left : x +'px', top : y +'px'}).fadeIn();
	
	return false;
}

function coordFix(x, y) {
	if ((x == 392 || x == 391 || x == 393) && (y == 151 || y == 150 || y == 152)) {
		return new Array(397, 60);
	}
	else if ((x == 427 || x == 426 || x == 428) && (y == 244 || y == 243 || y == 245)) {
		return new Array(440, 210);
	}
	else if ((x == 84 || x == 83 || x == 85) && (y == 318 || y == 317 || y == 319)) {
		return new Array(111, 366);
	}
	else if ((x == 304 || x == 303 || x == 305) && (y == 293 || y == 292 || y == 294)) {
		return new Array(310, 330);
	}
	else if ((x == 345 || x == 344 || x == 346) && (y == 178 || y == 177 || y == 179)) {
		return new Array(333, 135);
	}
	else if ((x == 215 || x == 214 || x == 216) && (y == 289 || y == 288 || y == 290)) {
		return new Array(275, 265);
	}
	else if ((x == 11 || x == 10 || x == 12) && (y == 242 || y == 241 || y == 243)) {
		return new Array(60, 250);
	}
	else if ((x == 399 || x == 398 || x == 400) && (y == 173 || y == 172 || y == 174)) {
		return new Array(420, 160);
	}
	else if ((x == 43 || x == 42 || x == 44) && (y == 324 || y == 323 || y == 325)) {
		return new Array(110, 395);
	}
	else if ((x == 322 || x == 321 || x == 323) && (y == 220 || y == 219 || y == 221)) {
		return new Array(370, 220);
	}
	else if ((x == 90 || x == 89 || x == 91) && (y == 181 || y == 180 || y == 182)) {
		return new Array(77, 162);
	}
	else if ((x == 269 || x == 268 || x == 270) && (y == 156 || y == 155 || y == 157)) {
		return new Array(253, 156);
	}
	else {
		return new Array(x, y);
	}
}

