// *********** AJAX ***************************
if( !window.XMLHttpRequest && window.ActiveXObject )
{
	window.XMLHttpRequest = function () {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

// *********** Map auf dieser Seite ***********
var counter = 0;
var points = [];
var markers = [];
var marker_html = [];
var zooms = [];
var to_htmls = [];
var from_htmls = [];
var icon = [];
var map = null;
var geocoder = false;
var 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");
	
function createMarker( point, title, html, n, zoom )
{
	var marker = new GMarker(point, icon[n]);
	
	to_htmls[counter] = html;
	to_htmls[counter] += '<form class="gmapDir" id="gmapDirTo" style="white-space: nowrap;" action="http://maps.google.com/maps" method="get" target="_blank">';
	to_htmls[counter] += '<p class="gmapDirHead" id="gmapDirHeadTo">Wegbeschreibung:<br/><strong>Hierher</strong> - <a href="javascript:fromhere(' + counter + ')">Von hier aus</a></p>';
	to_htmls[counter] += '<p class="gmapDirItem" id="gmapDirItemTo"><label for="gmapDirSaddr" class="gmapDirLabel" id="gmapDirLabelTo">Start-Adresse:<br /></label><input type="text" size="40" maxlength="40" name="saddr" class="gmapTextBox" id="gmapDirSaddr" value="" onfocus="this.style.backgroundColor = \'#e0e0e0\';" onblur="this.style.backgroundColor = \'#ffffff\';" /></p>';
	to_htmls[counter] += '<p class="gmapDirBtns" id="gmapDirBtnsTo"><input value="Wegbeschreibung anzeigen" type="submit" class="gmapDirButton" id="gmapDirButtonTo" /></p>';
	to_htmls[counter] += '<input type="hidden" name="daddr" value="' + point.y + ', ' + point.x + '(' + title + ')' + '" /></form>';
	
	from_htmls[counter] = html;
	from_htmls[counter] += '<form class="gmapDir" id="gmapDirFrom" style="white-space: nowrap;" action="http://maps.google.com/maps" method="get" target="_blank">';
	from_htmls[counter] += '<p class="gmapDirHead" id="gmapDirHeadFrom">Wegbeschreibung:<br/><a href="javascript:tohere(' + counter + ')">Hierher</a> - <strong>Von hier aus</strong></p>';
	from_htmls[counter] += '<p class="gmapDirItem" id="gmapDirItemFrom"><label for="gmapDirDaddr" class="gmapDirLabel" id="gmapDirLabelFrom">Ziel-Adresse:<br /></label><input type="text" size="40" maxlength="40" name="daddr" class="gmapTextBox" id="gmapDirSaddr" value="" onfocus="this.style.backgroundColor = \'#e0e0e0\';" onblur="this.style.backgroundColor = \'#ffffff\';" /></p>';
	from_htmls[counter] += '<p class="gmapDirBtns" id="gmapDirBtnsFrom"><input value="Wegbeschreibung anzeigen" type="submit" class="gmapDirButton" id="gmapDirButtonFrom" /></p>';
	from_htmls[counter] += '<input type="hidden" name="saddr" value="' + point.y + ',' + point.x + '(' + title + ')' + '" /></form>';
	
	html = html + '<br /><div id="gmapDirHead" class="gmapDir" style="white-space: nowrap;">Wegbeschreibung:<br/><a href="javascript:tohere(' + counter + ')">Hierher</a> - <a href="javascript:fromhere(' + counter + ')">Von hier aus</a></div>';
	GEvent.addListener(marker, "click", function() { map.centerAndZoom(point, zoom); marker.openInfoWindowHtml(html); });
	
	points[counter] = point;
	markers[counter] = marker;
	marker_html[counter] = html;
	zooms[counter] = zoom;
	counter++;
	
	return marker;
}

function click_sidebar(idx)
{
	markers[idx].openInfoWindowHtml(marker_html[idx]);
}

function showInfoWindow(idx,html)
{
	map.centerAndZoom(points[idx], zooms[idx]);
	markers[idx].openInfoWindowHtml(html);
}

function tohere(idx)
{
	markers[idx].openInfoWindowHtml(to_htmls[idx]);
}

function fromhere(idx)
{
	markers[idx].openInfoWindowHtml(from_htmls[idx]);
}

function draw_map( mapname, longi_avg, lati_avg, longi_max, lati_max, longi_min, lati_min )
{
	if( GBrowserIsCompatible() )
	{
		mapObj = document.getElementById("map_"+mapname);
		if( mapObj != "undefined" && mapObj != null )
		{
			map = new GMap(mapObj);
			//map.setMapType(G_MAP_TYPE);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.enableScrollWheelZoom();
			map.centerAndZoom(new GPoint( longi_avg, lati_avg ), 2 );
			var bds = map.getBoundsLatLng();
			for( var z = 2; bds.minX > longi_min || bds.minY > lati_min || bds.maxX < longi_max || bds.maxY < lati_max; z++ )
			{
				map.zoomTo(z);
				bds = map.getBoundsLatLng();
			}
		}
		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, url, mapicon, zoom )
{
	var point = new GPoint( longi, lati );
	url = unescape( url );
	var marker = createMarker(point, name, '<div id="gmapmarker" style="white-space: nowrap;"><b>' + name + '</b><br />' + street + ', ' + plz + ' ' + ort + '<br />' + url + '</div>', mapicon, zoom );
	map.addOverlay(marker);
}

function get_coords( adress, entryID )
{
	adress += ', Germany';
	if( geocoder == false )
		geocoder = new GClientGeocoder();
	if( geocoder )
	{
		geocoder.getLocations( adress, function( response ) {
			if( !response || response.Status.code != 200 )
			{
				//alert( adress + ' nicht gefunden!' );
				return 0;
			}
			else
			{
				place = response.Placemark[0];
				place.Point.coordinates[1];
				place.Point.coordinates[0];
				xmlhttp = new XMLHttpRequest();
				xmlhttp.onreadystatechange = function()
				{
					try {
						if( xmlhttp.readyState == 4 )
						{
							if( xmlhttp.status == 200 )
							{
								jah_result = xmlhttp.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.open( "GET", '/kita/public/ajax.php?a=store&lat=' + place.Point.coordinates[0] + '&lon=' + place.Point.coordinates[1] + '&entryID=' + entryID, true );
				xmlhttp.send('');
			}
		} );
	}
	else alert( 'Kein GeoCoder vorhanden!' );
}

