function locationsInit() {
	window.currentPin = '';
  var mapField = document.getElementById('regionSearch');
  if (!GBrowserIsCompatible() || !mapField) {
    return;
  }

  mapField.map = new GMap2(mapField);
  mapField.geocoder = new GClientGeocoder();
  // Coordinates for Hameln, Germany
  mapField.map.setCenter(new GLatLng(47.262692, 11.3947), 9);
  mapField.map.addMapType(G_PHYSICAL_MAP);
  mapField.map.setMapType(G_PHYSICAL_MAP);
  mapField.map.addControl(new GMapTypeControl());
  mapField.map.addControl(new GLargeMapControl());
  mapField.map.enableScrollWheelZoom();
  GEvent.addListener(mapField.map, 'click', locationsClickMap)

  if(typeof initMarkers == 'function') {
    initMarkers();
  }
}

function locationsClickMap(marker, loc, markerLoc) {
  console.log(marker, loc, markerLoc);
  var mapField = document.getElementById('regionSearch');
	if (!marker || !mapField) {
		return;
	}

	if (!markerClicked) {
		return;
	}

	markerClicked = false;
  
	// mapField.map.openInfoWindowHtml(markerLoc, marker.html);
	mapField.map.openInfoWindowHtml(markerLoc, marker.html, {pixelOffset: new GSize(0, -56)});
}

function createIcon(pin) {
  try {_icons} catch (e) {_icons = {};};
  if (!_icons[pin]) {
    var icon = new GIcon();
    icon.image = "/images/pins/" + pin + ".png";
    icon.iconSize = new GSize(58, 83);
    icon.shadow = "/images/pins/shadow.png";
    icon.shadowSize = new GSize(58, 83);
    icon.iconAnchor = new GPoint(29, 69);
    icon.infoWindowAnchor = new GPoint(28, 14);
    icon.transparent = "/images/pins/mask.png";
    icon.imageMap = [42,13,43,14,43,15,43,16,43,17,43,18,43,19,43,20,43,21,43,22,43,23,43,24,43,25,43,26,43,27,43,28,43,29,43,30,43,31,43,32,43,33,43,34,43,35,43,36,43,37,43,38,43,39,43,40,43,41,42,42,39,43,38,44,38,45,37,46,37,47,37,48,36,49,36,50,35,51,35,52,35,53,34,54,34,55,34,56,33,57,33,58,32,59,32,60,32,61,31,62,31,63,30,64,30,65,30,66,29,67,29,68,28,69,28,69,28,68,28,67,27,66,27,65,26,64,26,63,26,62,25,61,25,60,25,59,24,58,24,57,23,56,23,55,23,54,22,53,22,52,21,51,21,50,21,49,20,48,20,47,19,46,19,45,19,44,18,43,15,42,14,41,14,40,14,39,14,38,14,37,14,36,14,35,14,34,14,33,14,32,14,31,14,30,14,29,14,28,14,27,14,26,14,25,14,24,14,23,14,22,14,21,14,20,14,19,14,18,14,17,14,16,14,15,14,14,15,13];
    _icons[pin] = icon;
  }
  return _icons[pin];
}

function addMarker(entry, force) {
  try {_positions} catch (e) {_positions = {};};
  try {_pinsGrouped} catch (e) {_pinsGrouped = {};};
  var mapField = document.getElementById('regionSearch');
  var key = entry.pin + ":" + entry.latitude + "x" + entry.longitude;
  if (entry.pin) {
    if (!_pinsGrouped[entry.pin])
      _pinsGrouped[entry.pin] = [];
    _pinsGrouped[entry.pin].push(entry);
  }
  if (!_positions[key] || force) {
    if (entry.pin && entry.pin != "default") {
      marker = new GMarker(new GLatLng(entry.latitude, entry.longitude), {icon: createIcon(entry.pin)});
    } else {
      marker = new GMarker(new GLatLng(entry.latitude, entry.longitude));
    }
    marker.title = entry.title;
    marker.html = entry.html;
    GEvent.addListener(marker, 'click', locationsClickMarker);
    mapField.map.addOverlay(marker, { title : entry.title });
    marker.show();
    _positions[key] = true;
  }
}

function showPins(pin) {
	if(window.currentPin != pin){
		window.currentPin = pin;
	  var mapField = document.getElementById('regionSearch');
	  mapField.map.clearOverlays();
	  _positions = {};
	  try {
	    var entries = _pinsGrouped[pin] || [];
	  } catch (e) {
	    var entries = [];
	  }
	  for (var i = 0; i < entries.length; i++) {
	    var entry = entries[i];
	    var key = entry.pin + ":" + entry.latitude + "x" + entry.longitude;
	    if (!_positions[key]) {
	      if (entry.pin != "default") {
	        marker = new GMarker(new GLatLng(entry.latitude, entry.longitude), {icon: createIcon(entry.pin)});
	      } else {
	        marker = new GMarker(new GLatLng(entry.latitude, entry.longitude));
	      }
	      marker.title = entry.title;
	      marker.html = entry.html;
	      GEvent.addListener(marker, 'click', locationsClickMarker);
	      mapField.map.addOverlay(marker, { title : entry.title });
	      marker.show();
	      _positions[key] = true;
	    }
	  }	
	} else {
		window.currentPin = '';
		initMarkers();
	}
  
}

function locationsClickMarker(loc) {
  // console.log(loc);
  var mapField = document.getElementById('regionSearch');
	if (!marker || !mapField) {
		return;
	}

	markerClicked = true;
}

