var ancho;
var alto;
// para zoomventana
var sImageName = "img";
var oImageInfo     = { x : -1, y : -1, w : -1, h : -1 };
var oSelectInfo    = { x1 : -1, y1 : -1, x2 : -1, y2 : -1 };
var bDown          = false;
var bMustSetValues = false;
var MAPA_ANCHO;
var MAPA_ALTO; 
var LEFT_TO_MAPA = 0;
var TOP_TO_MAPA = 0;
var MAPA_TO_RIGHT = 42;
var mouseX;
var mouseY;

function inicio() {
	asignar_divs();	
	if ((parent.frames("Mapa").mapserv.CMD.value=='zoomvenin')||(parent.frames("Mapa").mapserv.CMD.value=='zoomvenout'))
	{
		zoomventana();
	}
}
	
function pan(direction) {    
	var x, y;
	document.mapserv.CMD.value="pan"; // force pan
	document.mapserv.CAPAS.value='0';
	document.mapserv.mode.value='browse';
	document.mapserv.elpan.value = direction;
	document.mapserv.submit();
}
function do_pan()
{
	document.BTzoomdir_in.src = '../images/zoomin_up.png';
	document.BTzoomdir_out.src = '../images/zoomout_up.png';
	document.BTzoomdirin_ven.src = '../images/zoomvenin_up.png';
	document.BTzoomdirout_ven.src = '../images/zoomvenout_up.png';	
	document.BTcentrar.src = '../images/centrar_up.png';
	document.BTimprimir.src = '../images/pan_btn_down.png';
	window.parent.Mapa.dragobject.initialize();
	
}
function do_zoomout() {
	document.BTzoomdir_in.src = '../images/zoomin_up.png';
	document.BTzoomdir_out.src = '../images/zoomout_down.png';
	document.BTzoomdirin_ven.src = '../images/zoomvenin_up.png';
	document.BTzoomdirout_ven.src = '../images/zoomvenout_up.png';	
	document.BTcentrar.src = '../images/centrar_up.png';
	document.BTimprimir.src = '../images/pan_btn_up.png';
	
	parent.frames("Mapa").mapserv.mode.value='browse';
	//document.mapserv.CAPAS.value='0';
	parent.frames("Mapa").mapserv.CMD.value='zoomout';
	window.status = '==> Pincha en un punto para alejar'; 
	//document.forms[0].submit();
}
function do_zoomin(){
	document.BTzoomdir_in.src = '../images/zoomin_down.png';
	document.BTzoomdir_out.src = '../images/zoomout_up.png';
	document.BTzoomdirin_ven.src = '../images/zoomvenin_up.png';
	document.BTzoomdirout_ven.src = '../images/zoomvenout_up.png';	
	document.BTcentrar.src = '../images/centrar_up.png';
	document.BTimprimir.src = '../images/pan_btn_up.png';
	parent.frames("Mapa").mapserv.CMD.value='zoomin';
	//document.mapserv.CAPAS.value='0';
	parent.frames("Mapa").mapserv.mode.value='browse';
	window.status = '==> Pincha en un punto para aproximar';  
}
function do_zoomvenin(){
	parent.frames("Funciones").BTzoomdir_in.src = '../images/zoomin_up.png';
	parent.frames("Funciones").BTzoomdir_out.src = '../images/zoomout_up.png';
	parent.frames("Funciones").BTzoomdirin_ven.src = '../images/zoomvenin_down.png';
	parent.frames("Funciones").BTzoomdirout_ven.src = '../images/zoomvenout_up.png';	
	parent.frames("Funciones").BTcentrar.src = '../images/centrar_up.png';
	parent.frames("Funciones").BTimprimir.src = '../images/pan_btn_up.png';
	parent.frames("Mapa").mapserv.CMD.value='zoomvenin';
	//document.mapserv.CAPAS.value='0';
	parent.frames("Mapa").mapserv.mode.value='browse';
	//window.status = '==> Pincha en un punto y arrastra para aproximar';  
	zoomventana();		
}
function do_zoomvenout(){
	parent.frames("Funciones").BTzoomdir_in.src = '../images/zoomin_up.png';
	parent.frames("Funciones").BTzoomdir_out.src = '../images/zoomout_up.png';
	parent.frames("Funciones").BTzoomdirin_ven.src = '../images/zoomvenin_up.png';
	parent.frames("Funciones").BTzoomdirout_ven.src = '../images/zoomvenout_down.png';	
	parent.frames("Funciones").BTcentrar.src = '../images/centrar_up.png';
	parent.frames("Funciones").BTimprimir.src = '../images/pan_btn_up.png';
	parent.frames("Mapa").mapserv.CMD.value='zoomvenout';
	//document.mapserv.CAPAS.value='0';
	parent.frames("Mapa").mapserv.mode.value='browse';
	//window.status = '==> Pincha en un punto y arrastra para aproximar';  
	zoomventana();		
}
function do_centrar(){
	document.BTzoomdir_in.src = '../images/zoomin_up.png';
	document.BTzoomdir_out.src = '../images/zoomout_up.png';
	document.BTzoomdirin_ven.src = '../images/zoomvenin_up.png';
	document.BTzoomdirout_ven.src = '../images/zoomvenout_up.png';	
	document.BTcentrar.src = '../images/centrar_down.png';
	document.BTimprimir.src = '../images/pan_btn_up.png';
	parent.frames("Mapa").mapserv.CMD.value='centrar';
	//document.mapserv.CAPAS.value='0';
	//window.status = '==> Pincha en un punto para centrar';
	parent.frames("Mapa").mapserv.mode.value='browse';
}
function volver(){
	
	document.BTzoomdir_in.src = '../images/zoomin_up.png';
	document.BTzoomdir_out.src = '../images/zoomout_up.png';
	document.BTzoomdirin_ven.src = '../images/zoomvenin_up.png';
	document.BTzoomdirout_ven.src = '../images/zoomvenout_up.png';	
	document.BTcentrar.src = '../images/centrar_up.png';
	document.BTimprimir.src = '../images/pan_btn_up.png';
	
	parent.frames("Mapa").mapserv.CMD.value='volver';
	//document.mapserv.CAPAS.value='0';
	parent.frames("Mapa").mapserv.mode.value='browse';
	parent.frames("Mapa").mapserv.submit();
}
		
function mostrarcoord(){
	var offsetInfo = "";
	offsetInfo = "The x coordinate is: " + window.event.offsetX + "\r";
	offsetInfo += "The y coordinate is: " + window.event.offsetY + "\r";
	parent.frames("Mapa").mapserv.coodx.value=window.event.offsetX;
	parent.frames("Mapa").mapserv.coody.value=window.event.offsetY;
}	
	
function VerOcultar(){
	parent.frames("Mapa").mapserv.CAPAS.value="capas";
	parent.frames("Mapa").mapserv.CMD.value=0;
	parent.frames("Mapa").mapserv.submit();
}

function actualizarcapas()
{
	parent.frames("Mapa").mapserv.CAPAS.value="capario";
	parent.frames("Mapa").mapserv.submit();
}

// ----060328 JCS-------- funciones para el Zoom ventana ----------------------------
function findObjectPosition(oTmp){
		var oPosition = { x : 0, y : 0 };
		while (oTmp.offsetParent){
				oPosition.x += oTmp.offsetLeft
				oPosition.y += oTmp.offsetTop
				oTmp = oTmp.offsetParent;
		}
		return oPosition;
}

function getMouse(e){
	if (document.all){
		mouseY = event.clientY + parent.frames("Mapa").document.body.scrollTop;
		mouseX = event.clientX + parent.frames("Mapa").document.body.scrollLeft;
	} 
	else {
		mouseY = e.pageY;
		mouseX = e.pageX;
	}
}

function showSelect() {
  parent.frames("Mapa").document.getElementById("selectLayer").style.visibility="visible"; 
}

function hideSelect() {
  parent.frames("Mapa").document.getElementById("selectLayer").style.visibility="hidden";
}

function redrawSelect() {
//solo pinta si es zoom ventana
if ((parent.frames("Mapa").mapserv.CMD.value=="zoomvenin")||(parent.frames("Mapa").mapserv.CMD.value=="zoomvenout")){
  var oTmp = parent.frames("Mapa").document.getElementById("selectLayer");
  parent.frames("Mapa").document.mapserv.imageX.value = (oSelectInfo.x1 < oSelectInfo.x2 ? oSelectInfo.x1 : oSelectInfo.x2) - oImageInfo.x;
  parent.frames("Mapa").document.mapserv.imageY.value = (oSelectInfo.y1 < oSelectInfo.y2 ? oSelectInfo.y1 : oSelectInfo.y2) - oImageInfo.y;
  parent.frames("Mapa").document.mapserv.imageW.value = Math.abs(oSelectInfo.x2 - oSelectInfo.x1);
  parent.frames("Mapa").document.mapserv.imageH.value = Math.abs(oSelectInfo.y2 - oSelectInfo.y1);
  oTmp.style.top  = (oSelectInfo.y1 < oSelectInfo.y2 ? oSelectInfo.y1 : oSelectInfo.y2) - (parent.frames("Mapa").document.all ? 0 : 1);
  oTmp.style.left = (oSelectInfo.x1 < oSelectInfo.x2 ? oSelectInfo.x1 : oSelectInfo.x2) - (parent.frames("Mapa").document.all ? 0 : 1);
  // abajo resto 1px, para q al hacer el click no clique sobre la capa recuadro, y 
  // si clique sobre el mapa y asi salta el img_click del mapa
  oTmp.style.width  = Math.abs(oSelectInfo.x2 - oSelectInfo.x1 -1);
  oTmp.style.height = Math.abs(oSelectInfo.y2 - oSelectInfo.y1 -1);
  }
}

function down(e){
	if((parent.frames("Mapa").document.layers && e.which!=1) || (parent.frames("Mapa").document.all && event.button!=1)) return true;
	bDown = true;
	bMustSetValues = true;
	getMouse(e);
	if (mouseX < oImageInfo.x) mouseX = oImageInfo.x;
	if (mouseX > (oImageInfo.x + oImageInfo.w)) mouseX = (oImageInfo.x + oImageInfo.w);
	if (mouseY < oImageInfo.y) mouseY = oImageInfo.y;
	if (mouseY > (oImageInfo.y + oImageInfo.h)) mouseY = (oImageInfo.y + oImageInfo.h);
//	alert(mouseX + " , " + mouseY);
  return false;
}

function move(e){
  if (bDown) {
    if (bMustSetValues) {
        //recoge valores de inicio
        oSelectInfo.x1 = mouseX;
	    oSelectInfo.y1 = mouseY;
	    bMustSetValues = false;
    }
    getMouse(e);
    if (mouseX < oImageInfo.x) mouseX = oImageInfo.x-1;
	if (mouseX > (oImageInfo.x + oImageInfo.w)) mouseX = (oImageInfo.x + oImageInfo.w)+1;
	if (mouseY < oImageInfo.y) mouseY = oImageInfo.y-1;
	if (mouseY > (oImageInfo.y + oImageInfo.h)) mouseY = (oImageInfo.y + oImageInfo.h)+1 ;
	// recoge valores de fin entre 298y298+792; 150y150+405
	oSelectInfo.x2 = mouseX;
	oSelectInfo.y2 = mouseY;
 	redrawSelect();
 	showSelect();
   }
	if(document.all) return false;
}

function up(e){
  var lamano = parent.frames("Mapa").document.getElementById(sImageName);
  lamano.className = 'micursor2';
  bDown = false;
  var oTmp = { x : mouseX, y : mouseY};
  getMouse(e);
	if (mouseX == oTmp.x && mouseY == oTmp.y) hideSelect;
	
	//ordenar los valores recogidos
	if (oSelectInfo.x1 > oSelectInfo.x2) 
	{
		zminX = oSelectInfo.x2;
		zmaxX = oSelectInfo.x1;
	}
	else
	{
		zminX = oSelectInfo.x1;
		zmaxX = oSelectInfo.x2;
	}
	if (oSelectInfo.y1 > oSelectInfo.y2)
	{
		zminY = oSelectInfo.y2;
		zmaxY = oSelectInfo.y1;
	}
	else
	{
		zminY = oSelectInfo.y1;
		zmaxY = oSelectInfo.y2;
	}
	//pasar los valores a la pagina PHP 	
	parent.frames("Mapa").document.mapserv.zx1.value = zminX - LEFT_TO_MAPA;
	parent.frames("Mapa").document.mapserv.zx2.value = zmaxX - LEFT_TO_MAPA;
	parent.frames("Mapa").document.mapserv.zy1.value = zminY - TOP_TO_MAPA;
	parent.frames("Mapa").document.mapserv.zy2.value = zmaxY - TOP_TO_MAPA;
	parent.frames("Funciones").BTzoomdirin_ven.src = '../images/zoomvenin_up.png';
	parent.frames("Funciones").BTzoomdirout_ven.src = '../images/zoomvenout_up.png';
	//if ((oSelectInfo.x1 > 298) && (oSelectInfo.x2 < (298 + MAPA_ANCHO)) && (oSelectInfo.y1 > 150) && (oSelectInfo.y2 < (150 + MAPA_ALTO))) 
	if ((zminX >= LEFT_TO_MAPA) && (zmaxX <= (LEFT_TO_MAPA + MAPA_ANCHO)) && (zminY >= TOP_TO_MAPA) && (zmaxY <= (TOP_TO_MAPA + MAPA_ALTO))) 
     {
		
		if ((document.mapserv.zx1.value!=0) || (document.mapserv.zx2.value!=0) || (document.mapserv.zy1.value!=0) || (document.mapserv.zy2.value!=0))   
		{
			// solo se recarga si la pulsacion ha sido dentro de la imagen del mapa
			recargar();
			
		}
    }  
	return true;
}

function recargar(){
	document.mapserv.submit(); //El submit no es necesario, porque al pinchar sobre el mapa, siempre se recarga.
		
}

function zoomventana() {
  if (document.getElementById) {
  //if (parent.frames("Mapa").getElementById){
	//var oTmp = document.getElementById(sImageName)
	var oTmp = parent.frames("Mapa").document.getElementById(sImageName)
	//alert(parent.frames("Mapa").document.getElementById(sImageName));
    var oPosition = findObjectPosition(oTmp);
    oImageInfo.x = oPosition.x;
    oImageInfo.y = oPosition.y;
    oImageInfo.w = oTmp.width;
    oImageInfo.h = oTmp.height;  
    parent.frames("Mapa").document.onmousedown = down;
  	parent.frames("Mapa").document.onmousemove = move;
  	parent.frames("Mapa").document.onmouseup = up;
  	} 
  	else {
    alert("Your browser is not campatible with this script")
  }  

}

function botonzoomvenin()
{
	window.parent.Mapa.zventanain();
}

function botonzoomvenout()
{
	window.parent.Mapa.zventanaout();
}

// ----fin funciones para Zoom Ventana


