function start_map() {
	Map24.loadApi(["core_api"], apiLoaded);
}

var map = null;
var lconn = null;
var canvas = null;
var static_map = null;
var applet_map = null;
var slat = null;
var slng = null;
var dlat = null;
var dlng = null;
var rcolor = [0,255,0,128];

function $( id ) { return document.getElementById(id); };

function apiLoaded() {
	//Map24.Debug.enable( true, Map24.E_ALL_DEBUG );
	map = new Map24.Map();
	lconn = map.Local.openConnection();
	checkParameter();
}

function checkCanvas() {
	if( canvas==null ) {
		canvas = new Map24.Canvas({Id:"map_canvas"});
		map.addCanvas( canvas, "c" );
	}
}

function showStaticMap() {
	checkCanvas();
	if( static_map==null ) {
		static_map = new Map24.MapClient.Static();
		map.addMapClient( static_map, "mgi" );
	}
	map.show("mgi");
}

function showAppletMap() {
	checkCanvas();
	if( applet_map==null ) {
		applet_map = new Map24.MapClient.Static();
		map.addMapClient( applet_map, "applet" );
	}
	map.show("applet");
}

function checkParameter() {
	try {
		// ?slat=40.714440918&slng=-74.006119792&dlat=32.778149414&dlng=-96.795402018&rcolor=255,0,0,255
		// ?slat=34.05331014&slng=-118.245003255&dlat=36.030818685&dlng=-114.98108724&rcolor=255,0,0,255
		
//		var LatLngRegExp = new RegExp("([0-9.]+),([0-9.]+)", "mgi"); 
//		var r = LatLngRegExp.exec( url.Param["saddr"] );
		
//		saddr = parseFloat( url.Param["saddr"] );
//		if( saddr.
		
		var url = new Map24.Url( document.URL );
		slng = parseFloat( url.Param["slng"] ); 
		slat = parseFloat( url.Param["slat"] ); 
		dlng = parseFloat( url.Param["dlng"] ); 
		dlat = parseFloat( url.Param["dlat"] );
		if( typeof url.Param["rcolor"]=="string" ) {
			var test = url.Param["rcolor"].split(",");
			if( (test instanceof Array) && 
				((test.length==3) || (test.length>=4)) ) 
			{
				var r = parseInt( test[0] );
				var g = parseInt( test[1] );
				var b = parseInt( test[2] );
				var a = rcolor[3];
				if( test.length>=4 )
					a = parseInt( test[3] );
				if( !isNaN(r) && r>=0 && r<=255 )
					rcolor[0] = r;
				if( !isNaN(g) && g>=0 && g<=255 )
					rcolor[1] = g;
				if( !isNaN(b) && b>=0 && b<=255 )
					rcolor[2] = b;
				if( !isNaN(a) && a>=0 && a<=255 )
					rcolor[3] = a;
			}
		}

		// if the start 
		if( isNaN(slng) || slng==0 ||
			isNaN(slat) || slat==0 )
		{
			return showStaticMap();
		}

		if( isNaN(dlng) || dlng==0 ||
			isNaN(dlat) || dlat==0 )
		{
			return showStaticMap();
		}

		return calculateRoute();
	} catch(e) {
	}
	showStaticMap();
}

function routeStartEncoded() {
}

function routeDestEncoded() {
}

function routeStartDestFailed() {
}

function routeCalcSuccess( conn, msg, response ) {
	try {
	showStaticMap();

	var r = response.getProperty("CalculateRouteResponse");
	r.update(true);
	var route = r.getProperty("Route");
	var route_id = route.RouteID;
	
	// show route using MLC
	var mlc = new Map24.WebServices.MapletRemoteControlRequest({
		Map24MRC: new Map24.WebServices.Map24MRC({
			Commands: [
				new Map24.WebServices.XMLCommandWrapper({
					DeclareMap24Location: new Map24.WebServices.DeclareMap24Location({
						MapObjectID: "start",
						Coordinate: new Map24.WebServices.Coordinate({
							Longitude: slng*60,
							Latitude: slat*60
						}),
						SymbolID: 20100
					})
				}),
				new Map24.WebServices.XMLCommandWrapper({
					DeclareMap24Location: new Map24.WebServices.DeclareMap24Location({
						MapObjectID: "dest",
						Coordinate: new Map24.WebServices.Coordinate({
							Longitude: dlng*60,
							Latitude: dlat*60
						}),
						SymbolID: 20101
					})
				}),
				new Map24.WebServices.XMLCommandWrapper({
					DeclareMap24RouteObject: new Map24.WebServices.DeclareMap24RouteObject({
						MapObjectID: "route",
						Map24RouteID: route_id,
						Color: new Map24.WebServices.Color({
							red: rcolor[0],
							green: rcolor[1],
							blue: rcolor[2],
							alpha: rcolor[3]
						})
					})
				}),
				new Map24.WebServices.XMLCommandWrapper({
					ControlMapObject: new Map24.WebServices.ControlMapObject({
						Control: "ENABLE",
						MapObjectIDs: ["route","start","dest"]
					})
				}),
				new Map24.WebServices.XMLCommandWrapper({
					SetMapView: new Map24.WebServices.SetMapView({
						MapObjectIDs: ["route","start","dest"],
						ClippingWidth: new Map24.WebServices.SetMapViewClippingWidth({
							ViewPercentage: "90"
						})
					})
				}),
				new Map24.WebServices.XMLCommandWrapper({
					ControlComponent: new Map24.WebServices.ControlComponent({
						Control: "SHOW",
						Properties: [
							new Map24.WebServices.Property({
								Key: "triggeraction",
								Value: "HAND"
							})
						]
					})
				})
			]
		})
	});
	var msg = new Map24.WebServices.Message.mapletRemoteControlRequest({
		MapletRemoteControlRequest: mlc
	});
	lconn.mapletRemoteControl( msg, function(){  } );
	
	} catch(e) {
		Map24.dump( e );
	}
}

function routeCalcError( conn, msg, response ) {
	//Map24.dump( response, 5 );
	//alert( "ERROR !" );
	showStaticMap();
}

function routeCalcTimeout( conn, msg ) {
	//alert( "TIMEOUT !" );
	showStaticMap();
}

function calculateRoute( ) {
	try {
		var route = map.WebServices.openConnection();
		var route_request = new Map24.WebServices.CalculateRouteRequest({
			Start: new Map24.WebServices.CoordinateAndAddress({
	//			Address: new Map24.WebServices.Address({
	//				Street: "Düsseldorfer Str. 40a",
	//				City: "Eschborn",
	//				Country: "de"
	//				ID: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				HouseNo: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				Street: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				ZIP: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				City: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				District: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				County: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				State: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				Country: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0})
	//			})
	// alternative:
				Coordinate: new Map24.WebServices.Coordinate({
					Longitude: slng*60,
					Latitude: slat*60
				})
			}),
			Destination: new Map24.WebServices.CoordinateAndAddress({
	//			Address: new Map24.WebServices.Address({
	//				City: "Frankfurt am Main",
	//				Country: "de"
	//				ID: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				HouseNo: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				Street: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				ZIP: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				City: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				District: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				County: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				State: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				Country: new Map24.Xsd.Element({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0})
	//			})
	// alternative:
				Coordinate: new Map24.WebServices.Coordinate({
					Longitude: dlng*60,
					Latitude: dlat*60
				})
			}),
			CalculationMode: "fastest",
//			VehicleType: "Truck",
//			Properties: [
//				new Map24.WebServices.Property({
//					Key: "dyndata",
//					Value: "attrib=TruckAttributes;weight=-1;maxvalue=80000;key=wa;key=we;maxvalue=80000;key=h;maxvalue=80000;key=w;maxvalue=80000;key=l; "
//				})
//			],
	//		ViaPoints: [
	//			new Map24.WebServices.CalculateRouteRequestViaPoint({
	//				Address: new Map24.Xsd.Element({Type: "Map24.WebServices.Address", Nillable: true, MinOccurs: 0}),
	//				Coordinate: new Map24.Xsd.Element({Type: "Map24.WebServices.Coordinate", Nillable: true, MinOccurs: 0}),
	//				ViaPointProperties: new Map24.Xsd.Array({Type: "Map24.Xsd.String", Nillable: true, MinOccurs: 0}),
	//				TransitRadius: new Map24.Xsd.Element({Type: "Map24.Xsd.Int", Nillable: false, MinOccurs: 1})
	//			}),
	//			new Map24.WebServices.CalculateRouteRequestViaPoint({...}),
	//			new Map24.WebServices.CalculateRouteRequestViaPoint({...}),
	//			...
	//		],
			DescriptionLanguage: "en"
//			LoadTMCs: true
		});
		var msg = new Map24.WebServices.Message.calculateRouteRequest({
			CalculateRouteRequest: route_request
		});
		route.calculateRoute( msg, routeCalcSuccess, routeCalcError, routeCalcTimeout );
	}catch(e) {
		Map24.dump( e );
	}
}

function search_free( txt, on_success, on_error ) {
	var name = "";
	var brand = "Shell";
	var city = "Frankfurt am Main";
	var country = "DE";
	var poiType = null;
	var conn = map.WebServices.openConnection();
	var searchProps = [];
//	searchProps[searchProps.length++] = new Map24.WebServices.Property({
//		Key: "Addresstype",
//		Value: 7
//	});
	if( country!= null && country != "") {  
		searchProps[searchProps.length++] = new Map24.WebServices.Property({
			Key: "COUNTRY",
			Value: country
		});
    }
	if( city!= null && city != "") {  
		searchProps[searchProps.length++] = new Map24.WebServices.Property({
			Key: "CITY",
			Value: city
		});
	}
	if(name!= null && name != "") {  
		searchProps[searchProps.length++] = new Map24.WebServices.Property({
			Key: "NAME",
			Value: name
		});
	}
	if(brand!= null && brand != "") {
		searchProps[searchProps.length++] = new Map24.WebServices.Property({
			Key: "Brand",
			Value: brand
		});
	}
/*
	if(poiType != null && poiType != "") {  
		searchProps[searchProps.length++] = new Map24.WebServices.Property({
			Key: "PoiThemeID",
			Value: poiType
		});
	}
*/
	var msg = new Map24.WebServices.Message.searchFreeRequest({
		MapSearchFreeRequest: new Map24.WebServices.MapSearchFreeRequest({
			MaxNoOfAlternatives: 100,
			Properties: searchProps
		})
	});
	conn.searchFree( msg, function(conn, request, response){
		var mapResponse = response.getProperty('MapSearchResponse');
		mapResponse.update(true);
		Map24.dump( mapResponse.Alternatives, 3 );
	});
}
