var mdvMapsArr = new Array();
var tools  = new Array();
var clickpoi = false;
var set_zoom;
var ctxCoords = null;

function onLoadMapHandler(form) {
	var active = '';
	for (var i=0; i < form.length; i++) {
		if (form[i].name.substring(0, 15) == 'itdLPxx_mdvMap_') {
			var name = form[i].name.substring(15, form[i].name.length);
			var value = form[i].value;

			if (mdvMapsArr[name] == null) {
				mdvMapsArr[name] = new VRNMapHelper(name, value);
			}
		}

		if (form[i].name == 'itdLPxx_mdvMap') {
			active = form[i].value;
		}
		if (form[i].name == 'itdLPxx_zoomLevel') {
			set_zoom = form[i].value;
		}
	}
	if (mdvMapsArr[active])
		mdvMapsArr[active].execute();
}


function VRNMapHelper(usage, value) {
	this.usage = usage;
	this.mdvMap = null;
	this.config = null;
	this.highlight = null;
	this.marker = null;
	this.scale = null;

	this.ctxMenuArr = new Array();

	if(usage =='origin')
		this.ctxMenuArr[0] = new MDVMapMenuItem("Fahrplanauskunft von hier", this.setCoordinatesMenu);
	else if(usage =='destination')
		this.ctxMenuArr[0] = new MDVMapMenuItem("Fahrplanauskunft nach hier", this.setCoordinatesMenu);
	else
		this.ctxMenuArr[0] = new MDVMapMenuItem("Punkt Übernehmen", this.setCoordinatesMenu);

	this.ctxMenu = new MDVMapMenu(this.ctxMenuArr);
}


VRNMapHelper.prototype.execute = function () {
	this.config = mdvMapConfig.clone();

	this.config.add('xCenterReal', '3461100');
	this.config.add('yCenterReal', '676030');
	this.config.add('defaultScale', '3');
	this.config.add('serverURL', 'http://maps.vrn.de/');
	this.config.add('efaURL', '../../lite/XSLT_COORD_REQUEST');
	this.config.add('transparentImg', 'images/map/transparent.gif');
	this.config.add('imagePath', 'images/map/');
	this.config.add('block', '100');
	this.config.add('mapName', 'NBWT');
	this.config.add('zoomOnDoubleClick', 'true');
	this.config.add('useMagnifyGlass', 'true');
	this.config.add('toolTimeOut', '200');
	this.config.add('cursorMove', 'images/grabbing.cur');
	this.config.add('useBubbleForEFAInfo', 'false');
	this.config.add('useBubbleOverflowForEFAInfo', 'true');
	this.config.add('info.stop.size.width', '300');
	this.config.add('info.stop.size.height', '350');
	this.config.add('info.poi.size.width', '130');
	this.config.add('info.poi.size.height', '65');
	this.config.add('poiArea', 'true');
	this.config.add('poiPoint', 'true');

	this.mdvMap = new MDVMap(document.getElementById('vrnMap'));
				  new MDVMapContextMenu(this.mdvMap, this.ctxMenu);
				  new MDVMapControl(this.mdvMap);
				  new MDVMapEFAInfoEx(this.mdvMap, { getMOTImage: getMOTImage, imagePath: 'images/map/' });	
				  new MDVMapNavigator(this.mdvMap);
				  new VRNMapAddOn(this.mdvMap);

	this.scale = new MDVMapScale(this.mdvMap, {});

//	this.mdvMap.events.registerEvent(MDVEvent_MAP_INITIALISED, this, this.checkLayers);
	this.mdvMap.events.registerEvent(MDVEvent_OBJECT_CLICKED, this, this.onObjectClicked);
	this.mdvMap.events.registerEvent(MDVEvent_LAYER_UPDATED, this, this.highlightObj);
	this.mdvMap.events.registerEvent(MDVEvent_TOOLTIP, this, this.onToolTip);
	this.mdvMap.events.registerEvent(MDVEvent_CONTEXT_MENU, this, ContextMenuHandler);
	this.mdvMap.execute(this.config);

	var nameState = this.getNameState();
	var placeState = this.getPlaceState();

	if (nameState == 'list' || nameState == 'identified')
		this.doList(null);
	else if ((nameState == 'empty' || nameState == 'notidentified') && placeState != 'empty')
		this.doPlace(null);

	this.scale.createDivs();
	this.scale.updatePosition(this.mdvMap.viewportHeight);
};

VRNMapHelper.prototype.checkLayers = function(id, msg, obj) {

	var zl = obj.config.getZoomLevel(obj.config.getZoomLevelIndex());
	var showSTOP = zl.get('showSTOP') == 'true';
	var showPOI  = zl.get('showPOI') == 'true';


	var pois = this.mdvMap.getLayer('efa_poi');

	if (pois) {
		pois.setVisibility(false);
	}
};

VRNMapHelper.prototype.getNameState = function() {
	var state = document.getElementById('nameState_' + this.usage);
	if (state)
		return state.value;
	else
		return '';
};

VRNMapHelper.prototype.getPlaceState = function() {
	var state = document.getElementById('placeState_' + this.usage);
	if (state)
		return state.value;
	else
		return '';
};

VRNMapHelper.prototype.doList = function(select) {
	var index = 0;

	if (select)
		index = select;

	// Unterscheidung nach Typ des Listeneintrags

	if (odv[index])
	{
		switch (odv[index].anyType) {
			case 'address':
				this.doAddress(odv[index]);
			break;
			case 'singlehouse': 	
				this.doAddress(odv[index]);
			break;
			case 'street':
				this.doAddress(odv[index]);
			break;
			case 'stop':
				this.doStop(odv[index]);
			break;
			case 'poi':
				this.doPOI(odv[index]);
			break;
			case 'loc':
				this.doPlace(odv[index]);
			break;
			default:
				if (odv[index].x > 0 && odv[index].y > 0) {
					var centre = new MDVCoordinates('NBWT', odv[index].x, odv[index].y);
	
					this.highlight = odv[index];
	
					this.setCoordinates(centre, 7);
				}
			break;
		}
	}
};

VRNMapHelper.prototype.doStop = function(obj) {
	var params = { name_sf: obj.id, type_sf: 'stopID', itdLPxx_usage: this.usage, itdLPxx_zoomLevel: '7' };
	params = $H(params).toQueryString();

	this.highlight = obj;

	var _ajax = new Ajax.Request('XML_STOPFINDER_REQUEST', { method: 'post', parameters: params, onComplete: VRNMapHelper_AjaxCallback });
};

VRNMapHelper.prototype.doPOI = function(obj) {
	var params = { name_sf: obj.id, type_sf: 'poiID', place_sf: 'placeID:'+obj.omc+':'+obj.placeID, itdLPxx_usage: this.usage, itdLPxx_zoomLevel: '7' };
	params = $H(params).toQueryString();

	this.highlight = obj;

	var _ajax = new Ajax.Request('XML_STOPFINDER_REQUEST', { method: 'post', parameters: params, onComplete: VRNMapHelper_AjaxCallback });
};

VRNMapHelper.prototype.doAddress = function(obj) {
	if (!obj)
		return;

	this.highlight = obj;

	var params = { type_sf: 'address', name_sf: 'streetID:' + obj.id + ':' + obj.buildingNumber + ':' + obj.omc, itdLPxx_usage: this.usage, itdLPxx_zoomLevel: '7' };
	params = $H(params).toQueryString();

	var _ajax = new Ajax.Request('XML_STOPFINDER_REQUEST', { method: 'post', parameters: params, onComplete: VRNMapHelper_AjaxCallback });
};

VRNMapHelper.prototype.doPlace = function(select) {	
	var index = 0;

	if (select)
		index = select;

	this.highlight = null;

//	if (odv[index])
	if(index)
	{
//		var params = { omc: odv[index].omc, placeID: odv[index].placeID, itdLPxx_usage: this.usage, createImage: 0, pinTypeStop: 0, pinTypePOI: 0, itdLPxx_zoomLevel: '3' };
		if (set_zoom)
			var params = { omc: index.omc, placeID: index.placeID, itdLPxx_usage: this.usage, createImage: 0, pinTypeStop: 0, pinTypePOI: 0, itdLPxx_zoomLevel: set_zoom };
		else	
			var params = { omc: index.omc, placeID: index.placeID, itdLPxx_usage: this.usage, createImage: 0, pinTypeStop: 0, pinTypePOI: 0, itdLPxx_zoomLevel: '5' };
		params = $H(params).toQueryString();
	
		var _ajax = new Ajax.Request('XML_MAP_REQUEST', { method: 'post', parameters: params, onComplete: VRNMapHelper_AjaxCallback });
	}
};


VRNMapHelper.prototype.setCoordinatesMenu = function() {

	var usage = document.eingabe.itdLPxx_mdvMap.value;
		
	if (usage)
	{
		if(usage  =='origin')
			mdvMapsArr[usage].onObjectClicked(-1,this,  { type: 'coord', name: 'gewählter Start aus Karte', x: parseInt(ctxCoords.x), y: parseInt(ctxCoords.y), mapName: ctxCoords.mapName });
		else if(usage =='destination')
			mdvMapsArr[usage].onObjectClicked(-1,this,  { type: 'coord', name: 'gewähltes Ziel aus Karte', x: parseInt(ctxCoords.x), y: parseInt(ctxCoords.y), mapName: ctxCoords.mapName });
		else
			mdvMapsArr[usage].onObjectClicked(-1,this,  { type: 'coord', name: 'gewählter Punkt aus Karte', x: parseInt(ctxCoords.x), y: parseInt(ctxCoords.y), mapName: ctxCoords.mapName });
	}
};

VRNMapHelper.prototype.setCoordinates = function(centre, zl, plMarker) {
	if (!centre)
		return;

	var vrnx = centre.x;
	var vrny = centre.y;

	var isOK = this.mdvMap.setCentre(centre);
	
	// Wenn isOK == false --> DIV ausblenden + return, da Koordinaten ungültig.oder nicht im VRN
	if (!isOK || !((((vrnx >= 3372180) && (vrnx <= 3586050)) && ((vrny >= 610000) && (vrny <= 750223))) || (((vrnx >= 3293340) && (vrnx <= 3372180)) && ((vrny >= 573073) && (vrny <= 670000)))))
	{
		divstate = document.getElementById('vrnMap');
		divstate.style.display = "none";
		return;
	} else {
		divstate = document.getElementById('vrnMap');
		divstate.style.display = "block";
	}
	
	if (zl >= 0)
		this.mdvMap.setZoomLevel(zl);

//	if (!plMarker)
	this.setMarker();
	this.mdvMap.update();
};

VRNMapHelper.prototype.highlightObj = function (id, msg, obj) {
	if (!this.highlight)
		return false;
	if (this.highlight.anyType == 'stop' && obj.name == 'efa_stops')
		layer = obj;
	else if (this.highlight.anyType == 'poi' && obj.name == 'efa_poi')
		layer = obj;
	else if (clickpoi)
		layer = obj;
	else
		return;

	var markers = layer.getMarkers();

	for (var m = 0; m < markers.length; m++) {
		var marker = markers[m];
		var object = marker.getObjectId();
		var obj_id = object.id;

		if (object.type == 'POI' || object.type == 'POI_POINT' || object.type == 'POI_AREA')
		{
			obj_id = object.id.substring(4,20);
		}

		if (obj_id == this.highlight.id) {
			var img = object.marker.getImage();
			var srcPath = this.usage == 'origin' ? 'images/map/origin.png' : 'images/map/destination.png';

			if (this.marker)
				return;

			this.marker = this.mdvMap.createMarker(this.mdvMap.getCentre().clone(), new MDVPoint(0.15, 1.15), srcPath);
			this.mdvMap.addMarker(this.marker);

			//if (img)
				//if (this.usage == 'origin')
				//{
				//	img.style.border = '2px dashed #ff0000';
				//} else {
				//	img.style.border = '2px dashed #009900';
				//}
		}
	}
};

VRNMapHelper.prototype.setMarker = function () {
	var layer = this.mdvMap.getLayer('mdvMarkers');
	layer.zIndex = 100;

	if (this.marker)
		this.mdvMap.removeMarker(this.marker);

//	if (this.highlight.anyType == 'stop' || this.highlight.anyType == 'poi')
	if (this.highlight)
		if (this.highlight.anyType == 'stop' || this.highlight.anyType == 'poi')
			return;

	var srcPath = this.usage == 'origin' ? 'images/map/origin.png' : 'images/map/destination.png';

	this.marker = this.mdvMap.createMarker(this.mdvMap.getCentre().clone(), new MDVPoint(0, 1), srcPath);
	this.mdvMap.addMarker(this.marker);
};

function VRNMapHelper_AjaxCallback(request) {
	var usage = request.responseXML.getElementsByTagName('itdLayoutParam');
	var u = '';
	for (var i=0; i < usage.length; i++) {
		if (usage[i].getAttribute('name') == 'usage')
			u = usage[i].getAttribute('value');
	}
	var targetZl = -1;
	var centre = null;

	var zoomlevel = request.responseXML.getElementsByTagName('itdLayoutParam');

	for (var i=0; i < zoomlevel.length; i++) {
		if (zoomlevel[i].getAttribute('name') == 'zoomLevel') {
			targetZl = zoomlevel[i].getAttribute('value');
			break;
		}
	}

	var place = request.responseXML.getElementsByTagName('itdMapRequest');
	var placemarker = null;
	
	// itdMapRequest

	if (place && place.length > 0) {
		place = request.responseXML.getElementsByTagName('odvNameElem');

		if (!place || place.length != 1){
			centre = new MDVCoordinates('NBWT', 0, 0);
			placemarker = true;
		} else {
			centre = new MDVCoordinates('NBWT', place[0].getAttribute('x'), place[0].getAttribute('y'));
			placemarker = true;
		}
	} else {
		/* itdStopFinderRequest */
		var name = request.responseXML.getElementsByTagName('odvNameElem');

		if (name && name.length > 0){
			centre = new MDVCoordinates('NBWT', name[0].getAttribute('x'), name[0].getAttribute('y'));
			placemarker = false;
		} else {
			centre = new MDVCoordinates('NBWT', 0, 0);
			placemarker = false;
		}
	}
	
	if (mdvMapsArr[u])
		mdvMapsArr[u].setCoordinates(centre, targetZl, placemarker);
};

VRNMapHelper.prototype.onObjectClicked = function(id, msg, obj) {
	var type_obj = obj.type;

	switch (this.usage){
		case 'origin':
			var name = document.eingabe.nameInfo_origin;
			var place = document.eingabe.placeInfo_origin;
			var placeState = document.eingabe.placeState_origin;
			var type = document.eingabe.typeInfo_origin;
			break;
		case 'destination':
			var name = document.eingabe.nameInfo_destination;
			var place = document.eingabe.placeInfo_destination;
			var placeState = document.eingabe.placeState_destination;
			var type = document.eingabe.typeInfo_destination;
			break;
		case 'via':
			var name = document.eingabe.nameInfo_via;
			var place = document.eingabe.placeInfo_via;
			var placeState = document.eingabe.placeState_via;
			var type = document.eingabe.typeInfo_via;
			break;
		case 'stt':
			var name = document.eingabe.nameInfo_stt;
			var place = document.eingabe.placeInfo_stt;
			var placeState = document.eingabe.placeState_stt;
			var type = document.eingabe.typeInfo_stt;
			break;
		case 'ttb':
			var name = document.eingabe.nameInfo_ttb;
			var place = document.eingabe.placeInfo_ttb;
			var placeState = document.eingabe.placeState_ttb;
			var type = document.eingabe.typeInfo_ttb;
			break;
		case 'rop':
			var name = document.eingabe.nameInfo_rop;
			var place = document.eingabe.placeInfo_rop;
			var placeState = document.eingabe.placeState_rop;
			var type = document.eingabe.typeInfo_rop;
			break;
		case 'dm':
			var name = document.eingabe.nameInfo_dm;
			var place = document.eingabe.placeInfo_dm;
			var placeState = document.eingabe.placeState_dm;
			var type = document.eingabe.typeInfo_dm;
			break;
		case 'ss':
			var name = document.eingabe.nameInfo_ss;
			var place = document.eingabe.placeInfo_ss;
			var placeState = document.eingabe.placeState_ss;
			var type = document.eingabe.typeInfo_ss;
			break;
	}
	var execInst = document.eingabe.execInst;
	var mapusage = document.eingabe.itdLPxx_mapusage.value;
		
	if(this.usage == '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 (type_obj) {
		case 'STOP':
			name.value = obj.id;
			type.value = 'stopID';
			placeState.value = 'empty';
			place.value = '';
			if (this.marker)
				this.mdvMap.removeMarker(this.marker);
			if(this.usage != 'dm')
				execInst.value = 'verifyOnly';
			this.mdvMap.mapper.style.cursor = 'wait';
			if (mapusage == 'ps_Trip')
			check_value_2();
			else
			check_value();
			document.forms[0].submit();
			break;
		case 'POI':
			place.value = obj.omc + ':-1';
			name.value = obj.id.substring(4,20);;
			if (this.marker)
				this.mdvMap.removeMarker(this.marker);
			if(this.usage != 'dm')
				execInst.value = 'verifyOnly';
			this.mdvMap.mapper.style.cursor = 'wait';
			if (mapusage == 'ps_Trip')
			check_value_2();
			else
			check_value();
			document.forms[0].submit();
			break;
		case 'POI_POINT':
			place.value = obj.omc + ':-1';
			name.value = obj.id.substring(4,20);;
			type.value = 'poiID';
			if (this.marker)
				this.mdvMap.removeMarker(this.marker);
			if(this.usage != 'dm')
				execInst.value = 'verifyOnly';
			this.mdvMap.mapper.style.cursor = 'wait';
			if (mapusage == 'ps_Trip')
			check_value_2();
			else
			check_value();
			document.forms[0].submit();
			break;
		case 'POI_AREA':
			place.value = obj.omc + ':-1';
			name.value = obj.id.substring(4,20);;
			type.value = 'poiID';
			if (this.marker)
				this.mdvMap.removeMarker(this.marker);
			if(this.usage != 'dm')
				execInst.value = 'verifyOnly';
			this.mdvMap.mapper.style.cursor = 'wait';
			if (mapusage == 'ps_Trip')
			check_value_2();
			else
			check_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.usage != 'dm')
				execInst.value = 'verifyOnly';
			this.mdvMap.mapper.style.cursor = 'wait';
			//this.input.value = '';
			check_value();
			document.forms[0].submit();
			break;
	}
};

VRNMapHelper.prototype.onToolTip = function(id, msg, obj) {
	var text = obj.getInnerHTML();
	var div = document.createElement('div');
	var stopid = obj.getParent().objectId.id;
	var type = obj.getParent().objectId.type;
	var obj_attr = obj.getParent().objectId.attrs;
	var obj_length = obj.getParent().objectId.attrs.length;
	var obj_text;
	
	if (obj && obj.getParent().objectId && type=='STOP') {
		innerHTML = '<div class="header">... bitte warten ...</div>';
		obj.setInnerHTML(innerHTML);
		obj.status = true;	
		if (stopid.length == 7)
		 stopid = '0' + stopid;
		var _params = { name_dm: stopid, type_dm: 'stopID', mode: 'direct', itdLPxx_id: stopid, limit: '5' };
			_params = $H(_params);
			_params = _params.toQueryString();
		var ajax = new Ajax.Request('../dm/XSLT_DM_REQUEST', { method: 'post', parameters: _params, onComplete: MDVEFADepartureMonitor_onAjaxComplete });		
		obj.update();
		tools[obj.getParent().objectId.id] = obj;
	} else if ((obj && type=='POI_POINT') || (obj && type=='POI') || (obj && type=='POI_AREA')) {
		for (var i=0; i < obj_length; i++) 
		{
			if (obj_attr[i].name == 'POI_ADDRESS')
			{
				obj_text = obj_attr[i].value.replace(/#/g, "<br>");
				obj_text = '<b>Verkaufsstelle:</b><br>' + obj_text;
			} else if (obj_attr[i].name == 'POI_PHONE'){
				obj_text = obj_text + '<br>' + obj_attr[i].value + ' &nbsp;';
			} else {
					obj_text = text.replace(/Landmark:/g, "");;
			}

		}
		obj.setInnerHTML(obj_text);
		obj.status = true;	
	}
};

function MDVEFADepartureMonitor_onAjaxComplete(request) {
	if (request) {
		MDVEFADepartureMonitor_populateTable(request);
	}
}

function MDVEFADepartureMonitor_populateTable(request) {
	if (request.responseText.length > 0) {
		var id = request.responseText.substr(request.responseText.indexOf('id="')+4, 8);
		if (id.substring(0,1) == 0)
			id = id.substring(1,8);
		if (id.substring(7,8) == '"')
			id = id.substring(0,7);
		if (tools && tools[id]) {
			var obj = tools[id];
			innerHTML = request.responseText;
			obj.setInnerHTML(innerHTML);
			obj.update();
		}
	}
}

function VRNMapAddOn(mdvMap) {
 	if (!mdvMap)
 		return;

 	this.name = 'MapControl';
 	this.mdvMap = mdvMap;

	this.origin = null;
 	this.zoomLevel = null;

 	this.images = new Array();
 	this.images['top_normal'] = new Image();
 	this.images['top_hover'] = new Image();
 	this.images['left_normal'] = new Image();
 	this.images['left_hover'] = new Image();
 	this.images['centre_normal'] = new Image();
 	this.images['centre_hover'] = new Image();
 	this.images['right_normal'] = new Image();
 	this.images['right_hover'] = new Image();
 	this.images['bottom_normal'] = new Image();
 	this.images['bottom_hover'] = new Image();
 	this.images['zoomIn_normal'] = new Image();
 	this.images['zoomIn_hover'] = new Image();
 	this.images['zoomOut_normal'] = new Image();
 	this.images['zoomOut_hover'] = new Image();
 	this.images['zoomLevel_normal'] = new Image();
 	this.images['zoomLevel_hover'] = new Image();
 	this.images['zoomLevel_active'] = new Image();
 	this.images['skala'] = new Image();
 	this.images['transparent'] = new Image();
 	this.images['spacer'] = new Image();

 	this.mapControl = null;

 	this.arrowContainer = null;
 	this.zoomContainer = null;

 	this.mdvMap.registerGadget(this);

    for (var method in MDVGadgetBase.prototype) {

        if (!VRNMapAddOn.prototype[method])
            VRNMapAddOn.prototype[method] = MDVGadgetBase.prototype[method];

    }

    this.mdvMap.events.registerEvent(MDVEvent_INITIALISED, this, this.execute);
    this.mdvMap.events.registerEvent(MDVEvent_ZOOM_CHANGED, this, this.update);
  }

  VRNMapAddOn.prototype.preloadImgs = function () {

  	var imgPath = 'images/MDVMapControl/';

 	this.images['top_normal'].src 			= imgPath + 'topNormal.gif';
 	this.images['top_normal'].alt			= "Norden";
 	this.images['top_normal'].title			= this.images['top_normal'].alt;

 	this.images['top_hover'].src 			= imgPath + 'topHover.gif';
 	this.images['top_hover'].alt 			= this.images['top_normal'].alt;
 	this.images['top_hover'].title 			= this.images['top_normal'].alt;

 	this.images['left_normal'].src 			= imgPath + 'leftNormal.gif';
 	this.images['left_normal'].alt			= "Westen";
  	this.images['left_normal'].title		= this.images['left_normal'].alt;

 	this.images['left_hover'].src 			= imgPath + 'leftHover.gif';
 	this.images['left_hover'].alt			= this.images['left_normal'].alt;
 	this.images['left_hover'].title			= this.images['left_normal'].alt;

 	this.images['centre_normal'].src 		= imgPath + 'center.gif';
 	this.images['centre_normal'].alt		= "center";
 	this.images['centre_normal'].title		= this.images['centre_normal'].alt;

 	this.images['centre_hover'].src 		= imgPath + 'centreHover.gif';
 	this.images['centre_hover'].alt			= this.images['centre_normal'].alt;
 	this.images['centre_hover'].title		= this.images['centre_normal'].alt;

 	this.images['right_normal'].src 		= imgPath + 'rightNormal.gif';
 	this.images['right_normal'].alt			= "Osten";
 	this.images['right_normal'].title		= this.images['right_normal'].alt;

 	this.images['right_hover'].src 			= imgPath + 'rightHover.gif';
	this.images['right_hover'].alt			= this.images['right_normal'].alt;
	this.images['right_hover'].title		= this.images['right_normal'].alt;

 	this.images['bottom_normal'].src 		= imgPath + 'bottomNormal.gif';
 	this.images['bottom_normal'].alt 		= "Sueden";
 	this.images['bottom_normal'].title		= this.images['bottom_normal'].alt;

 	this.images['bottom_hover'].src 		= imgPath + 'bottomHover.gif';
 	this.images['bottom_hover'].alt			= this.images['bottom_normal'].alt;
 	this.images['bottom_hover'].title		= this.images['bottom_normal'].alt;

 	this.images['zoomIn_normal'].src 		= imgPath + 'zoominnormal.gif';
	this.images['zoomIn_normal'].alt		= "einzoomen";
	this.images['zoomIn_normal'].title		= this.images['zoomIn_normal'].alt;

 	this.images['zoomIn_hover'].src 		= imgPath + 'zoominhover.gif';
 	this.images['zoomIn_hover'].alt			= this.images['zoomIn_normal'].alt;
 	this.images['zoomIn_hover'].title		= this.images['zoomIn_normal'].alt;

 	this.images['zoomOut_normal'].src 		= imgPath + 'zoomOutNormal.gif';
  	this.images['zoomOut_normal'].alt		= "auszoomen";
  	this.images['zoomOut_normal'].title		= this.images['zoomOut_normal'].alt;

 	this.images['zoomOut_hover'].src 		= imgPath + 'zoomOutHover.gif';
  	this.images['zoomOut_hover'].alt		= this.images['zoomOut_normal'].alt;
  	this.images['zoomOut_hover'].title		= this.images['zoomOut_normal'].alt;

 	this.images['zoomLevel_normal'].src 	= imgPath + 'zoomlevelnormal.gif';

 	this.images['zoomLevel_active'].src 	= imgPath + 'zoomlevelactive.gif';

 	this.images['zoomLevel_hover'].src 		= imgPath + 'zoomlevelhover.gif';

  	this.images['skala'].src 			= imgPath + 'skala.gif';

  	this.images['transparent'].src 			= imgPath + 'transparent.gif';

  	this.images['spacer'].src 				= imgPath + 'spacer.gif';
  };

  VRNMapAddOn.prototype.createDiv = function () {
  	var newDiv = document.createElement('div');
  	return newDiv;
  };

  VRNMapAddOn.prototype.execute = function () {
  	this.preloadImgs();

	this.origin = new MDVCoordinates(this.mdvMap.config.get('mapName'),
  	parseInt(this.mdvMap.config.get('xCenterReal')),
  	parseInt(this.mdvMap.config.get('yCenterReal')));

  	this.zoomLevel = this.mdvMap.config.get('defaultScale');

  	this.mapControl = this.createDiv();
  	this.mapControl.className = 'MDVMapControl';
  	this.mdvMap.viewport.appendChild(this.mapControl);

  	this.arrowContainer = this.createDiv();
  	this.arrowContainer.className = 'MDVMapControl2';
  	this.arrowContainer.id = 'test2';
  	this.mdvMap.viewport.appendChild(this.arrowContainer);

  	this.zoomContainer = this.createDiv();
	this.zoomContainer.className = 'MDVMapControl_Arrow3';
		
  	this.zoomLevels = new Array();

//  	this.mapControl.appendChild(this.arrowContainer);
  	this.arrowContainer.appendChild(this.zoomContainer);

  	this.populateContainers();
  	this.update();

	var panel = document.getElementById('test2');
	Drag.init(panel);
	panel.style.top = "20px";
	panel.style.left = "650px";
  };

  VRNMapAddOn.prototype.setZoomLevel = function(zoomLevel) {
  	if (this.mdvMap.config.getZoomLevel(zoomLevel)) {
	  	this.zoomLevel = zoomLevel;
	  	return true;
  	}
  	return false;
  };

  VRNMapAddOn.prototype.setOriginCoords = function(coord) {
  	if (coord && coord.x && coord.y && coord.mapName) {
  		this.origin = coord;
  		return true;
  	}
  	return false;
  };

  VRNMapAddOn.prototype.populateContainers = function () {
  	if (!this.arrowContainer || !this.zoomContainer)
  		return false;

  	var firstRow 	= this.createDiv();
  	var secondRow 	= this.createDiv();
  	var thirdRow	= this.createDiv();
  	var zoomInRow	= this.createDiv();
  	var skalaRow	= this.createDiv();
  	var zoomOutRow	= this.createDiv();

  	this.arrowContainer.appendChild(firstRow);
  	this.arrowContainer.appendChild(secondRow);
  	this.arrowContainer.appendChild(thirdRow);
  	this.arrowContainer.appendChild(zoomInRow);
  	this.arrowContainer.appendChild(skalaRow);
	this.arrowContainer.appendChild(zoomOutRow);

	var arrow_2_1 = this.createDiv();
		arrow_2_1.className = 'MDVMapControl_Arrow';
		arrowImg_2_1 = this.createImage();
		arrowImg_2_1.hover = this.images['top_hover'];
		arrowImg_2_1.normal = this.images['top_normal'];
		arrowImg_2_1.alt = this.images['top_normal'].alt;
		arrowImg_2_1.title = this.images['top_normal'].title;
		arrowImg_2_1.onclick = this.goNorth;
		arrowImg_2_1.src = this.images['top_normal'].src;
		arrowImg_2_1.style.position = "absolute";
		arrowImg_2_1.style.top = "0px";
		arrowImg_2_1.style.left = "27px";
		arrow_2_1.appendChild(arrowImg_2_1);
	firstRow.appendChild(arrow_2_1);

	var arrow_1_2 = this.createDiv();
		arrow_1_2.className = 'MDVMapControl_Arrow';
		arrowImg_1_2 = this.createImage();
		arrowImg_1_2.hover = this.images['left_hover'];
		arrowImg_1_2.normal = this.images['left_normal'];
		arrowImg_1_2.alt = this.images['left_normal'].alt;
		arrowImg_1_2.title = this.images['left_normal'].title;
		arrowImg_1_2.onclick = this.goEast;
		arrowImg_1_2.src = this.images['left_normal'].src;
		arrowImg_1_2.style.position = "absolute";
		arrowImg_1_2.style.top = "23px";
		arrowImg_1_2.style.left = "4px";
		arrow_1_2.appendChild(arrowImg_1_2);
 	secondRow.appendChild(arrow_1_2);

	var arrow_2_2 = this.createDiv();
		arrow_2_2.className = 'MDVMapControl_Arrow';
		arrowImg_2_2 = this.createImage();
		arrowImg_2_2.hover = this.images['centre_hover'];
		arrowImg_2_2.normal = this.images['centre_normal'];
		arrowImg_2_2.alt = this.images['centre_normal'].alt;
		arrowImg_2_2.title = this.images['centre_normal'].title;
		arrowImg_2_2.onclick = this.zoomIn;
		arrowImg_2_2.src = this.images['centre_normal'].src;
		arrowImg_2_2.style.position = "absolute";
		arrowImg_2_2.style.top = "23px";
		arrowImg_2_2.style.left = "27px";
		arrow_2_2.appendChild(arrowImg_2_2);
 	secondRow.appendChild(arrow_2_2);

	var arrow_3_2 = this.createDiv();
		arrow_3_2.className = 'MDVMapControl_Arrow';
		arrowImg_3_2 = this.createImage();
		arrowImg_3_2.hover = this.images['right_hover'];
		arrowImg_3_2.normal = this.images['right_normal'];
		arrowImg_3_2.alt = this.images['right_normal'].alt;
		arrowImg_3_2.title = this.images['right_normal'].title;
		arrowImg_3_2.onclick = this.goWest;
		arrowImg_3_2.src = this.images['right_normal'].src;
		arrowImg_3_2.style.position = "absolute";
		arrowImg_3_2.style.top = "23px";
		arrowImg_3_2.style.left = "50px";
		arrow_3_2.appendChild(arrowImg_3_2);
 	secondRow.appendChild(arrow_3_2);

 	var arrow_2_3 = this.createDiv();
		arrow_2_3.className = 'MDVMapControl_Arrow';
  	arrowImg_2_3 = this.createImage();
  	arrowImg_2_3.hover = this.images['bottom_hover'];
  	arrowImg_2_3.normal = this.images['bottom_normal'];
  	arrowImg_2_3.alt = this.images['bottom_normal'].alt;
  	arrowImg_2_3.title = this.images['bottom_normal'].title;
  	arrowImg_2_3.onclick = this.goSouth;
  	arrowImg_2_3.src = this.images['bottom_normal'].src;
		arrowImg_2_3.style.position = "absolute";
		arrowImg_2_3.style.top = "46px";
		arrowImg_2_3.style.left = "27px";
  	arrow_2_3.appendChild(arrowImg_2_3);
  thirdRow.appendChild(arrow_2_3);

 	var zoomIn = this.createDiv();
		zoomIn.className = 'MDVMapControl_Arrow';
 		zoomInImg = this.createImage();
 		zoomInImg.hover = this.images['zoomIn_hover'];
 		zoomInImg.normal = this.images['zoomIn_normal'];
 		zoomInImg.alt = this.images['zoomIn_normal'].alt;
 		zoomInImg.title = this.images['zoomIn_normal'].title;
 		zoomInImg.src = this.images['zoomIn_normal'].src;
		zoomInImg.style.position = "absolute";
		zoomInImg.style.top = "79px";
		zoomInImg.style.left = "27px";
 		zoomInImg.onclick = this.zoomIn;
 		zoomIn.appendChild(zoomInImg);
	zoomInRow.appendChild(zoomIn); 

 	var skala = this.createDiv();
		skala.className = 'MDVMapControl_Arrow3';
 		skalaImg = this.createImage();
 		skalaImg.normal = this.images['skala'];
 		skalaImg.src = this.images['skala'].src;
		skalaImg.style.position = "absolute";
		skalaImg.style.top = "105px";
		skalaImg.style.left = "27px";
 		skalaImg.onclick = this.zoomIn;
 		skala.appendChild(skalaImg);
		this.zoomContainer.appendChild(skala);

  	var zoomLevels = 0;
  	var zoomLevelArr = this.mdvMap.config.getZoomLevels();
  	if (zoomLevelArr)
  		zoomLevels = zoomLevelArr.length;
	
	var zl= 10;

  	for (var z = 0; z < zoomLevels; z++) {
		zl --;
  		var level = zoomLevelArr[zoomLevels - z - 1];
  		var topimg = 108;
			if (level) {
	  		var tmpZL = this.createDiv();
//	  			tmpZL.className = 'MDVMapControl_Arrow4';
	  			tmpZLImg = this.createImage();
	  			tmpZLImg.level = level.level;
	  			tmpZLImg.hover = this.images['zoomLevel_hover'];
	  			tmpZLImg.normal = this.images['zoomLevel_normal'];
	  			tmpZLImg.src = this.images['zoomLevel_normal'].src;
	  			tmpZLImg.style.position = "absolute";
	  			if (z == 0)
	  			{
						tmpZLImg.style.top = topimg + "px";
				  	var trans_zoom = this.createDiv();
				  			trans_zoom.className = 'MDVMapControl_Arrow';
					  		trans_zoomImg = this.createImage();
					 			trans_zoomImg.normal = this.images['transparent'];
					  		trans_zoomImg.src = this.images['transparent'].src;
					  		trans_zoomImg.style.height = "108px";
					  		trans_zoom.appendChild(trans_zoomImg);
			  		this.zoomContainer.appendChild(trans_zoom);
	  			}
					tmpZLImg.style.left = "27px";
				//tmpZLImg.alt = "1:" + level.getScale();
				tmpZLImg.alt = "Zoomlevel " + zl;
	  			tmpZLImg.title = tmpZLImg.alt;
	  			tmpZLImg.onclick = MDVMapControl_onclick;
	  			tmpZL.appendChild(tmpZLImg);
	  			this.zoomContainer.appendChild(tmpZL);
	  			this.zoomLevels.push(tmpZLImg);
			}
	  	var trans_zoom = this.createDiv();
	  			trans_zoom.className = 'MDVMapControl_Arrow';
		  		trans_zoomImg = this.createImage();
		 			trans_zoomImg.normal = this.images['transparent'];
		  		trans_zoomImg.src = this.images['transparent'].src;
		  		trans_zoomImg.style.height = "19px";
		  		trans_zoom.appendChild(trans_zoomImg); 
  		this.zoomContainer.appendChild(trans_zoom);
   	}

  	var zoomOut = this.createDiv();
//			zoomOut.className = 'MDVMapControl_Arrow';
  		zoomOutImg = this.createImage();
  		zoomOutImg.hover = this.images['zoomOut_hover'];
  		zoomOutImg.normal = this.images['zoomOut_normal'];
  		zoomOutImg.alt = this.images['zoomOut_normal'].alt;
  		zoomOutImg.title = this.images['zoomOut_normal'].title;
  		zoomOutImg.src = this.images['zoomOut_normal'].src;
			zoomOutImg.style.position = "absolute";
			zoomOutImg.style.top = "282px";
			zoomOutImg.style.left = "27px";
  		zoomOutImg.onclick = this.zoomOut;
  		zoomOut.appendChild(zoomOutImg);
		zoomOutRow.appendChild(zoomOut);

  	return true;
  };

  VRNMapAddOn.prototype.createImage = function() {
  	var newImage = new Image();
  		newImage.mdvMap = this.mdvMap;
  		newImage.hover 	= null;
  		newImage.normal = null;
  		newImage.onmouseover = MDVMapControl_onmouseover;
  		newImage.onmouseout  = MDVMapControl_onmouseout;
  		newImage.onclick 	 = MDVMapControl_onclick;
  		newImage.ondragstart = new Function([], 'var e=e?e:event;e.cancelBubble=true;e.returnValue=false;return false;');
  	return newImage;
  };

  VRNMapAddOn.prototype.update = function() {
  	if (!this.zoomLevels)
  		return false;

  	var currentZoomLevel = this.mdvMap.config.getZoomLevelIndex();

  	var len = this.zoomLevels.length;

  	for (var i=0; i < len; i++) {
  		if (this.zoomLevels[i].level == currentZoomLevel) {
  			this.zoomLevels[i].src = this.images['zoomLevel_active'].src;
  			this.zoomLevels[i].hover = this.images['zoomLevel_active'];
  			this.zoomLevels[i].normal = this.images['zoomLevel_active'];
  		} else {
  			this.zoomLevels[i].src = this.images['zoomLevel_normal'].src;
  			this.zoomLevels[i].hover = this.images['zoomLevel_hover'];
  			this.zoomLevels[i].normal = this.images['zoomLevel_normal'];
  		}
  	}

  	return true;
  };

  VRNMapAddOn.prototype.zoomIn = function() {
	var zoomLevel = this.mdvMap.config.getZoomLevel(parseInt(this.mdvMap.config.getZoomLevelIndex())+1);
	if (zoomLevel != null) {
  		this.mdvMap.setZoomLevel(parseInt(this.mdvMap.config.getZoomLevelIndex())+1);
  		this.mdvMap.update();
	}
  	return true;
  };

  VRNMapAddOn.prototype.zoomOut = function() {
	var zoomLevel = this.mdvMap.config.getZoomLevel(parseInt(this.mdvMap.config.getZoomLevelIndex())-1);
	if (zoomLevel != null) {
  		this.mdvMap.setZoomLevel(parseInt(this.mdvMap.config.getZoomLevelIndex())-1);
  		this.mdvMap.update();
	}
  	return true;
  };

  VRNMapAddOn.prototype.goEast = function() {
  	var realWidth = this.mdvMap.viewportRealWidth * 0.6;

  	var newCoords = new MDVCoordinates(this.mdvMap.getCentre().mapName,
  		this.mdvMap.getCentre().x,
  		this.mdvMap.getCentre().y);

  	newCoords.x = newCoords.x - Math.floor(realWidth + 0.5);

  	this.mdvMap.setCentre(newCoords);
  	this.mdvMap.update();

  	return true;
  };

  VRNMapAddOn.prototype.goWest = function() {
  	var realWidth = this.mdvMap.viewportRealWidth * 0.6;

  	var newCoords = new MDVCoordinates(this.mdvMap.getCentre().mapName,
  		this.mdvMap.getCentre().x,
  		this.mdvMap.getCentre().y);

  	newCoords.x = newCoords.x + Math.floor(realWidth + 0.5);

  	this.mdvMap.setCentre(newCoords);
  	this.mdvMap.update();

  	return true;
  };

  VRNMapAddOn.prototype.goNorth = function() {
  	var realHeight = this.mdvMap.viewportRealHeight * 0.6;

  	var newCoords = new MDVCoordinates(this.mdvMap.getCentre().mapName,
  		this.mdvMap.getCentre().x,
  		this.mdvMap.getCentre().y);

  	newCoords.y = newCoords.y - Math.floor(realHeight + 0.5);

  	this.mdvMap.setCentre(newCoords);
  	this.mdvMap.update();

  	return true;
  };

  VRNMapAddOn.prototype.goSouth = function() {
  	var realHeight = this.mdvMap.viewportRealHeight * 0.6;

  	var newCoords = new MDVCoordinates(this.mdvMap.getCentre().mapName,
  		this.mdvMap.getCentre().x,
  		this.mdvMap.getCentre().y);

  	newCoords.y = newCoords.y + Math.floor(realHeight + 0.5);

  	this.mdvMap.setCentre(newCoords);
  	this.mdvMap.update();

  	return true;
  };

  VRNMapAddOn.prototype.goBack = function() {
	this.mdvMap.setZoomLevel(this.mapControl.zoomLevel);
	this.mdvMap.setCentre(this.mapControl.origin);
	this.mdvMap.update();
  };

  function MDVMapControl_onmouseover(e) {
	if (this.hover && this.hover.src) {
	  	this.src = this.hover.src;
	  	this.style.cursor = 'pointer';
	}
  	return true;
  }

  function MDVMapControl_onmouseout(e) {
  	if (this.normal && this.normal.src) {
  		this.src = this.normal.src;
	  	this.style.cursor = 'auto';
  	}
  	return true;
  }

  function MDVMapControl_onclick(e) {
  	if (this.level) {
		var zoomLevel = this.mdvMap.config.getZoomLevel(this.level);
		if (zoomLevel != null) {
	  		this.mdvMap.setZoomLevel(this.level);
	  		this.mdvMap.update();
		}
  	}
  	return true;
  }


function wmsOverlay(x, y) {
	var zoomLevel = this.mdvMap.config.getZoomLevelIndex();
	var src  	  = this.getSrcPath();
	
	// map config
	var mapWidth = parseInt(this.mdvMap.config.zoomLevels[zoomLevel].params.realWidth);
	var mapHeight = parseInt(this.mdvMap.config.zoomLevels[zoomLevel].params.realHeight);
	var realOffsetX = parseInt(this.mdvMap.config.zoomLevels[zoomLevel].params.realOffsetX);
	var realOffsetY = parseInt(this.mdvMap.config.zoomLevels[zoomLevel].params.realOffsetY);
	
	// bounding box of  map
	var mapX1 = realOffsetX;
	var mapX2 = realOffsetX + mapWidth;
	var mapY1 = realOffsetY;
	var mapY2 = realOffsetY + mapHeight;
	
	// map tile number
	var mapTileNoX = this.mdvMap.startTileWidth + x;
	var mapTileNoY = this.mdvMap.startTileHeight + y;
	
	// tile size (in coordinates) 
	var tileWidthCoord = mapWidth / parseInt(this.mdvMap.config.zoomLevels[zoomLevel].params.numberOfTilesX);
	var tileHeightCoord = mapHeight / parseInt(this.mdvMap.config.zoomLevels[zoomLevel].params.numberOfTilesY);
	
	// bounding box of current wms tile
	var wmsTileX1 = mapX1 + mapTileNoX * tileWidthCoord;
	var wmsTileX2 = mapX1 + (mapTileNoX + 1) * tileWidthCoord;
	var wmsTileY1 = mapY1 + mapTileNoY * tileHeightCoord;	
	var wmsTileY2 = mapY1 + (mapTileNoY + 1) * tileHeightCoord;
	
	// transform coordinates from mdv coord system to wms coord system and compute bounding box
	var temp = offset - wmsTileY2;
	wmsTileY2 = offset - wmsTileY1;
	wmsTileY1 = temp;
	var tileBoundingBox = wmsTileX1 + ',' + wmsTileY1 + ',' + wmsTileX2 + ',' + wmsTileY2;
	
	// tile size
	var tileSizeX = parseInt(this.mdvMap.config.zoomLevels[zoomLevel].params.tileSizeX);
	var tileSizeY = parseInt(this.mdvMap.config.zoomLevels[zoomLevel].params.tileSizeY);
	
	// variable parts of wms url
	src += '&' + 'width=' + tileSizeX;
	src += '&' + 'height=' + tileSizeY;
	src += '&' + 'bbox=' + tileBoundingBox;

	return src;
}


MDVMapScale.prototype.setScale = function() {
	
	var label = 0;
	var zoomlevel = this.mdvMap.config.currentZoomLevelIndex;
	var level = this.levels[this.mdvMap.config.getZoomLevelIndex()];
	var bottomText = level.meterOut + ' ' + level.meterUnit;
	
	if (zoomlevel == 0)
	{
		label = Math.round((level.pxMeter * 50) / 30);
		bottomText = '50 km';
	} else if (zoomlevel == 1) 
	{
		label = level.pxMeter;
	} else if (zoomlevel == 2) 
	{
		label = Math.round((level.pxMeter * 10) / 6);
		bottomText = '10 km';
	} else if (zoomlevel == 3) 
	{
		label = Math.round((level.pxMeter * 5) / 3);
		bottomText = '5 km';
	} else if (zoomlevel == 4) 
	{
		label = level.pxMeter;
	} else if (zoomlevel == 5)
	{
		label = Math.round((level.pxMeter * 1000) / 700);
		bottomText = '1 km';
	} else if (zoomlevel == 6)
	{
		label = Math.round((level.pxMeter * 500) / 300);
		bottomText = '500 m';
	} else if (zoomlevel == 7)
	{
		label = level.pxMeter;
	} else if (zoomlevel == 8)
	{
		label = Math.round((level.pxMeter * 100) / 80);
		bottomText = '100 m';
	}

	if (this.divScaleTextBottom) {
		this.divScaleTextBottom.innerHTML = bottomText; 
	}

	mdvLib.style([this.divScaleBar], {
		width: label + 'px',
//		width: level.pxMeter + 'px',
		backgroundPosition: ((this.scaleImg.width - label) * -1)  + 'px 0'
	});
	
	var leftstyle = label + 2;
	var widthstyle = label + 7;

	this.divScale.style.width =  widthstyle + 'px';
	this.divScaleTickBottom.style.left = leftstyle - 1 + 'px';
	
	return true;
}; 

MDVMapScale.prototype.createDivs = function() {
      
	  this.divScale = createDiv('divScale');
      this.divScaleLeft = createDiv('divScaleLeft');
      this.divScaleBar = createDiv('divScaleBar');
      this.divScaleTickTop = createDiv('divScaleTickTop');
      this.divScaleTickBottom = createDiv('divScaleTickBottom');
      this.divScaleTextTop = createDiv('divScaleTextTop');
      this.divScaleTextBottom = createDiv('divScaleTextBottom');

	  var color = '#000000';
	var layer_map = this.mdvMap;

	  if(this.mdvMap.config.params.scaleColor){
		color = this.mdvMap.config.params.scaleColor;
	  }
      
      mdvLib.style([this.divScale], {
            position: 'absolute',
            top: this.mdvMap.viewportHeight - 26 + 'px',
            //left: '5px',
			color: color,
            zIndex: 999
      });
      
      mdvLib.style([this.divScaleBar, this.divScaleLeft, this.divScaleTextBottom,
            this.divScaleTextTop, this.divScaleTickBottom, this.divScaleTickTop], {
            position: 'absolute',
            display: 'inline'
      });
      
      mdvLib.style([this.divScaleBar, this.divScaleLeft, this.divScaleTickBottom, this.divScaleTickTop], {
            background: 'transparent',
            backgroundImage: 'url(' + this.scaleImg.src + ')',
            backgroundPosition: '0 0',
            backgroundRepeat: 'no-repeat'

      });
      
      mdvLib.style([this.divScaleLeft], {
            width: '4px',
            top: '2px',
            height: '18px'
      });
      
      mdvLib.style([this.divScaleBar], {
            width: '100px',
            height: '4px',
            left: '5px',
            top: '9px'
      });

      mdvLib.style([this.divScaleTickTop], {
            width: '4px',
            height: '12px',
            backgroundPosition: '-4px 0',
            top: '2px'
      });

      mdvLib.style([this.divScaleTickBottom], {
            width: '4px',
            height: '18px',
            backgroundPosition: '-8px 0',
            top: '2px'
      });

      mdvLib.style([this.divScaleTextTop], {
            width: '60px',
            left: '8px',
            top: '0px',
            fontSize: '10px'
      });
      
      mdvLib.style([this.divScaleTextBottom], {
            width: '60px',
            left: '10px',
            top: '13px',
            fontSize: '10px'
      });

      this.divScale.appendChild(this.divScaleLeft);
      this.divScale.appendChild(this.divScaleBar);
      this.divScale.appendChild(this.divScaleTickTop);
      this.divScale.appendChild(this.divScaleTickBottom);

      if (this.displayYards)
            this.divScale.appendChild(this.divScaleTextTop);

      if (this.displayMeters)
            this.divScale.appendChild(this.divScaleTextBottom);

      this.mdvMap.viewport.appendChild(this.divScale);

      this.mdvMap.events.registerEvent(MDVEvent_ZOOM_CHANGED, this, this.setScale);

      this.init = true;

      this.setScale();
	  
      function createDiv(id) {
	  
		  if(document.getElementById(id)){
			var div = document.getElementById(id);
			
			return div;
		  }
		  else{
            var div = document.createElement('div');
			
            div.id = id;
            return div;
		}
      }     
};

MDVMapScale.prototype.updatePosition = function(viewportHeight) {
    if (this.divScale && viewportHeight) {
        this.divScale.style.top = parseInt(viewportHeight, 10) - 30 + 'px';
    }              
};



function ContextMenuHandler(id, status, obj) {
	if (status)
		ctxCoords = obj;
}


function getMOTImage(mot, type, pin) {
	var imgPath = 'images/map/';
	var zl = this.mdvMap.config.getZoomLevelIndex(); 

	if((type=='POI_POINT') && pin.attrs[1].value)
	{
		if (pin.attrs[1].value == 'CambioCar')
		{
			this.options['alignment'] = new MDVPoint(0.5, 0.5);
			imgPath += 'car_sharing.png';
		} else if (pin.attrs[1].value == 'TicketCounter'){
			this.options['alignment'] = new MDVPoint(0.5, 0.5);
			imgPath += 'ticket.png';
		} else {
			if (zl == 8)
			{
				this.options['alignment'] = new MDVPoint(0.3, 0.9);
				imgPath += 'poi_big.png';
			} else {
				this.options['alignment'] = new MDVPoint(0.5, 0.9);
				imgPath += 'poi.png';
			}
		}
	} else 	if((type=='POI_AREA') && pin.attrs[1].value)
	{
		if (zl == 8)
		{
			imgPath += 'poi_big.png';
		} else {
			imgPath += 'poi.png';
		}
	} else if(type=='STOP'){
		if (zl == 6 || zl == 7 || zl == 8)
		{
			this.options['alignment'] = new MDVPoint(0.5, 0.5);
			imgPath += 'stop_big.png';
			return imgPath;
		} else {
			this.options['alignment'] = new MDVPoint(0.5, 0.5);
			imgPath += 'stop.png';
			return imgPath;
		}
	} 
	return imgPath;
}
