// ==================================================================
// Map configuartion
// ==================================================================

function initMdvMapConfig() {
	mdvMapConfig.add('serverURL', absoluteServerPath.mapTiles + mapTilesParking);
	mdvMapConfig.add('efaURL', absoluteServerPath.efa + 'XML_MAP_REQUEST');
	mdvMapConfig.add('transparentImg', imgPath + 'transparent.gif');
	mdvMapConfig.add('imagePath', imgPath);
	mdvMapConfig.add('network', 'aut');
	mdvMapConfig.add('trips.useJsonForPath', 'true');
	mdvMapConfig.add('defaultScale', '1');
	mdvMapConfig.add('xCenterReal', mapCenterX);
	mdvMapConfig.add('yCenterReal', mapCenterY);
	mdvMapConfig.add('mapName', nameMap);
	mdvMapConfig.add('block', '100');
	mdvMapConfig.add('language', mapLanguage);
	mdvMapConfig.add('zoomOnDoubleClick', 'true');
	mdvMapConfig.add('hotspotOnMouseWheel', 'true');
	mdvMapConfig.add('cursorMove', imgPath + 'grabbing.cur');
	mdvMapConfig.add('useMagnifyGlass', 'true');
};


// ==================================================================
// Common map functionality
// ==================================================================

// This function loads the map.
function MDVMapHelper() {
	this.mdvMap = null;
	this.marker = null;
	this.markerLayer = null;
	this.events = null;
    this.ESOverlay = null;
    this.ZoneOverlay = null;

	if (this.mdvMap == null) {
		this.mdvMap = new MDVMap(document.getElementById('mdvMap'));
		new MDVMapNavigator(this.mdvMap);
		this.createMapControl();
		this.createZoomRectangle();

		this.mdvMap.events.registerEvent(MDVEvent_OBJECT_CLICKED, this, this.onClick);
		this.mdvMap.events.registerEvent(MDVEvent_TOOLTIP, this, this.onToolTip);
		
		this.markerLayer = this.mdvMap.createLayer('parkingMarkers');
		this.mdvMap.addLayer(this.markerLayer);
	}

	if (this.mdvMap && document.getElementById('mdvMap')) {
		createMapOverlays(this);
		this.mdvMap.execute(mdvMapConfig);
		displayParkObjects ();
	}
	
	// display external markers
	if (enableParkingSituationMarkers=='true') {
		this.displayExternalMarkers(parkingSituationMapMarkers);
	}
	
	// map does not move if it is grapped and the mouse moved over the borders of the map
	attachEventListener(document, 'mouseover', this.mdvMap.release.bind(this.mdvMap), false);
}


// This function zooms and centers on a clicked marker.
MDVMapHelper.prototype.onClick = function (id, msg, obj) {
    if (obj && obj.type == 'coord') {
        var centre = obj.marker.getCoords().clone();
        this.mdvMap.setCentre(centre);
        this.mdvMap.setZoomLevel(3);
        this.mdvMap.update();
    }
}


// ==================================================================
// Tooltips
// ==================================================================

// Tooltip
function MDVToolTipHelper(toolTip) {
	this.toolTip = toolTip;
	this.processed = false;
}


// This function creates the tooltip bubble.
// Parameter:	tooltip - tooltip object
MDVToolTipHelper.prototype.execute = function(tooltip) {
	var name = 'name_' + tooltip.id;
	if (!this.processed) {
		var body = '<div id="' + name + '"></div>';
		this.toolTip.setInnerHTML(body);
		
		tooltip.parking = new MDVEFAParkObjectInfo(name, absoluteServerPath.efa + '/parkingInfo/XML_PARKOBJECT_REQUEST');
		this.processed = true;
	}
	tooltip.parking.getParkingInfo(name, tooltip.id);
}


// ==================================================================
// Park Objects
// ==================================================================

// This function gets the json file park and ride objects to display on the map.
function displayParkObjects () {
	if (parkingObjectsPath != '') {
		var _ajax = mdvLib.ajax({ host: absoluteServerPath.efa + '' + parkingObjectsPath, parameters: null, method: 'get', onComplete: displayParkObjectsComplete});
	}
}


// This function displays the park object icons on the map.
function displayParkObjectsComplete (response) {
	var size = new MDVPoint(300, 200);
	var json;
	var _response = response.responseText || response;
	eval('json=' + _response + ';');
	
	for (var i=0; i < json.length; i++) {
		if (json[i].objectType != 'Zone') {
			var coord = new MDVCoordinates(nameMap, parseInt(json[i].x,10), parseInt(json[i].y,10));
			var marker;
			if (json[i].PR == true) {
				if (json[i].hasRealtime) {
					marker = mdvMap.mdvMap.createMarker(coord, 0.5, imgPath + 'parkAndRideRealtime.gif');
				}
				else {
					marker = mdvMap.mdvMap.createMarker(coord, 0.5, imgPath + 'parkAndRide.gif');
				}
			}
			else {
				if (json[i].hasRealtime) {
					marker = mdvMap.mdvMap.createMarker(coord, 0.5, imgPath + 'garageRealtime.gif');
				}
				else {
					marker = mdvMap.mdvMap.createMarker(coord, 0.5, imgPath + 'garage.gif');
				}
			}
			var tool = mdvMap.mdvMap.createToolTip(size,'<b>' + json[i].name + '</b>');
			tool.id = json[i].id;
			marker.setToolTip(tool);
			mdvMap.markerLayer.addMarker(marker);
		}
	}

	mdvMap.mdvMap.update();
}


// Route to a park object (link in bubble).
// Parameter:	x - x coordinate
//				y - y coordinate
//				parkingName - display text for the parking object
//				id - id of the parking object
function routingToParking (x, y, parkingName, id) {
	document.getElementById('nameInfo_destination').value = x + ':' + y + ':' + nameMap + ':' + parkingName;
	document.getElementById('itdLPxx_paID').value = id;
	document.getElementById('currentPage').value = 'journeyPlanner';
	document.getElementById('sessionID').value = 0;
	document.getElementById('requestID').value = 0;
	document.getElementById('itdLPxx_zoomLevel').value = '6';
    document.getElementById('itdLPxx_mapCentre').value = x + ':' + y + ':' + nameMap;
	document.forms[0].submit();
}


