var mdvMapsArr = new Array();
var mdvMapConfigs = new Array();
var mdvMiniMapConfig = new Array();
var ctxCoords = null;

function onUnloadMapHandler() {
	if (mdvMapsArr['origin'])
		mdvMapsArr['origin'].destroy();
	if (mdvMapsArr['destination'])
		mdvMapsArr['destination'].destroy();
	if (mdvMapsArr['via'])
		mdvMapsArr['via'].destroy();
	if (mdvMapsArr['dm'])
		mdvMapsArr['dm'].destroy();
	if (mdvMapsArr['stt'])
		mdvMapsArr['stt'].destroy();
	if (mdvMapsArr['ttb'])
		mdvMapsArr['ttb'].destroy();
	if (mdvMapsArr['ss'])
		mdvMapsArr['ss'].destroy();
		
}

function initMdvMapConfig() {

	mdvMapConfigs['origin'] = mdvMapConfig.clone();
	mdvMapConfigs['origin'].add('serverURL', '/map/');
	mdvMapConfigs['origin'].add('efaURL', 'XML_MAP_REQUEST');
	mdvMapConfigs['origin'].add('transparentImg', 'images/transparent.gif');
	mdvMapConfigs['origin'].add('network', 'rlz');
	mdvMapConfigs['origin'].add('defaultScale', '2');	
	mdvMapConfigs['origin'].add('xCenterReal', '2313318');
	mdvMapConfigs['origin'].add('yCenterReal', '657525');
	mdvMapConfigs['origin'].add('mapName', 'TIEV');
	mdvMapConfigs['origin'].add('block', '100');
	mdvMapConfigs['origin'].add('toolTimeOut', '20');
	mdvMapConfigs['origin'].add('zoomOnDoubleClick', 'true');
	mdvMapConfigs['origin'].add('cursorMove', 'images/grabbing.cur');
	mdvMapConfigs['origin'].add('block', '100');
	//mdvMapConfigs['origin'].add('useBubbleForEFAInfo', 'true');
    //mdvMapConfigs['origin'].add('info.stop.size.width', '220');
    //mdvMapConfigs['origin'].add('info.stop.size.height', '150');
    //mdvMapConfigs['origin'].add('info.poi.size.width', '200');
    //mdvMapConfigs['origin'].add('info.poi.size.height', '70');
    mdvMapConfigs['origin'].add('useBubbleOverflowForEFAInfo', 'true');

	
	
	mdvMapConfigs['destination'] = mdvMapConfig.clone();
	mdvMapConfigs['destination'].add('serverURL', '/map/');
	mdvMapConfigs['destination'].add('efaURL', 'XML_MAP_REQUEST');
	mdvMapConfigs['destination'].add('transparentImg', 'images/transparent.gif');
	mdvMapConfigs['destination'].add('network', 'rlz');
	mdvMapConfigs['destination'].add('defaultScale', '2');	
	mdvMapConfigs['destination'].add('xCenterReal', '2313318');
	mdvMapConfigs['destination'].add('yCenterReal', '657525');
	mdvMapConfigs['destination'].add('mapName', 'TIEV');
	mdvMapConfigs['destination'].add('block', '100');
	mdvMapConfigs['destination'].add('toolTimeOut', '20');
	//mdvMapConfigs['destination'].add('zoomOnDoubleClick', 'true');
	mdvMapConfigs['destination'].add('cursorMove', 'images/grabbing.cur');
	mdvMapConfigs['destination'].add('useBubbleForEFAInfo', 'true');
    mdvMapConfigs['destination'].add('info.stop.size.width', '220');
    mdvMapConfigs['destination'].add('info.stop.size.height', '150');
    mdvMapConfigs['destination'].add('info.poi.size.width', '200');
    mdvMapConfigs['destination'].add('info.poi.size.height', '70');
    mdvMapConfigs['destination'].add('useBubbleOverflowForEFAInfo', 'true');

	
	mdvMapConfigs['dm'] = mdvMapConfig.clone();
	mdvMapConfigs['dm'].add('serverURL', '/map/');
	mdvMapConfigs['dm'].add('efaURL', 'XML_MAP_REQUEST');
	mdvMapConfigs['dm'].add('transparentImg', 'images/transparent.gif');
	mdvMapConfigs['dm'].add('network', 'rlz');
	mdvMapConfigs['dm'].add('defaultScale', '2');	
	mdvMapConfigs['dm'].add('xCenterReal', '2313318');
	mdvMapConfigs['dm'].add('yCenterReal', '657525');
	mdvMapConfigs['dm'].add('mapName', 'TIEV');
	mdvMapConfigs['dm'].add('block', '100');
	mdvMapConfigs['dm'].add('toolTimeOut', '20');
	mdvMapConfigs['dm'].add('zoomOnDoubleClick', 'true');
	mdvMapConfigs['dm'].add('cursorMove', 'images/grabbing.cur');
	// mdvMapConfigs['dm'].add('useBubbleForEFAInfo', 'true');
    mdvMapConfigs['dm'].add('info.stop.size.width', '220');
    mdvMapConfigs['dm'].add('info.stop.size.height', '150');
    mdvMapConfigs['dm'].add('info.poi.size.width', '200');
    mdvMapConfigs['dm'].add('info.poi.size.height', '70');
    mdvMapConfigs['dm'].add('useBubbleOverflowForEFAInfo', 'true');

	
	
	
	mdvMiniMapConfig['origin'] = mdvMiniMapConfig.clone();
	mdvMiniMapConfig['origin'].add('serverURL', '/map/');
	mdvMiniMapConfig['origin'].add('efaURL', 'XML_MAP_REQUEST');
	mdvMiniMapConfig['origin'].add('transparentImg', 'images/transparent.gif');
	mdvMiniMapConfig['origin'].add('network', 'rlz');
	mdvMiniMapConfig['origin'].add('defaultScale', '2');	
	mdvMiniMapConfig['origin'].add('xCenterReal', '2313318');
	mdvMiniMapConfig['origin'].add('yCenterReal', '657525');
	mdvMiniMapConfig['origin'].add('mapName', 'TIEV');
	mdvMiniMapConfig['origin'].add('block', '100');
	mdvMiniMapConfig['origin'].add('toolTimeOut', '20');
	mdvMiniMapConfig['origin'].add('zoomOnDoubleClick', 'true');
	mdvMiniMapConfig['origin'].add('cursorMove', 'images/grabbing.cur');
	
	mdvMiniMapConfig['destination'] = mdvMiniMapConfig.clone();
	mdvMiniMapConfig['destination'].add('serverURL', '/map/');
	mdvMiniMapConfig['destination'].add('efaURL', 'XML_MAP_REQUEST');
	mdvMiniMapConfig['destination'].add('transparentImg', 'images/transparent.gif');
	mdvMiniMapConfig['destination'].add('network', 'rlz');
	mdvMiniMapConfig['destination'].add('defaultScale', '2');	
	mdvMiniMapConfig['destination'].add('xCenterReal', '2313318');
	mdvMiniMapConfig['destination'].add('yCenterReal', '657525');
	mdvMiniMapConfig['destination'].add('mapName', 'TIEV');
	mdvMiniMapConfig['destination'].add('block', '100');
	mdvMiniMapConfig['destination'].add('toolTimeOut', '20');
	mdvMiniMapConfig['destination'].add('zoomOnDoubleClick', 'true');
	mdvMiniMapConfig['destination'].add('cursorMove', 'images/grabbing.cur');
	
	mdvMiniMapConfig['via'] = mdvMiniMapConfig.clone();
	mdvMiniMapConfig['via'].add('serverURL', '/map/');
	mdvMiniMapConfig['via'].add('efaURL', 'XML_MAP_REQUEST');
	mdvMiniMapConfig['via'].add('transparentImg', 'images/transparent.gif');
	mdvMiniMapConfig['via'].add('network', 'rlz');
	mdvMiniMapConfig['via'].add('defaultScale', '2');	
	mdvMiniMapConfig['via'].add('xCenterReal', '2313318');
	mdvMiniMapConfig['via'].add('yCenterReal', '657525');
	mdvMiniMapConfig['via'].add('mapName', 'TIEV');
	mdvMiniMapConfig['via'].add('block', '100');
	mdvMiniMapConfig['via'].add('toolTimeOut', '20');
	mdvMiniMapConfig['via'].add('zoomOnDoubleClick', 'true');
	mdvMiniMapConfig['via'].add('cursorMove', 'images/grabbing.cur');
	
	mdvMiniMapConfig['dm'] = mdvMiniMapConfig.clone();
	mdvMiniMapConfig['dm'].add('serverURL', '/map/');
	mdvMiniMapConfig['dm'].add('efaURL', 'XML_MAP_REQUEST');
	mdvMiniMapConfig['dm'].add('transparentImg', 'images/transparent.gif');
	mdvMiniMapConfig['dm'].add('network', 'rlz');
	mdvMiniMapConfig['dm'].add('defaultScale', '2');	
	mdvMiniMapConfig['dm'].add('xCenterReal', '2313318');
	mdvMiniMapConfig['dm'].add('yCenterReal', '657525');
	mdvMiniMapConfig['dm'].add('mapName', 'TIEV');
	mdvMiniMapConfig['dm'].add('block', '100');
	mdvMiniMapConfig['dm'].add('toolTimeOut', '20');
	mdvMiniMapConfig['dm'].add('zoomOnDoubleClick', 'true');
	mdvMiniMapConfig['dm'].add('cursorMove', 'images/grabbing.cur');
	
}

function onLoadMapHandler() {

	var form = document.forms[0];
	initMdvMapConfig();

	
	 for (var i=0; i < form.length; i++) {
	
	
		if (form[i].name && form[i].name.substring(0, 15) == 'itdLPxx_mdvMap_') {
		
		
		if(document.getElementById('nameState_' + form[i].name.substring(15)).value!='list')
			{
				var name = form[i].name.substring(15, form[i].name.length);
				var value = form[i].value;

				if (mdvMapsArr[name] == null) {
					mdvMapsArr[name] = new MDVMapHelper(name, value);
				}
			}
		}
	} 


}

function ContextMenuHandler(id, status, obj) {
	if (status)
		ctxCoords = obj;
}

/* Highlights a preselected stop or poi*/
function hightlightStop(id, msg, layer) {
	// highlight stop
	
	var ids_origin;
	var idArr = new Array();
	
	
	if(document.getElementById('itdLPxx_id_origin')) 
		ids_origin = document.getElementById('itdLPxx_id_origin').value;
		
	else if(document.getElementById('itdLPxx_id_dm')) 
		ids_origin = document.getElementById('itdLPxx_id_dm').value;
		
	else if(document.getElementById('itdLPxx_id_stt')) 
		ids_origin = document.getElementById('itdLPxx_id_stt').value;
		
	else if(document.getElementById('itdLPxx_id_ttb')) 
		ids_origin = document.getElementById('itdLPxx_id_ttb').value;
		
	else if(document.getElementById('itdLPxx_id_ss')) 
		ids_origin = document.getElementById('itdLPxx_id_ss').value;
		
		
	if(document.getElementById('itdLPxx_id_destination')) {
		var ids_destination;
		ids_destination = document.getElementById('itdLPxx_id_destination').value;
		idArr[1] = ids_destination.split(':');
	}
	if(document.getElementById('itdLPxx_id_via')) {
		var ids_via;
		ids_via = document.getElementById('itdLPxx_id_via').value;
		idArr[2] = ids_via.split(':');
	}
	
	
	// Substitute with column (':') separated list of ids. 
	idArr[0] = ids_origin.split(':');


	var marked = 0;

	if (layer) {
		var markers = layer.getMarkers();

		for (var m = 0; m < markers.length; m++) {
			var marker = markers[m];
			var object = marker.getObjectId();
			for (var j=0; j<idArr.length; j++){
				for (var i = 0; i < idArr[j].length - 1; i++) {
					if(document.getElementById('odvNameList_' + idArr[j][idArr[j].length - 1])){
						if(document.getElementById('odvNameList_' + idArr[j][idArr[j].length - 1]).options[i] && document.getElementById('listElem_' + idArr[j][idArr[j].length - 1] + '_' + i)) {
							if (document.getElementById('odvNameList_' + idArr[j][idArr[j].length - 1]).options[i].selected && object.id == document.getElementById('listElem_' + idArr[j][idArr[j].length - 1] + '_' + i).value) {
								var img = marker.getImage();
								// Give it a nice red border...
								img.style.border='solid #FF0000 3px;';
								marked++;
								// List is ought to be unique. No need to continue iteration cycle. Leave...
								break;
							}
						}
					}
					else{
					
						if (object.id == idArr[j][i]) {
							var img = marker.getImage();
							// Give it a nice red border...
							img.style.border='solid #FF0000 3px;';
							marked++;
							// List is ought to be unique. No need to continue iteration cycle. Leave...
							break;
						}
					}

				}

				if (marked >= idArr.length) {
					// Leave iteration as soon as required count of markers have been highlighted.
					break;
				}
			}

		}
	}
}









function MDVMapHelper(id, value) {
	this.id = id;
	this.mdvMap = null;
	this.mdvMiniMap = null;
	

	this.input = document.getElementById('itdLPxx_mdvMap_' + id);

	this.value = value;

	this.marker = null;

	this.sync = null;
	
	
	var text = id;
	
	if(id != 'origin' && id != 'destination'){
		text= 'point';
	}
	

	this.ctxMenuArr = new Array();
	if(mapLanguage =='en')
		this.ctxMenuArr[0] = new MDVMapMenuItem("Set " + text, this.setCoordinates);
		
	else if(mapLanguage =='it')
		this.ctxMenuArr[0] = new MDVMapMenuItem("Seleziona punto di mappa", this.setCoordinates);
	else if(mapLanguage=='fr')
		this.ctxMenuArr[0] = new MDVMapMenuItem("Punkt übernehmen", this.setCoordinates);
	else if(mapLanguage=='ar')
		this.ctxMenuArr[0] = new MDVMapMenuItem("Set " + text, this.setCoordinates);
	else
		this.ctxMenuArr[0] = new MDVMapMenuItem("Punkt übernehmen", this.setCoordinates);
	this.ctxMenu = new MDVMapMenu(this.ctxMenuArr);

	this.execute();
}

MDVMapHelper.prototype.destroy = function() {
	this.mdvMap.destroy();
	this.mdvMiniMap.destroy();

	this.mdvMap = null;
	this.mdvMiniMap = null;
};

MDVMapHelper.prototype.getImage = function() {
	var img = 'images/address.gif';

	switch(this.id) {
		case 'origin':
			img = 'images/origin.gif';
			break;
		case 'destination':
			img = 'images/destination.gif';
			break;
	}

	var i = new Image();
		i.src = img;

	return img;
};

MDVMapHelper.prototype.setCoordinates = function() {
	var name = this.mdvMap.name.substring(this.mdvMap.name.lastIndexOf('_')+1);
	if (mdvMapsArr[name])
		if(this.id == 'stt' || this.id == 'dm') {
			if(document.getElementById('itdLPxx_mdvMap_stt'))
				document.getElementById('itdLPxx_mdvMap_stt').value='';
			if(document.getElementsByName('sessionID')[0])
				document.getElementsByName('sessionID')[0].value='0';
			if(document.getElementsByName('requestID')[0])
				document.getElementsByName('requestID')[0].value='0';
		}
		
		var usage = this.mdvMap.name.substring(7);
		
		
		
		if(mapLanguage =='en')
			mdvMapsArr[name].onObjectClicked(-1, '', { type: 'coord', name: 'Map ' + usage, x: parseInt(ctxCoords.x), y: parseInt(ctxCoords.y), mapName: ctxCoords.mapName });
		else if(mapLanguage =='ar')
			mdvMapsArr[name].onObjectClicked(-1, '', { type: 'coord', name: 'Map ' + usage, x: parseInt(ctxCoords.x), y: parseInt(ctxCoords.y), mapName: ctxCoords.mapName });
		else if(mapLanguage =='it')
			mdvMapsArr[name].onObjectClicked(-1, '', { type: 'coord', name: 'Mappa ', x: parseInt(ctxCoords.x), y: parseInt(ctxCoords.y), mapName: ctxCoords.mapName });
		else if(mapLanguage =='fr')
			mdvMapsArr[name].onObjectClicked(-1, '', { type: 'coord', name: 'Gewaehlter Punkt aus Karte', x: parseInt(ctxCoords.x), y: parseInt(ctxCoords.y), mapName: ctxCoords.mapName });
		else
			mdvMapsArr[name].onObjectClicked(-1, '', { type: 'coord', name: 'Gewaehlter Punkt aus Karte', x: parseInt(ctxCoords.x), y: parseInt(ctxCoords.y), mapName: ctxCoords.mapName });
};

MDVMapHelper.prototype.execute = function () {
	var value = this.value;
	var nameState = this.getNameState();
	var placeState = this.getPlaceState();

	this.doDefault();

	if (value.length > 0 && this.mdvMap && this.mdvMiniMap) {
		var mdvMapTarget = document.getElementById('mdvMap_' + this.id);

		if (mdvMapTarget) {
			mdvMapTarget.style.display = 'block';

			//var T1 = (new Date()).getTime();
			this.mdvMap.execute(mdvMapConfigs[this.id]);
			//alert(((new Date()).getTime() - T1) / 1000);
			this.mdvMiniMap.execute(mdvMiniMapConfig[this.id]);

			if (this.mdvMap && this.mdvMiniMap)
				this.sync = new MDVMapSyncBound(this.mdvMap, this.mdvMiniMap);
		}
	}
	else if (value.length <= 0) {
		var mdvMapTarget = document.getElementById('mdvMap_' + this.id);

		if (mdvMapTarget)
			mdvMapTarget.style.display = 'none';
	}

	if (value.length > 0 && nameState == 'identified')
		this.doIdentified();
	else if (value.length > 0 && nameState == 'list')
		this.doPlace();
	else if (value.length > 0 && placeState == 'list')
		this.onChange(null)
	else if (value.length > 0 && placeState == 'identified')
		this.doPlace();
	else if (value.length > 0 && (this.getCommand() == 'changeRequest' || this.getCommand() == 'tripRetoure'))
		this.doNotIdentified();
};

MDVMapHelper.prototype.getCommand = function() {
	var command = document.getElementById('itdLPxx_command');

	if (command)
		return command.value;
	return '';
};

MDVMapHelper.prototype.getType = function() {
	var typeSelectors = document.getElementsByName('type_' + this.id);

	if (typeSelectors.length == 1)
		return typeSelectors[0].value;
	else if (typeSelectors.length > 1) {
		for (var i=0; i < typeSelectors.length; i++) {
			var temp = typeSelectors[i];
			if (temp.checked)
				return temp.value;
		}
	} else {
		return '';
	}
};

MDVMapHelper.prototype.getNameState = function() {
	var state = document.getElementById('nameState_' + this.id);
	if (state)
		return state.value;
	else
		return '';
};

MDVMapHelper.prototype.getPlaceState = function() {
	var state = document.getElementById('placeState_' + this.id);
	if (state)
		return state.value;
	else
		return '';
};

MDVMapHelper.prototype.onNewCentre = function (id, msg, obj) {

	var centre = obj.getCentre();
	this.input.value = centre.x + ':' + centre.y + ':' + centre.mapName + ':' + obj.config.getZoomLevelIndex();
};

MDVMapHelper.prototype.doDefault = function() {

	var values = this.value.split(':');
	
	
	if (this.mdvMap == null) {
		this.mdvMap = new MDVMap(document.getElementById('mdvMap_' + this.id));
		new MDVMapContextMenu(this.mdvMap, this.ctxMenu);
		new MDVMapEFAInfo(this.mdvMap);
		new MDVMapNavigator(this.mdvMap);
		new MDVMapEFAInfo(this.mdvMap);
		
		new MDVMapControl(this.mdvMap);

		if (!mdvMapConfigs[this.id].get('xCenterReal') &&
			!mdvMapConfigs[this.id].get('yCenterReal') &&
			!mdvMapConfigs[this.id].get('mapName')) {

			mdvMapConfigs[this.id].add('xCenterReal', values[0]);
			mdvMapConfigs[this.id].add('yCenterReal', values[1]);
			mdvMapConfigs[this.id].add('mapName', values[2]);
			
			
			if(document.getElementById('nameState_' + this.id).value=='identified')
			{
				mdvMapConfigs[this.id].add('defaultScale', '4');
			}
			else if (values[3]) {
				mdvMapConfigs[this.id].add('defaultScale', values[3]);
			} 
			else {
				mdvMapConfigs[this.id].add('defaultScale', '1');
			}
			
		}
		


		this.mdvMap.events.registerEvent(MDVEvent_MAP_INITIALISED, this, this.onNewCentre);
		this.mdvMap.events.registerEvent(MDVEvent_OBJECT_CLICKED, this, this.onObjectClicked);
		this.mdvMap.events.registerEvent(MDVEvent_TOOLTIP, this, this.onToolTip);
		this.mdvMap.events.registerEvent(MDVEvent_CONTEXT_MENU, null, ContextMenuHandler);
		this.mdvMap.events.registerEvent(MDVEvent_LAYER_UPDATED, null, hightlightStop);
		this.mdvMap.events.registerEvent(MDVEvent_MAP_INITIALISED, this, this.onInit);
	}

	
	if (this.mdvMiniMap == null) {
		this.mdvMiniMap = new MDVMap(document.getElementById('mdvMiniMap_' + this.id));

		if (!mdvMiniMapConfig[this.id].get('xCenterReal') &&
			!mdvMiniMapConfig[this.id].get('yCenterReal') &&
			!mdvMiniMapConfig[this.id].get('mapName')) {
				new MDVMapNavigator(this.mdvMiniMap);

				mdvMiniMapConfig[this.id].add('xCenterReal', values[0]);
				mdvMiniMapConfig[this.id].add('yCenterReal', values[1]);
				mdvMiniMapConfig[this.id].add('mapName', values[2]);

				if (values[3]) {
					mdvMiniMapConfig[this.id].add('defaultScale', values[3]);
				}
				else {
					mdvMiniMapConfigs[this.id].add('defaultScale', '2');
				}
		}
	}
	
};

MDVMapHelper.prototype.onInit = function (id, msg, obj) {
	if (this.mdvMiniMap && this.mdvMiniMap.config) {
		var mini = document.getElementById('mdvMiniMap_' + this.id);
		var zl = this.mdvMiniMap.config.getZoomLevel(obj.config.getZoomLevelIndex());
		
		if (zl == null) {
			mini.style.display = 'none';
		} else if (mini.style.display != 'block') {
			mini.style.display = 'block';			
			this.mdvMiniMap.update();
		}
	}
};


MDVMapHelper.prototype.onToolTip = function(id, msg, obj) {
	
	//if (obj && obj.isVisible()) {
	
	switch (obj.getParent().objectId.type) {
			case 'poi':
	  if (obj && obj.isVisible()) {
		obj.setInnerHTML('Place of Interest: <b>'
			+ obj.getParent().objectId.desc
			+ '</b>'); 

	/*	obj.dm = new MDVEFADepartureMonitor(obj.parentMarker.objectId.id, '../dm/XML_DM_REQUEST');
		obj.dm.getDepartures(obj.getParent().objectId.id); */
	} 




			break;

			default:
			
	  if (obj && obj.isVisible()) {
		obj.setInnerHTML('Stop: <b>'
			+ obj.getParent().objectId.desc
			+ '</b><br><br><div id="'
			+ obj.getParent().objectId.id
			+ '"></div>'); 

		/* obj.dm = new MDVEFADepartureMonitor(obj.parentMarker.objectId.id, '../dm/XML_DM_REQUEST');
		obj.dm.getDepartures(obj.getParent().objectId.id); */
	} 			
			break;
		}
	
	
};





MDVMapHelper.prototype.doNotIdentified = function() {
	var values = this.value.split(':');

	var display = document.getElementById('nameDisplay_' + this.id).value;

	if (values.length >= 3) {
		var mapName = values[2];
		var x = values[0];
		var y = values[1];

		var nc = new MDVCoordinates(mapName, parseInt(x), parseInt(y));
		this.mdvMap.setCentre(nc);
		this.mdvMap.setZoomLevel(8);

		this.setMarker({ type: this.getType(), name: display, mapName: mapName, x: x, y: y });
	}

	this.mdvMap.update();

};

MDVMapHelper.prototype.doIdentified = function() {

	var mapName = document.getElementById('nameMapName_' + this.id).value;
	var x = document.getElementById('nameX_' + this.id).value;
	var y = document.getElementById('nameY_' + this.id).value;
	var display = document.getElementById('nameDisplay_' + this.id).value;
	
	if (x <= 0 || y <= 0)
		return;

	var nc = new MDVCoordinates(mapName, parseInt(x), parseInt(y));
	this.mdvMap.setCentre(nc);
	this.mdvMap.setZoomLevel(6);

	this.setMarker({ type: this.getType(), name: display, mapName: mapName, x: x, y: y });

	this.mdvMap.update();
};

MDVMapHelper.prototype.doAddress = function(obj) {
	var params = { name_sf: 'streetID:' + obj.streetID + ':' + obj.houseNumber + ':' + obj.omc, type_sf: 'address', itdLPxx_usage: this.id, 'itdLPxx_zoomLevel': 6 };
	params = $H(params).toQueryString();

	var _ajax = new Ajax.Request('XML_STOPFINDER_REQUEST', { method: 'post', parameters: params, onComplete: MDVMapHelper_AjaxCallback });
}

MDVMapHelper.prototype.doCoordinates = function(obj) {

	var nc = null;

	var zl = 5;


	if (obj && obj.x && obj.y)
		nc = new MDVCoordinates(obj.mapName, parseInt(obj.x), parseInt(obj.y));
	else if (obj && obj.firstChild) {
		var target = obj.getElementsByTagName('odvNameElem');
		if (target.length == 1)
			nc = new MDVCoordinates(target[0].getAttribute('mapName'), parseInt(target[0].getAttribute('x')), parseInt(target[0].getAttribute('y')));
		else {
			for (var i=0; i < target.length; i++) {
				if(target[i].getAttribute('streetID') && parseInt(target[i].getAttribute('streetID')) == 0) {
					nc = new MDVCoordinates(target[i].getAttribute('mapName'), target[i].getAttribute('x'), target[i].getAttribute('y'));
					break;
				}
			}
		}
		var itdLayoutParams = obj.getElementsByTagName('itdLayoutParam');
		for (var i=0; i < itdLayoutParams.length; i++) {
			if (itdLayoutParams[i].getAttribute('name') == 'zoomLevel') {
				zl = itdLayoutParams[i].getAttribute('value');
				break;
			}
		}
	}

	if (nc && nc.x > 0 && nc.y > 0) {
		this.mdvMap.setCentre(nc);
		this.mdvMap.setZoomLevel(parseInt(zl));

		if (obj && obj.type && (obj.type == 'address' || obj.type == 'crossing'))
			this.setMarker(obj);

		this.mdvMap.update();
	}

};

MDVMapHelper.prototype.setMarker = function(obj) {

	var mc = new MDVCoordinates(obj.mapName, parseInt(obj.x), parseInt(obj.y));

	if (this.marker == null && obj.type != 'stop' && obj.type != 'poi' && obj.type != 'any') {
		this.marker = this.mdvMap.createMarker(mc, 0, this.getImage());
		var tool = this.mdvMap.createToolTip('<b>' + obj.name + '</b>');
		this.marker.setToolTip(tool);
		this.mdvMap.addMarker(this.marker);
	} else if (this.marker && obj.type != 'stop' && obj.type != 'poi' && obj.type != 'any') {
		this.marker.setCoords(mc);
		this.marker.toolTip.setInnerHTML('<b>' + obj.name + '</b>');
	}
};

MDVMapHelper.prototype.doPOI = function(obj) {

	var params = { name_sf: obj.poiID, type_sf: 'poiID', place_sf: 'placeID:'+obj.omc+':'+obj.placeID, itdLPxx_usage: this.id };
	params = $H(params).toQueryString();

	var _ajax = new Ajax.Request('XML_STOPFINDER_REQUEST', { method: 'post', parameters: params, onComplete: MDVMapHelper_AjaxCallback });
};

MDVMapHelper.prototype.doStop = function(obj) {

	var params = { name_sf: obj.stopID, type_sf: 'stopID', itdLPxx_usage: this.id };
	params = $H(params).toQueryString();

	var _ajax = new Ajax.Request('XML_STOPFINDER_REQUEST', { method: 'post', parameters: params, onComplete: MDVMapHelper_AjaxCallback });
};

MDVMapHelper.prototype.doPlace = function(obj) {

	var omc = 0; var placeID = -1;

	if (obj == null)
		omc = document.getElementById('placeOMC_' + this.id).value;
	else {
		omc = obj.omc;
		placeID = obj.placeID;
	}

	var params = { omc: omc, placeID: placeID, itdLPxx_usage: this.id, createImage: 0, pinTypeStop: 0, pinTypePOI: 0, itdLPxx_zoomLevel: '7' };
	params = $H(params).toQueryString();

	var _ajax = new Ajax.Request('XML_MAP_REQUEST', { method: 'post', parameters: params, onComplete: MDVMapHelper_AjaxCallback });
};

MDVMapHelper.prototype.onChange = function(obj) {
	if (this.value <= 0)
		return;

	this.resetInfo();

	var selectedIndex = 0;
	if (obj != null) {
		selectedIndex = obj.selectedIndex;
	}

	var temp = odv[this.id][selectedIndex];

	switch (temp.type) {
		case 'place':
			this.doPlace(temp);
			break;
		case 'stop':
			this.doStop(temp);
			break;
		case 'poi':
			this.doPOI(temp);
			break;
		case 'address':
			if (temp.x == '' && temp.y == '' && temp.streetID > 0) {
				this.doAddress(temp);
				break;
			}
		default:
			this.doCoordinates(temp);
			break;
	}
	
};

MDVMapHelper.prototype.resetInfo = function() {
	var name 		= document.getElementById('nameInfo_' + this.id);
	var place 		= document.getElementById('placeInfo_' + this.id);
	var placeState 	= document.getElementById('placeState_' + this.id);
	var type 		= document.getElementById('typeInfo_' + this.id);

	if (name)
		name.value = 'invalid';
	if (place)
		place.value = 'invalid';
	if (type)
		type.value = 'invalid';
	if (placeState);
		placeState.value = 'invalid';
};

MDVMapHelper.prototype.onObjectClicked = function(id, msg, obj) {

	var name 		= document.getElementById('nameInfo_' + this.id);
	var place 		= document.getElementById('placeInfo_' + this.id);
	var placeState 	= document.getElementById('placeState_' + this.id);
	var type 		= document.getElementById('typeInfo_' + this.id);
	var execInst 	= document.getElementById('execInst');
	
	if(this.id == 'stt' || this.id == 'dm') {
		if(document.getElementById('itdLPxx_mdvMap_stt'))
			document.getElementById('itdLPxx_mdvMap_stt').value='';
		if(document.getElementsByName('sessionID')[0])
			document.getElementsByName('sessionID')[0].value='0';
		if(document.getElementsByName('requestID')[0])
			document.getElementsByName('requestID')[0].value='0';
	}
	switch (obj.type) {
		case 'stop':
			name.value = obj.id;
			type.value = 'stopID';
			placeState.value = 'empty';
			place.value = '';
			if (this.marker)
				this.mdvMap.removeMarker(this.marker);
			if(this.id != 'dm' && this.id != 'stt' && this.id != 'ttb' && this.id != 'ss')
				execInst.value = 'verifyOnly';
			this.mdvMap.mapper.style.cursor = 'wait';
			this.input.value = '';
			document.forms[0].submit();
			break;
		case 'poi':
			place.value = obj.omc + ':-1';
			name.value = obj.id;
			type.value = 'poiID';
			if (this.marker)
				this.mdvMap.removeMarker(this.marker);
			if(this.id != 'dm' && this.id != 'stt' && this.id != 'ttb' && this.id != 'ss')
				execInst.value = 'verifyOnly';
			this.mdvMap.mapper.style.cursor = 'wait';
			this.input.value = '';
			document.forms[0].submit();
			break;
		case 'coord':
			if (obj.id)
				return;
			name.value = obj.x + ':' + obj.y + ':' + obj.mapName + ':' + obj.name;
			type.value = 'coord';
			placeState.value = 'empty';
			place.value = '';
			if(this.id != 'dm' && this.id != 'stt' && this.id != 'ttb' && this.id != 'ss')
				execInst.value = 'verifyOnly';
			this.mdvMap.mapper.style.cursor = 'wait';
			this.input.value = '';
			document.forms[0].submit();
			break;
	}
};

function MDVMapHelper_AjaxCallback(request) {
	var usage = request.responseXML.getElementsByTagName('itdLayoutParam');
	for (var i=0; i < usage.length; i++) {
		if (usage[i].getAttribute('name') == 'usage') {
			mdvMapsArr[usage[i].getAttribute('value')].doCoordinates(request.responseXML);
			break;
		}
	}
}

function doTripRetoure() {
	var origin = document.getElementById('itdLPxx_mdvMap_origin');
	var destination = document.getElementById('itdLPxx_mdvMap_destination');

	if (origin && destination) {
		var temp = destination.value;
		destination.value = origin.value;
		origin.value = temp;
	}
}



/**
* MDVEFADepartureMonitor
*/
function MDVEFADepartureMonitor(id, url) {
	this.id 	= id;
	this.url 	= url;
}

MDVEFADepartureMonitor.prototype.getDepartures = function(identifier) {
	var _params = { itdLPxx_id: identifier, name_dm: this.id, type_dm: 'stopID', rlze: '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;
	}
}







