var mdvMapsArr = new Array();
var mdvMapConfigs = new Array();
var mdvMiniMapConfigs = new Array();
// coordinates of the right mouse click
var ctxCoords = null;
var toolTipHelpers = new Array();
var usages = new Array("origin", "destination", "via", "dm", "ttb", "stt");
// output
var mdvOutputMaps = new Array();

// ================================================================
//  Input/output map configuration
// ================================================================
// input map configuration
function initMdvMapConfig() {
	for(var i=0; i<usages.length; i++) {
		var usage = usages[i];
		
		// map configuration
		mdvMapConfigs[usage] = mdvMapConfig.clone();
		mdvMapConfigs[usage].add('serverURL', mapBase + '/map/'); 
		mdvMapConfigs[usage].add('efaURL', '../lite/XSLT_COORD_REQUEST');
		mdvMapConfigs[usage].add('transparentImg', 'images/mdvMap/transparent.gif');
		mdvMapConfigs[usage].add('network', 'mvv');
		mdvMapConfigs[usage].add('defaultScale', '2');	
		mdvMapConfigs[usage].add('xCenterReal', '4467255');
		mdvMapConfigs[usage].add('yCenterReal', '826310');
		mdvMapConfigs[usage].add('mapName', 'MVTT');
		mdvMapConfigs[usage].add('block', '100');
		mdvMapConfigs[usage].add('toolTimeOut', '20');
		mdvMapConfigs[usage].add('zoomOnDoubleClick', 'true');
		mdvMapConfigs[usage].add('cursorMove', 'images/grabbing.cur');
		mdvMapConfigs[usage].add('useMagnifyGlass', 'true');
		mdvMapConfigs[usage].add('imagePath', 'images/mdvMap/');
		mdvMapConfigs[usage].add('language', mapLanguage);
		mdvMapConfigs[usage].add('useBubbleForEFAInfo', 'true');
		mdvMapConfigs[usage].add('useBubbleOverflowForEFAInfo', 'true');
		mdvMapConfigs[usage].add('useBubblePinForEFAInfo', 'true');
		mdvMapConfigs[usage].add('info.stop.size.width', '300');
		mdvMapConfigs[usage].add('info.stop.size.height', '200');
		mdvMapConfigs[usage].add('info.poi.size.width', '250');
		mdvMapConfigs[usage].add('info.poi.size.height', '100');

		// mini map configuration
		mdvMiniMapConfigs[usage] = mdvMiniMapConfig.clone();
		mdvMiniMapConfigs[usage].add('serverURL', mapBase + '/map/');
		mdvMiniMapConfigs[usage].add('efaURL', '../lite/XSLT_COORD_REQUEST');
		mdvMiniMapConfigs[usage].add('transparentImg', 'images/mdvMap/transparent.gif');
		mdvMiniMapConfigs[usage].add('network', 'mvv');
		mdvMiniMapConfigs[usage].add('defaultScale', '2');	
		mdvMiniMapConfigs[usage].add('xCenterReal', '4467255');
		mdvMiniMapConfigs[usage].add('yCenterReal', '826310');
		mdvMiniMapConfigs[usage].add('mapName', 'MVTT');
		mdvMiniMapConfigs[usage].add('block', '100');
		mdvMiniMapConfigs[usage].add('toolTimeOut', '20');
		mdvMiniMapConfigs[usage].add('zoomOnDoubleClick', 'true');
		mdvMiniMapConfigs[usage].add('useMagnifyGlass', 'true');
		mdvMiniMapConfigs[usage].add('cursorMove', 'images/grabbing.cur');
		mdvMiniMapConfigs[usage].add('language', mapLanguage);
	}
}


// output map configuration
function mdvOutputMapConfig (obj) {
	// map
	obj.config.add('serverURL', mapBase + '/map/'); 
	obj.config.add('transparentImg', 'images/mdvMap/transparent.gif');
	obj.config.add('tripURL', '../lite/XSLT_TRIP_REQUEST2');
	obj.config.add('network', 'mvv');
	obj.config.add('defaultScale', '2');
	obj.config.add('xCenterReal', '4467255');
	obj.config.add('yCenterReal', '826310');
	obj.config.add('mapName', 'MVTT');
	obj.config.add('imagePath', 'images/');
	obj.config.add('block', '100');
	obj.config.add('zoomOnDoubleClick', 'true');
	obj.config.add('useMagnifyGlass', 'true');
	obj.config.add('cursorMove', 'images/grabbing.cur');
	obj.config.add('trips.marker.size.height', '100');
	obj.config.add('trips.marker.size.width', '200');
	obj.config.add('toolTimeOut', '20');
	obj.config.add('imagePath', 'images/mdvMap/');
	obj.config.add('language', mapLanguage);
	obj.config.add('zoomOnDoubleClick', 'true');
	obj.config.add('useMagnifyGlass', 'true');
	obj.config.add('cursorMove', 'images/grabbing.cur');
	
	// mot colours 	
	obj.config.add('trips.polyline.opacity', '1');
	obj.config.add('trips.polyline.weight', '4');
	obj.config.add('motColour_0', '#0000FF');
	obj.config.add('motColour_1', '#27dee7');	// u-bahn
	obj.config.add('motColour_2', '#00FF00');	// s-bahn
	obj.config.add('motColour_3', '#0000FF');	// bus
	obj.config.add('motColour_4', '#FF0000'); 	// tram 
	obj.config.add('motColour_5', '#0000FF');	// lowfloor bus
	obj.config.add('motColour_6', '#000000');	// train
	obj.config.add('motColour_7', '#0000FF');
	obj.config.add('motColour_8', '#6c513d');	// taxi
	obj.config.add('motColour_9', '#0000FF');
	obj.config.add('motColour_10', '#000000');	// ship
	obj.config.add('motColour_11', '#0000FF');
	obj.config.add('motColour_12', '#85188f');	// sev
	obj.config.add('motColour_15', '#00FF00');	// s-bahn
	obj.config.add('motColour_16', '#6c513d');	// taxi
	obj.config.add('motColour_20', '#6c513d');	// taxi
	obj.config.add('motColour_98', '#ea6f14');	// walk
	obj.config.add('motColour_99', '#ea6f14');	// walk
	obj.config.add('motColour_100', '#ea6f14');	// walk
	obj.config.add('motColour_101', '#1db89d');	// bike
	obj.config.add('motColour_102', '#1db89d');	// bike
	obj.config.add('motColour_103', '#6c513d');	// car
	obj.config.add('motColour_104', '#6c513d');	// car
	obj.config.add('motColour_105', '#6c513d');	// taxi
	obj.config.add('motColour_106', '#6c513d');	// car
	obj.config.add('motColour_107', '#1db89d');	// bike
	obj.config.add('motColour_108', '#0000FF');
	
	// mini map
	obj.miniMapConfig.add('serverURL', mapBase + '/map/'); 
	obj.miniMapConfig.add('transparentImg', 'images/mdvMap/transparent.gif');
	obj.miniMapConfig.add('tripURL', '../lite/XSLT_TRIP_REQUEST2');
	obj.miniMapConfig.add('network', 'mvv');
	obj.miniMapConfig.add('defaultScale', '2');
	obj.miniMapConfig.add('xCenterReal', '4467255');
	obj.miniMapConfig.add('yCenterReal', '826310');
	obj.miniMapConfig.add('mapName', 'MVTT');
	obj.miniMapConfig.add('imagePath', 'images/');
	obj.miniMapConfig.add('block', '100');
	obj.miniMapConfig.add('zoomOnDoubleClick', 'true');
	obj.miniMapConfig.add('useMagnifyGlass', 'true');
	obj.miniMapConfig.add('cursorMove', 'images/grabbing.cur');
	obj.miniMapConfig.add('trips.marker.size.height', '100');
	obj.miniMapConfig.add('trips.marker.size.width', '200');
	obj.miniMapConfig.add('toolTimeOut', '20');
	obj.miniMapConfig.add('imagePath', 'images/mdvMap/');
}


// ================================================================
//  Load/unload input/output  map
// ================================================================

// This function loads and initializes the input/output map when the site is loaded.
function onLoadMapHandler(mapUsage) {
	if (document.getElementById('mdvMap_' + mapUsage)) {
		document.getElementById('mdvMap_' + mapUsage).style.display = 'block';
		document.getElementById('closeIAMap_' + mapUsage).style.display = 'block';
	}

	initMdvMapConfig();
	// input maps
	if (mapUsage && document.getElementById('mdvMap_' + mapUsage)) {
		if (mdvMapsArr[mapUsage] == null) 
			mdvMapsArr[mapUsage] = new MDVMapHelper(mapUsage);
	}
	// output maps
	else {
		var maps = document.getElementsByTagName('div');
		for (var i=0; i < maps.length; i++) {
			if (maps[i].className == 'mdvMap outputmap') {
				mdvOutputMaps[maps[i].id]  = new MDVMapHelperResults(maps[i]);
			}
		}
	}
}


// This function loads and initializes the input map with a centre and zoom level when the site is loaded.
function onLoadMapHandlerCentre(mapUsage, x, y, mapName, z) {
	initMdvMapConfig();
	if (mdvMapsArr[mapUsage] == null) {
		mdvMapsArr[mapUsage] = new MDVMapHelper(mapUsage);
		var nc = new MDVCoordinates(mapName, parseInt(x), parseInt(y));
		mdvMapsArr[mapUsage].mdvMap.setCentre(nc);
		mdvMapsArr[mapUsage].mdvMap.setZoomLevel(z);
		mdvMapsArr[mapUsage].mdvMap.update();
	}
}


// This function unload the input/output map when the site is unloaded.
function onUnloadMapHandler() {
	for(var i=0; i<usages.length; i++) {
		var usage = usages[i];
		if (mdvMapsArr[usage]) 
			mdvMapsArr[usage].destroy();
	}
	var maps = document.getElementsByTagName('div');
	for (var i=0; i < maps.length; i++) {
		if (maps[i].className == 'mdvMap outputmap' && mdvOutputMaps[maps[i].id] && mdvOutputMaps[maps[i].id].state > -1) {
			mdvOutputMaps[maps[i].id].destroy();
		}
	}
};


// This function destroys the map and mini map and clears the cache.
MDVMapHelper.prototype.destroy = function() {
	this.mdvMap.destroy();
	this.mdvMiniMap.destroy();
	this.mdvMap = null;
	this.mdvMiniMap = null;
};


// ================================================================
//  Input map functionality.
// ================================================================

// This function loads the map and minimap
function MDVMapHelper(mapUsage) {
	this.mdvMap = null;
	this.mdvMiniMap = null;
	this.markerLayer = null;
	this.value = null;
	this.marker = null;
	this.sync = null;
	this.usage = mapUsage;
	
	// initialize map and register events
	if (this.mdvMap == null) {
		this.mdvMap = new MDVMap(document.getElementById('mdvMap_' + this.usage));
		
		// context menu for enquiry screen
		this.ctxMenuArr = new Array();
		if (mapUsage=='destination')
			this.ctxMenuArr[0] = new MDVMapMenuItem(setDestination, this.setCoordinates);
		else if (mapUsage=='via')
			this.ctxMenuArr[0] = new MDVMapMenuItem(setVia, this.setCoordinates);
		else 
			this.ctxMenuArr[0] = new MDVMapMenuItem(setOrigin, this.setCoordinates);
		this.ctxMenu = new MDVMapMenu(this.ctxMenuArr);
		new MDVMapContextMenu(this.mdvMap, this.ctxMenu);
		this.mdvMap.events.registerEvent(MDVEvent_CONTEXT_MENU, null, ContextMenuHandler);
		this.mdvMap.events.registerEvent(MDVEvent_LAYER_UPDATED, null, this.highlightPoint);
		this.mdvMap.events.registerEvent(MDVEvent_OBJECT_DBLCLICKED, null, handleMarkerDblClick);
		
		new MDVMapNavigator(this.mdvMap);
		new MDVMapEFAInfoEx(this.mdvMap);	
		new MDVMapControl(this.mdvMap);
		
		this.mdvMap.events.registerEvent(MDVEvent_TOOLTIP, this, this.onToolTip);
		
		this.markerLayer = this.mdvMap.createLayer('vipMarkers');
		this.mdvMap.addLayer(this.markerLayer);
	}
	
	// initialize the mini map
	if (this.mdvMiniMap == null) {
		this.mdvMiniMap = new MDVMap(document.getElementById('mdvMiniMap_' + this.usage));
		new MDVMapNavigator(this.mdvMiniMap); 
	}
	
	// execute map and minimap
	if (this.mdvMap && this.mdvMiniMap) {
		this.mdvMap.execute(mdvMapConfigs[mapUsage]);			
		this.mdvMiniMap.execute(mdvMiniMapConfigs[mapUsage]);
		this.sync = new MDVMapSyncBound(this.mdvMap, this.mdvMiniMap);
	}
	
	// centre on identified origin
	if (document.forms[0].itdLPxx_mapOpen.value == this.usage) 
		this.doIdentified(this.usage);
		
	// 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);
	attachEventListener(document, 'mouseover', this.mdvMiniMap.release.bind(this.mdvMiniMap), false);
};

// display an identified poi, stop or address
MDVMapHelper.prototype.doIdentified = function(usage) {
	var zl = 7;
	var nc = null;
	var mapName = document.getElementById('mapName_' + usage).value;
	var x = document.getElementById('nameX_' + usage).value;
	var y = document.getElementById('nameY_' + usage).value;
	
	var type = document.getElementById('type_' + usage);
	var anyType = document.getElementById('anyType_' + usage);
	
	// address (selected by click or entered by form) 

	if (type && (type.value === 'coord' || (type.value === 'any' && (anyType && anyType.value !== 'stop' && anyType.value !== 'loc')))) { // && anyType.value !== 'poi'
		// set origin/destination marker
		if (x && y && mapName) {
			var marker = new MDVCoordinates(mapName, parseInt(x), parseInt(y));
			this.setMarker(marker, usage);
		}
	}
	
	if (x <= 0 || y <= 0) 
		return;
	
	nc = new MDVCoordinates(mapName, parseInt(x), parseInt(y));
	this.mdvMap.setCentre(nc);
	this.mdvMap.setZoomLevel(parseInt(zl));
	this.mdvMap.update();
};


// higlight a preselected poi, stop or address
MDVMapHelper.prototype.highlightPoint = function(id, msg, layer) {
	if (document.forms[0].itdLPxx_mapOpen) {
		var usage = document.forms[0].itdLPxx_mapOpen.value;
		var id = '';
		if (document.getElementById('itdLPxx_odv_' + usage))
			id = document.getElementById('itdLPxx_odv_' + usage).value;
		if (layer && id) {	
			var markers = layer.getMarkers();
			for (var m = 0; m < markers.length; m++) {
				var marker = markers[m];
				var object = marker.getObjectId();
				var type = object.type;
				var poiID;
				if (type=='POI_POINT'){
					var separator = object.id.indexOf('-');
					if (separator > 0)
						poiID = object.id.substring(separator+1, object.id.length);
				}
				if (object.id == id || poiID == id ) { 
					var img = marker.getImage();
					// give it a red border
					img.style.border='solid #FF0000 3px;';
					break;	
				}
			}
		}
	}
};


// This function sets the origin/destination marker
MDVMapHelper.prototype.setMarker = function(obj, usage) {
	var mc = new MDVCoordinates(obj.mapName, parseInt(obj.x), parseInt(obj.y));
	var text;
	var size = new MDVPoint(250, 180);
	
	if (document.getElementById('name_' + usage) && document.getElementById('name_' + usage).value!='')
		text = document.getElementById('name_' + usage).value;
	else if (usage == 'destination')
		text = mapDestination;
	else if (usage == 'via')
		text = mapVia;
	else 
		text = mapOrigin;

	this.marker = this.mdvMap.createMarker(mc, 0, this.getImage(usage));
	var tool = this.mdvMap.createToolTip(size, text);
	this.marker.setToolTip(tool);
	this.markerLayer.addMarker(this.marker);
};


// This function returns the marker image.
MDVMapHelper.prototype.getImage = function(usage) {
	var img = 'images/mdvMap/address.gif';
	switch(usage) {
		case 'destination':
			img = 'images/mdvMap/destination.gif';
			break;
		default:
			img = 'images/mdvMap/origin.gif';
			break;
	}

	var i = new Image();
	i.src = img;

	return img;
};


// ================================================================
// Common map functionality.
// ================================================================

// This function opens and closes the mini map.
function toggleMiniMap (usage) {
	if(document.getElementById('miniMapBorder_' + usage) && document.getElementById('miniMapOpener_' + usage)) {
		if (document.getElementById('miniMapBorder_' + usage).style.display == 'none') {
			document.getElementById('miniMapBorder_' + usage).style.display = 'block';
			document.getElementById('miniMapOpener_' + usage).style.display = 'none';
		}
		else {
			document.getElementById('miniMapBorder_' + usage).style.display = 'none';
			document.getElementById('miniMapOpener_' + usage).style.display = 'block';
		}
	}
}


// ==================================================================
// Tooltips (with departure monitor)
// ==================================================================

// simple tooltip for stops, poi and addresses.
MDVMapHelper.prototype.onToolTip = function(id, msg, obj) {
	var id = obj.getParent().objectId.id;
	
	if (obj.processed == null) {
		var type = obj.getParent().objectId.type;
		var omc = obj.getParent().objectId.omc;
		var id = obj.getParent().objectId.id;
		if (type=='POI_POINT'){
			var separator = id.indexOf('-');
			if (separator > 0)
				id = id.substring(separator+1, id.length);
		}
		var origin = 1;
		var destination = 2;
		var via = 3;
		var text = obj.getInnerHTML();
		text = text.replace(/Landmark: /, '');
		text = text.replace(/Stop: /, '');
		text += '<br/>';
		// select as origin/destination
		if (this.usage == 'destination') {
			text += ' <a href="javascript:submitStop(' + id + ',\'' + type + '\',\'' + omc + '\');">' + setDestination + '</a>';
			text += '<br/>';
		}
		else if (this.usage == 'via') {
			text += ' <a href="javascript:submitStop(' + id + ',\'' + type + '\',\'' + omc + '\');">' + setVia + '</a>';
			text += '<br/>';
		}
		else {
			text += ' <a href="javascript:submitStop(' + id + ',\'' + type + '\',\'' + omc + '\');">' + setOrigin + '</a><br/>';
			text += '<br/>';
		}
		// container for departure monitor
		if (type=='STOP'){
			text += '<div id="'+ id + '">' + wait +'</div>';
		}
		obj.setInnerHTML(text);
		obj.update();
		// load departure monitor
		if (type=='STOP'){
			obj.dm = new MDVEFADepartureMonitor(obj.parentMarker.objectId.id, '../dm/XML_DM_REQUEST');
			obj.dm.getDepartures(id);
		}
		obj.processed = true;
	} else {
		if (obj.getParent().objectId.type == 'STOP') { 
			obj.dm.getDepartures(id);
		}
	}
};


// sets the parameters for the request and submits the form
function submitStop (id, type, omc) {
	var usage;
	var frame = 0;
	var prefix;
	if (document.forms[0].itdLPxx_mapOpen) {
		usage = document.forms[0].itdLPxx_mapOpen.value;
	}
	if (usage == 'ttb' && document.forms[1]) {
		frame = 1;
	}
	if (type == 'POI_POINT') {
		prefix = '';
		type = 'poiID';
	}
	else 
		prefix = 'stopID:';
	
	toggleMap(usage);
	
	switch (type) {
		case 'poiID':
			// reset values
			if (document.forms[frame].placeInfo_origin)
				document.forms[frame].placeInfo_origin.value = '';
			if (document.forms[frame].placeInfo_destination)
				document.forms[frame].placeInfo_destination.value = '';
			if (document.forms[frame].placeInfo_via)
				document.forms[frame].placeInfo_via.value = '';
			if (document.getElementById('placeInfo_' + usage))
				document.getElementById('placeInfo_' + usage).value = omc + ':-1';
		default:	
			// reset values
			if (document.forms[frame].type_origin)
				document.forms[frame].type_origin.value = '';
			if (document.forms[frame].nameInfo_origin)
				document.forms[frame].nameInfo_origin.value = '';
			if (document.forms[frame].type_destination)
				document.forms[frame].type_destination.value = '';
			if (document.forms[frame].nameInfo_destination)
				document.forms[frame].nameInfo_destination.value = '';
			if (document.forms[frame].type_via)
				document.forms[frame].type_via.value = '';
			if (document.forms[frame].nameInfo_via)
				document.forms[frame].nameInfo_via.value = '';			
			if (document.getElementById('typeInfo_' + usage)) 	
				document.getElementById('typeInfo_' + usage).value = type;
			if (document.getElementById('nameInfo_' + usage))
				document.getElementById('nameInfo_' + usage).value = prefix + id;
		
			if (usage != '' && document.getElementById('itdLPxx_odv_' + usage))
				document.getElementById('itdLPxx_odv_' + usage).value = id;
	}
	document.forms[frame].submit();
};


function handleMarkerDblClick(id, status, objId) {

	if (typeof submitStop !== 'function') {
		return true;
	} 
	
	var type = objId.type;
	var omc = objId.omc;
	var id = objId.id;

	if (type=='POI_POINT'){
		var separator = id.indexOf('-');
		if (separator > 0)
			id = id.substring(separator+1, id.length);
	}

	submitStop(id, type, omc);
}


// ==================================================================
// Departure Monitor functions.
// ==================================================================

function MDVEFADepartureMonitor(id, url) {
	this.id 	= id;
	this.url 	= url;
}


MDVEFADepartureMonitor.prototype.getDepartures = function(identifier) {
	var _params = { language: mapLanguage, itdLPxx_id: identifier, name_dm: this.id, type_dm: 'stopID', mode: 'direct', limit: '5' };
		_params = $H(_params);
		_params = _params.toQueryString();

	var ajax = new Ajax.Request(this.url, { method: 'post', parameters: _params, onComplete: MDVEFADepartureMonitor_onAjaxComplete });
};


function MDVEFADepartureMonitor_onAjaxComplete(request) {
	if (request) {
		MDVEFADepartureMonitor_populateTable(request);
	}
}


function MDVEFADepartureMonitor_populateTable(request) {
	if (request.responseXML) {

		var firstChild = request.responseXML.firstChild;

		while (firstChild.nodeType != 1) {
			firstChild = firstChild.nextSibling;
		}

		var id = firstChild.getAttribute('id');
		var text = request.responseText;

		var target = document.getElementById(id);
			target.innerHTML = text;
	}
}


// ==================================================================

// Context menu functions.
// ==================================================================

// The context menu handler gets the mouse coordinates and the centre coordinates 
// when on origen/destination was selected via context menu. The centre coordinates 
// are useful to set the origin/destination marker on the clicked point. 
function ContextMenuHandler(id, status, obj) {
	if(status == true) 
		ctxCoords = obj;
};


// This context menu functions submits a selected point.
MDVMapHelper.prototype.setCoordinates = function() {
	
	var usage = document.forms[0].itdLPxx_mapOpen.value;
	
	toggleMap(usage);
	
	var frame = 0;
	if (usage == 'ttb' && document.forms[1]) {
		frame = 1;
	}
	if (mdvMapsArr[usage] && mdvMapsArr[usage].mdvMap) {
		var mapCentre = mdvMapsArr[usage].mdvMap.getCentre();
		var txt = (usage === 'destination') ? mapDestination : mapOrigin;
		document.getElementById('type_' + usage).value = 'coord';
		
		if (document.getElementById('nameState_' + usage)) {
			document.getElementById('nameState_' + usage).value = 'notidentified';
		}
		if (document.getElementById('placeState_' + usage)) {
			document.getElementById('placeState_' + usage).value = 'notidentified';
		}
		
		document.getElementById('name_' + usage).value = parseInt(ctxCoords.x) + ':' + parseInt(ctxCoords.y) + ':' + ctxCoords.mapName + ':' + txt;
		document.forms[frame].submit();
	}
	
	
};	


// ====================================================
// output maps
// ====================================================

function MDVMapHelperResults(viewport) {
	this.viewport = viewport;
	this.config = mdvMapConfig.clone();
	this.miniMapConfig = mdvMiniMapConfig.clone();
	
	if (this.config.params.length > 0)
		return false;
		
	// load map configuration
	mdvOutputMapConfig(this);	

	this.mdvMap = null;
	this.mdvMiniMap = null;
	this.trips = null;
	this.mdvMapTripsLoaded = false;
	
}


// create output map
MDVMapHelperResults.prototype.display = function() {
	// lazy creation
	if (!this.mdvMap) {
		this.mdvMap = new MDVMap(this.viewport);
		new MDVMapEFAInfoEx(this.mdvMap);
		new MDVMapControl(this.mdvMap);

		this.trips =	new MDVMapEFATrips(this.mdvMap);
		new MDVMapNavigator(this.mdvMap);
		// initialize the mini map
		
		if (!this.mdvMiniMap) {
			var minimapId = this.viewport.id.split('_')[1];
			if (document.getElementById('mdvMiniMap_' + minimapId)) {
				this.mdvMiniMap = new MDVMap(document.getElementById('mdvMiniMap_' + minimapId));
				new MDVMapNavigator(this.mdvMiniMap); 
				this.mdvMiniMap.execute(this.miniMapConfig);
				
				this.mdvMapSync = new MDVMapSyncBound(this.mdvMap, this.mdvMiniMap);
				
			}
		}
		this.mdvMap.execute(this.config);
		this.mdvMapSync.updateMiniMap(null, null, this.mdvMap);
	}
};

// display a journey on output map
MDVMapHelperResults.prototype.displayJourney = function(sessionID, requestID, tripIndex) {
	if (!this.mdvMapTripsLoaded) {
		this.trips.loadTrip(sessionID, requestID, tripIndex, true);
		this.mdvMapTripsLoaded = true;
	}
}

// add functions to the current onload stack
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) 
                oldonload();
            func();
        }
    }
}

addLoadEvent(onLoadMapHandler);





// ==================================================================
// Validate Time / Date input
// ==================================================================

var date = new Date();
	var curYear = date.getFullYear();
	var isLeapYear = (((curYear % 400) == "0") ? true
					: (((curYear % 100) == "0") ? false
					: (((curYear % 4) == "0") ? true
					: false)));
	
	
	var dateObj = {};
	dateObj.monthLen = [31,28,31,30,31,30,31,31,30,31,30,31];
	
	var errDiv = null;
	var parEl = null;
	
	function appendError(msg, el) {
		
		var parentEl = el.parentNode;
		var cnt = 0;
		
		while (cnt < 10 ) {
			if (parentEl && parentEl.className === 'box') {
				if (parEl === null) {
					parEl = parentEl;
					errDiv = document.createElement('div');
					errDiv.className = 'error';
					errDiv.innerHTML = msg;
					parentEl.appendChild(errDiv);
					break;
				}
			}
			parentEl = parentEl.parentNode;
			cnt++;
		} 
	}
	
	function clearError() {
		
		if (parEl && errDiv) {
			parEl.removeChild(errDiv);
			parEl = null;
			errDiv = null;
		}
		return true;
	}
	
	function initTimeFields() {
	
		var timeHour = document.getElementById('time1');
		var timeMinutes = document.getElementById('time2');
		
		if (!timeHour || !timeMinutes) {
			return true;
		}
		
		timeHour.onblur = function() {
			
			if (isNaN(parseInt(timeHour.value, 10)) || parseInt(timeHour.value, 10) > 23) {
				appendError(errHr, this);
			} else {
				clearError();
			}
		};
		
		
		timeMinutes.onblur = function() {
			if (isNaN(parseInt(timeMinutes.value, 10)) || parseInt(timeMinutes.value, 10) > 59) {
				appendError(errMin, this);
			} else {
				clearError();
			}
		};
	}
	
	function checkDayOfMonth(m) {
		var timeDay = document.getElementById('date1').value;
		var monLen = (m-1 === 1 && isLeapYear === true) ? 29 : dateObj.monthLen[m-1];	
		if (timeDay <= monLen) {
			return true;
		} else {
			return false;
		}
	}
	
	function initDateFields() {
		
		var timeDay = document.getElementById('date1');
		var timeMonth = document.getElementById('date2');
		
		if (!timeDay || !timeMonth) {
			return true;
		}
		
		timeDay.onblur = function() {
			if (isNaN(parseInt(timeDay.value, 10)) || parseInt(timeDay.value, 10) > 31) {
				appendError(errDay, this);
			} else {
				clearError();
			} 
		};
		
		timeMonth.onblur = function() {
			if (isNaN(parseInt(timeMonth.value, 10)) || parseInt(timeMonth.value, 10) > 12) {
				appendError(errMon, this);
			} else if (checkDayOfMonth(this.value) === false) {
				appendError(errDay, this);
			} else {
				clearError();
			}	
		};
	}
	
	addLoadEvent(initTimeFields);
	addLoadEvent(initDateFields);

