var map;
var mgr;
var bounds;

$(function(){
	if (!GBrowserIsCompatible()) {
		alert("Entschuldigung, Google Maps kann in Ihrem Browser nicht angezeigt werden.");
		return;
	}

	map = new GMap2(document.getElementById("Map"));
	map.addControl(new GLargeMapControl());
	
	// ====== Restricting the range of Zoom Levels =====
  // Get the list of map types      
  var mt = map.getMapTypes();
  // Overwrite the getMinimumResolution() and getMaximumResolution() methods
  for (var i=0; i<mt.length; i++) {
    mt[i].getMinimumResolution = function() {return 6;}
    mt[i].getMaximumResolution = function() {return 11;}
  }
	
	var row, minLng, minLat, maxLng, maxLat;
	
	for(var i in rows) {
		row = rows[i];
		if(typeof minLng == 'undefined' || minLng > row.longitude) {
			minLng = row.longitude;
		}
		if(typeof minLat == 'undefined' || minLat > row.latitude) {
			minLat = row.latitude;
		}
		if(typeof maxLng == 'undefined' || maxLng < row.longitude) {
			maxLng = row.longitude;
		}
		if(typeof maxLat == 'undefined' || maxLat < row.latitude) {
			maxLat = row.latitude;
		}
	}
	
	bounds = new GLatLngBounds(new GLatLng(minLat, minLng), new GLatLng(maxLat, maxLng))
	
	// Add a move listener to restrict the bounds range
  GEvent.addListener(map, "move", function() {
    checkBounds();
  });
	map.setCenter(new GLatLng(47.665387, 13.172607), 7);
	map.enableDoubleClickZoom();
	map.enableScrollWheelZoom();
	// map.enableContinuousZoom();
	map.addControl(new GMapTypeControl());
	
	addMarkers();
});

$(document).unload(GUnload);

// If the map position is out of range, move it back
function checkBounds() {
  // Perform the check and return if OK
  if (bounds.contains(map.getCenter())) {
    return;
  }
  // It`s not OK, so find the nearest allowed point and move there
  var C = map.getCenter();
  var X = C.lng();
  var Y = C.lat();

  var AmaxX = bounds.getNorthEast().lng();
  var AmaxY = bounds.getNorthEast().lat();
  var AminX = bounds.getSouthWest().lng();
  var AminY = bounds.getSouthWest().lat();

  if (X < AminX) {X = AminX;}
  if (X > AmaxX) {X = AmaxX;}
  if (Y < AminY) {Y = AminY;}
  if (Y > AmaxY) {Y = AmaxY;}
  //alert ("Restricting "+Y+" "+X);
  map.setCenter(new GLatLng(Y,X));
}


// *** ADD MARKERS TO THE MAP *** ///
function addMarkers() {
	mgr = new MarkerManager(map);

	// *** LOCATIONS ***
	var icon = new GIcon();
	icon.image = "/images/logo.png";
	icon.iconSize = new GSize(26, 34);
	icon.iconAnchor = new GPoint(13, 34);
	icon.infoWindowAnchor = new GPoint(13, 0);

	var markers = [];
	for (var i in rows) {
		var row = rows[i];

		var marker = new GMarker(new GLatLng(row.latitude, row.longitude), { icon: icon });
		marker.id = row.id;
		marker.index = i;
		
		GEvent.addListener(marker, 'click', function() {
			var m = this;
			if(rows[this.index].image)
				this.openInfoWindowHtml('<div style="min-height: 100px; width: 370px;"><div style="width: 165px;">' + rows[this.index].text + '</div><div class="MapImage"><img src="' + rows[this.index].image + '" /></div></div>', {onOpenFn: function() {}});
			else
				this.openInfoWindowHtml('<div>' + rows[this.index].text + '</div>', {onOpenFn: function() {}});
			// geht ned gscheid
			//map.panTo(this.getLatLng());
		});

		markers.push(marker);
	}
	mgr.addMarkers(markers, 0);

	mgr.refresh();
}

$(document).ready(function(){
	$j("#Map img").removeAttr("title");
});