var routeID = 0;
var div_content = "";

function goMap24() 
{
	//Load core API and wrapper classes
    Map24.loadApi( ["core_api", "wrapper_api"] , map24ApiLoaded );
}

//If function name was defined in the loadApi() function,  
//this function is called when the API was loaded
function map24ApiLoaded()
{
   Map24.MapApplication.setStartMapView( { 
      UpperLeftLongitude: 314.55059814453125,
      UpperLeftLatitude: 3137.9486328125,
      LowerRightLongitude: 314.75059814453125,
      LowerRightLatitude: 3138.9486328125
    } );

   	//Initialize mapping client
   	Map24.MapApplication.init( { NodeName: "map24" } );

    htmlObj = new Map24.HtmlObject({
        Longitude: 314.01259814453125, 
        Latitude: 3138.1786328125,
        Content: "<div class='zig_map24' style='background-color: #EAEAEA; font-family: arial;'><img src='/images/map24_zig_logo.png' border='0' /><br/>ZIG Websoftware<br/>Botterstraat<br/>51c 1271 XL Huizen</div>",
        Layout: Map24.MapObject.LAYOUT_NONE
    });
    
    //alert(Map24.MapObject.SIZE_S );
    
    //Call commit() on the HTML object. The HTML object is shown on the map.
    htmlObj.commit();
    
    Map24.MapApplication.center( {Longitude:314.012059814453125, Latitude:3138.1786328125, MinimumWidth: 1000} );  	
}

function calcRoute()
{
	document.getElementById("map24PrintRoute").disabled = true;
	
	var adres = "";
	ElmStraat = document.getElementById("map24Straat");
	ElmHuisnr = document.getElementById("map24Huisnummer");
	ElmPlaats = document.getElementById("map24Plaats");
	
	if(ElmStraat.value == "")
	{
		alert("U dient een straatnaam in te vullen.");
		return false;
	}
	
	else if(ElmPlaats.value == "")
	{
		alert("U dient een woonplaats in te vullen.");
		return false;	
	}
	else
	{
		adres = ElmStraat.value;
		if(ElmHuisnr.value != "")
		{
			adres += " "+ElmHuisnr.value;
		}
		adres += ", "+ElmPlaats.value;
	
		//Create a geocoder stub
		var geocoder = new Map24.GeocoderServiceStub();		
		
		//Geocode the destination point of the route
	    geocoder.geocode({
	      SearchText: adres,  
	      CallbackFunction: setRouteEndPoint
	    });
    }
}

function setRouteEndPoint(locations)
{
	if(locations.length == 0)
	{
		alert("Het door u opgegeven adres is niet gevonden.");
	}
	else
	{
		var toPoint = new Map24.Coordinate();
		toPoint.setLatitude(3138.1786328125);
		toPoint.setLongitude(314.012059814453125);
	   	
	   	router = new Map24.RoutingServiceStub();
	    router.calculateRoute({
	      Start: locations[0],
	      Destination: toPoint,
	      DescriptionLanguage: "nl",
	      CallbackFunction: showRoute
	    });
	}
}

function showRoute(route)
{
    //Remember the routeId. It is used e.g. to hide the route.
    routeID = route.RouteID;
    
    //Access the assumed time needed for traversing the route in hours
    var totalTime = ((route.TotalTime)/(60*60) ).toPrecision(2) 
    //Access the total lenght of the route in kilometers
    var totalLength = (route.TotalLength/1000) .toPrecision(2)
    //Create table with description of the route
    div_content = "Totale tijd: <strong>" + totalTime + " uur</strong><br/>" ;     
    div_content += "Totale Lengte: <strong>"+ totalLength +" km</strong><br/>";
    div_content += "<br/><table><tr><td><strong>Beschrijving:</strong></td></tr>";
    
    //Iterate through the route segments and output the step-by-step textual description of the route
    for(var i = 0; i < route.Segments.length; i++){
      for(var j = 0; j < route.Segments[i].Descriptions.length; j++){
      	if(i%2)
      		var oddclass = "class='oneven'";
      	else
      		var oddclass = "";
      	if(j%2)
      		var oddclass = "class='beschrijving'";
      		
      	//The route description contains tags for further evaluation. For example, the [M24_STREET] tag is used 
      	//to denote a street in the description. Add the following line of code to replace these tags by a blank:
        div_content += "<tr><td "+oddclass+">" + route.Segments[i].Descriptions[j].Text.replace(/(\[|\[\/)[0-9A-Z_]+\]/g, '' ) + "</td></tr>";
      }
    }  
    div_content += "</table>";
    document.getElementById("map24PrintRoute").disabled = false; 
}

function printRoute()
{
	var sWidth = screen.width / 2;
	var sHeight = screen.height;

    var windowPrint = window.open('','','left=0,top=0,width='+sWidth+',height='+sHeight+',toolbar=0,scrollbars=yes,status=0');
    windowPrint.document.write("<html><head>");
    windowPrint.document.write("<title>Uw route naar ZIG websoftware</title>");
	windowPrint.document.write("<link rel='stylesheet' type='text/css' href='/styles/1' />");
    windowPrint.document.write("</head><body><div class='print_header'></div><div class='route_print'>");
    windowPrint.document.write(div_content);
    windowPrint.document.write("</div></body></html>");
    windowPrint.document.close();
    windowPrint.focus();
    windowPrint.print();
}

addOnloadFunction(goMap24);