$(document).ready(function() {
	  
	  /*************************/
	 /* Dynamic manhattan map */
	/*************************/
	var clicked = new Array();
	
	$("#map li").click(function() {
		
		//Get the regions attribute for the currently clicked map area and convert to an array
		var regions = $(this).attr("region");
		var regArray = regions.split(', ');
		
		//If the map area is not already highlighted
		if ($(this).attr('class').indexOf('map' + $(this).attr('id') + 'clicked') == -1) {
			
			//add to array of highlighted items and highlight all clicked map areas
			clicked.push($(this));
			for(var i in clicked) {
				$(clicked[i]).addClass('map' + $(clicked[i]).attr('id') + 'clicked');
			}
			
			//and check off all appropriate box(es)
			for ( var i in regArray ) {
		    	checkoff( regArray[i] );
			}
		
		//otherwise remove the highlighting class and any applicable checkbox
		} else {
			
			//remove from highlighted array and remove highlighting class
			clicked.splice(clicked.indexOf($(this)), 1);
			$(this).removeClass('map' + $(this).attr('id') + 'clicked');
			
			//initialize variables
			var thisreg = $(this).attr('region');   //comma-separated string of regions
			var thisregbits = thisreg.split(', ');  //array of regions
			var rundecheck = false;                 //boolean whether or not decheck needs to be run
			var todecheck = new Array();            //array of regions that should be dechecked
			
			//make sure the regions aren't included by other highlighted map areas
			for(var j in thisregbits) {
				for(var k in clicked) {
					if ($(clicked[k]).attr('reg') != thisregbits[j]) {
						todecheck.push(thisregbits[j]);
						rundecheck=true;
					}
				} 
			}
			
			//decheck verified regions
			if (rundecheck) {
				for(var m in todecheck) {
					decheck(todecheck[m]);
				}
			}
		}
		
	});
	
	//Hover
		
	$("#map li").mouseenter(function() {
		$(this).addClass('map' + $(this).attr('id') + 'hover');
		$(this).mouseleave(function() {
			$(this).removeClass('map' + $(this).attr('id') + 'hover');
		});
	});
	
	//when a check box is clicked, highlight appropriate map areas
	$("#location input[type='checkbox']").click(function() {
		var region = ($(this).attr('id')).replace("ctl00_ContentPlaceHolder1_", "");
		if (region == 'nAM' && $(this).attr('checked')) {
			$("#location input[type='checkbox']").each(function() {
				if ($(this).attr('id') != 'ctl00_ContentPlaceHolder1_n11' && $(this).attr('id') != 'ctl00_ContentPlaceHolder1_n9' 
					&& $(this).attr('id') != 'ctl00_ContentPlaceHolder1_n10' && $(this).attr('id') != 'ctl00_ContentPlaceHolder1_nofee') {
					$(this).attr('checked', true);
				}
			});
			$("#map li").each(function() {
				$(this).addClass('map' + $(this).attr('id') + 'clicked');
			});
		} else if (region == 'nAM' && !$(this).attr('checked')) {
			$("#location input[type='checkbox']").each(function() {
				$(this).attr('checked', false);
			});
			$("#map li").each(function() {
				$(this).removeClass('map' + $(this).attr('id') + 'clicked');
			});
		} else if ($(this).attr('checked')) {
			$("#map li[region*='"+region+"']").each(function () {	
				$(this).addClass('map' + $(this).attr('id') + 'clicked');
			});
		} else {
			$("#map li[region*='"+region+"']").each(function () {	
				$(this).removeClass('map' + $(this).attr('id') + 'clicked');
			});
		}
	});
	
});

function checkoff(region) {
	var regionId = "ctl00_ContentPlaceHolder1_" + region;
	$("#"+regionId).attr('checked', true);
}
function decheck(region) {
	var regionId = "ctl00_ContentPlaceHolder1_" + region;
	$("#"+regionId).attr('checked', false);
}
