
var options = {
	basepath: null,
	startlat: 47.872,
	startlon: 12.4267,
	startzoom: 11,
	maptype: 'map',
	overviewmap: true,
	doubleclickzoom: true,
	scalecontrol: true,
	maptypecontrol: true,
	markerlist: true
}
var map = null;
var markertable = null;

function onLoad() {
	var mapObj = document.getElementById("map");
	if (mapObj != "undefined" && mapObj != null) {
		map = new GMap2(mapObj);

		var maptype = G_NORMAL_MAP;
		switch (options.maptype) {
			case 'hybrid':
				maptype = G_HYBRID_MAP;
				break;
			case 'satellite':
				maptype = G_SATELLITE_MAP;
				break;
		}
		map.setCenter(new GLatLng(options.startlat, options.startlon), options.startzoom, maptype);
		map.addControl(new GLargeMapControl());
		if (options.maptypecontrol) map.addControl(new GMapTypeControl());
		if (options.scalecontrol) map.addControl(new GScaleControl());
		if (options.overviewmap) map.addControl(new GOverviewMapControl());
		if (options.doubleclickzoom) {
			map.enableDoubleClickZoom();
			map.enableContinuousZoom();
		}

		getMarkers();
	}
}

function getIcon(name) {
	var icon = new GIcon();
	icon.image = options.basepath + '/codebase/layout/images/googlemap/' + name + '.png';
	icon.shadow = options.basepath + '/codebase/layout/images/googlemap/' + name + '_s.png';
	icon.shadowSize = new GSize(59,32);
	icon.iconSize = new GSize(32,32);
	icon.iconAnchor = new GPoint(16,16);
	icon.infoWindowAnchor = new GPoint(32,0);

	return icon;
}

function getMarkers() {
	//map.clearOverlays();
	if (options.markerlist) {
		/*
		markertable = document.createElement('table');
		document.getElementById("map").parentNode.appendChild(markertable);

		var loTr = markertable.insertRow(0);

		var loTh = document.createElement('th');
		loTh.appendChild(document.createTextNode('Wichtige Orte'));
		loTr.appendChild(loTh);
		*/
		markertable = $('markertable');
	}
	GDownloadUrl(options.basepath + '/googlemap/dataxml_1.htm', function(data, responseCode) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("location");
		for (var i = 0; i < markers.length; i++) {
			addMarker(markers[i]);
		}
	});
}

function addMarker(node) {
	var point = new GLatLng(node.getAttribute('lat'),node.getAttribute('lon'));
	var mapicon = getIcon(node.getAttribute('icon'));
	//var marker = new GMarker(point, {icon: mapicon, draggable: true} );
	var marker = new GMarker(point, {icon: mapicon} );
	if (node.hasChildNodes()) {
		var html = node.firstChild.data;
		GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
	}
	map.addOverlay(marker);

	if (options.markerlist) {
		var loTr = markertable.insertRow(markertable.rows.length);

		var loTd = document.createElement('td');
		if (node.hasChildNodes()) {
			var html = node.firstChild.data;
			var loA = document.createElement('a');
			loA.href = 'javascript:;';
			loA.onclick = function() { marker.openInfoWindowHtml(html); };
			loA.appendChild(document.createTextNode(node.getAttribute('title')));
			loTd.appendChild(loA);
		}
		else {
			loTd.appendChild(document.createTextNode(node.getAttribute('title')));
		}
		loTr.appendChild(loTd);

	}
}
