/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  *
 *
 *  load specific Icons for our map
 *
 * it's easy to change, so we can take rapidly our own icons
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  */

var customIcons = [];
customIcons["autoevents"] = iconGreen;
customIcons["autotreffen"] = iconYellow;
customIcons["messen"] = iconBlue;
customIcons["oldtimerevents"] = iconRed;

var execdir = 'events/eventplaner/';

var gmarkers = [];
var sidebar = "";
var i = 0;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  *
 *
 *  Google map function to get object properties
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  */
function objProperties(myObject){
	var out = new Array();
	for( property in myObject ) {
		out.push(property + " = " + myObject[property]);
	}
	return out;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  *
 *
 * if sb. click in a marker, it will show information about this
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  */
function clickShowPlace(i) {
	GEvent.trigger(gmarkers[i], "click");
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  *
 *
 *  Google map function to load coordinates, zoom etc.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  */
//<![CDATA[

function load() {
	// check, browser can laod google map
	if (GBrowserIsCompatible()) {

	// set width and height from map element, only if javascript is enabled
	document.getElementById("map").style.width = "650px";
	document.getElementById("map").style.height = "500px";

	// create a new GMap-Object and set it on the <div>-tag with id "map"
	var map = new GMap2(document.getElementById("map"));

	// add zoom an navigation control
	map.addControl(new GSmallMapControl());

	// add the map type selection
	map.addControl(new GMapTypeControl());

	// center the map on lat and long coordinates
	map.setCenter(new GLatLng(51.124213, 10.546875), 6);

	// enable mousewheelzoom
	map.enableScrollWheelZoom();

	// listener for mousewheelzoom to zoom in map
	GEvent.addListener( map, "wheelup", function(point){
	if (map.getZoomLevel() > 0 ) {
	 map.centerAndZoom(p.scaleRelative( map.getCenterLatLng() ),  map.getZoomLevel() - 1);
	 }
	});

	// listener for mousewheelzoom to zoom out of map
	GEvent.addListener( map, "wheeldown", function(point){
	if (map.getZoomLevel() <= 16 )
	  map.centerAndZoom(
	    p.scaleRelative( map.getCenterLatLng(), -1 ),
	    map.getZoomLevel() + 1
	  );
	});

	// load php function to generate xml file from database information
	//GDownloadUrl("phpsqlajax_genxml.php", function(data) {
//	GDownloadUrl("events/eventplaner/xml_details/all_events.xml", function(data, responseCode) {
	GDownloadUrl(execdir+"xml.php", function(data, responseCode) {
		// set xml variable
		var xml = GXml.parse(data);
		var content = new Array();

		// create markers from xml file
		var markers = xml.documentElement.getElementsByTagName("marker");
		// for each marker, create a new marker
		for (var i = 0; i < markers.length; i++) {
			// just add time o clock
			content['Event_Name'] = markers[i].getAttribute("Event_Name");
			content['Type'] = markers[i].getAttribute("Category");
			content['Event_Id'] = markers[i].getAttribute("idEvents");
			content['Date_From'] = markers[i].getAttribute("Date_From");
			content['Date_To'] = markers[i].getAttribute("Date_To");
			content['Short_Desc'] = markers[i].getAttribute("Short_Desc");
			var point = new GLatLng(parseFloat(markers[i].getAttribute("Latitude")), parseFloat(markers[i].getAttribute("Longitude")));
			var marker = createMarker(point, content);
			map.addOverlay(marker);
			//document.getElementById("jslist").innerHTML = sidebar;
		}
	});
	} // close GBrowserIsCompatible
}

function cleanName(name) {
	var temp_name;
	var rep_array;
	rep_array = new Array(/&(amp|#38);/g, /\./g, /[\(|\)]/g, /ä/g, /ü/g, /ö/g, /ß/g, /\'/g, /,/g, /!/g, /&/g);

	temp_name = name;

	for (var i = 0; i < rep_array.length; i++)
	{
		temp_name = temp_name.replace(rep_array[i], '_');
	}

	temp_name = temp_name.replace(/[\.| |-]/g, '_'); // $rep_array = array('/&(amp|#38);/i', '/\./i', '/[\(|\)]/i', '/ä/i', '/ü/i', '/ö/i', '/ß/i', '/\'/i', '/,/i', '/!/i');
	return temp_name.replace(/_+/g, '_');
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  *
 *
 *  function to create Marker for map
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  */
function createMarker(poing, content) {
	var marker = new GMarker(poing, customIcons[content['Type']]);
	var execdir = 'events/';
	var Category = content['Type'];

	switch (content['Type']) {
		case 'autoevents':
			content['Type'] = "Auto Event";
			break;
		case 'oldtimerevents':
			content['Type'] = "Oldtimer Event";
			break;
		case 'messen':
			content['Type'] = "Messe";
			break;
		case 'autotreffen':
			content['Type'] = "Auto Treffen";
			break;
		default:
			// if there is a type which is not in list, take type
			content['Type'] = content['Type'];
	}

   var year = content['Date_From'].split('-');

	var html = "\
	<b>" + content['Event_Name'] + "</b><hr width=\"220px\ align=\"left\"> \
	"+content['Type']+"<br />\
	"+content['Date_From']+" - "+content['Date_To']+"<br /><br />\
	"+content['Short_Desc']+"<br />\
	<br /><a href=\"/"+execdir+Category+"/"+year[0]+"-"+content['Event_Id']+"-"+cleanName(content['Event_Name'])+".html"+"\">mehr Informationen</a>";
	GEvent.addListener(marker, 'mouseover', function() {
		marker.openInfoWindowHtml(html);
		});
	gmarkers[i] = marker;
	sidebar += '<a href="javascript:clickShowPlace(' + i + ')">' + content['Event_Name'] + '</a><br />';
	i++;
	return marker;
	}

//]]>
