﻿//var FlotanteHeight, FlotanteWidth;
var prm;
var layer;
    
function load()
{
//q(LtnWebViewer1);
     LtnWebViewer1.notifyLoad(onVisorloaded);
    LtnWebViewer1.notifyError(onVisorError);
    
   objCoordenadas = GE("divCoordenadas");

    LtnWebViewer1.AttachMouseMove(mouseMove);
    LtnWebViewer1.AttachChangeScale(changeScale);
    
    SetCargando(false);
    
    //prm = Sys.WebForms.PageRequestManager.getInstance();
    //prm.add_initializeRequest(InitializeRequest);
    //prm.add_endRequest(EndRequest);
    
    window.setInterval(callVisitas, 2*60000);
	callVisitas();
	
	 
	 
	  try{
	    window.moveTo(0,0);
            window.resizeTo(window.screen.width,screen.availHeight);
        }catch(Err){}
   
    FlotanteHeight = parseInt(screen.height * 0.6, 10);
    
    FlotanteWidth = parseInt(screen.width - 75, 10);
    
    try{
        $find("DragPanelExtender1").set_location( new Sys.UI.Point(window.screen.width-188,233));
        $find("DragPanelExtender4").set_location( new Sys.UI.Point(window.screen.width-218,55));
        $find("DragPanelExtender5").set_location( new Sys.UI.Point(window.screen.width-190,170));
       }catch(Erro){}
 
}
var beh;
function onVisorloaded()
{
    //alert("onVisorloaded");
    LtnWebViewer1.removeSystemBehavior(WVBehaviorType.UTMCOORDS);
    setInterval(VisorActual,5*60000);

     beh = new MiComportamiento();
    beh.init(LtnWebViewer1);
    LtnWebViewer1.addCustomBehavior(beh);
    
   
   
    /*LtnWebViewer1.subscribeEvent(WVEventType.ICON_CLICK, iconClick);
    LtnWebViewer1.subscribeEvent(WVEventType.ICON_OVER, iconOver);
    LtnWebViewer1.subscribeEvent(WVEventType.ICON_OUT, iconOut);*/
    
    layer = LtnWebViewer1.createCustomLayer("test");
    
    var x = gup( 'x' );
    var y = gup( 'y' );
    var es = gup( 'scale' );
    
    var bus = gup( 'buscar' );
    
    if(x!="" && y!=""){
        x=parseFloat(x,10)*1000;
        y=parseFloat(y,10)*1000;
        LtnWebViewer1.locate(x,y);
    }
    if(es!=""){
        es=parseFloat(es,10)*10;
        LtnWebViewer1.setScale(es);
     }
    cambiarEnlace();
    
    
    if(bus!=""){
        bus= unescape(bus);
        buscar(bus);
        GE('txbBuscar').value=bus;
    }
    
    //GE("PanelOrto").style.right="3%";
    
}



function VisorActual(){
//Mantenemos el visor vivo
    LtnWebViewer1.ping(); 
}

function onVisorError() { alert("onVisorError"); }

function clickPrueba()
{
    var img = document.createElement('IMG');
    img.style.width="30";
    img.style.height="30";
    img.src="images/interfaz/informacion.jpg";
    var layer = LtnWebViewer1.createCustomLayer("test");
    layer.addItem(img, LtnWebViewer1.getCenterUtmX(), LtnWebViewer1.getCenterUtmY());
    
    //lockControllers()
    //unlockControllers()
    
    //LtnWebViewer1.refreshSystemLayer(WVLayerType.VECTOR);
}

function iconClick(id, layer, filepos)
{
/*    if(LtnWebViewer1!=null && LtnWebViewer1.getSystemBehavior(WVBehaviorType.ZOOM_POWER_TWO))
    {
    (new IMGButtonManager('BG37')).setMarked();
    LtnWebViewer1.setActiveBehavior(LtnWebViewer1.getSystemBehavior(WVBehaviorType.ZOOM_POWER_TWO));
    LtnWebViewer1.getSystemBehavior(WVBehaviorType.ZOOM_POWER_TWO).setDirection(WVZoomOp.IN);
    LtnWebViewer1.getSystemBehavior(WVBehaviorType.ZOOM_POWER_TWO).beginScale();

    LtnWebViewer1.getSystemBehavior(WVBehaviorType.ZOOM_POWER_TWO).endScale();
    (new IMGButtonManager('BG37')).setUnmarked();

    LtnWebViewer1.getBrowser().stopEvent();
    }
*/   
//alert(id+" "+layer+" "+filepos);
getIconOver(id, layer);
}
function iconOver(obj, id, layer, filepos)
{
status = "OVER = "+id+" "+layer+" "+filepos;
}
function iconOut(obj, id, layer, filepos)
{
status = "OUT = "+id+" "+layer+" "+filepos;
}

function seleccionar(ltnid)
{
    //SetCargando(true);
    var ret = WebService.SetLtnId(ltnid, OnCompleteseleccionar, OnErrorBuscar, OnErrorBuscar);
}

function OnCompleteseleccionar(arg)
{
    if(arg!=null && arg!="")
        LtnWebViewer1.refreshSystemLayer(WVLayerType.VECTOR);
    SetCargando(false);
}

function buscar(value)
{
    //SetCargando(true);
     GE("DR").innerHTML = '<center><img src="images/interfaz/loading_animation_liferay.gif" alt="cargando..." ismap="isMap"/></center>';
           GE("DR").style.height="0px";
    
    var ret = WebService.GetResults(value, OnCompleteBuscar, OnErrorBuscar, OnErrorBuscar);
}

function leerXML(text)
{
    var xmlDoc = null;
    try //Internet Explorer
      {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(text);
      }
    catch(e)
      {
      xmlDoc = null;
      try //Firefox, Mozilla, Opera, etc.
        {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(text,"text/xml");
        }
      catch(e) {
        xmlDoc = null;
        alert("error leyendo xml: "+e.message)
        }
      }
      return xmlDoc;
}

var entraGup=false;
function OnCompleteBuscar(arg)
{
    
    //alert("OnCompleteBuscar "+arg);
    var s = "";
    var minx=9999999999;
    var miny=9999999999;
    var maxx=0;
    var maxy=0;
    var minXaux;
    var minYaux;
    var maxXaux;
    var maxYaux;
    var entra=false;
    
    var xml = leerXML(arg);
    if (xml)
    {
        opt = xml.getElementsByTagName('salida');
        if(opt)
        {
            layer.clear();
            for(var i=0;i<opt.length;i++)
            {
                if(opt[i].firstChild)
                for(var j=0;j<opt[i].firstChild.childNodes.length;j++)
                {
                    entra=true;
                    
                    if((gup("idFicha")=="") || (entraGup) || (opt[i].firstChild.nodeName,opt[i].firstChild.childNodes[j].getAttribute('id')== gup("idFicha")))
                    {
                        
                      // s += "<tr>";
    //                    s += "<td><img src=\"" + getIcono(opt[i].firstChild.childNodes[j]) +"\"/></td>";
    //                    s += "<td>"+trans(opt[i].firstChild.nodeName,opt[i].firstChild.childNodes[j])+"</td>";
    //                    s += "</tr>";
                        s += "<img src=\"" + getIcono(opt[i].firstChild.childNodes[j]) +"\" style=\"vertical-align:middle; padding:3px\" width='20' />";
                        s += ""+trans(opt[i].firstChild.nodeName,opt[i].firstChild.childNodes[j])+"";
                        s += "<br/>\n\r";
                        
                        var aux= trans2(opt[i].firstChild.nodeName,opt[i].firstChild.childNodes[j]).split('#');
                        minXaux=aux[0];
                        minYaux=aux[1];
                        maxXaux= aux[2];
                        maxYaux= aux[3];
                    
                    
                        
                        if(aux[4]!="NO"){
                        
                           if(minXaux!=null && minXaux < minx) 
                                minx=minXaux;
                                
                           if(minYaux!=null &&  minYaux < miny) 
                                miny=minYaux;
                           
                           if(maxXaux!=null && maxXaux > maxx) 
                                maxx=maxXaux;
                           
                           if(maxYaux!=null &&  maxYaux > maxy) 
                                maxy=maxYaux;
                     }
                     
                     entraGup=true;
                    }  
                }
            }
           
            minx=parseFloat(minx,10)-0;
            maxx=parseFloat(maxx,10)+ 0;
            miny=parseFloat(miny,10)- 0;
            maxy=parseFloat(maxy,10)+ 0;
            
            //alert(minx+"#"+miny+"#"+maxx+"#"+maxy);
           if(entra && minx!="9999999999" && minx!="")
            goMinMax(minx+"#"+miny+"#"+maxx+"#"+maxy,null);
            
            
            LtnWebViewer1.refreshSystemLayer(WVLayerType.ICON);
            LtnWebViewer1.refreshSystemLayer(WVLayerType.VECTOR);
                
        }
    }
    
    if(entra){

      GE("DR").style.height=( LtnWebViewer1.getHeight()-235)+"px";
      GE("DR").style.overflow="auto";
      GE("DR").innerHTML = "<table border='0'>"+s+"</table>";
    }else{
      GE("DR").innerHTML = '';
      GE("DR").style.height="0px";
    }
    SetCargando(false);
}


function devolverPosMinMax(o){



}

function getIcono(o)
{
    var ic = o.getAttribute('Icono');
    return ((!ic)?"http://www.geonet.es/callejero/images/calle.gif" : "" + ic);
}


function trans2(name, o)
{
    var s="";
    var x = o.getAttribute('x'); 
    var y = o.getAttribute('y');
    var ltnid = o.getAttribute('LtnId');
    var minx = o.getAttribute('minx');
    var href = o.getAttribute('href');
 
    switch(name)
    {
        case "Poblaciones":
            
             return minx + "#" + o.getAttribute('miny') + "#" + o.getAttribute('maxx') + "#" + o.getAttribute('maxy');
            break;
        case "PoblacionesCP":
            return  minx + "#" + o.getAttribute('miny') + "#" + o.getAttribute('maxx') + "#" + o.getAttribute('maxy') ;
            break;
        case "TramosCP":
            return  minx + "#" + o.getAttribute('miny') + "#" + o.getAttribute('maxx') + "#" + o.getAttribute('maxy') ;
            break;
        case "Tramos":
            return "0#0#0#0";
            break;
        case "TramosLibres":
            
            return o.getAttribute('minx')+ "#" +o.getAttribute('miny')+ "#" +o.getAttribute('maxx')+ "#" +o.getAttribute('maxy')+ "#NO" ;
            break;
        case "TramosExactos":
            return minx + "#" + o.getAttribute('miny') + "#" + o.getAttribute('maxx') + "#" + o.getAttribute('maxy');
            break;
        case "resultados":
            return x + "#" + y + "#"+x + "#" + y; 
            break;
    }
    
        
    return s;
}

function trans(name, o)
{
    var s="";
    var x = o.getAttribute('x'); 
    var y = o.getAttribute('y');
    var ltnid = o.getAttribute('LtnId');
    var minx = o.getAttribute('minx');
    var href = o.getAttribute('href');
 
    switch(name)
    {
        case "Poblaciones":
            s += (minx && minx!="") ? ("<a style=\"font-size:10pt\" href=\"javascript:goMinMax('" + minx + "#" + o.getAttribute('miny') + "#" + o.getAttribute('maxx') + "#" + o.getAttribute('maxy') + "'," + ((ltnid && ltnid!="") ? ltnid : "null") + ")\">" + o.getAttribute('titulo') + "</a>") : ""+o.getAttribute('titulo');
            s += " ("+o.getAttribute('Muni')+")";
            break;
        case "PoblacionesCP":
            s += (minx && minx!="") ? ("<a href=\"javascript:goMinMax('" + minx + "#" + o.getAttribute('miny') + "#" + o.getAttribute('maxx') + "#" + o.getAttribute('maxy') + "'," + ((ltnid && ltnid!="") ? ltnid : "null") + ")\">" + o.getAttribute('titulo') + "</a>") : ""+o.getAttribute('titulo');
            s += " ("+o.getAttribute('Muni')+") CP:"+o.getAttribute('CodigoPostal');
            break;
        case "TramosCP":
            //s += "<a href=\"javascript:goMinMaxLtnIds('" + getLtnIds(o) + "')\">" + o.getAttribute('titulo') + "</a> ("+o.getAttribute('Muni')+") CP:"+o.getAttribute('CodigoPostal');
            s += (minx && minx!="") ? ("<a href=\"javascript:goMinMax('" + minx + "#" + o.getAttribute('miny') + "#" + o.getAttribute('maxx') + "#" + o.getAttribute('maxy') + "'," + ((ltnid && ltnid!="") ? ltnid : "null") + ")\">" + o.getAttribute('titulo') + "</a>") : ""+o.getAttribute('titulo');
            s += " ("+o.getAttribute('Muni')+") CP:"+o.getAttribute('CodigoPostal');
            break;
        case "Tramos":
            s += "<a href=\"javascript:goMinMaxLtnIds('" + getLtnIds(o) + "')\">" + o.getAttribute('titulo') + "</a> ("+o.getAttribute('Muni')+") CP:"+o.getAttribute('CodigoPostal');
            break;
        case "TramosLibres":
            s += "<a href=\"javascript:goMinMaxLtnIds('" + getLtnIds(o) + "')\">" + o.getAttribute('titulo') + "</a> ("+o.getAttribute('Muni')+") CP:"+o.getAttribute('CodigoPostal');
            break;
        case "TramosExactos":
            s += (minx && minx!="") ? ("<a href=\"javascript:goMinMax('" + minx + "#" + o.getAttribute('miny') + "#" + o.getAttribute('maxx') + "#" + o.getAttribute('maxy') + "'," + ((ltnid && ltnid!="") ? ltnid : "null") + ")\">" + o.getAttribute('titulo') + "</a>") : ""+o.getAttribute('titulo');
            s += " ("+o.getAttribute('Muni')+") CP:"+o.getAttribute('CodigoPostal');
            break;
        case "resultados":
            s += (x && x!="") ? ("<a  href=\"javascript:goXYID(" + x + "," + y + ","+((ltnid && ltnid!="")?ltnid:"null")+")\">" + o.getAttribute('titulo') + "</a>" ) : ""+o.getAttribute('titulo');
            s +=  " ("+o.getAttribute('NombreTipo')+")";
           
            s += (href && href!="") ? " <img src='images/interfaz/informacion.gif' style='cursor:pointer' title='Abrir ficha en ventana aparte' url="+href+" onclick='abrirVentana(this.url)' />" : "";
            
            break;
    }
    
    if(x && x!="" && y && y !="")
    {
        layer.addItem(nuevaFoto(getIcono(o), o.getAttribute('titulo'), o.getAttribute('href')), x, y);
        //LtnWebViewer1.PutTack(x, y, "X="+x+" Y="+y);
    }
    
    return s;
}

function getLtnIds(o)
{
    var ob, minx, miny, maxx, maxy;
    var mminx=999999999, mminy=9999999999, mmaxx=0, mmaxy=0;
    var s = "";
    for(var i=0;i<o.childNodes.length;i++)
    {
        ob = o.childNodes[i];
        minx = parseFloat(ob.getAttribute('minx'));
        miny = parseFloat(ob.getAttribute('miny'));
        maxx = parseFloat(ob.getAttribute('maxx'));
        maxy = parseFloat(ob.getAttribute('maxy'));
        if(minx<mminx) mminx = minx;
        if(miny<mminy) mminy = miny;
        if(maxx>mmaxx) mmaxx = maxx;
        if(maxy>mmaxy) mmaxy = maxy;
        s += parseFloat(ob.getAttribute('LtnId'))+";";
    }
    if(o.childNodes.length==0)
    {
        ob = o;
        minx = parseFloat(ob.getAttribute('minx'));
        miny = parseFloat(ob.getAttribute('miny'));
        maxx = parseFloat(ob.getAttribute('maxx'));
        maxy = parseFloat(ob.getAttribute('maxy'));
        if(minx<mminx) mminx = minx;
        if(miny<mminy) mminy = miny;
        if(maxx>mmaxx) mmaxx = maxx;
        if(maxy>mmaxy) mmaxy = maxy;
        s += parseFloat(ob.getAttribute('LtnId'))+";";
    }
    s += s.substr(0, s.length-1); //le quito el último ';'
    s += "|"+mminx+"#"+mminy+"#"+mmaxx+"#"+mmaxy;
    return s;    
}

function goMinMaxLtnIds(cad)
{
    var o = cad.split('|');
    if(o.length>1)
    {
        //alert(o[1]+"  --  "+o[0]);
        goMinMax(o[1], o[0]);
    }
}

function nuevaFoto(src, title, click)
{
    var img = document.createElement('IMG');
    img.style.width="20";
    img.style.height="20";
    img.src = (src)?src:"images/interfaz/informacion.jpg";
    img.title = ""+title;
    img.style.cursor = "pointer";
    img.onclick = function() { abrirVentana(click);}
    img.onmouseout   = function() { this.style.width="20";this.style.height="20"; }
           img.onmouseover  = function() { this.style.width="25";this.style.height="25"; }
    return img;
}

function OnErrorBuscar(arg)
{
    alert("OnErrorBuscar arg="+arg._message);
    SetCargando(false);
}

function getIconOver(id, layer)
{
    SetCargando(true);
    var ret = WebService.GetDescription(id, layer, OnCompleteIcon1, OnErrorIcon1, OnErrorIcon2);
}

function OnCompleteIcon1(arg)
{
alert("OnCompleteIcon arg="+arg);
    /*if(iconOverCtrl)
    {
        if (arg=="") arg="Sin descripción";
        iconOverCtrl.alt=arg;
        iconProcessing=false;
    }*/
        SetCargando(false);
}

function OnErrorIcon1(arg)
{
alert("OnErrorIcon1 arg="+arg._message);
    //iconProcessing=false;
        SetCargando(false);
}

function OnErrorIcon2(arg)
{
alert("OnErrorIcon2 arg="+arg._message);
    //iconProcessing=false;
        SetCargando(false);
}

function tecla(e)
{
    
    if(esEnter(e)){
        //SetCargando(true);
        //buscar(GE('txbBuscar').value);
        //SetCargando(false);
        var o=GE('btnBuscar');
        o.click();
        return;
        }
        
}

function esEnter(e)
{
	var codigo=null;
	if(window.Event) 
	    codigo=e.which;
	else
	{
		if(e.type=="keypress") codigo=e.keyCode;
	}

	if (e.type=="keypress" && codigo==13)
		return true;
	return false;
}

function comprobarCapas(){

    capasVisor= LtnWebViewer1.getSystemLayers();
    var sal="";
    
    for(var i=0; i<3; i++){
        var d0=capasVisor[i];
        d0=d0.getContainer();
        sal+="opacidad: "+d0.style.opacity+"\n";
        sal+="filter: "+d0.style.filter+"\n";
        sal+="display: "+d0.style.display+"\n";
        sal+="isdisable: "+d0.isDisabled+"\n";
        sal+="ancho: "+d0.style.width+"\n";
        sal+="alto: "+d0.style.height+"\n";
        sal+="isdisable: "+d0.isDisabled+"\n";
        sal+="--------------------\n";
    }
    alert(sal);
}

