CEMapa.prototype.divDestino;
CEMapa.prototype.divMapa;
CEMapa.prototype.divNoMapa;
CEMapa.prototype.urlVinyeta;
CEMapa.prototype.divCargando;
CEMapa.prototype.iconoBase;
CEMapa.prototype.mapa;
CEMapa.prototype.marcas;
CEMapa.prototype.contenidos;
CEMapa.prototype.numMarcas;
CEMapa.prototype.centrosProcesados;
CEMapa.prototype.geocoder;

var CEMapa;

function CEMapa(idDivDestino, idMapaGoogle, idDivCargando, idDivNoMapa , numMarcas) {
    this.divDestino = document.getElementById(idDivDestino);
    this.divCargando = document.getElementById(idDivCargando);
    this.divNoMapa = document.getElementById(idDivNoMapa);
    this.divMapa = document.getElementById(idMapaGoogle);
    
    this.marcas = new Array();
    this.contenidos = new Array();
    this.numMarcas = numMarcas;
    this.centrosProcesados = 0;
    this.crearIconoBase();    
}

CEMapa.prototype.setUrlVinyeta = function(url){    
    this.urlVinyeta = url;
}

CEMapa.prototype.getMapa = function(){
    if ( this.mapa == null ){
        var mapa = new GMap2(this.divMapa);
        mapa.addControl(new GSmallMapControl());
        mapa.addControl(new GMapTypeControl());    
        this.mapa = mapa;
    }
    return this.mapa;    
}

CEMapa.prototype.crearIconoBase = function(){
    var iconoBase = new GIcon(G_DEFAULT_ICON);
    iconoBase.shadow = "/web/img/centro_mapa_sombra.png";
    iconoBase.iconSize = new GSize(20, 34);
    iconoBase.shadowSize = new GSize(37, 34);
    iconoBase.iconAnchor = new GPoint(9, 34);
    iconoBase.infoWindowAnchor = new GPoint(9, 2);
    
    this.iconoBase = iconoBase;
}

CEMapa.prototype.centroProcesado = function(){
    this.centrosProcesados++;
    if ( this.numMarcas == this.centrosProcesados )        
        this.pintar();    
}

CEMapa.prototype.getGeocoder = function(){
    if ( this.geocoder == null ){
        this.geocoder = new GClientGeocoder();
        this.geocoder.setBaseCountryCode("es");
    }
    return this.geocoder;
}

CEMapa.prototype.desplegarMarca = function(i){        
    this.marcas[i].openInfoWindowHtml(this.contenidos[i]);
}

CEMapa.prototype.setCentro = function(nombre, direccion, point, html){
    
    if ( point == null ){    
        this.getGeocoder().getLatLng(direccion, function(point){            
            if (point!=null)
                CEMapa.setCentro(nombre,direccion,point,html, true);
            
            CEMapa.pintar();
            
            if (point!=null)
                CEMapa.desplegarMarca(0);            
        });
        return;
    }
    
    var marca = new GMarker(point);
    GEvent.addListener(marca, "click", function() {
        marca.openInfoWindowHtml(html);
    });
    this.contenidos[0] = html;
    this.marcas[0] = marca;
    
    if( arguments.length==4 ){
        CEMapa.pintar();        
        CEMapa.desplegarMarca(0);
    }
}

CEMapa.prototype.addCoordenada = function(ra, point, index){
    var icono = new GIcon(this.iconoBase);
    
    icono.iconSize = new GSize(12, 20);
    icono.shadowSize = new GSize(22, 20);
    icono.iconAnchor = new GPoint(6, 20);
    icono.infoWindowAnchor = new GPoint(5, 1);
    icono.image = "/web/img/centro_mapa2.png";
    icono.shadow = "/web/img/centro_mapa_sombra2.png";    
    
    var marca = new GMarker(point, { icon:icono });
    var urlVinyeta = this.urlVinyeta;
    GEvent.addListener(marca, "click", function() {        
        var http = getHTTPObject();        
        http.open("POST", urlVinyeta+"/"+ra, true );
        http.onreadystatechange = function(){
            if (http.readyState == 4){                
                marca.openInfoWindowHtml(http.responseText);
            }
        };
        http.send(null);
    });
    this.marcas[index] = marca;
    this.centroProcesado();
}


CEMapa.prototype.addCentro = function(nombre, direccion, point, html, index){
    if ( point == null ){        
        this.getGeocoder().getLatLng(direccion, function(point){            
            if (!point){                                                                
                CEMapa.centroProcesado();
            }else{
                CEMapa.addCentro(nombre,direccion,point,html,index);
            }            
        });
        return;
    }
    
    var letra = String.fromCharCode("A".charCodeAt(0) + index);
    var iconoLetra = new GIcon(this.iconoBase);
    
    iconoLetra.image = "/web/img/marker" + letra + ".png";
    
    var indexResultado = index+1;
    
    var resultado = $("#resultado_"+indexResultado);
    resultado.addClass("letra_"+indexResultado);
    
    resultado.append("<div class='enlace_mapa' id='mostrar_mapa_"+index+"'><img src='"+iconoLetra.image+"'/><div><a href='#mapa' class='destacado azul' onclick='CEMapa.desplegarMarca("+index+")' title='"+translate('localizar_mapa','web')+"'>"+translate('localizar_mapa','web')+"</a></div></div>");    
    $("#mostrar_mapa_"+index).hide();
    $("#mostrar_mapa_"+index).fadeIn("slow");
    
    var marca = new GMarker(point, { icon:iconoLetra });
    GEvent.addListener(marca, "click", function() {
        marca.openInfoWindowHtml(html);
    });
    
    this.contenidos[index] = html;
    this.marcas[index] = marca;
    
    this.centroProcesado();
}

CEMapa.prototype.pintar = function(){    
    this.divCargando.className="oculto";
    if (this.marcas.length==0){        
        this.divMapa.style.visibility="hidden";
        this.divMapa.style.display="none";
        this.divMapa.innerHTML = "";
        this.divNoMapa.className="visible";
    }else{
        this.centrar(this.marcas);
        for ( i in this.marcas )
            this.getMapa().addOverlay(this.marcas[i]);
    }
}

CEMapa.prototype.centrar = function(marcas) {
    
    var bounds;
    for ( i in marcas ){
        if ( bounds == null ){
            bounds = new GLatLngBounds(marcas[i].getPoint(), marcas[i].getPoint())
        }else{
            bounds.extend(marcas[i].getPoint());
        }
    }
    
    var lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
    var lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
    if(bounds.getNorthEast().lng() < bounds.getSouthWest().lng())
      lng += 180;
    
    var center = new GLatLng(lat,lng);
    var zoom = this.getMapa().getBoundsZoomLevel(bounds);
    if ( zoom > 14 )
        zoom = 14;    
    this.getMapa().setCenter(center, zoom);
}