﻿// JScript-Datei

var map = null;

function GetMap() {

	var address = GetAddress(document.getElementById("EventStreet").innerHTML, document.getElementById("EventZip").innerHTML, document.getElementById("EventTown").innerHTML, document.getElementById("EventCountry").innerHTML);

	map = new VEMap('myMap');
	map.LoadMap();
	map.SetDashboardSize(VEDashboardSize.Normal);
	map.SetMapMode(VEMapMode.Mode2D);
	map.SetMapStyle(VEMapStyle.Shaded);
	map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
	map.Find(null, address, null, null, 0, 10, true, true, true, true, callback);
	map.ShowMiniMap(0, 0, VEMiniMapSize.Small);
	RealignMiniMap();
}
function callback(a,b,c,d,e) {
	var shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
	shape.SetTitle(document.getElementById("EventLocation").innerHTML);
	shape.SetDescription(GetDescription(document.getElementById("EventStreet").innerHTML, document.getElementById("EventZip").innerHTML, document.getElementById("EventTown").innerHTML));
	map.AddShape(shape);
	//map.ClearInfoBoxStyles();
	map.ShowInfoBox(shape);
}    

function RealignMiniMap() {
	// Realign the position of the Mini Map so it appears
	// where we want it - The Upper Right Corner
	var minimap = document.getElementById("MSVE_minimap");
	var xoffset = (GetMapWidth() - minimap.offsetWidth);
	map.ShowMiniMap(xoffset, 0, VEMiniMapSize.Small);

	/// Hide the Mini Map resizer so the Mini Map cannot be resized
	document.getElementById("MSVE_minimap_resize").style.display = "none";
}
function GetMapWidth() {
	// Get the Width of the Map as an integer
	return document.getElementById("myMap").offsetWidth;
}
function GetMapHeight() {
	// Get the Height of the Map as an integer
	return document.getElementById("myMap").offsetHeight;
}

function GetRouteMap() {
	SetDirections("<span class='highlight'>Einen Moment bitte, die Route wird berechnet...</span>");

	//disable & enable Buttons
	document.getElementById("getroutemap").disabled = true
	document.getElementById('formStreet').disabled = true
	document.getElementById('formZIP').disabled = true
	document.getElementById('formTown').disabled = true

	var myRouteTo = GetAddress(document.getElementById("EventStreet").innerHTML, document.getElementById("EventZip").innerHTML, document.getElementById("EventTown").innerHTML, document.getElementById("EventCountry").innerHTML)
	var myRouteFrom = GetAddress(document.getElementById("formStreet").value, document.getElementById("formZip").value, document.getElementById("formTown").value, "Germany")
	
	var locations = new Array(myRouteFrom, myRouteTo);

	var options = new VERouteOptions;
	options.DistanceUnit = VERouteDistanceUnit.Kilometer;
	options.DrawRoute = true;
	options.RouteCallback = ShowTurns;
	options.RouteMode = VERouteMode.Driving; 
	options.RouteOptimize = VERouteOptimize.MinimizeTime; 
	options.SetBestMapView = true;
	options.ShowDisambiguation = true;
	options.ShowErrorMessages = true;
	options.UseTraffic = false;

	map.GetDirections(locations, options);
}
function ShowTurns(route)
{
	var turns = "";
	turns += "<hr />"
	turns += "<h2>Fahranweisungen</h2>"
	turns += "<p>(Abweichungen aufgrund von Rundungen möglich)</p>";
	turns += "<p>"
	turns += "<span class='stress'>Entfernung:</span> <span class='highlight'>" + route.Distance.toFixed(1).replace(/\./, ',') +" km</span><br/>";
	turns += "<span class='stress'>voraussichtliche Fahrzeit:</span> <span class='highlight'>" + GetTimeLong(route.Time) + "</span>";
	turns += "</p>"
	
	// Unroll route and populate DIV
	var legs          = route.RouteLegs;
	var leg           = null;
	var turnNum       = 0;  // The turn #

	turns += "<table class='tbl-route'><col class='col1' /><col class='col2' /><col class='col3' /><col class='col4' />";
	turns += "<tbody>";

	// Get intermediate legs
	for(var i = 0; i < legs.length; i++) {
		// Get this leg so we don't have to derefernce multiple times
		leg = legs[i];  // Leg is a VERouteLeg object
		var legNum = i + 1;
		//turns += "<br/><b>Entfernung für Abschnitt " + legNum + ":</b> " + leg.Distance.toFixed(1) + " km" +
		//	   "<br/><b>Zeit für Abschnitt "     + legNum + ":</b> " + GetTime(leg.Time) + "<br/><br/>";
		// Unroll each intermediate leg
		var turn        = null;  // The itinerary leg
		var legDistance = null;  // The distance for this leg
		for(var j = 0; j < leg.Itinerary.Items.length; j ++) {
			turnNum++;
			turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
			turns += "<tr><td class='colNum'>" + turnNum + "</td><td class='colText'>" + turn.Text + "</td>";
			legDistance    = turn.Distance;
			// So we don't show 0.0 for the arrival
			if(legDistance > 0) {
				// Round distances to 1/10ths
				turns += "<td class='colDistance'>" + legDistance.toFixed(1).replace(/\./, ',') + "&nbsp;km</td><td class='colTime'>";
				// Append time if found
				if(turn.Time != null) {
					turns += GetTime(turn.Time);
				}
				turns += "</td></tr>";
			}
		}
		turns += "";
	}
	turns += "</tbody>";
	turns += "</table>";

	// Populate DIV with directions
	SetDirections(turns);
	document.getElementById("clearroutemap").disabled = false;

}

function SetDirections(s) {
	var d = document.getElementById("directions");
	d.innerHTML = s;
}

function GetAddress(street, postalcode, city, country) {
	var address = "";
	if (street != null & street != "")
		address += street;
	if (postalcode != null & postalcode != "") {
		if (address != "")
			address += ", ";
		address += postalcode;
	}
	if (city != null & city != "") {
		if (address != "") {
			if (postalcode != null & postalcode != "")
				address += " ";
			else
				address += ", ";
		}
		address += city;
	}
	if (country != null & country != "") {
		if (address != "")
			address += ", ";
		address += country;
	}
	return address
}
function GetDescription(street, postalcode, city) {
	var description = "";
	if (street != null & street != "")
		description += street;
	if (postalcode != null & postalcode != "") {
		if (description != "")
			description += "<br /> ";
		description += postalcode;
	}
	if (city != null & city != "") {
		if (description != "") {
			if (postalcode != null & postalcode != "")
				description += " ";
			else
				description += "<br /> ";
		}
		description += city;
	}
	return description
}
    
// time is an integer representing seconds
// returns a formatted string
function GetTimeLong(time) {
	if(time == null) {
		return("");
	}
	if(time > 60) {                    // if time == 100
		var seconds = time % 60;       // seconds == 40
		var minutes = time - seconds;  // minutes == 60
		minutes     = minutes / 60;    // minutes == 1
		if(seconds > 30) {
			minutes = minutes + 1;
		}
		if(minutes > 60) {                     // if minutes == 100
			var minLeft = minutes % 60;        // minLeft    == 40
			var hours   = minutes - minLeft;   // hours      == 60
			hours       = hours / 60;          // hours      == 1
			//return (hours + " Stunde(n),&nbsp;" + minLeft + "&nbsp;Minute(n),&nbsp;" + seconds + "&nbsp;Sekunde(n)");
			return (hours + "&nbsp;Stunde(n),&nbsp;" + minLeft + "&nbsp;Minute(n)");
		}
		else {
			//return (minutes + "&nbsp;Minute(n),&nbsp;" + seconds + "&nbsp;Sekunde(n)");
			return (minutes + "&nbsp;Minute(n)");
		}
	}
	else {
		//return(time + "&nbsp;Sekunde(n)");
		return("unter 1 Minute");
	}
}

function GetTime(time) {
	if(time == null) {
		return("");
	}
	if(time > 60) {                    // if time == 100
		var seconds = time % 60;       // seconds == 40
		var minutes = time - seconds;  // minutes == 60
		minutes     = minutes / 60;    // minutes == 1
		if(seconds > 30) {
			minutes = minutes + 1;
		}
		if(minutes > 60) {                     // if minutes == 100
			var minLeft = minutes % 60;        // minLeft    == 40
			var hours   = minutes - minLeft;   // hours      == 60
			hours       = hours / 60;          // hours      == 1
			//return (hours + "&nbsp;h,&nbsp;" + minLeft + "&nbsp;min,&nbsp;" + seconds + "&nbsp;sec");
			return (hours + "&nbsp;h,&nbsp;" + minLeft + "&nbsp;min");
		}
		else {
			//return (minutes + "&nbspmin,&nbsp;" + seconds + "&nbsp;sec");
			return (minutes + "&nbspmin");
		}
	}
	else {
		//return (time + "&nbsp;sec");
		return ("<1&nbsp;min");	}
	}

function ClearAll() {
	map.DeleteRoute();
	SetDirections("");
	GetMap();

	//disable & enable Buttons
	document.getElementById("getroutemap").disabled = false;
	document.getElementById("clearroutemap").disabled = true;
	document.getElementById('formStreet').disabled = false
	document.getElementById('formStreet').value = ""
	document.getElementById('formZIP').disabled = false
	document.getElementById('formZIP').value = ""
	document.getElementById('formTown').disabled = false
	document.getElementById('formTown').value = ""
}

function DisposeMap() {
	if (map != null) {
		map.Dispose();
	}
}
