// JavaScript Document
function procesarMouseDown(e)
{
	switch (getRadioVal(document.accion.mapaction))
	{
		case "zoomin":		
			recuadroAbajo(e);
		break;
		case "zoomout":		
			recuadroAbajo(e);
		break;
		case "identify":		
			recuadroAbajo(e);
		break;
		case "hiperlink":		
			recuadroAbajo(e);
		break;
		case "link":		
			recuadroAbajo(e);
		break;
		case "pan":
			paneoAbajo(e);
		break;
	}
}

function procesarMouseMove(e)
{
	switch (getRadioVal(document.accion.mapaction))
	{
		case "zoomin":
			recuadroMover(e);
		break;
		case "zoomout":
			recuadroMover(e);
		break;
		case "hiperlink":
			recuadroMover(e);
		break;
		case "identify":
			recuadroMover(e);
		break;
		case "link":
			recuadroMover(e);
		break;
		case "pan":
			paneoMover(e);
		break;
	}
}


function procesarMouseUp(e)
{
	switch (getRadioVal(document.accion.mapaction))
	{
		case "zoomin": 
			recuadroArriba(e);
		break;
		case "identify": 
			recuadroArriba(e);
		break;
		case "zoomout": 
			recuadroArriba(e);
		break;
		case "hiperlink": 
			recuadroArriba(e);
		break;
		case "pan":
			paneoArriba(e);
		break;
		case "link": 
			recuadroArriba(e);
		break;
	}
}
function paneoAfuera(e)
{
	return false;
}

function paneoAbajo(e)
{
	getMapPixels(e);
	xInicio=xActual;
	yInicio=yActual;
	xActual=xInicio+1;
	yActual=yInicio+1;
	procesandoEvento = true;	
	return false;	
}
function paneoMover(e)
{
	if (procesandoEvento) {
		getMapPixels(e);
		if (xActual >= 0 && xActual <= imagenMapa.imagen.width
			&& yActual >= 0 && yActual <= imagenMapa.imagen.height) {
		
			xMove = xActual-xInicio;
			yMove = yActual-yInicio;
			cLeft = -xMove;
			cTop = -yMove;
			cRight = imagenMapa.imagen.width;
			cBottom = imagenMapa.imagen.height;
			if (xMove>0) {
				cLeft = 0;
				cRight = imagenMapa.imagen.width - xMove;
			}
			if (yMove>0) {
				cTop = 0;
				cBottom = imagenMapa.imagen.height - yMove;
			}
			cortarLayer("movible",cLeft,cTop,cRight,cBottom);
			moverLayer("movible",xMove,yMove,"imagenFija");			
		}
		return false;
	}
}


function paneoArriba(e)
{
	if (procesandoEvento) 
	{
		procesandoEvento = false;
		/*Llamado de actualización de mapa*/
		actualizarParametros(document.accion, xActual-xInicio, yActual-yInicio, 0, 0);
	}
}

function getRadioVal(rb)
{	
	return rb.value;
	var L=rb.length;
	var ret="";
	for (var i = 0 ; i< L ; i++)
	{ 
	 if(rb[i].checked) 
	 { 
	 	ret=rb[i].value; 
		return(ret);
	 } 
	}
	return(ret);
}

function toggleSinRefresh()
{
	apagarToc();
	var encender = imagenMapa.contenido;
	if (document.getElementById(encender) != null)
	{		
		document.getElementById(encender).className = "pestanaA";
	}
}

function apagarToc()
{		
	for(i=0;i<pestanas.length;i++)
	{	
		document.getElementById(pestanas[i]).className = "pestanaI";		
	}	
}	
function cambiarToc(contenido)
{
	imagenMapa.contenido = contenido;
	toggle();
}

function recuadroAbajo(e)
{
	//envelope coordinates in pixels
	//Absolute for dynamically locating the div in the browser
	//Relative for converting to map coordinates on the server

	getPagePixels(e);
	xInicio = xActual;
	yInicio = yActual;

	getMapPixels(e); 
	xImagen = xActual;
	yImagen = yActual;

	//switch the procesandoEvento flag so it will be rendered dynamically
	procesandoEvento = true;

	var recuadro = getLayer("recuadro");	
    recuadro.borderColor =  "red";
    recuadro.borderStyle =  "solid";
    recuadro.borderWidth =  "1px";
	
	//locate the div and initialize its dimensions
	recuadro.left = xInicio;
	recuadro.top = yInicio;
	recuadro.height = "0";
	recuadro.width = "0";
	recuadro.visibility = "visible";
}
function recuadroMover(e)
{
	if (procesandoEvento) 
	{			
		var recuadro = getLayer("recuadro");
		getPagePixels(e);
		//store the current cursor coordinates		
		absDestX = xActual;
		absDestY = yActual;

		//height and width will always be the absolute value of the difference in client coordinates
		var height = Math.abs(yInicio - absDestY);
		var width = Math.abs(xInicio - absDestX);
		recuadro.height = height;
		recuadro.width  = width;	

		//branch on the cartesian coordinate system quadrants relative to origin
		if (absDestX > xInicio && absDestY <= yInicio ) // I
		{
		   recuadro.left = xInicio;
		   recuadro.top  = absDestY;
		}
		else if (absDestX < xInicio && absDestY <= yInicio) // II
		{
		   recuadro.left = absDestX;
		   recuadro.top  = absDestY;
		}
		else if (absDestX < xInicio && absDestY >= yInicio) // III
		{
		   recuadro.left = absDestX;
		   recuadro.top  = yInicio;
		}
		else if (absDestX > xInicio && absDestY >= yInicio) // IV
		{
		   recuadro.left = xInicio;
		   recuadro.top  = yInicio;
		}	
	}
}
function recuadroArriba(e)
{
	if (procesandoEvento) 
	{
		getMapPixels(e);
		var recuadro = getLayer("recuadro");
		procesandoEvento = false;
		recuadro.visibility = "hidden";		
		actualizarParametros(document.accion, xImagen, yImagen, xActual, yActual);
	}
}


function getLayer(nombre)
{
	var theObj = document.getElementById(nombre);
	if (theObj)
		return theObj.style;
	else
		return null;
}

function getAbsX(elt)
{ 
	return (elt.x) ? elt.x : getAbsPos(elt,"Left"); 
}
function getAbsY(elt)
{ 
	return (elt.y) ? elt.y : getAbsPos(elt,"Top"); 
}

function getAbsPos(elt,which) {
	iPos = 0;
	while (elt != null) {
		iPos += elt["offset" + which];
		elt = elt.offsetParent;
	}
	return iPos;
}


	function getMapPixels(e) {
	if (navigator.appName.indexOf("Netscape")>=0) {
		xActual=e.pageX;
		yActual=e.pageY;
	} else {
		xActual=event.clientX + document.body.scrollLeft;
		yActual=event.clientY + document.body.scrollTop;
	}
	// subtract offsets from page left and top
	var layerPositioner = document.getElementById("imagenFija");
	xActual = xActual-getAbsX(layerPositioner);
	yActual = yActual-getAbsY(layerPositioner);
	
	// we don't want to go outside our defined area
	xActual = Math.max(0,xActual);
	xActual = Math.min(xActual,imagenMapa.imagen.width);
	yActual = Math.max(0,yActual);
	yActual = Math.min(yActual,imagenMapa.imagen.height);
}

	function validarEstadoSinForma()
	{
		if (estado)
		{			
			centrarEspera();
			return true;
		}
		else
		{
			alert("No se ha recibido respuesta del servidor, espere un momento");
			return false;			
		}
	}

	function validarEstado (forma)
	{		
		if (estado)
		{
			estado = false;
			centrarEspera();
			if(document.all)
			{
		        //thisbrowser="ie"
				document.all.pleasewaitScreen.style.visibility="visible";
		    }
		    if(!document.all && document.getElementById)
		    {
		        //thisbrowser="NN6";
				document.getElementById("pleasewaitScreen").style.visibility="visible";
		    }
			
			forma.submit();
			return true;
		}
		else
		{
			alert("No se ha recibido respuesta del servidor, espere un momento");
			return false;			
		}
	}
	
	function actualizarParametros(forma, cuadroMenorX, cuadroMenorY, cuadroMayorX, cuadroMayorY)
	{
		forma.cuadroMenorX.value = cuadroMenorX;
		forma.cuadroMenorY.value = cuadroMenorY;
		forma.cuadroMayorX.value = cuadroMayorX;
		forma.cuadroMayorY.value = cuadroMayorY;		
	
		validarEstado(forma);
	}

	function actualizarTodo(forma, accion, cuadroMenorX, cuadroMenorY, cuadroMayorX, cuadroMayorY)
	{
		forma.mapaction.value = accion;
		actualizarParametros(forma, cuadroMenorX, cuadroMenorY, cuadroMayorX, cuadroMayorY)
	}

function getPagePixels(e)
{
	if (navigator.appName.indexOf("Netscape")>=0) 
	{
		xActual=e.pageX;
		yActual=e.pageY;
	} else {
		xActual=event.clientX + document.body.scrollLeft;
		yActual=event.clientY + document.body.scrollTop;
	}
}
function restaurarImagen(nombre)
{
	var layer = getLayer(nombre);		
	if (layer != null)
		layer.clip = 'rect(0,'+ imagenMapa.imagen.width +', '+ imagenMapa.imagen.height +', 0)';
}

function cortarLayer(nombre, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(nombre);		
	if (layer != null)
		layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

function moverLayer(nombre, x, y, imagen)
{	
  	var layer = getLayer(nombre);		
	if (layer != null) {
		var layerPositioner = document.getElementById(imagen);
   		layer.left = (x+getAbsX(layerPositioner)) + "px";
		layer.top  = (y+getAbsY(layerPositioner)) + "px";
	}
}

function centrarEspera()
{
	moverLayer("pleasewaitScreen",((imagenMapa.imagen.width)/2 - (document.getElementById("tablaEspera").width)/2 ),((imagenMapa.imagen.height)/2 - (document.getElementById("tablaEspera").height)/2),"imagenFija");
}