var ViaM_isClick = false;
var ViaM_mouseStartX = -1;
var ViaM_mouseStartY = -1;
var ViaM_minX = 0;
var ViaM_maxX = 0;
var ViaM_minY = 0;
var ViaM_maxY = 0;
var isInit = false;
var minY,maxY,offsetY,offsetX;
var daDiv;
var ViaM_SelectionDiv,ViaM_CrossDiv,map;

//coordenada de borwser para colocar o pin de origem entre iteracoes no mapa
var origemx=-1;
var origemy=-1;
var destinox=-1;
var destinoy=-1;
var llx =-1;
var lly =-1;

//variavel que determina de estou a usar a func de back e prev zoom. Em caso afirmativo, faco set para true, para que nao seja
//atribuido o valor das coordenadas em valores e browser as variaveis usadas pelo servidor para fazer o calculo.
var isPrevNextZoom = false; 

//variaveis para computacoes de rota entre origem e destino com eventual passagem por um ponto
var CoordenadaOrigemX,CoordenadaOrigemY;
var CoordenadaDestinoX,CoordenadaDestinoY;
var CoordenadaIntermediaX,CoordenadaIntermediaY;

//variaveis para zoom de uma determinada area
var ViaM_x0,ViaM_y0,ViaM_x1,ViaM_y1;

var selectedTool;
var isComputingMap = false;

//browser check up
var ViaM_ns = (document.layers ? true : false); //netscape
var ViaM_ie = (document.all ? true : false); //iexplorer
var ViaM_navig = navigator.userAgent; //netscape 4
var ViaM_moz = (ViaM_navig.indexOf("Gecko")>=0?true : false); //opera

var daMap,mapWidth,mapHeight,mapTop,mapLeft,oImg,daDiv,bgOutDiv;
var overMap;
function ViaM_init(o) { 
	
	o = "contentuc_mapa"; //id da imagen do mapa
		
	daMap = document.getElementById(o); 
	
	//tsa, 13-04-2006: alteracao para desactivar as operacoes de mapa qd estou na minha linha e tenho a nova imagem 
	//para escolher uma linha.
	if (daMap.src.indexOf("fundo_escolha_linha_") != -1) return;
	
	mapWidth = parseInt(daMap.width); 
	mapHeight = parseInt(daMap.height); 
	mapLeft = parseInt(daMap.parentNode.parentNode.offsetLeft);  //funciona porque o parent e uma td com style="position:relative"
	mapTop = parseInt(daMap.parentNode.parentNode.offsetTop); //funciona porque o parent e uma td com style="position:relative"
	ratioMap = parseInt(daMap.width)/parseInt(daMap.height); 
	isZoom = false; 
	navZoom = 1; 
	
	//this
	/*offsetX = daMap.offsetLeft; 
	offsetY = daMap.offsetTop; */
	//became this
	offsetX = mapLeft; 
	offsetY = mapTop;
	
	
	daDiv = daMap.cloneNode(true); 
	document.body.appendChild(daDiv); 
	daDiv.id = "autreDiv"; 
	daDiv.style.display = "none"; 
	daDiv.style.position = "absolute"; 
	daDiv.style.top = offsetY; 
	daDiv.style.left = offsetX; 
	//minY = document.getElementById("scaleGif").offsetTop - (oImg.clientHeight/2); 
	//maxY = document.getElementById("scaleGif").offsetTop + 109 - (oImg.clientHeight/2); 
	bgOutDiv = document.createElement("IMG"); 
	document.body.appendChild(bgOutDiv); 
	bgOutDiv.style.display= "none"; 
	bgOutDiv.style.position = "absolute"; 
	bgOutDiv.style.top = offsetY; 
	bgOutDiv.style.left = offsetX; 
	bgOutDiv.style.zIndex = 11; 
	
	mapSize = "standard";
	bgOutDiv.src = "images/map"+((mapSize == "standard") ? "1" : "2")+".gif"; 
	
		
	ViaM_SelectionDiv = document.createElement("DIV"); 
	ViaM_SelectionDiv.style.position = "absolute"; 
	ViaM_SelectionDiv.style.height = "1px"; 
	ViaM_SelectionDiv.style.width = "1px"; 
	ViaM_SelectionDiv.style.visibility = "hidden"; 
	ViaM_SelectionDiv.style.border = "2px solid #FF0000"; 
	ViaM_SelectionDiv.style.zIndex = 100; 
	ViaM_SelectionDiv.innerHTML = "<img src='images/s.gif' height=1 width=1>"; 
	document.body.appendChild(ViaM_SelectionDiv); 
	
	map = document.getElementById(o); 
	map.style.cursor = "crosshair"; 
	
	//ViaM_minX = parseInt(map.offsetLeft); 
	ViaM_minX = mapLeft; 
	
	ViaM_maxX = ViaM_minX + parseInt(map.width); 
	
	//ViaM_minY = parseInt(map.offsetTop); 
	ViaM_minY = mapTop;
	
	ViaM_maxY = ViaM_minY + parseInt(map.height); 
	map.onmousedown = ViaM_mouseDown; 
	map.onmousemove = ViaM_mouseMove; 
	map.onmouseup = ViaM_mouseUp; 
	ViaM_SelectionDiv.onmousemove = ViaM_mouseMove; 
	ViaM_SelectionDiv.onmouseup = ViaM_mouseUp;
	
	document.getElementById("themap").onmouseup = panend;
	initActiveTool(document.getElementById("contentuc_toolAction").value);
}

var panStartX,panStartY,panEndX,panEndY;
var panstarted;
function ViaM_mouseDown(e) { 

	if (ViaM_moz) 
		e.preventDefault(); 
		
	if ((ViaM_ns && e.which != 1) || (ViaM_ie && event.button != 1) ||(ViaM_moz && e.which != 1) ) { return true; } 
	
	var ViaM_mouseX = (ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft); 
	var ViaM_mouseY = (ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop); 
	ViaM_mouseStartX = ViaM_mouseX; 
	ViaM_mouseStartY = ViaM_mouseY; 

	if (selectedTool == "move") { 
		if (!panstarted) {
			panStartX = ViaM_mouseX - mapLeft;
			panStartY = ViaM_mouseY - mapTop;
		}
		panstarted = true;
		event.cancelBubble = true;
		document.getElementById("themap").onmousemove = pan;
	} else {
		ViaM_isClick = ((ViaM_mouseStartX >= ViaM_minX) && (ViaM_mouseStartX <= ViaM_maxX)) && ((ViaM_mouseStartY >= ViaM_minY) && 
					(ViaM_mouseStartY <= ViaM_maxY)&&(!isComputingMap)); 
					
		if (ViaM_isClick) { 
			ViaM_showSelection(ViaM_mouseStartX, ViaM_mouseStartY); 
			ViaM_SelectionDiv.style.visibility = "visible"; 
		} 
	}
	
	return true; 
}

var deltAxe;
function ViaM_mouseMove(e) { 
	var ViaM_mouseX = (ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft); 
	var ViaM_mouseY = (ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop); 

	if ((ViaM_isClick)&&(!isComputingMap)) { 
		ViaM_showSelection(ViaM_mouseX, ViaM_mouseY); 
		return false; 
	} 

	return true;
}

function pan() {
	if (panstarted) {
		var ViaM_mouseX = (ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft) - mapLeft; 
		var ViaM_mouseY = (ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop) - mapTop; 
	
		document.getElementById("contentuc_mapa").style.left = ViaM_mouseX - panStartX + "px";
		document.getElementById("contentuc_mapa").style.top = ViaM_mouseY - panStartY + "px";
		
		//tsa,21-12-2004: quando movo o mapa, escondo os marcadores
		origempin.hide();
		destinopin.hide();
	}
	return false;
}

function panend() {	
	var ViaM_mouseX = (ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft); 
	var ViaM_mouseY = (ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop); 
	
	panEndX = ViaM_mouseX - mapLeft;
	panEndY = ViaM_mouseY - mapTop;
	
	document.getElementById("contentuc_zoomAreaLeft").value = panStartX;
	document.getElementById("contentuc_zoomAreaTop").value = panStartY;
	document.getElementById("contentuc_zoomAreaRight").value = panEndX;
	document.getElementById("contentuc_zoomAreaBottom").value = panEndY;
	
	if (selectedTool == "move") {
		//alert("to submit move");
		document.forms[0].submit();
		showEsperaLayer();
	}
	
	panstarted = false;	
}

function ViaM_mouseUp(e) { 
    var isnovarede;
	var mapWizCoords = new Array();

	if ((ViaM_ns && e.which != 1) || (ViaM_ie && event.button != 1) ||(ViaM_moz && e.which != 1) ) { return true; }  
	var ViaM_mouseX = (ViaM_ns||ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft); 
	var ViaM_mouseY = (ViaM_ns||ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop); 
	if (ViaM_isClick){ 
		var ViaM_x1 = ViaM_mouseStartX; 
		var ViaM_x2 = ViaM_mouseX; 
		ViaM_x0 = ViaM_mouseStartX - ViaM_minX; 
		ViaM_y0 = ViaM_mouseStartY - ViaM_minY; 
		ViaM_x1 = ViaM_mouseX - ViaM_minX; 
		ViaM_y1 = ViaM_mouseY - ViaM_minY; 
		
		var ViaM_mouseMoved = (ViaM_x1 != ViaM_x0) || (ViaM_y1 != ViaM_y0); 
		//ViaM_urlact = document.forms[theForm].cmd.value; 
		
		if (ViaM_mouseMoved) { 
			document.getElementById("contentuc_zoomAreaLeft").value = ViaM_x0;
			document.getElementById("contentuc_zoomAreaTop").value = ViaM_y0;
			document.getElementById("contentuc_zoomAreaRight").value = ViaM_x1;
			document.getElementById("contentuc_zoomAreaBottom").value = ViaM_y1;
		} else {
			document.getElementById("contentuc_zoomAreaLeft").value = ViaM_x0;
			document.getElementById("contentuc_zoomAreaTop").value = ViaM_y0;
			document.getElementById("contentuc_zoomAreaRight").value = -1;
			document.getElementById("contentuc_zoomAreaBottom").value = -1;
		}
		
		ViaM_SelectionDiv.style.visibility = "hidden"; 
		
		if (document.all) 
			daMap.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=100)"; 
			
		if (document.all) daDiv.style.display = "none"; 
		if (((ViaM_x1 > ViaM_x0)&&(ViaM_x1 < ViaM_x0 + 5))||((ViaM_y1 > ViaM_y0)&&(ViaM_y1 < ViaM_y0 + 5))||
			((ViaM_x0 > ViaM_x1)&&(ViaM_x0 < ViaM_x1 + 5))||((ViaM_y0 > ViaM_y1)&&(ViaM_y0 < ViaM_y1 + 5))) {
				ViaM_x1 = ViaM_x0;
				ViaM_y1 = ViaM_y0;
		} 
		
		//ViaM_doCalc(ViaM_urlact,"?x0=" + ViaM_x0 + "&y0=" + ViaM_y0 + "&x1=" + ViaM_x1 + "&y1=" + ViaM_y1+"&act=" + document.forms[theForm].act.value); 
		ViaM_mouseStartX = -1; 
		ViaM_mouseStartY = -1; 
	} 

	ViaM_isClick = false; 
	
	if (selectedTool != "move" && selectedTool != "od") {
		//alert("to submit not move");
		if (ViaM_x1 - ViaM_x0 >= 10 || ViaM_x1 - ViaM_x0 <= -10) {
			document.getElementById("contentuc_toolAction").value = "zoomin";
			document.forms[0].submit();
			showEsperaLayer();
		} else {			
			if(selectedTool == "info") { 
                if ( typeof( window[ 'menuuc_redeandanteuc_redeandantecheck' ] ) != "undefined" ) {
                    isnovarede = document.getElementById("menuuc_redeandanteuc_redeandantecheck").checked;
                } else {
                    isnovarede = false;
                }                
                if(!isnovarede) {
                    if (document.location.href.indexOf("redeandate=true") != -1) isnovarede = true;
                }
            
				//tsa,22-12-2004: se so cliquei no mapa, entao isto sinifica que estou a usar o info tool e portanto, 
				//mostro a iframe com esta funcionalidade.
				var point = new Array();
				point = converteToMapCoord(ViaM_x0,ViaM_y0);
				document.getElementById("info").src="info.aspx?x=" + point[0] + "&y=" + point[1] + "&zf=" + document.getElementById("contentuc_toolsuc_selectedZoomFactor").value + "&imgExt=" + getExtentInfo(document.getElementById("contentuc_imagenActual").value).replace(/#/g,"_") + "&compare=" + (isnovarede?1:0);
                document.getElementById("infolayer").style.display = "";
				document.getElementById("info").style.top = ViaM_y0;
				document.getElementById("info").style.left = ViaM_x0;
				document.getElementById("info").style.zIndex = "100";
			} else {
				if(document.location.href.indexOf("mapwiz") != -1 ) { //estou na escolha de pontos para o wizard
					if (document.location.href.indexOf("OD=X") != -1 ) {
						if (window.opener != null) {
							mapWizCoords = converteToMapCoord(ViaM_x0,ViaM_y0);
							window.opener.document.getElementById("ContentObj_ODX").value = mapWizCoords[0];
							window.opener.document.getElementById("ContentObj_ODY").value = mapWizCoords[1];
							window.opener.setPontoEscolhidoMapa("OD");
							window.close();	
						}
					} else if (document.location.href.indexOf("OD=Y") != -1 ) {
						if (window.opener != null) {
							mapWizCoords = converteToMapCoord(ViaM_x0,ViaM_y0);
							window.opener.document.getElementById("ContentObj_DDX").value = mapWizCoords[0];
							window.opener.document.getElementById("ContentObj_DDY").value = mapWizCoords[1];
							window.opener.setPontoEscolhidoMapa("DD");
							window.close();
						}
					} else {
						
					}
				}
			}	
		}
	}
		
	return true;
}

function ViaM_showSelection(ViaM_mouseX, ViaM_mouseY) { 
	var ViaM_x1 = ViaM_mouseStartX; 
	var ViaM_x2 = ViaM_mouseX; 
	if (ViaM_x1 > ViaM_x2) { 
		var ViaM_a = ViaM_x2; 
		ViaM_x2 = ViaM_x1; 
		ViaM_x1 = ViaM_a; 
	} 
	
	if (ViaM_x1 < ViaM_minX) 
		ViaM_x1 = ViaM_minX; 
		
	if (ViaM_x2 > ViaM_maxX) 
		ViaM_x2 = ViaM_maxX; 
		
	var ViaM_y1 = ViaM_mouseStartY; 
	var ViaM_y2 = ViaM_mouseY; 
	
	if (ViaM_y1 > ViaM_y2) { 
		ViaM_a = ViaM_y2; 
		ViaM_y2 = ViaM_y1; 
		ViaM_y1 = ViaM_a; 
	} 
	
	if (ViaM_y1 < ViaM_minY) 
		ViaM_y1 = ViaM_minY; 
		
	if (ViaM_y2 > ViaM_maxY) 
		ViaM_y2 = ViaM_maxY; 
		
	ViaM_SelectionDiv.style.visibility = "visible"; 
	ViaM_SelectionDiv.style.left = ViaM_x1; 
	ViaM_SelectionDiv.style.top = ViaM_y1; 
	ViaM_SelectionDiv.style.width = ViaM_x2 - ViaM_x1; 
	ViaM_SelectionDiv.style.height = ViaM_y2 - ViaM_y1; 
	
	if (document.all) 
		map.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=50)"; 
		
	if (document.all) 
		daDiv.style.display =""; 
		
	if (document.all) 
		daDiv.style.clip="rect(" + (ViaM_y1-parseInt(daDiv.style.top)) + "px " + ((parseInt(ViaM_SelectionDiv.style.width)+ViaM_x1)-parseInt(daDiv.style.left)) + "px " + (parseInt(ViaM_SelectionDiv.style.height)+ViaM_y1-parseInt(daDiv.style.top)) + "px " + (ViaM_x1-parseInt(daDiv.style.left)) + "px)";
}

//para guardar a ferramenta activa
var lastSelectedObj;
function setActiveTool(obj,toolid) {
	if (toolid == "zoomin" || toolid == "zoomout") {
		document.getElementById("contentuc_toolAction").value = "";
		map.style.cursor = "crosshair";
		selectedTool = "crosshair";
		showEsperaLayer();
	} else if(toolid == "zoominbtn") {
		if (document.getElementById("contentuc_toolAction").value == "zoominbtn") {
			document.forms[0].submit();
			showEsperaLayer();
		} else {
			if (document.getElementById("contentuc_toolAction").value == "zoomin") { 
				document.getElementById("contentuc_toolAction").value = "zoominbtn";
				document.forms[0].submit();
				showEsperaLayer();
			} else {
				document.getElementById("contentuc_toolAction").value = "zoominbtn";
				document.getElementById("contentuc_toolsuc_zoomin").src="images/lupamais_on.gif";
				document.getElementById("contentuc_toolsuc_zoomout").src="images/lupamenos_off.gif";
				document.getElementById("contentuc_toolsuc_mapmover").src="images/drag_off.gif";
				document.getElementById("contentuc_toolsuc_infoTool").src="images/b_informacao_off.gif";
				document.getElementById("contentuc_infoToolIsOn").value = "false"; 
				map.style.cursor = "crosshair";
				selectedTool = "crosshair";
			}			
		}
	} else {
		document.getElementById("contentuc_toolAction").value = toolid;
		document.getElementById("contentuc_infoToolIsOn").value = "false"; 
		if(selectedTool == "crosshair") {
			document.getElementById("contentuc_toolsuc_zoomin").src="images/lupamais_off.gif";
			document.getElementById("contentuc_toolsuc_zoomout").src="images/lupamenos_off.gif";
		}
		selectedTool = toolid;
		if (selectedTool == "move") {
			map.style.cursor = "move";
			document.getElementById("contentuc_toolsuc_mapmover").src="images/drag_on.gif";
			document.getElementById("contentuc_toolsuc_infoTool").src="images/b_informacao_off.gif";
		} else if(selectedTool == "info") {
			map.style.cursor = "crosshair";
			document.getElementById("contentuc_toolsuc_mapmover").src="images/drag_off.gif";	
			document.getElementById("contentuc_toolsuc_infoTool").src="images/b_informacao_on.gif";
			document.getElementById("contentuc_infoToolIsOn").value = "true"; //como selecionei a infoTool, entao ponho-a active.
		} else {
			showEsperaLayer();
		}
	}
}

function initActiveTool(toolid) {
	document.getElementById("contentuc_toolAction").value = toolid;
	document.getElementById("contentuc_infoToolIsOn").value = "false";
	if (toolid == "zoomin" || toolid == "zoomout" || toolid == "zoominbtn") {
		map.style.cursor = "crosshair";
		selectedTool = "crosshair";
	} else if(toolid == "info") {
		map.style.cursor = "crosshair";
		selectedTool = "info";
		document.getElementById("contentuc_infoToolIsOn").value = "true";
	} else {
		selectedTool = toolid;
		if (selectedTool == "move") {
			map.style.cursor = "move"; 			
		}
	}
}

//function setMiniMapSelectedArea(minX,minY) {
function setMiniMapSelectedArea(minX,minY,maxX,maxY) {

	//tsa, 13-04-2006: alteracao para desactivar as operacoes de mapa qd estou na minha linha e tenho a nova imagem 
	//para escolher uma linha.
	if (document.getElementById("contentuc_mapa").src.indexOf("fundo_escolha_linha_") != -1) return;


	document.getElementById("loc").style.position = "absolute";
	document.getElementById("loc").style.left = minX + "px";
	document.getElementById("loc").style.top = maxY + "px";	
	
	document.getElementById("loc").style.width = Math.abs(eval(maxX - minX)) + "px";
	document.getElementById("loc").style.height = Math.abs(eval(maxY - minY)) + "px";	
	
	//document.getElementById("loc").style.background = "#FF7E00";
	document.getElementById("loc").style.border = "solid 2px #FF7E00";
}


function centerBigMapAt() {
	document.getElementById("contentuc_toolAction").value = "center";	
	document.getElementById("contentuc_zoomAreaLeft").value = event.offsetX;
	document.getElementById("contentuc_zoomAreaTop").value = event.offsetY;	
	
	document.forms[0].submit();
	showEsperaLayer();
}

function changeImg(img,newimgsrc) {
	var r, re;
	re = /_on/ig;
	if (img.src.search(re) == -1) {
		img.src = newimgsrc;
	}        
}

function setPrevZoomImg() {
	var prevImgId;
	var imagemExtentActual;
	
	prevImgId = getPreviousImgId();
	if (prevImgId != null) { 		
		imagemExtentActual = getExtentInfo(document.getElementById("contentuc_imagenActual").value);
		document.getElementById("contentuc_mapa").src = imagesZoom[prevImgId][2];
		document.getElementById("contentuc_imagenActual").value = imagesZoom[prevImgId][2];
		setActiveZoomFactor(imagesZoom[prevImgId][4]);
		eval(imagesZoom[prevImgId][5]);
		ViaM_init();
		setMarkerPos(imagemExtentActual,getExtentInfo(imagesZoom[prevImgId][2]));
		if (activeParagem != "") setParagemNoMapa(document.getElementById(activeParagem),activeParagem.replace("img",""));
	}
}

function setNextZoomImg() {
	var nextImgId;
	var imagemExtentActual;
	
	nextImgId = getNextImgId();
	if (nextImgId != null) { 
		imagemExtentActual = getExtentInfo(document.getElementById("contentuc_imagenActual").value);
		document.getElementById("contentuc_mapa").src = imagesZoom[nextImgId][2];
		document.getElementById("contentuc_imagenActual").value = imagesZoom[nextImgId][2];
		setActiveZoomFactor(imagesZoom[nextImgId][4]);
		eval(imagesZoom[nextImgId][5]);
		ViaM_init();
		setMarkerPos(imagemExtentActual,getExtentInfo(imagesZoom[nextImgId][2]));
		if (activeParagem != "") setParagemNoMapa(document.getElementById(activeParagem),activeParagem.replace("img",""));
	}
}

function getPreviousImgId() {
	for (el in imagesZoom) {
		if (imagesZoom[el][0]) {
			if (imagesZoom[el][3] != "") {
				imagesZoom[el][0] = false;
				imagesZoom[imagesZoom[el][3]][0] = true;
				return imagesZoom[el][3];	
			} else {
				return el;	
			}
		}
	}
	return null;	
}

function getNextImgId() {
	var elActual= "" ;
	
	for (el in imagesZoom) {
		if (imagesZoom[el][0]) {
			elActual = el;
			continue;
		} else {
			if (elActual != "") {
				imagesZoom[elActual][0]	= false;
				imagesZoom[el][0] = true;
				return el;
			}
		}
	}
	return null;	
}

function setActiveZoomFactor(zoomFactor) {

	var currentZoomFactor;
	currentZoomFactor = document.getElementById("contentuc_toolsuc_selectedZoomFactor").value
	document.getElementById("contentuc_toolsuc_zoominfactor" + currentZoomFactor).src = 
		document.getElementById("contentuc_toolsuc_zoominfactor" + currentZoomFactor).src.replace("overon","off");
		
	document.getElementById("contentuc_toolsuc_zoominfactor" + currentZoomFactor).attributes.item("onmouseover").nodeValue = 
		document.getElementById("contentuc_toolsuc_zoominfactor" + currentZoomFactor).attributes.item("onmouseover").nodeValue.replace("off","overon");
		
	document.getElementById("contentuc_toolsuc_zoominfactor" + currentZoomFactor).attributes.item("onmouseout").nodeValue = 
		document.getElementById("contentuc_toolsuc_zoominfactor" + currentZoomFactor).attributes.item("onmouseout").nodeValue.replace("overon","off");
		
	document.getElementById("contentuc_toolsuc_selectedZoomFactor").value = zoomFactor;
	
	document.getElementById("contentuc_toolsuc_zoominfactor" + zoomFactor).src = 
		document.getElementById("contentuc_toolsuc_zoominfactor" + zoomFactor).src.replace("off","overon");
		
	document.getElementById("contentuc_toolsuc_zoominfactor" + zoomFactor).attributes.item("onmouseover").nodeValue = 
		document.getElementById("contentuc_toolsuc_zoominfactor" + zoomFactor).attributes.item("onmouseover").nodeValue.replace("overon","off");
		
	document.getElementById("contentuc_toolsuc_zoominfactor" + zoomFactor).attributes.item("onmouseout").nodeValue = 
		document.getElementById("contentuc_toolsuc_zoominfactor" + zoomFactor).attributes.item("onmouseout").nodeValue.replace("off","overon");
}

var w; //largura do mapa
var h; //altura do mapa

var origX; //extent x min
var origY; //extent y min
var diff; //variavel que guarda o tamanho em metros do mapa actual.
var envExtent; //variavel que guarda a parte do extent da imagem actual
function setMarkerPos(extentActual,novoExtent) {
		
	w = document.getElementById("contentuc_mapa").width;
	h = document.getElementById("contentuc_mapa").height;
	
	//ponto de origem
	if (document.getElementById("contentuc_CoordenadaOrigemX").value != -1 && document.getElementById("contentuc_CoordenadaOrigemY").value != -1) {
		if (document.getElementById("contentuc_CoordenadaOrigemX").value.indexOf("#") == 0) { //coordenada ja em mapa units, uso o novo extent
			origX = parseFloat(novoExtent.split("#")[1]);
			origY = parseFloat(novoExtent.split("#")[3]);
		 
			diff = parseFloat(novoExtent.split("#")[0]) - origX;
			resW = diff / w;
		 
			diff = parseFloat(novoExtent.split("#")[2]) - origY;
			resH = diff / h;		
			
			origemx = toImageCoordX(document.getElementById("contentuc_CoordenadaOrigemX").value.replace("#",""));
			origemy = toImageCoordY(document.getElementById("contentuc_CoordenadaOrigemY").value.replace("#",""),diff);
	
		} else { //coordenadas em browser units
			origX = parseFloat(extentActual.split("#")[1]); //xmin
			origY = parseFloat(extentActual.split("#")[3]); //ymin
		 
			diff = parseFloat(extentActual.split("#")[0]) - origX;
			resW = diff / w;
		 
			diff = parseFloat(extentActual.split("#")[2]) - origY;
			resH = diff / h;		
			
			origemx = toMapCoordX(document.getElementById("contentuc_CoordenadaOrigemX").value);
			origemy = toMapCoordY(document.getElementById("contentuc_CoordenadaOrigemY").value);
			
			//guardo pontos de mapa
			document.getElementById("contentuc_CoordenadaOrigemX").value = "#" + Math.round(origemx);
			document.getElementById("contentuc_CoordenadaOrigemY").value = "#" + Math.round(origemy);
			
			origX = parseFloat(novoExtent.split("#")[1]);
			origY = parseFloat(novoExtent.split("#")[3]);
		 
			diff = parseFloat(novoExtent.split("#")[0]) - origX;
			resW = diff / w;
		 
			diff = parseFloat(novoExtent.split("#")[2]) - origY;
			resH = diff / h;		
			
			origemx = toImageCoordX(origemx);			
			origemy = toImageCoordY(origemy,diff);
		}
		
		if (document.location.href.indexOf("tab=2") != -1) {
			setImgPosition(locOrgImgPin,origemx,origemy);
		} else {
			setImgPosition(origempin,origemx,origemy);
		}
		
		isToSet = true; //tenho que por esta variavel a true para que qd puser o pin de destino seja despoletada a pesquisa od
	}
	
	//ponto de destino
	if (document.getElementById("contentuc_CoordenadaDestinoX").value != -1 && document.getElementById("contentuc_CoordenadaDestinoY").value != -1) {
		
		if (document.getElementById("contentuc_CoordenadaDestinoX").value.indexOf("#") == 0) { //coordenada ja em mapa units, uso o novo extent
			origX = parseFloat(novoExtent.split("#")[1]);
			origY = parseFloat(novoExtent.split("#")[3]);
		 
			diff = parseFloat(novoExtent.split("#")[0]) - origX;
			resW = diff / w;
		 
			diff = parseFloat(novoExtent.split("#")[2]) - origY;
			resH = diff / h;		
			
			destinox = toImageCoordX(document.getElementById("contentuc_CoordenadaDestinoX").value.replace("#",""));
			destinoy = toImageCoordY(document.getElementById("contentuc_CoordenadaDestinoY").value.replace("#",""),diff);
		} else { //coordenadas em browser units
			origX = parseFloat(extentActual.split("#")[1]); //xmin
			origY = parseFloat(extentActual.split("#")[3]); //ymin
		 
			diff = parseFloat(extentActual.split("#")[0]) - origX;
			resW = diff / w;
		 
			diff = parseFloat(extentActual.split("#")[2]) - origY;
			resH = diff / h;		
			
			destinox = toMapCoordX(document.getElementById("contentuc_CoordenadaDestinoX").value);
			destinoy = toMapCoordY(document.getElementById("contentuc_CoordenadaDestinoY").value);
			
			//guardo os pontos de mapa
			document.getElementById("contentuc_CoordenadaDestinoX").value = "#" + Math.round(destinox);
			document.getElementById("contentuc_CoordenadaDestinoY").value = "#" + Math.round(destinoy);
			
			origX = parseFloat(novoExtent.split("#")[1]);
			origY = parseFloat(novoExtent.split("#")[3]);
		 
			diff = parseFloat(novoExtent.split("#")[0]) - origX;
			resW = diff / w;
		 
			diff = parseFloat(novoExtent.split("#")[2]) - origY;
			resH = diff / h;		
			
			destinox = toImageCoordX(destinox);			
			destinoy = toImageCoordY(destinoy,diff);
			
		}

		setImgPosition(destinopin,destinox,destinoy);
		isToSet = true; //tenho que por esta variavel a true para que qd puser o pin de origem seja despoletada a pesquisa od
		//window.status = document.getElementById("contentuc_CoordenadaDestinoX").value + ":" + document.getElementById("contentuc_CoordenadaDestinoY").value;
	}
}

function converteToMapCoord(x,y) {
	var curExt;

	w = document.getElementById("contentuc_mapa").width;
	h = document.getElementById("contentuc_mapa").height;
	curExt = getExtentInfo(document.getElementById("contentuc_imagenActual").value);
	
	origX = parseFloat(curExt.split("#")[1]); //xmin
	origY = parseFloat(curExt.split("#")[3]); //ymin
	
	diff = parseFloat(curExt.split("#")[0]) - origX;
	resW = diff / w;
	
	diff = parseFloat(curExt.split("#")[2]) - origY;
	resH = diff / h;		
	
	return new Array(toMapCoordX(x),toMapCoordY(y));
}


function converteToImageCoord(x,y) {
	var curExt;

	w = document.getElementById("contentuc_mapa").width;
	h = document.getElementById("contentuc_mapa").height;
	curExt = getExtentInfo(document.getElementById("contentuc_imagenActual").value);
	
	if (w == 0) w = 600;
	if (h == 0) h = 512;

	origX = parseFloat(curExt.split("#")[1]); //xmin
	origY = parseFloat(curExt.split("#")[3]); //ymin
	
	diff = parseFloat(curExt.split("#")[0]) - origX;
	resW = diff / w;
	
	diff = parseFloat(curExt.split("#")[2]) - origY;
	resH = diff / h;		

	return new Array(toImageCoordX(x),toImageCoordY(y,diff));
}


function toMapCoordX(val) {
	return val * resW + origX;
}

function toMapCoordY(val) {
	return (h - val) * resH + origY;
}

function toImageCoordX(val) {

	return (val - origX) / resW ;      
}

function toImageCoordY(val,diff) {
    //return (val - origY) / resH ;      
    return (origY - val + diff) / resH ;      
}

function getExtentInfo(extentStr) {
	return extentStr.substr(extentStr.indexOf("=") + 1)
}

//tsa, 18-01-2005: rotina que informa o utilizador que escolheu um origem ou destino e que pode combinar os marcadores para fazer a sua pesquisa.
function showPesqFiltroInfo() {
	var extentActual;
	extentActual = getExtentInfo(document.getElementById("contentuc_imagenActual").value);
	var msg_o_pt = "Escolheu uma origem. Agora se quiser, pode arrastar o marcador azul para calcular a rota entre os dois pontos.";
	var msg_o_en = "You have chosen the start point. You can now drag de blue marker in order to compute the course.";
	var msg_d_pt = "Escolheu um destino. Agora se quiser, pode arrastar o marcador laranja para calcular a rota entre os dois pontos.";
	var msg_d_en = "You have chosen the destiny point. You can now drag de orange marker in order to compute the course.";

	w = document.getElementById("contentuc_mapa").width;
	h = document.getElementById("contentuc_mapa").height;
	
	origX = parseFloat(extentActual.split("#")[1]);
	origY = parseFloat(extentActual.split("#")[3]);
	
	diff = parseFloat(extentActual.split("#")[0]) - origX;
	resW = diff / w;
	
	diff = parseFloat(extentActual.split("#")[2]) - origY;
	resH = diff / h;		
		
	//if (document.getElementById("menuuc_pesqfrmuc_pontoorigem").value != "" && document.getElementById("menuuc_pesqfrmuc_pontodestino").value != "") return;
	
	if (document.getElementById("menuuc_pesqfrmuc_pontoorigem").value != "") {
		origemx = toImageCoordX(document.getElementById("menuuc_pesqfrmuc_pontoorigem").value.split("#")[0]);
		origemy = toImageCoordY(document.getElementById("menuuc_pesqfrmuc_pontoorigem").value.split("#")[1],diff);	
		
		//tenho de guardar os valores escolhidos para depois poder operar o mapa o ficar com o marcador no sitio escolhido
		document.getElementById("contentuc_CoordenadaOrigemX").value = "#" + Math.round(document.getElementById("menuuc_pesqfrmuc_pontoorigem").value.split("#")[0]);
		document.getElementById("contentuc_CoordenadaOrigemY").value = "#" + Math.round(document.getElementById("menuuc_pesqfrmuc_pontoorigem").value.split("#")[1]);
		
		setImgPosition(origempin,origemx,origemy);
		
		if (document.cookie.search(/pt-pt/ig) != -1) {
			document.getElementById("mensagensuc_mensagemContent").innerHTML = msg_o_pt;
		} else {
			document.getElementById("mensagensuc_mensagemContent").innerHTML = msg_o_en;
		}
		
		//se ja tenho um ponto de destino escolhido, entao despoleto a pesquisa od 
		if (document.getElementById("contentuc_CoordenadaDestinoX").value != "-1") {
			force=true;
			isToSet = true;
			dd.obj = origempin;	
			my_DropFunc();	
		} else { //se nao, aviso o user para escolher um destino.
			showlayer("mensagens");
		}		
		
	}
	
	if (document.getElementById("menuuc_pesqfrmuc_pontodestino").value != "") {
		destinox = toImageCoordX(document.getElementById("menuuc_pesqfrmuc_pontodestino").value.split("#")[0]);
		destinoy = toImageCoordY(document.getElementById("menuuc_pesqfrmuc_pontodestino").value.split("#")[1],diff);	
		
		//tenho de guardar os valores escolhidos para depois poder operar o mapa o ficar com o marcador no sitio escolhido
		document.getElementById("contentuc_CoordenadaDestinoX").value = "#" + Math.round(document.getElementById("menuuc_pesqfrmuc_pontodestino").value.split("#")[0]);
		document.getElementById("contentuc_CoordenadaDestinoY").value = "#" + Math.round(document.getElementById("menuuc_pesqfrmuc_pontodestino").value.split("#")[1]);
		
		setImgPosition(destinopin,destinox,destinoy);
		
		if (document.cookie.search(/pt-pt/ig) != -1) {
			document.getElementById("mensagensuc_mensagemContent").innerHTML = msg_d_pt;
		} else {
			document.getElementById("mensagensuc_mensagemContent").innerHTML = msg_d_en;
		}
		
		//se ja tenho um ponto de origem escolhido, entao despoleto a pesquisa od 
		if (document.getElementById("contentuc_CoordenadaOrigemX").value != "-1") {
			force=true;
			isToSet = true;
			
			dd.obj = destinopin;	
			my_DropFunc();	
		} else { //se nao, aviso o user para escolher uma origem.
			showlayer("mensagens");
		}		
	}
	
	temasToogle('contentuc_pesquisafiltrouc_pesquisa');
	//hidelayer('contentuc_pesquisafiltrouc_pesquisa');
	//origempin.show();
	//destinopin.show();
	//locOrgImgPin.show();
}