// global array stores information about image maps
var imageMapProperties = [];

$(document).ready(function() {
	// find all image maps and read default image, current region, etc.
	$(".imagemap-container").each(function() {
		var s = $(this);
		var id = s.find("map").attr("id");
		var defaultImageSrc = s.find("#" + id + "-image").attr("src");
		
		imageMapProperties[id] = {
			defaultImage: defaultImageSrc,
			currentImage: defaultImageSrc,
			currentRegionID: null
		};
		
		// hide all region divs
		s.find(".imagemap-region").css("display", "none");
		
		// preload image map overlay images and show first region
		var i = 0;
		s.find("area").each(function() {
			var imagePath = $(this).attr("href");
			if (i == 0) {
				ShowMapRegion(id, 1, imagePath);
			} else {
				var img = new Image();
				$(img).attr("src", imagePath);
			}
			i++;
		});
				
	});
});

// show image on map
function ShowMapImg(targetMapID, imagePath) {
	$("#" + targetMapID + "-image").attr("src", imagePath);
}

// restores the current map image
function RestoreCurrentMapImage(targetMapID) {
	$("#" + targetMapID + "-image").attr("src", imageMapProperties[targetMapID].currentImage);
}

// switches to a map region
function ShowMapRegion(targetMapID, regionID, imagePath) {
	ShowMapImg(targetMapID, imagePath);
	$("#" + targetMapID + "-container .imagemap-region").css("display", "none");
	$("#" + targetMapID + "-region-" + regionID).css("display", "block");
	imageMapProperties[targetMapID].currentImage = imagePath;
	imageMapProperties[targetMapID].currentRegionID = regionID;
}