//<![CDATA[

var places = [
      {
        "name": "greenroom", // 0
        "zoom": [0, 17],
        "posn": [53.474242, -2.242325],
        "html": "<div id=\"gr_map_info\"><h3>greenroom</h3>54-56 Whitworth Street West<br />Manchester M1 5WW</div>"
      },
];

var map;
var mgr;
var icons = {};

function grIcon(wpURL) {
  var icon = new GIcon();
  icon.image = wpURL + "/wp-content/plugins/gr_google_map/gr_map_icon.png";
  icon.shadow = wpURL + "/wp-content/plugins/gr_google_map/gr_map_shadow.png";
  icon.iconSize = new GSize(40, 40);
  icon.shadowSize = new GSize(40, 40);
  icon.iconAnchor = new GPoint(17, 17);
  icon.infoWindowAnchor = new GPoint(17, 17);
  return icon;
}

function mapDisplay(visible) {
  if (visible=="hide") {
    var linkHtml = "[<a href=\"javascript:mapDisplay('show')\">Show map</a>]";
    var display = "none"
  }
  else {
    var linkHtml = "[<a href=\"javascript:mapDisplay('hide')\">Hide map</a>]";
    var display = "block"
  }
  var infoClass = (visible=="hide") ? "show" : "hide";
  document.getElementById("map_info").setAttribute("class", infoClass);
  document.getElementById("map_info").innerHTML = linkHtml;
  document.getElementById("map").style.display = display;
}

function initDisplay() {
	var posn = new GLatLng(53.475187, -2.242325);
	var zoom = 16;
	map.setCenter(posn, zoom, G_NORMAL_MAP);
	mapDisplay("show");
}

function hideMap(wpURL, showCenter) {
  if (GBrowserIsCompatible()) {
    document.getElementById("map_info").innerHTML = "[<a href=\"javascript:loadMap('" + wpURL + "'," + showCenter + ")\">Show map</a>]";
    document.getElementById("map_info").setAttribute("class", "show");
  }
}

function loadMap(wpURL, showCenter) {
  if (GBrowserIsCompatible()) {
    document.getElementById("map").style.display = "block";
	map = new GMap2(document.getElementById("map"));
	if (showCenter) { // Display Lat, Lng for centre of map in div called 'map_info'
  	  GEvent.addListener(map, "moveend", function() {
	    var center = map.getCenter();
	    document.getElementById("map_info").innerHTML = center.toString();
	  });
	}
	GEvent.addListener(map, "load", function() {
	  var place = places[0];
	  var posn = new GLatLng(place["posn"][0], place["posn"][1]);
	  var html = place["html"];
	  map.openInfoWindowHtml(posn,html);
	});
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
//	map.addControl(new GOverviewMapControl());
	initDisplay();
	map.enableDoubleClickZoom();
//	window.setTimeout(setupMarkers, 0);
    setupMarkers(wpURL);
  }
}
/*
function getIcon(images) {
  var icon = null;
  if (images) {
	if (icons[images[0]]) {
	  icon = icons[images[0]];
	} else {
	  icon = new GIcon();
	  icon.image = "markermanager/" + images[0] + ".png";
	  var size = iconData[images[0]];
	  icon.iconSize = new GSize(size.width, size.height);
	  icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
	  icon.shadow = "markermanager/" + images[1] + ".png";
	  size = iconData[images[1]];
	  icon.shadowSize = new GSize(size.width, size.height);
	  icons[images[0]] = icon;
	}
  }
  return icon;
}
*/

function createMarker(posn, name, icon, html) {
	var marker = new GMarker(posn, { title: name, icon: icon });
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function setupMarkers(wpURL) {
  mgr = new GMarkerManager(map);
  for (var i in places) {
	var markers = [];
	var place = places[i];
//	var icon = getIcon(place["icon"]);
	var icon = grIcon(wpURL);
	var posn = new GLatLng(place["posn"][0], place["posn"][1]);
	var name = place["name"];
	var html = place["html"];
	markers.push(new createMarker(posn, name, icon, html));
	mgr.addMarkers(markers, place["zoom"][0], place["zoom"][1]);
  }
  mgr.refresh();
}

//]]>
