/*******************************************************************************
 * Javascript für twGmap01einfach
 ******************************************************************************/

var elementId = "twGmap";  // muss im html an ein <div> als id vergeben werden
//var lat       = "47.693703371072026";  // Breitengrad (Latitude) z.B: 51.041
//var lon       = "10.7975435256958";  // Längengrad (Longitude) z.B: 13.748
var zoom      = 10;        // einen Wert von 1 bis ca 17 (nicht in "" setzen)

var map;
var mm 		//MarkerManager
var z_bef = 0;
var i;

var gmarkers = [];
var side_bar_html = '';
var ort_bar_html = '';

var marker  = new Array();
var point   = new Array();
var tooltip = new Array();
var html    = new Array();
var icon    = new Array();

var poi    = new Array();

var cnt; // Anzahl POIs

var rubrik;

/**
 * Prüft auf kompatiblen Browser und vorhandene Variablen und ruft die
 * Funktion auf, die die GoogleMap erstellt.
 */
process_it = function(doc) {
		lines = doc.split("\n");
		var j = 0;

		for (var i=0; i<lines.length; i++) {
			if (lines[i].length > 1) {
				parts = lines[i].split("|");

				poi[j] = new Array();
				poi[j]["flag"] = parts[0];
				poi[j]["tooltip"] = parts[1];
				poi[j]["typ"] = parseInt(parts[2]);
				poi[j]["level"] = parseInt(parts[3]);
				poi[j]["icon"] = parseInt(parts[4]);
				poi[j]["lat"] = parseFloat(parts[5]);
				poi[j]["lon"] = parseFloat(parts[6]);
				poi[j]["text"] = parts[7];
				poi[j]["bild"] = parts[8];
				poi[j]["url"] = parts[9];
				poi[j]["rubrik"] = parts[10];

				j++;
			}
		}
		//cnt = poi.length;
		cnt = j;

		machTwGmap(elementId, poi[0]['lat'], poi[0]['lon'], zoom);
	}
	
function twGmapLoad() {
	if (GBrowserIsCompatible()) {
		if (!document.getElementById(elementId)) {
			alert("Fehler: das Element mit der id "+ elementId+ " konnte nicht auf dieser Webseite gefunden werden!");
			return false;
		} else {
			GDownloadUrl("pos.txt", process_it);
		}
	}
}


function makeIcons() {
	//HOME
	icon[0] = new GIcon();
	icon[0].image = "http://google-maps-icons.googlecode.com/files/hotel.png";
	icon[0].iconSize = new GSize(32, 37);
	icon[0].iconAnchor = new GPoint(6, 20);
	icon[0].infoWindowAnchor = new GPoint(5, 1);
	//Schloss
	icon[1] = new GIcon();
	icon[1].image = "http://google-maps-icons.googlecode.com/files/castle.png";
	icon[1].iconSize = new GSize(22, 27);
	icon[1].iconAnchor = new GPoint(6, 20);
	icon[1].infoWindowAnchor = new GPoint(5, 1);
	//Kirche
	icon[2] = new GIcon();
	icon[2].image = "http://google-maps-icons.googlecode.com/files/cathedral2.png";
	icon[2].iconSize = new GSize(22, 27);
	icon[2].iconAnchor = new GPoint(6, 20);
	icon[2].infoWindowAnchor = new GPoint(5, 1);
	//Supermarkt
	icon[3] = new GIcon();
	icon[3].image = "http://google-maps-icons.googlecode.com/files/supermarket.png";
	icon[3].iconSize = new GSize(22, 27);
	icon[3].iconAnchor = new GPoint(6, 20);
	icon[3].infoWindowAnchor = new GPoint(5, 1);
	//Spielplatz
	icon[4] = new GIcon();
	icon[4].image = "http://google-maps-icons.googlecode.com/files/playground.png";
	icon[4].iconSize = new GSize(22, 27);
	icon[4].iconAnchor = new GPoint(6, 20);
	icon[4].infoWindowAnchor = new GPoint(5, 1);
	//Arzt
	icon[5] = new GIcon();
	icon[5].image = "http://google-maps-icons.googlecode.com/files/firstaid.png";
	icon[5].iconSize = new GSize(22, 27);
	icon[5].iconAnchor = new GPoint(6, 20);
	icon[5].infoWindowAnchor = new GPoint(5, 1);
	//Ruine
	icon[6] = new GIcon();
	icon[6].image = "http://google-maps-icons.googlecode.com/files/ruins.png";
	icon[6].iconSize = new GSize(22, 27);
	icon[6].iconAnchor = new GPoint(6, 20);
	icon[6].infoWindowAnchor = new GPoint(5, 1);	
	//Skifahren
	icon[7] = new GIcon();
	icon[7].image = "http://google-maps-icons.googlecode.com/files/skiing.png";
	icon[7].iconSize = new GSize(22, 27);
	icon[7].iconAnchor = new GPoint(6, 20);
	icon[7].infoWindowAnchor = new GPoint(5, 1);	
	//Skilift
	icon[8] = new GIcon();
	icon[8].image = "http://google-maps-icons.googlecode.com/files/skilift.png";
	icon[8].iconSize = new GSize(22, 27);
	icon[8].iconAnchor = new GPoint(6, 20);
	icon[8].infoWindowAnchor = new GPoint(5, 1);
	//Kartbahn
	icon[9] = new GIcon();
	icon[9].image = "http://google-maps-icons.googlecode.com/files/karting.png";
	icon[9].iconSize = new GSize(22, 27);
	icon[9].iconAnchor = new GPoint(6, 20);
	icon[9].infoWindowAnchor = new GPoint(5, 1);
	//Golf
	icon[10] = new GIcon();
	icon[10].image = "http://google-maps-icons.googlecode.com/files/baseball.png";
	icon[10].iconSize = new GSize(22, 27);
	icon[10].iconAnchor = new GPoint(6, 20);
	icon[10].infoWindowAnchor = new GPoint(5, 1);
	//Eisstadion
	icon[11] = new GIcon();
	icon[11].image = "http://google-maps-icons.googlecode.com/files/icehockey.png";
	icon[11].iconSize = new GSize(22, 27);
	icon[11].iconAnchor = new GPoint(6, 20);
	icon[11].infoWindowAnchor = new GPoint(5, 1);
	//Restaurant
	icon[12] = new GIcon();
	icon[12].image = "http://google-maps-icons.googlecode.com/files/restaurant.png";
	icon[12].iconSize = new GSize(22, 27);
	icon[12].iconAnchor = new GPoint(6, 20);
	icon[12].infoWindowAnchor = new GPoint(5, 1);
	//Cafe
	icon[13] = new GIcon();
	icon[13].image = "http://google-maps-icons.googlecode.com/files/coffee.png";
	icon[13].iconSize = new GSize(22, 27);
	icon[13].iconAnchor = new GPoint(6, 20);
	icon[13].infoWindowAnchor = new GPoint(5, 1);
	//Apotheke
	icon[14] = new GIcon();
	icon[14].image = "http://google-maps-icons.googlecode.com/files/drugs.png";
	icon[14].iconSize = new GSize(22, 27);
	icon[14].iconAnchor = new GPoint(6, 20);
	icon[14].infoWindowAnchor = new GPoint(5, 1);
	//Klettern
	icon[15] = new GIcon();
	icon[15].image = "http://google-maps-icons.googlecode.com/files/climbing.png";
	icon[15].iconSize = new GSize(22, 27);
	icon[15].iconAnchor = new GPoint(6, 20);
	icon[15].infoWindowAnchor = new GPoint(5, 1);
}
 
function AddRemove() {
			for (var i = 0; i < cnt; i++) {

					if (poi[i]["typ"] == "0") {
						if (poi[i]["rubrik"] != rubrik){
							ort_bar_html += "<b>" + poi[i]["rubrik"] + "</b><br>";
						}
						point[i]  = new GPoint(poi[i]["lon"], poi[i]["lat"]);
						eval("marker_" + i + " = new GMarker(point[i],{icon:icon[poi[i]['icon']], title:poi[i]['tooltip']});");
						eval("gmarkers.push(marker_" + i + ");");
						ort_bar_html += '<a href="javascript:ort_click(' + (gmarkers.length-1) + ')"> &nbsp; &nbsp; &raquo; &nbsp;' + poi[i]["tooltip"] + '<\/a><br>';
						
						eval("mm.addMarker(marker_" + i + ",poi[i]['level'],17);");
						rubrik = poi[i]["rubrik"];
					}
					if (poi[i]["typ"] == "2") {
						if (poi[i]["rubrik"] != rubrik) {
							side_bar_html += "<b>" + poi[i]["rubrik"] + "</b><br>";
						}
						point[i]  = new GPoint(poi[i]["lon"], poi[i]["lat"]); 
						eval("var marker_" + i + " = new GMarker(point[i],{icon:icon[poi[i]['icon']], title:poi[i]['tooltip']});");
						eval("mm.addMarker(marker_" + i + ",poi[i]['level'],17);");
						html[i] = "";
						html[i] = "<div class='twGmapInfo'>";
						html[i] += "  <p class='headline'>" + poi[i]["tooltip"] + "</p>";
						html[i] += "  <p class='img'>";
						html[i] += "    <a href='" + poi[i]["url"] + "' target=_blank> ";
						html[i] += "      <img border=0 src='" + poi[i]["bild"] + "' alt='" + poi[i]["tooltip"] + "' />";
						html[i] += "    </a>"
						html[i] += "  </p>";
						html[i] += "  <p class='text'>" + poi[i]["text"] + "</p>";
						html[i] += "  <p class='pos' align=center><br><br>GPS: " + poi[i]["lat"] + " " + poi[i]["lon"] + "</p>";
						html[i] += "  <p class='twClear'>&nbsp;</p>";
						html[i] += "</div>";
						eval("var html_" + i + " = html[i];");
	
						eval("marker_" + i + ".bindInfoWindowHtml(html_" + i + ");");
	
						eval("GEvent.addListener(marker_" + i + ", 'click', function() {marker_" + i + ".openInfoWindowHtml(html_" + i + ");});");
						
						eval("gmarkers.push(marker_" + i + ");");
						side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')"> &nbsp; &nbsp; &raquo; &nbsp;' + poi[i]["tooltip"] + '<\/a><br>';
						
						eval("mm.addMarker(marker_" + i + ",poi[i]['level'],17);");
						rubrik = poi[i]["rubrik"];
					}
			}
			mm.refresh();
} 
	
function myclick(i) {
    map.setZoom(zoom);
	GEvent.trigger(gmarkers[i], "click");
}	

function ort_click(i) {
    //GEvent.trigger(gmarkers[i], "click");
	var map_typ = map.getCurrentMapType();
	map.setCenter(new GLatLng(poi[i]["lat"], poi[i]["lon"]), poi[i]["level"] + 1, map_typ);

}

function ReLoadSite() {
	var map_typ = map.getCurrentMapType();
	map.setCenter(new GLatLng(poi[0]["lat"], poi[0]["lon"]), zoom, map_typ);
}

function machTwGmap(id, lat, lon, zoom) {
	// die Map (für das div mit der id="...")
	map = new GMap2(document.getElementById(id));
	// mit Zoom (auch noch: GSmallMapControl, GLargeMapControl, GSmallZoomControl, GScaleControl)
	map.addControl(new GLargeMapControl());
	// mit Typ-Auswahl (auch noch: GMapTypeControl, GHierarchicalMapTypeControl, GOverviewMapControl)
	map.addControl(new GMapTypeControl());
	// ((Breitengrad, Längengrad), Zoomstufe, G_NORMAL_MAP/G_SATELLITE_MAP/G_HYBRID_MAP)
	map.setCenter(new GLatLng(poi[0]["lat"], poi[0]["lon"]), zoom, G_HYBRID_MAP);	
	
	map.enableScrollWheelZoom();
	mm = new GMarkerManager(map, {borderPadding:1}); 
	
	//GEvent.addListener(map, "moveend", function() {AddRemove();});
	//GEvent.addListener(map, "click", function() {AddRemove();});
	
	makeIcons();
	AddRemove();
	document.getElementById("side_bar").innerHTML = side_bar_html;
	document.getElementById("ort_bar").innerHTML = ort_bar_html;
}




