// 3. Google Maps *****************************************

var Standorte = new Array();
var Markers = new Array();
var Infowindows = new Array();
var geocoder;
var map;
var bounds = new google.maps.LatLngBounds();

var infowindow = null;

function refreshMap(){
	Standorte = new Array();	
	if(jQuery("#merkerUL").length > 0) {
		jQuery('#merkerUL li').each(function(index) {		
			Standorte[index] = new Object();
			Standorte[index][0] = jQuery(this).find("a").attr("title");
			Standorte[index][1] = jQuery(this).find("a").attr("name");
			Standorte[index][2] = jQuery(this).find(".lat").html();
			Standorte[index][3] = jQuery(this).find(".lng").html();
			Standorte[index][4] = jQuery(this).find(".detail").html();
			Standorte[index][5] = index;		
		 });
		initialize();
	}
}

function refreshMapliste(){
	if(jQuery(".merkerInfoTitle_nr").length > 0) {
		jQuery(".merkerInfoTitle_nr").each(function(index) {
			jQuery(this).html((index+1));
		});		
	}
}

jQuery(document).ready(function(){
	refreshMap();
	
	jQuery("#merkerUL li a[class*=merkerInfo]").hover(function() {
		fitMaptoMarker(jQuery(this).parent().find(".lat").html(),jQuery(this).parent().find(".lng").html());
	});		
});	

function initialize() {
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(53.7967533, 12.1729877);
	var myOptions = {
	  zoom: 10,
	  center: latlng,
	  mapTypeId: google.maps.MapTypeId.TERRAIN,
	  mapTypeControl: true,
	  streetViewControl: false,
      	  mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DEFAULT}
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);		
	setMarkers(map, Standorte);
	
}

function setMarkers(map, locations) {
  /*var shadow = new google.maps.MarkerImage('http://www.agentur-mvweb.de/lab/html/diak/_conf/system/images/mapsmarker_shadow.png',
      new google.maps.Size(40, 28),
      new google.maps.Point(0,0),
      new google.maps.Point(0, 28));*/
  var shape = {
      coord: [1, 1, 1, 20, 28, 20, 28 , 1],
      type: 'poly'
  };
  for (var i = 0; i < locations.length; i++) {
		//
		(function() {
		//Ort
		var standort = locations[i];
		// Geo
		var myLatLng = new google.maps.LatLng(standort[2], standort[3]);
		// Marker Symbol
		var image = new google.maps.MarkerImage('http://195.98.199.86/_conf/system/getMarker.png?mT='+ (i+1),
			new google.maps.Size(31, 41),
			new google.maps.Point(0, 0),
			new google.maps.Point(12, 41));
		// Marker
		var marker = new google.maps.Marker({
			position: myLatLng,
			map: map,
			/*shadow: shadow,*/
			icon: image,
			shape: shape,
			title: standort[0],
			zIndex: standort[5]
		});

		// Infowindow
		var contentString = '<strong>'+standort[0]+'</strong><br/>'+standort[4]+'<br/><a href="'+standort[1]+'">weitere Informationen</a>';
		var thisInfowindow = new google.maps.InfoWindow({
			content: contentString
		});			

		google.maps.event.addListener(marker, 'click', function() { 	
			if(infowindow!=null)infowindow.close();
			infowindow = thisInfowindow;
			infowindow.open(map,marker);
		});
			
		//Ausschnitt
		bounds.extend(myLatLng);
		map.fitBounds(bounds);
		//
		})();
		//
  }   
}

function fitMaptoMarker(thisLat,thisLng) {
	map.setCenter(new google.maps.LatLng(thisLat,thisLng));
	map.setZoom(10);
}


function codeAddress() {
	var address = "R?bel, Mecklenburg";
	geocoder.geocode( { 'address': address}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK) {
		map.setCenter(results[0].geometry.location);
		alert(results[0].geometry.location);
		var marker = new google.maps.Marker({
			map: map, 
			position: results[0].geometry.location
		});
	  } else {
		alert("Geocode was not successful for the following reason: " + status);
	  }
	});
}

// -- div

function ow (url,w,h,t) {
	 fenster = window.open(url, t, "width="+w+",height="+h+",status=yes,scrollbars=yes,resizable=yes");
	 fenster.focus();
	}

