// *********** AJAX ***************************
if( !window.XMLHttpRequest && window.ActiveXObject )
{
	window.XMLHttpRequest = function () {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

// *********** Lade GoogleMaps API ************
google.load("maps", "2.x");

// *********** Map auf dieser Seite ***********
var counter = 0;
var markers = [];
var icon = [];
var map = null;
var mapname;
var longi_avg;
var lati_avg;
var longi_max;
var lati_max;
var longi_min;
var lati_min;
var mybaseIcon;
var bounds;
var geocoder = false;
var gc_points = [];
var ajax_url = '/karte.php';
var counter_gc = 0;
var xmlhttp = [];
var counter_ajax = 0;

function initializeEnfieldMap()
{
	mybaseIcon = new GIcon();

	mybaseIcon.iconSize = new GSize(32, 32);
	mybaseIcon.shadowSize = new GSize(56, 32);
	mybaseIcon.iconAnchor = new GPoint(16, 32);
	mybaseIcon.infoWindowAnchor = new GPoint(16, 0);

	icon[10] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon2.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon2s.png");
	icon[20] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon3.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon3s.png");
	icon[30] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon21.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon21s.png");
	icon[40] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon48.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon48s.png");
	icon[50] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon50.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon50s.png");
	icon[60] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon32.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon32s.png");
	icon[70] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon19.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon19s.png");
	icon[80] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon18.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon18s.png");
	icon[90] = new GIcon(mybaseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon55.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon55s.png");

	if( GBrowserIsCompatible() )
	{
		mapObj = document.getElementById("map_"+mapname);
		if( mapObj != "undefined" && mapObj != null )
		{
			map = new google.maps.Map2(mapObj);
			map.setMapType(G_NORMAL_MAP);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.enableScrollWheelZoom();
			map.setCenter(new google.maps.LatLng( longi_avg, lati_avg ), 2 );
			// Display all points
			bounds = new GLatLngBounds;
			for( p = 0; p < counter; p++ )
			{
				GEvent.addListener(markers[p], "click", function(point) {map.setCenter(point);map.setZoom(this.zoomlevel);map.openInfoWindowHtml(point,this.html); });
				map.addOverlay(markers[p]);
				bounds.extend(markers[p].getPoint());
			}
			// Center points
			map.setZoom(map.getBoundsZoomLevel(bounds));
			map.setCenter(bounds.getCenter());
			// Get missing points via ajax
			geocoder = new GClientGeocoder();
			for( p = 0; p < counter_gc; p++ )
			{
				if( p > 5 )
					break;
				ajax_get_coords( gc_points[ p ][ 0 ], gc_points[ p ][ 1 ] );
			}
      		}
		else	alert( 'Der MapLayer existiert nicht!' );
	}
	else
	{
		alert( 'Google Maps ist mit diesem Browser leider nicht kompatibel.' );
	}
}

function set_point( lati, longi, name, street, plz, ort, purl, mapicon, zoomlevel )
{
	createMarker(new GPoint( longi, lati ), name, '<div id="gmapmarker" style="white-space: nowrap;"><b><a href="' + unescape( purl ) + '" target="_blank">' + name + '</a></b><br />' + ort + '</div>', mapicon, zoomlevel );
}

function createMarker( point, title, html, n, zoomlevel )
{
	markers[counter] = new GMarker(point, icon[n]);
	markers[counter].point = point;
	markers[counter].html = html;
	markers[counter].zoomlevel = zoomlevel;
	counter++;
}

function get_coords( adress, entryID )
{
	gc_points[ counter_gc ] = [ adress, entryID ];
	counter_gc++;
}

function ajax_get_coords( adress, entryID )
{
	counter_ajax++;
	xmlhttp[counter_ajax] = new XMLHttpRequest();
	xmlhttp[counter_ajax].onreadystatechange = function()
	{
		try {
			if( xmlhttp[counter_ajax].readyState == 4 )
			{
				if( xmlhttp[counter_ajax].status == 200 )
				{
					jah_result = xmlhttp[counter_ajax].responseText;
					jah_search = /<script(.*?)>(.*?)<\/script>/gi;
					SearchResult = jah_search.exec(jah_result);
					while( SearchResult != null )
					{
						eval( SearchResult[2] );
						SearchResult = jah_search.exec(jah_result);
					}
				}
			}
		}
		// Catch errors
		catch( err )
		{
			return null;
		}
	}
	xmlhttp[counter_ajax].open( "GET", '' + ajax_url + '?a=store&entryID=' + entryID + '&entry_adress=' + adress, true );
	xmlhttp[counter_ajax].send('');
}

/* Init */
google.setOnLoadCallback(initializeEnfieldMap);
