var version = parseInt(navigator.appVersion),
canShowLayers = Boolean (navigator.appCodeName=="Mozilla" && version >= 4),
firsttime = true,
s_base = 'images/layers/',
a_places = [
	['map', null, null, '0,1,760,420'],
	['art_council', 'Arts Council of Princeton', '350,301,15', '0,1,760,420'],
	['drumthwacket', 'Drumthwacket', '2,431,42,467', '0,108,760,528'],
	['historical_society', 'Historical Society / Bainbridge House', '450,419,15', '0,108,760,528'],
	['mccarter', 'McCarter Theatre / Berlind Theatre', '202,665,15', '0,370,760,790'],
	['morven', 'Morven', '74,431,12', '0,108,760,528'],
	['princeton_university', 'Princeton University', '354,464,15', '0,108,760,528'],
	['nassau_inn', 'Nassau Inn', '317,370,10', '0,108,760,528'],
	['njtransit', 'NJ Transit Princeton Station / DINKY', '250,722,15', '0,370,760,790'],
	['palmer_square', 'Palmer Square', '316,392,10', '0,108,760,528'],
	['regional_chamber', 'Princeton Regional Chamber and CVB', '482,412,15', '0,108,760,528'],
	['plaza', 'Albert E. Hinds Community Plaza', '376,350,10', '0,108,760,528'],
	['princeton_boro', 'Princeton Borough Hall / POLICE', '101,417,12', '0,108,760,528'],
	['princeton_shopping', 'Princeton Shopping Center', '818,35,856,86', '120,1,890,420'],
	['princeton_library', 'Princeton Public Library', '376,329,10', '0,108,760,528'],
	['theological_seminary', 'Princeton Theological Seminary', '73,604,15', '0,370,760,790'],
	['umcp', 'University Medical Center at Princeton', '399,82,15', '120,1,890,420'],
	['westminster_choir', 'Westminster Choir College', '712,100,15', '120,1,890,420'],
	['ymca_ywca', 'YMCA / YWCA', '194,271,15', '0,1,760,420']
],
getScrollPos = window.pageXOffset == null
	? function (w) { return {'x' : w.document.body.scrollLeft, 'y' : w.document.body.scrollTop} }
	: function (w) { return {'x' : w.pageXOffset, 'y' : w.pageYOffset} }
getSize = window.pageXOffset == null
	? function (w) { return {'x' : w.frameElement.offsetWidth, 'y' : w.frameElement.offsetHeight} }
	: function (w) { return {'x' : w.innerWidth, 'y' : w.innerHeight} }

function showLayer(n_idx) {
  if (canShowLayers) {
		var s_image = a_places[n_idx][0],
    mapLayer = new Image(860,790)
    mapLayer.src = s_image ? s_base + s_image + '.gif' : "images/layers/mapbase.gif",
		e_frame = window.top.map
    e_frame.document.images["map"].src = mapLayer.src;
    // To fix MSIE bug which blackens the screen.
    if (firsttime && (navigator.appName == "Microsoft Internet Explorer")) {
      mapLayer2 = new Image(860,790);
      mapLayer2.src = "images/spacer.gif";
      e_frame.document.images["map"].src = mapLayer2.src;
      e_frame.document.images["map"].src = mapLayer.src;
    }
    firsttime = false;
	}
}

function showNScroll(n_idx) {
  if (canShowLayers) {
		var e_frame = window.top.map, a_coords
		if (a_places[n_idx][3]) {
			a_coords = a_places[n_idx][3].split(',')
			var	o_off = getScrollPos(e_frame), o_size = getSize(e_frame),
			x = Math.min(a_coords[0], Math.max(a_coords[2] - o_size.x, o_off.x)),
			y = Math.max(a_coords[1], Math.min(a_coords[3] - o_size.y, o_off.y))
			if (x != o_off.x || y != o_off.y)
				e_frame.scrollTo(x, y)
		}
		showLayer(n_idx)
  }
}