// origin an destination map
var mdvMapOrig = null;
var mdvMapDest = null;
// mini maps
var mdvMapMiniOrig = null;
var mdvMapMiniDest = null;
// update map/minimap?
var bUpdMapOrig = false;
var bUpdMapDest = false;
var bUpdMapMiniOrig = false;
var bUpdMapMiniDest = false;
// coordinates of the mouse click
var mapCoords = null;
// marker for origin and destintion
var originMarker;
var destinationMarker;

// Control
var mdvMapControlOrig;
var mdvMapControlDest;


/**
 * Initialize the map for input of origin.
 */

function onLoadHandler_origin() {
	if (mdvMapOrig == null) {
		// create contet menu items
		var _menuItemsOrig = new Array();
		_menuItemsOrig[0] = new MDVMapMenuItem("Set Origin", setOriginCoordinates);
		// create the menu
		var _mapContextMenuOrig = new MDVMapMenu(_menuItemsOrig);

		// map
		mdvMapOrig = new MDVMap(document.getElementById('mdvMap_origin'));
		var mdvMapContextMenuOrig = new MDVMapContextMenu(mdvMapOrig, _mapContextMenuOrig);
		var mdvMapNavOrig = new MDVMapNavigator(mdvMapOrig);
		var mdvMapEfaInfoOrig = new MDVMapEFAInfo(mdvMapOrig);
		mdvMapControlOrig = new MDVMapControl(mdvMapOrig);

		mdvMapConfigOrig.add('defaultScale', '3');
		mdvMapConfigOrig.add('efaURL', 'XML_MAP_REQUEST');
		mdvMapConfigOrig.add('transparentImg', 'images/transparent.gif');
		mdvMapConfigOrig.add('mapName', 'GDAV');
		mdvMapConfigOrig.add('network', 'VIC');
		mdvMapConfigOrig.add('xCenterReal', '4320084');
		mdvMapConfigOrig.add('yCenterReal', '4187408');
		mdvMapConfigOrig.add('zoomOnDoubleClick', 'true');
		mdvMapConfigOrig.add('cursorMove', 'images/grabbing.cur');
		mdvMapConfigOrig.add('useMagnifyGlass', 'true');
		//mdvMapConfigOrig.add('cursorIdle', 'images/grab.cur');
		mdvMapConfigOrig.add('block', '100');

		mdvMapOrig.events.registerEvent(MDVEvent_CONTEXT_MENU, null, getMapCoordinates);
		//mdvMapOrig.events.registerEvent(MDVEvent_ERROR, null, errorAlert);
		mdvMapOrig.events.registerEvent(MDVEvent_CENTRE_CHANGED, null, setMiniMapCentreOrig);
		mdvMapOrig.events.registerEvent(MDVEvent_ZOOM_CHANGED, null, setMiniMapZoomOrig);
		mdvMapOrig.events.registerEvent(MDVEvent_MAP_INITIALISED, null, updateMiniMapOrig);

		if (mapDisplayCentre) {
			mdvMapOrig.events.registerEvent(MDVEvent_MOUSE_MOVED, null, updateCoordsInStatusBar);
		}

		document.getElementById('mdvMap_origin').style.width = '98%';
		if(mdvMapOrig.getObjWidth(document.getElementById('mdvMap_origin')) != 0)
			document.getElementById('mdvMap_origin').style.height = mdvMapOrig.getObjWidth(document.getElementById('mdvMap_origin')) / 3 * 2 + 'px';
		else
			document.getElementById('mdvMap_origin').style.height = '400px';
		document.getElementById('mdvMap_origin').style.border = '1px solid black';
		mdvMapOrig.execute(mdvMapConfigOrig);

		// mini map
		mdvMapMiniOrig = new MDVMap(document.getElementById('mdvMapMini_origin'));
		var mdvMapMiniNavOrig = new MDVMapNavigator(mdvMapMiniOrig);

		mdvMapConfigMiniOrig.add('defaultScale', '3');
		mdvMapConfigMiniOrig.add('efaURL', 'XML_MAP_REQUEST');
		mdvMapConfigMiniOrig.add('transparentImg', 'images/transparent.gif');
		mdvMapConfigMiniOrig.add('mapName', 'GDAV');
		mdvMapConfigMiniOrig.add('network', 'VIC');
		mdvMapConfigMiniOrig.add('xCenterReal', '4320084');
		mdvMapConfigMiniOrig.add('yCenterReal', '4187408');
		mdvMapConfigMiniOrig.add('zoomOnDoubleClick', 'true');
		mdvMapConfigMiniOrig.add('block', '100');

		mdvMapMiniOrig.events.registerEvent(MDVEvent_CENTRE_CHANGED, null, setMapOrig);
		mdvMapMiniOrig.events.registerEvent(MDVEvent_ZOOM_CHANGED, null, setMapOrig);
		mdvMapMiniOrig.events.registerEvent(MDVEvent_MAP_INITIALISED, null, updateMapOrig);

		document.getElementById('mdvMapMini_origin').style.width = '20%';
		if(mdvMapMiniOrig.getObjWidth(document.getElementById('mdvMapMini_origin')) != 0)
			document.getElementById('mdvMapMini_origin').style.height = mdvMapMiniOrig.getObjWidth(document.getElementById('mdvMapMini_origin')) / 3 * 2 + 'px';
		else
			document.getElementById('mdvMapMini_origin').style.height = '80px';
		document.getElementById('mdvMapMini_origin').style.border = '1px solid black';
		document.getElementById('mdvMapMini_origin').style.zIndex = 260;
		mdvMapMiniOrig.execute(mdvMapConfigMiniOrig);
	}
	else {
		mdvMapOrig.update();
		mdvMapMiniOrig.update();
	}
}



/**
 * Initialize the map for input of destination.
 */

function onLoadHandler_destination() {
	
	if (mdvMapDest == null) {
		// create contet menu items
		var _menuItemsDest = new Array();
		_menuItemsDest[0] = new MDVMapMenuItem("Set Destination", setDestinationCoordinates);
		// create the menu
		var _mapContextMenuDest = new MDVMapMenu(_menuItemsDest);

		// map
		mdvMapDest = new MDVMap(document.getElementById('mdvMap_destination'));
		var mdvMapContextMenuDest = new MDVMapContextMenu(mdvMapDest, _mapContextMenuDest);
		var mdvMapNavDest = new MDVMapNavigator(mdvMapDest);
		var mdvMapEfaInfoDest = new MDVMapEFAInfo(mdvMapDest);
		mdvMapControlDest = new MDVMapControl(mdvMapDest);

		mdvMapConfigDest.add('defaultScale', '3');
		mdvMapConfigDest.add('efaURL', 'XML_MAP_REQUEST');
		mdvMapConfigDest.add('transparentImg', 'images/transparent.gif');
		mdvMapConfigDest.add('mapName', 'GDAV');
		mdvMapConfigDest.add('network', 'VIC');
		mdvMapConfigDest.add('xCenterReal', '4320084');
		mdvMapConfigDest.add('yCenterReal', '4187408');
		mdvMapConfigDest.add('zoomOnDoubleClick', 'true');
		mdvMapConfigDest.add('cursorMove', 'images/grabbing.cur');
		mdvMapConfigDest.add('useMagnifyGlass', 'true');
		//mdvMapConfigDest.add('cursorIdle', 'images/grab.cur');
		mdvMapConfigDest.add('block', '100');

		mdvMapDest.events.registerEvent(MDVEvent_CONTEXT_MENU, null, getMapCoordinates);
		mdvMapDest.events.registerEvent(MDVEvent_CENTRE_CHANGED, null, setMiniMapCentreDest);
		mdvMapDest.events.registerEvent(MDVEvent_ZOOM_CHANGED, null, setMiniMapZoomDest);
		mdvMapDest.events.registerEvent(MDVEvent_MAP_INITIALISED, null, updateMiniMapDest);

		if (mapDisplayCentre) {
			mdvMapDest.events.registerEvent(MDVEvent_MOUSE_MOVED, null, updateCoordsInStatusBar);
		}

		document.getElementById('mdvMap_destination').style.width = '98%';
		if (mdvMapDest.getObjWidth(document.getElementById('mdvMap_destination')) != 0)
			document.getElementById('mdvMap_destination').style.height = mdvMapDest.getObjWidth(document.getElementById('mdvMap_destination')) / 3 * 2 + 'px';
		else
			document.getElementById('mdvMap_destination').style.height='400px';
		document.getElementById('mdvMap_destination').style.border = '1px solid black';
		mdvMapDest.execute(mdvMapConfigDest);

		// mini map
		mdvMapMiniDest = new MDVMap(document.getElementById('mdvMapMini_destination'));
		var mdvMapMiniNavDest = new MDVMapNavigator(mdvMapMiniDest);

		mdvMapConfigMiniDest.add('defaultScale', '3');
		mdvMapConfigMiniDest.add('efaURL', 'XML_MAP_REQUEST');
		mdvMapConfigMiniDest.add('transparentImg', 'images/transparent.gif');
		mdvMapConfigMiniDest.add('mapName', 'GDAV');
		mdvMapConfigMiniDest.add('network', 'VIC');
		mdvMapConfigMiniDest.add('xCenterReal', '4320084');
		mdvMapConfigMiniDest.add('yCenterReal', '4187408');
		mdvMapConfigMiniDest.add('zoomOnDoubleClick', 'true');
		mdvMapConfigMiniDest.add('block', '100');

		mdvMapMiniDest.events.registerEvent(MDVEvent_CENTRE_CHANGED, null, setMapDest);
		mdvMapMiniDest.events.registerEvent(MDVEvent_ZOOM_CHANGED, null, setMapDest);
		mdvMapMiniDest.events.registerEvent(MDVEvent_MAP_INITIALISED, null, updateMapDest);

		document.getElementById('mdvMapMini_destination').style.width = '20%';
		if (mdvMapMiniDest.getObjWidth(document.getElementById('mdvMapMini_destination')) != 0)
			document.getElementById('mdvMapMini_destination').style.height = mdvMapMiniDest.getObjWidth(document.getElementById('mdvMapMini_destination')) / 3 * 2 + 'px';
		else
			document.getElementById('mdvMapMini_destination').style.height = '80px';
		document.getElementById('mdvMapMini_destination').style.border = '1px solid black';
		document.getElementById('mdvMapMini_destination').style.zIndex = 10;
		mdvMapMiniDest.execute(mdvMapConfigMiniDest);
	}
	else {
		mdvMapDest.update();
		mdvMapMiniDest.update();
	}
}


function updateCoordsInStatusBar(id, msg, coords) {
	window.status = coords.toString();
}


/**
 * Show message of error event as an alert (error event).
 * @param id the event id
 * @param msg the error message
 */

function errorAlert(id, msg){
	alert(msg);
}



/**
 * Get the coordinates of the map when the contex menu is opend (context menu event).
 * @param id the event id
 * @param status true when the context menu is open, otherwise false
 * @param mapCoord the map coordinates
 */

function getMapCoordinates(id, status, mapCoord) {
	if(status == true){
		mapCoords = mapCoord;
	}
}



/**
 * Send back the clicked map coordinate as result.
 */

function setOriginCoordinates() {
	if (document.tripRequest){
		enableMapCoordinates('origin');
		var mapName = mdvMapOrig.config.get('mapName');
		// coordinates for enquiry
		document.getElementById('nameInfo_origin').value = mapCoords.x +":" + mapCoords.y + ":" + mapName + ":Map Location (Origin)";
		// mark origin
		if (!originMarker) {
			originMarker = mdvMapOrig.createMarker(mapCoords, 0, 'images/map/origin.gif');
			//originMarker.img.style.z-index = 100;
			var originMarkerTitle = mdvMapOrig.createToolTip('Map Location (Origin)');
      			originMarker.setToolTip(originMarkerTitle);
      			mdvMapOrig.addMarker(originMarker);
		}
		else {
			originMarker.setCoords(mapCoords);
		}
		// conserve zoomlevel and marker position for go back functionality of map navigation
		mdvMapControlOrig.setZoomLevel(mdvMapOrig.config.getZoomLevelIndex());
		mdvMapControlOrig.setOriginCoords(mapCoords);
		deactivateTabs('origin');
		// hide potential former user input (shown on map-tab)
		if(document.getElementById('usrInputMap_origin')) document.getElementById('usrInputMap_origin').style.display="none";
		// delete suburb/town
		if(document.forms['tripRequest'].place_origin) document.forms['tripRequest'].place_origin.value="";
		if(document.forms['tripRequest'].placeState_origin) document.forms['tripRequest'].placeState_origin.value="empty";
		// set marker layer on top of stop layer
		var layer = mdvMapOrig.getLayer("mdvMarkers");
		if(layer) layer.setZIndex(10);
	}
}

function setDestinationCoordinates() {
	if (document.tripRequest){
		enableMapCoordinates('destination');
		var mapName = mdvMapDest.config.get('mapName');
		// coordinates for enquiry
		document.getElementById('nameInfo_destination').value = mapCoords.x +":" + mapCoords.y + ":" + mapName + ":Map Location (Destination)";
		// mark destination
		if (!destinationMarker) {
			destinationMarker = mdvMapDest.createMarker(mapCoords, 0, 'images/map/destination.gif');
			//destinationMarker.img.style.z-index = 100;
			var destinationMarkerTitle = mdvMapDest.createToolTip('Map Location (Destination)');
     			destinationMarker.setToolTip(destinationMarkerTitle);
      			mdvMapDest.addMarker(destinationMarker);
		}
		else {
			destinationMarker.setCoords(mapCoords);
		}
		// conserve zoomlevel and marker position for go back functionality of map navigation
		mdvMapControlDest.setZoomLevel(mdvMapDest.config.getZoomLevelIndex());
		mdvMapControlDest.setOriginCoords(mapCoords);
		deactivateTabs('destination');
		// hide potential former user input (shown on map-tab)
		if(document.getElementById('usrInputMap_destination')) document.getElementById('usrInputMap_destination').style.display="none";
		// delete suburb/town
		if(document.forms['tripRequest'].place_destination) document.forms['tripRequest'].place_destination.value="";
		if(document.forms['tripRequest'].placeState_destination) document.forms['tripRequest'].placeState_destination.value="empty";
		// set marker layer on top of stop layer
		var layer = mdvMapDest.getLayer("mdvMarkers");
		if(layer) layer.setZIndex(10);
	}
}

function setKnownOriginCoordinates(x, y, zoomlevel) {
	if (document.tripRequest && x && y){
		var mapName = mdvMapOrig.config.get('mapName');
		// set centre
		setMapCentreOrig(x, y);
		// set zoomlevel
		mdvMapOrig.setZoomLevel(zoomlevel);
		if(document.getElementById('nameInfo_origin')) {
			// coordinates for enquiry
			document.getElementById('nameInfo_origin').value = x +":" + y + ":" + mapName + ":Map Location (Origin)";
		}
		var mapCoords = new MDVCoordinates(mapName, x, y);
		// mark origin
		if (!originMarker) {
			originMarker = mdvMapOrig.createMarker(mapCoords, 0, 'images/map/origin.gif');
			//originMarker.img.style.z-index = 100;
			var originMarkerTitle = mdvMapOrig.createToolTip('Map Location (Origin)');
      			originMarker.setToolTip(originMarkerTitle);
      			mdvMapOrig.addMarker(originMarker);
		}
		else {
			originMarker.setCoords(mapCoords);
		}
		// conserve zoomlevel and marker position for go back functionality of map navigation
		mdvMapControlOrig.setZoomLevel(mdvMapOrig.config.getZoomLevelIndex());
		mdvMapControlOrig.setOriginCoords(mapCoords);
		// set http parameter for coordinate
		if(document.getElementById('type_origin').value == 'coord'){
			enableMapCoordinates('origin');
			document.getElementById('nameInfo_origin').value = x + ":" + y + ":" + mapName + ":Map Location (Origin)";
		}
		// set marker layer on top of stop layer
		var layer = mdvMapOrig.getLayer("mdvMarkers");
		if(layer) layer.setZIndex(10);
		mdvMapOrig.update();
		mdvMapMiniOrig.update();
	}
}

function setKnownDestinationCoordinates(x, y, zoomlevel) {
	if (document.tripRequest && x && y){
		var mapName = mdvMapDest.config.get('mapName');
		// set centre
		setMapCentreDest(x, y);
		// set zoomlevel
		mdvMapDest.setZoomLevel(zoomlevel);
		if(document.getElementById('nameInfo_destination')) {
			// coordinates for enquiry
			document.getElementById('nameInfo_destination').value = x +":" + y + ":" + mdvMapDest.config.get('mapName') + ":Map Location (Destination)";
		}
		var mapCoords = new MDVCoordinates(mapName, x, y);
		// mark destination
		if (!destinationMarker) {
			destinationMarker = mdvMapDest.createMarker(mapCoords, 0, 'images/map/destination.gif');
			//destinationMarker.img.style.z-index = 100;
			var destinationMarkerTitle = mdvMapDest.createToolTip('Map Location (Destination)');
      			destinationMarker.setToolTip(destinationMarkerTitle);
      			mdvMapDest.addMarker(destinationMarker);
		}
		else {
			destinationMarker.setCoords(mapCoords);
		}
		// conserve zoomlevel and marker position for go back functionality of map navigation
		mdvMapControlDest.setZoomLevel(mdvMapDest.config.getZoomLevelIndex());
		mdvMapControlDest.setOriginCoords(mapCoords);
		// set http parameter for coordinate
		if(document.getElementById('type_destination').value == 'coord'){
			enableMapCoordinates('destination');
			document.getElementById('nameInfo_destination').value = x + ":" + y + ":" + mapName + ":Map Location (Origin)";
		}
		// set marker layer on top of stop layer
		var layer = mdvMapDest.getLayer("mdvMarkers");
		if(layer) layer.setZIndex(10);
		mdvMapDest.update();
		mdvMapMiniDest.update();
	}
}


/**
 * Set the mini map if center or zoom of the original map have changed
 * @param id id of the mini map
 * @param msg
 * @param value
 */

function setMiniMapZoomOrig(id, msg, value) {
	if (mdvMapMiniOrig) {
		var zoomLevel = mdvMapMiniOrig.config.getZoomLevel(mdvMapOrig.config.getZoomLevelIndex());

		if (zoomLevel != null) {
			if (mdvMapMiniOrig.setZoomLevel(mdvMapOrig.config.getZoomLevelIndex()))
				bUpdMapMiniOrig = true;
		}
	}
}

function setMiniMapCentreOrig(id, msg, value) {
	if (mdvMapMiniOrig) {

		if (mdvMapMiniOrig.setCentre(mdvMapOrig.getCentre()))
			bUpdMapMiniOrig = true;
	}
}

function setMiniMapZoomDest(id, msg, value) {
	if (mdvMapMiniDest) {
		var zoomLevel = mdvMapMiniDest.config.getZoomLevel(mdvMapDest.config.getZoomLevelIndex());

		if (zoomLevel != null) {
			if (mdvMapMiniDest.setZoomLevel(mdvMapDest.config.getZoomLevelIndex()))
				bUpdMapMiniDest = true;
		}
	}
}

function setMiniMapCentreDest(id, msg, value) {
	if (mdvMapMiniDest) {

		if (mdvMapMiniDest.setCentre(mdvMapDest.getCentre()))
			bUpdMapMiniDest = true;
	}
}

/**
 * Update the mini map
 * @param id id of the mini map
 * @param msg
 */

function updateMiniMapOrig(id, msg) {
	if (mdvMapMiniOrig && bUpdMapMiniOrig) {
		bUpdMapMiniOrig = false;
		mdvMapMiniOrig.update();
	}
}

function updateMiniMapDest(id, msg) {
	if (mdvMapMiniDest && bUpdMapMiniDest) {
		bUpdMapMiniDest = false;
		mdvMapMiniDest.update();
	}
}


/**
 * Update the map
 * @param id id of the mini map
 * @param msg
 */

function setMapOrig(id, msg, value) {
	if (mdvMapOrig && mdvMapMiniOrig.getCentre()) {
		if (mdvMapOrig.setCentre(mdvMapMiniOrig.getCentre()))
			bUpdMapOrig = true;

		var zoomLevel = mdvMapOrig.config.getZoomLevel(mdvMapMiniOrig.config.getZoomLevelIndex());
		if (zoomLevel != null) {
			if (mdvMapOrig.setZoomLevel(mdvMapMiniOrig.config.getZoomLevelIndex()))
				bUpdMapOrig = true;
		}
	}
}

function setMapDest(id, msg, value) {
	if (mdvMapDest && mdvMapMiniDest.getCentre()) {
		if (mdvMapDest.setCentre(mdvMapMiniDest.getCentre()))
			bUpdMapDest = true;

		var zoomLevel = mdvMapDest.config.getZoomLevel(mdvMapMiniDest.config.getZoomLevelIndex());
		if (zoomLevel != null) {
			if (mdvMapDest.setZoomLevel(mdvMapMiniDest.config.getZoomLevelIndex()))
				bUpdMapDest = true;
		}
	}
}


/**
 * Update the map
 * @param id id of the mini map
 * @param msg
 */

function updateMapOrig(id, msg) {
	if (mdvMapOrig && bUpdMapOrig) {
			bUpdMapOrig = false;
			mdvMapOrig.update();
	}
}

function updateMapDest(id, msg) {
	if (mdvMapDest && bUpdMapDest) {
			bUpdMapDest = false;
			mdvMapDest.update();
	}
}


/**
 * Set Map Centre
 * @param x x-coordinate
 * @param y y-coordinate
 */

 function setMapCentreOrig(x, y) {
 	 var mapName = mdvMapOrig.config.get('mapName');
 	 var mapCoords = new MDVCoordinates(mapName, x, y);
 	 mdvMapOrig.setCentre(mapCoords);
 }

 function setMapCentreDest(x, y) {
  	 var mapName = mdvMapDest.config.get('mapName');
  	 var mapCoords = new MDVCoordinates(mapName, x, y);
  	 mdvMapDest.setCentre(mapCoords);
 }
 

/**
 * Recalculate map height: 3/2 of width
 * @param usage 
 */

 function recalcMapHeight(usage){
 	if(usage=='origin' && mdvMapOrig && mdvMapMiniOrig){
 		document.getElementById('mdvMap_origin').style.height = mdvMapOrig.getObjWidth(document.getElementById('mdvMap_origin')) / 3 * 2 + 'px';
 		document.getElementById('mdvMapMini_origin').style.height = mdvMapMiniOrig.getObjWidth(document.getElementById('mdvMapMini_origin')) / 3 * 2 + 'px';
 	}
 	else if(usage=='destination' && mdvMapDest && mdvMapMiniDest){
 		document.getElementById('mdvMap_destination').style.height = mdvMapDest.getObjWidth(document.getElementById('mdvMap_destination')) / 3 * 2 + 'px';
 		document.getElementById('mdvMapMini_destination').style.height = mdvMapMiniDest.getObjWidth(document.getElementById('mdvMapMini_destination')) / 3 * 2 + 'px';
 	}
 }