
var locationsearch = null;
var allLocations = new Array();
var searchStartTime = new Date();
var missingLocations = new Array();
var coordinates = new Array();
var dealerlocations = new Array();
var dealercodes = new Array();

var map24data = {
	showDealerInfos : true
};

function showDealerInfos(params) {
	var d = document.getElementById('dealerinfo');
	if (d) {
		var dealercode = params['dealercode'];
		new Ajax.Updater('dealerinfo', '/ajax/dealerinfo?dealercode='+dealercode, {} );
	}
}

function goMap24() {
  Map24.loadApi( ["core_api", "wrapper_api"] , map24ApiLoaded );
}

function controlLayer(_params) {
	    if(!conn) {
			var conn=Map24.MapApplication.Map.WebServices.openConnection();
		}
		conn.mapletRemoteControl(
			new Map24.WebServices.Message.mapletRemoteControlRequest({
				MapletRemoteControlRequest: new Map24.WebServices.MapletRemoteControlRequest({
					Map24MRC: new Map24.WebServices.Map24MRC({
						Commands: [new Map24.WebServices.XMLCommandWrapper({
							ControlLayer: new Map24.WebServices.ControlLayer({
								Control: _params.Control,
								LayerIDs: _params.LayerIds,
								Map24Layers: _params.Map24Layers
							})
						})]
					})
				})
			})
		);
  }

function map24ApiLoaded(){
	// initial auf Deutschland zoomen
	Map24.MapApplication.setStartMapView({
		  UpperLeftLongitude : 97.18715346010507
		, UpperLeftLatitude : 3333.5759875523518
		, LowerRightLongitude : 1158.6530951571901
		, LowerRightLatitude : 2835.311659206503
	});
	
	Map24.MapApplication.init( { 
	 	NodeName: "maparea"
	 	, MapType:"Static"
	} );

	controlLayer({
		Control:"HIDE",
		LayerIds:[4060,6805,6806],
		Map24Layers:false
	});

	Map24.Location.ShowOnlyOneTooltip = true;

	if (dealerlocations.length>0) {
		showDealerLocations();
	}
	
	locationsearch = new Map24.ProximityServiceStub();
	searchStartTime = new Date();
	callbackMap24Locations(null, null);
}

/*
 * http://developer.navteq.com/site/global/zones/ms/ajax_api_2_1_0/doc/Map24.ProximityServiceStub.jsp#findCustomerLocations
 * locations enthält ein Array von Map24.Location (http://developer.navteq.com/site/global/zones/ms/ajax_api_2_1_0/doc/Map24.Location.jsp)
 */
function callbackMap24Locations(locations, params) {
	if (locations!=null) {
		for(var i=0; i<locations.length; i++) {
			allLocations.push(locations[i]);
			//if (params!=null) addDebugInfo('got '+params['dealercode']);
		}
		try {
			if (params!=null && locations.length==0)
				missingLocations.push(params['dealercode']);
		} catch (exc) { alert(e); }
		
		if (allLocations.length >= 10) {
			showAllMap24Locations(false);
		}
	}

	if (dealercodes.length > 0) {
		var dealercode = dealercodes.pop();
		// fuehrende 0 entsorgen
		while (dealercode.length>0 && dealercode.charAt(0)=='0') dealercode = dealercode.substring(1);
		try {
			locationsearch.findCustomerLocations( {
				CallbackFunction : callbackMap24Locations ,
				CallbackParameters : { 'dealercode' : dealercode } ,
				CustomerLayerIDs : '4060|6805|6806' ,
				SearchFields : {
					CustomerLocationID : dealercode
				}
			} );
		} catch (exc) { alert(e); }
	} else {
		showAllMap24Locations(true);
	}
}

function showAllMap24Locations(finished) {
	while (allLocations.length > 0) {
		var a = allLocations.pop();
		coordinates.push(new Map24.Coordinate(a.getLongitude(),a.getLatitude()));
		
		try {
			if (map24data.showDealerInfos) {
				a.addListener("OnMouseOver", showDealerInfos, { 'dealercode' : a.getAdditionalProperty('CustomerLocationID') } );
			}
		} catch (exc) { alert(exc); }
		a.commit();
	}
	
	try {
		if (finished && coordinates.length>0) {
			Map24.MapApplication.setMapView({
				Coordinates : coordinates
				, ViewPercentage : 40
			});
		}
	} catch (exc) { alert(e); }
	
	var currentcount = document.getElementById('currentdebugnode');
	if (currentcount)  {
		currentcount.innerHTML='found='+coordinates.length + '  time='+ ( (new Date().getTime() - searchStartTime.getTime())/1000.0 ) + 
			's  missing=' + missingLocations.length+' ('+missingLocations+') finished='+finished;
	}
}

function showDealerLocations() {
	for(var i=0; i<dealerlocations.length;i++) {
		try {
			var d=dealerlocations[i];
			if (d.longitude>0 && d.latitude>0) {
				coordinates.push(new Map24.Coordinate(d.longitude,d.latitude));
			}
			var l = new Map24.Location({
				Longitude:d.longitude ,
				Latitude:d.latitude,
				Description:d.name,
				LogoURL:d.icon
			});
			if (map24data.showDealerInfos) {
				l.addListener("OnMouseOver", showDealerInfos, { 'dealercode' : d.id } );
			}
			l.commit();
		} catch (exc) {}
	}
}