var mouseDown = false;
var debx = 0, deby = 0;
var finx = 0, finy = 0;
var basex = 0, basey = 0;
var origx = 0, origy = 0;
var ox, oy;
var mouseX, mouseY;

var oRulerInfo = { x1 : -1, y1 : -1, x2 : -1, y2 : -1 };

var puntos = "";

var polyX=0;
var polyY=0;

var tabPuntos = new Array();

var iZoom, iWidth;
		
function trtDown(e) {
	initDown();
	
    mouseDown = true;
    
    getMouse(e);
    
    if (parent.document.toolForm.toolSel.value == "acercar" 
     || parent.document.toolForm.toolSel.value == "info"
	 || parent.document.toolForm.toolSel.value == "infoCirculo"
	 || parent.document.toolForm.toolSel.value == "geoCercaRectangulo"
	 || parent.document.toolForm.toolSel.value == "geoCercaCirculo") {
        debx = mouseX;
        deby = mouseY;

        layer.style.left = debx;
        layer.style.top = deby;
    }
    
    if (parent.document.toolForm.toolSel.value == "pan") {
    
        document.getElementById("divHtmlPuntos").style.display = "none";
    
		var brick = event.srcElement;
	    
		debx = mouseX;
		deby = mouseY;
			
		basex = event.offsetX;
		basey = event.offsetY;
				
		origx = (isNaN(parseInt(brick.style.left)))? parseInt(brick.offsetLeft): parseInt(brick.style.left);
		origy = (isNaN(parseInt(brick.style.top)))? parseInt(brick.offsetTop): parseInt(brick.style.top);

		ox = ((isNaN(parseInt(brick.style.left)))? parseInt(brick.offsetLeft): parseInt(brick.style.left)) - origx;
		oy = ((isNaN(parseInt(brick.style.top)))? parseInt(brick.offsetTop): parseInt(brick.style.top)) - origy;
    }
    
    if (parent.document.toolForm.toolSel.value == "distancia") {
        trtDownDistancia();
    }
    
    document.status.xMouseDown.value = mouseX;
    document.status.yMouseDown.value = mouseY;
    
    return true;
}

function initDown() {
	layer.style.visibility = 'hidden';
	layer.style.width = 0;
    layer.style.height = 0;
    layer.style.top = 0;
    layer.style.left = 0;
    circulo.style.visibility = 'hidden';
    circulo.style.width = 0;
    circulo.style.height = 0;
    circulo.style.top =0;
    circulo.style.left = 0;

    circulo.src = 'controlImages/trans.gif';
   
 
    for (var i = 0; i < tabPuntos.length; i++) {
    	document.body.removeChild(tabPuntos[i]);
    }

	oTmp = document.getElementById("rulerLayer");
    lin = document.getElementById("line");
    ver = document.getElementById("vertical");
    hor = document.getElementById("horizontal");
    dis = document.getElementById("distancia");

    lin.style.display = 'none';
    lin.style.visibility = 'hidden';

    ver.style.display = 'none';
    hor.style.display = 'none';
    dis.style.display = 'none';

    ver.style.visibility = 'hidden';
    hor.style.visibility = 'hidden';
    dis.style.visibility = 'hidden';
		
    oTmp.style.display = 'none';
    oTmp.style.visibility = 'hidden';
	
    iZoom = parseFloat(document.status.z.value);
    iWidth = parseInt(document.status.mapWidth.value);
    
    puntos = "";
    polyX = 0;
    polyY = 0;
    tabPuntos = new Array();
    
    document.getElementById("divToolTip").style.display = "none";
    
    document.getElementById("star").style.display = "none";
    document.getElementById("star1").style.display = "none";
    
}

function getMouse(e){
    if (document.all){
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
    } else {
		mouseY = e.pageY;
		mouseX = e.pageX;
    }
}

function trtMove(e) {
    if (mouseDown == true) {
        getMouse(e);
        
        if (parent.document.toolForm.toolSel.value == "acercar"
		 || parent.document.toolForm.toolSel.value == "info"
		 || parent.document.toolForm.toolSel.value == "geoCercaRectangulo") {
        	layer.style.visibility = 'visible';
        	
	    	finx = mouseX;
            finy = mouseY;
            
            layer.style.width = Math.abs(finx - debx);
            layer.style.height = Math.abs(finy - deby);
        
            if (finx - debx < 0 && finy - deby < 0) {
                layer.style.left = finx;
                layer.style.top = finy;
            } else if (finx - debx < 0) {
                layer.style.left = finx;
            } else if (finy - deby < 0) {
                layer.style.top = finy;
            }
        }
        
        if (parent.document.toolForm.toolSel.value == "infoCirculo"
		 || parent.document.toolForm.toolSel.value == "geoCercaCirculo") {
        	circulo.style.visibility = 'visible';
        	
        	finx = mouseX;
            finy = mouseY;
            
            layer.style.width = Math.abs(finx - debx);
            layer.style.height = Math.abs(finy - deby);

			//window.status = '---[' +  Math.abs(finx - debx) + ' --[' + Math.abs(finy - deby);
            
            if (finx - debx < 0 && finy - deby < 0) {
                layer.style.left = finx;
                layer.style.top = finy;
            } else if (finx - debx < 0) {
                layer.style.left = finx;
                layer.style.top = deby;
            } else if (finy - deby < 0) {
            	layer.style.left = debx;
                layer.style.top = finy;
            } else {
            	layer.style.left = debx;
                layer.style.top = deby;
            }
        	
        	var circleW = parseInt(layer.style.width);
			var circleH = parseInt(layer.style.height);
			
			var radio = Math.sqrt(Math.pow(circleW, 2) + Math.pow(circleH, 2));
			
			if (radio < 60) {
				circulo.src = 'controlImages/circulo6.gif';
			} else {
				circulo.src = 'controlImages/circulo5.gif';
			}
			
			if (finy - deby < 0) {
				layer.style.top = parseInt(layer.style.top) - (radio - circleH);
			} else {
				layer.style.top = parseInt(layer.style.top) - (radio);
			}
			
			if (finx - debx < 0) {
				layer.style.left = parseInt(layer.style.left) - (radio - circleW);
			} else {
				layer.style.left = parseInt(layer.style.left) - (radio);
			}
			
			circulo.style.height = radio * 2;
			circulo.style.width = radio * 2;
        }
        
        if (parent.document.toolForm.toolSel.value == "infoPoligono"
		 || parent.document.toolForm.toolSel.value == "geoCercaLinea"
		 || parent.document.toolForm.toolSel.value == "geoCercaPoligono") {
        	if (polyX == 0 && polyY == 0
        	 || Math.sqrt(Math.pow(mouseX - polyX, 2) + Math.pow(mouseY - polyY, 2)) >= 10) {
        	 	var img = "";
        	 	img += "<img src='controlImages/punto.gif' height='3' width='3' border='0' ";
        	 	img += "' style='position:absolute; ";
        	 	img += "z-index:auto;' />";
        	 	//alert(img);
        	 	tabPuntos[tabPuntos.length] = document.createElement(img);
        		document.body.insertBefore(tabPuntos[tabPuntos.length - 1]);
        		tabPuntos[tabPuntos.length - 1].style.left = mouseX;
        		tabPuntos[tabPuntos.length - 1].style.top = mouseY;
        		polyX = mouseX;
        		polyY = mouseY;
        		puntos += mouseX + ",";
		    	puntos += mouseY + ",";
        	}
        }
        
        if (parent.document.toolForm.toolSel.value == "pan") {
            //document.getElementById("divHtmlPuntos").style.display = "none";
            //document.getElementById("divToolTip").style.display = "none";

            var brick = event.srcElement;
			
            var myClientX = mouseX - basex;
            var myClientY = mouseY - basey;
				
            var mx = brick.width / 2;
            var my = brick.height / 2;
				
            brick.style.left = ox + myClientX;
            brick.style.top = oy + myClientY;
		}
		
		if (parent.document.toolForm.toolSel.value == "distancia") {
		    trtMoveDistancia();
        }
    }
}

function trtUp(e) {
    mouseDown = false;
    
    getMouse(e);
    
    document.status.xMouseUp.value = mouseX;
    document.status.yMouseUp.value = mouseY;
    
    if (parent.document.toolForm.toolSel.value == "acercar") {
        document.status.op.value = 2;
        actualisarMapa();
    }
    
    if (parent.document.toolForm.toolSel.value == "alejar") {
        document.status.op.value = 3;
        actualisarMapa();
    }
    
    if (parent.document.toolForm.toolSel.value == "centrar") {
        document.status.op.value = 4;
        actualisarMapa();
    }
    
    if (parent.document.toolForm.toolSel.value == "pan") {
		
		var brick = event.srcElement;
				
		var mx = brick.width / 2;
		var my = brick.height / 2;
				
		var indf = brick.style.left.indexOf("p");
		var brickleft = parseInt(brick.style.left.slice(0, indf));
		if (isNaN(brickleft)) {
		    brickleft = 0;
		}
		
		indf = brick.style.top.indexOf("p");
		var bricktop = parseInt(brick.style.top.slice(0, indf));
		if (isNaN(bricktop)) {
		    bricktop = 0;
		}
		
		document.status.xMouseUp.value = parseInt(mx - brickleft);
        document.status.yMouseUp.value = parseInt(my - bricktop);
        
		document.status.op.value = 4;
        actualisarMapa();
        
        document.status.xMouseUp.value = 0;
        document.status.yMouseUp.value = 0;
    }

    if (parent.document.toolForm.toolSel.value == "marcar") {
        
        //parent.showTab(new Array('page3'), new Array('page1', 'page2', 'page4', 'page5'));
        
        document.getElementById("star").style.display = "block";
        document.getElementById("star").style.left = mouseX - 8;
        document.getElementById("star").style.top = mouseY - 8;
        
        var parametros = "";
        parametros += "?xScreenPunto=" + mouseX;
        parametros += "&yScreenPunto=" + mouseY;
        parametros += "&x=" + document.status.x.value;
        parametros += "&y=" + document.status.y.value;
        parametros += "&z=" + document.status.z.value;
        parametros += "&mapWidth=" + document.status.mapWidth.value;
        parametros += "&mapHeight=" + document.status.mapHeight.value;

        parent.document.frames["frameDerecho"].location = "geocodificacion/guardarPunto.asp" + parametros;
        
    }
    
    if (parent.document.toolForm.toolSel.value == "info"
     || parent.document.toolForm.toolSel.value == "infoCirculo"
     || parent.document.toolForm.toolSel.value == "infoPoligono"
	 || parent.document.toolForm.toolSel.value == "geoCercaLinea"
	 || parent.document.toolForm.toolSel.value == "geoCercaRectangulo"
	 || parent.document.toolForm.toolSel.value == "geoCercaCirculo"
	 || parent.document.toolForm.toolSel.value == "geoCercaPoligono") {
	    /*
		if (parent.document.toolForm.toolSel.value.slice(0, 4) == "info") {
			parent.showTab(new Array('page2'), new Array('page1', 'page3', 'page4', 'page5'));
		}*/

        var parametros = "";
        parametros += "?xScreenDown=" + document.status.xMouseDown.value;
        parametros += "&yScreenDown=" + document.status.yMouseDown.value;
        parametros += "&xScreenUp=" + mouseX;
        parametros += "&yScreenUp=" + mouseY;
        parametros += "&x=" + document.status.x.value;
        parametros += "&y=" + document.status.y.value;
        parametros += "&z=" + document.status.z.value;
        parametros += "&mapWidth=" + document.status.mapWidth.value;
        parametros += "&mapHeight=" + document.status.mapHeight.value;
		
		if (parent.document.toolForm.toolSel.value == "geoCercaLinea") {
			parametros += "&linea=1";
		} else {
			if (parent.document.toolForm.toolSel.value.slice(0, 3) == "geo") {
				parametros += "&linea=0";
			}
		}
		
        if (parent.document.toolForm.toolSel.value == "infoCirculo"
		 || parent.document.toolForm.toolSel.value == "geoCercaCirculo") {
        	parametros += "&circulo=1";
        } else {
        	parametros += "&circulo=0";
        }
        if (parent.document.toolForm.toolSel.value == "infoPoligono"
		 || parent.document.toolForm.toolSel.value == "geoCercaPoligono") {
        	parametros += "&poligono=1";
        	puntos += mouseX + ",";
	    	puntos += mouseY;
        } else {
        	parametros += "&poligono=0";
        }
        parametros += "&puntos=" + puntos;

		if (parent.document.toolForm.toolSel.value.slice(0, 4) == "info") {
			parent.document.frames["frameDerecho"].location = "informacion/informacion.asp" + parametros;
		} else {
			parent.document.frames["frameGeoCerca"].location = "geocercas/crearGeoCerca.asp" + parametros;
		}
    }
}

function trtDownDistancia() {
    oTmp = document.getElementById("rulerLayer");
    lin = document.getElementById("line");
    ver = document.getElementById("vertical");
    hor = document.getElementById("horizontal");
    dis = document.getElementById("distancia");

    lin.style.display = 'block';
    lin.style.visibility = 'visible';

    ver.style.display = 'block';
    hor.style.display = 'block';
    dis.style.display = 'block';

    ver.style.visibility = 'visible';
    hor.style.visibility = 'visible';
    dis.style.visibility = 'visible';
		
    oTmp.style.display = 'block';
    oTmp.style.visibility = 'visible';
	
    lin.style.height = 0;
    lin.style.width = 0;

    oTmp.style.visibility = "visible";

    oTmp.style.left =  mouseX ;
    oTmp.style.right =  mouseY ;
    oTmp.style.left = mouseX;
    oTmp.style.top = mouseY;
    
    oRulerInfo.x1 = mouseX;
    oRulerInfo.y1 = mouseY;

    oTmp.style.width = 1;
    oTmp.style.height = 1;
}

function trtMoveDistancia() {
    if (mouseX -  oRulerInfo.x1 > 0) {
		oTmp.style.borderColor = "red";
        oTmp.style.borderRightStyle = "dotted";
		oTmp.style.borderRightWidth = 1;
		oTmp.style.borderLeftStyle = "none";
		hor.style.left = oRulerInfo.x1 +  parseFloat( oTmp.style.width )/2;
		hor.style.top = oRulerInfo.y1;
		dis.style.left = oRulerInfo.x1 +  parseFloat( oTmp.style.width )/2 - 10;
    } else {
        oTmp.style.borderColor = "red";
        oTmp.style.borderLeftStyle = "dotted";
		oTmp.style.borderLeftWidth = 1;
		oTmp.style.borderRightStyle = "none";
		oTmp.style.left = oRulerInfo.x1 - Math.abs( mouseX -  oRulerInfo.x1  );
		hor.style.left = oRulerInfo.x1 - parseFloat( oTmp.style.width )/2;
		hor.style.top = oRulerInfo.y1;
		dis.style.left = oRulerInfo.x1 - parseFloat( oTmp.style.width )/2 - 25;
    }
		
    if (mouseY -  oRulerInfo.y1 >  0) {
        oTmp.style.borderColor = "red";
		oTmp.style.borderTopStyle = "dotted";
		oTmp.style.borderTopWidth = 1;
		oTmp.style.borderBottomStyle = "none";	
	
		ver.style.top = oRulerInfo.y1 +  parseFloat( oTmp.style.height )/2;
		ver.style.left = mouseX;
		dis.style.top = oRulerInfo.y1 +  parseFloat( oTmp.style.height )/2 + 12;
    } else {
        oTmp.style.borderColor = "red";
		oTmp.style.borderBottomStyle = "dotted";
		oTmp.style.borderBottomWidth = 1;
		oTmp.style.borderTopStyle = "none";
		oTmp.style.top = oRulerInfo.y1 - Math.abs( mouseY -  oRulerInfo.y1  );
	
		ver.style.top = oRulerInfo.y1 -  parseFloat( oTmp.style.height )/2;
		ver.style.left = mouseX;
		dis.style.top = oRulerInfo.y1 -  parseFloat( oTmp.style.height )/2 + 12;
    }	


    if ((mouseX - oRulerInfo.x1 >= 0 && mouseY - oRulerInfo.y1 < 0)  
     || (mouseX - oRulerInfo.x1 < 0 && mouseY - oRulerInfo.y1  >= 0)) {
        if (lin.src != diag2.src) {
		    lin.src = diag2.src;
		}
    } else {
        if (lin.src != diag1.src) {
	    	lin.src = diag1.src;
		}
    }
    
    //window.status = lin.src;
    
    oTmp.style.width =  Math.abs( mouseX -  oRulerInfo.x1  );
    oTmp.style.height = Math.abs( mouseY -  oRulerInfo.y1 );
    lin.style.width =  Math.abs( mouseX -  oRulerInfo.x1  );
    lin.style.height = Math.abs( mouseY -  oRulerInfo.y1 );

    if (parseInt(oTmp.style.width) < 10) {
        if (oTmp.style.borderLeftStyle != "solid"  ) {
            oTmp.style.borderLeftStyle = "solid";
            oTmp.style.borderLeftWidth = 2;
	    	oTmp.style.borderRightStyle = "none";
            oTmp.style.borderColor = "black";
		}	    
    }
	
    if (parseInt(oTmp.style.height) < 10) {
        if (oTmp.style.borderTopStyle != "solid" ) {
	    	oTmp.style.borderTopStyle = "solid";
		    oTmp.style.borderTopWidth = 2;
		    oTmp.style.borderBottomStyle = "none";
	    	oTmp.style.borderColor = "black";
        }
    }
	 
    if (ver.style.visibility != "visible") ver.style.visibility = "visible";

    var ancho = parseInt(  ( ( Math.abs(mouseX -  oRulerInfo.x1) * iZoom  ) / iWidth  )  * 1000 );
    ancho = parseFloat( ancho/1000 ) + 'Km';

    var alto = parseInt( ( ( Math.abs(mouseY -  oRulerInfo.y1) *  iZoom ) /  iWidth  ) * 1000 );
    alto = parseFloat( alto/1000 ) + 'Km';
    
    var diagonal = Math.sqrt(  Math.pow( parseFloat(ancho), 2 ) + Math.pow( parseFloat(alto), 2 )   );
    diagonal = parseInt( diagonal * 1000 );
    diagonal = parseFloat( diagonal / 1000 ) +  'Km';
    
    dis.innerText = diagonal;
}



