$(function(){
	var offsetX  = -22; // tooltip X offset
	var offsetY = 12;  // tooltip Y offset
	var mapwidth, mapheight;
	$(".imagemap dl.map").addClass("on");
	
	if ($.browser.msie) {
		mapwidth = $(".imagemap img.map").width();
		mapheight = $(".imagemap img.map").height();
		
		// set the location of the dl to the image map
		$(".imagemap dl.map").css("width", mapwidth);
		$(".imagemap dl.map").css("height", mapheight);
	}
	else {
		$(".imagemap img.map").load(function() {
			mapwidth = $(this).width();
			mapheight = $(this).height();

			// set the location of the dl to the image map
			$(".imagemap dl.map").css("width", mapwidth);
			$(".imagemap dl.map").css("height", mapheight);
		});
	}
	
	//var imagemaptop = $(".imagemap img.map").offset().top; (doesn't work in Safari)
	var imagemaptop = $("div.title").offset().top + $("div.title").height();
	$(".imagemap dl.map").css("top", imagemaptop);
	var imagemapleft = $(".imagemap img.map").offset().left;
	$(".imagemap dl.map").css("left", 0);
	//$(".imagemap dl.map").css("left", imagemapleft);

	$(".imagemap dt").mouseover(function() {
		var dd = $(this).next();
		//get width and height of the dd
		var ddwidth = dd.width();
		var ddheight = dd.height();
		//figure out where tooltip should be places based on point location
		var newX = $("a", this).offset().left - imagemapleft + offsetX;
		var newY = $("a", this).offset().top - imagemaptop + offsetY;
		//check if tooltip fits map width 
		if ((newX + ddwidth) > mapwidth - 24) {
			dd.css("left", newX - ddwidth - 24);
		} else {
			dd.css("left", newX);
		};
		//check if tooltip fits map height 
		if ((newY + ddheight) > mapheight) {
			dd.css("top", newY - ddheight - 14);
		} else {
			dd.css("top", newY);
		};
		// set the location link to selected
		$("div.locationlinks a#locationlink" + $("a", this).attr("id")).addClass("hover");
		// scroll to the link's location in the list (GWRR specific)
        //$(".railroad #secondary-nav li.selected ul ul.tall")[0].scrollTo("a#locationlink" + $("a", this).attr("id"));
	});
	$(".imagemap dt, div.locationlinks a").mouseout(function() {
		$(".imagemap dd").css("left", -9999);
		// remove the hover class from locationlinks and dt
		$(".imagemap dt a, div.locationlinks a").removeClass("hover");
	});
});

function showImageMapLocation(locationID) {
    $(".imagemap dt a#" + locationID).parent().trigger("mouseover");
    $(".imagemap dt a#" + locationID).addClass("hover");
}