      // ====== Plot a marker after positive reponse to "did you mean" ======
        function place(lat,lng, search) {

            var point = new GLatLng(lat,lng);
            map.setCenter(point,13); 
            
            //creo el icono diferente al resto
            var tinyIcon = new GIcon();
            tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
            tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
            tinyIcon.iconSize = new GSize(12, 20);
            tinyIcon.shadowSize = new GSize(22, 20);
            tinyIcon.iconAnchor = new GPoint(6, 20);
            tinyIcon.infoWindowAnchor = new GPoint(5, 1);
            markerOptions = { icon:tinyIcon };
                    
            //creo la marca          
            var marker_search = new GMarker(point, markerOptions);
            
            marker_search.infowindow = "<b>*** " +search+ " ***</b>";
            marker_search.search_point=1;   // indico que es una marca de lugar buscado para no abrir vntana
        
            map.addOverlay(marker_search);
            
            document.getElementById("message").innerHTML = "";
            $("#message").hide();
            getMarkers();
    
        }
                  
        
        // ====== Geocoding ======
        function showAddress(search) {
            //var search = document.getElementById("search").value;
            // ====== Perform the Geocoding ======        
            geo.getLocations(search, function (result){
                map.clearOverlays(); 
                if (result.Status.code == G_GEO_SUCCESS) {
                    // ===== If there was more than one result, "ask did you mean" on them all =====
                    if (result.Placemark.length > 1) { 
                        document.getElementById("message").innerHTML = "<p>Se han encontrado varios lugares:</p>";
                        // Loop through the results
                        for (var i=0; i<result.Placemark.length; i++) {
                            var p = result.Placemark[i].Point.coordinates;
                            document.getElementById("message").innerHTML += "<br>"+(i+1)+": <a href='javascript:place(" +p[1]+","+p[0]+")'>"+ result.Placemark[i].address+"<\/a>";     
                        }
                        if ($("#message").is(":hidden")) {
                            $("#message").slideDown("slow");
                        }
                    }
                    // ===== If there was a single marker =====
                    else {
                        document.getElementById("message").innerHTML = "";
                        var p = result.Placemark[0].Point.coordinates;
                        place(p[1],p[0], search);
                    }
                }
                // ====== Decode the error status ======
                else {
                    //var reason="Code "+result.Status.code;
                    
                    if (reasons[result.Status.code]) {
                        reason = reasons[result.Status.code];
                        
                        if (result.Status.code==G_GEO_MISSING_ADDRESS){ 
                            //no ha indicado nada en el buscador
                            document.getElementById("message").innerHTML = "<p style='color:#f00'>"+reason+"</p>" ;
                        }else{
                            //ha habido un error diferente
                            document.getElementById("message").innerHTML = "<p style='font-weight:bold'>Uppss! algo ha pasado!:</p><br/>";
                            document.getElementById("message").innerHTML += reason;
                        
                        }
                        
                        $("#message").css("border:1px solid #f00");
                        if ($("#message").is(":hidden")) {
                            $("#message").slideDown("slow");
                        }   
                    }

                }
            });
        }
        
       //===========================================================================0
       
       function getMarkers(){            
             
            //var zonas;
            //miFecha = new Date() 
                
            GDownloadUrl("locations.php", function(doc) {
                var xmlDoc = GXml.parse(doc);
                var markers = xmlDoc.documentElement.getElementsByTagName("marker");
    
                ShowStatus('Cargando zonas ...');
               
                for (var i = 0; i < markers.length; i++) {
                  
                  zonas[i] = new GMarker(new GLatLng(markers[i].getAttribute("lat"),markers[i].getAttribute("lng")));
                        
                    zonas[i].infowindow = "<div class='infowin'>";
                    //zonas[i].infowindow += "<p>"+markers[i].getAttribute("lat")+", "+markers[i].getAttribute("lng")+"</p>";
                    
                    zonas[i].infowindow += "<p><a href='zona/"+markers[i].getAttribute("enlace")+"' class='titulo'>"+markers[i].getAttribute("titulo")+"</a></p>";
    
                    zonas[i].infowindow += "<p>"+markers[i].getAttribute("text")+"</p>";
                    zonas[i].infowindow += "<p class='cord'>" + converter(markers[i].getAttribute("lat"),markers[i].getAttribute("lng")) +"</p>";
                                    
                    //zonas[i].infowindow += "<p>&raquo; <a href='zona/"+markers[i].getAttribute("enlace")+"'><strong>Ver ficha de la zona</strong></a></p><br/><hr/>";
                    
                    numcomentarios=markers[i].getAttribute("numcomentarios");
                    comentarios="";
                    if (numcomentarios==1){ comentarios="(1 comentario)";}
                    if (numcomentarios!=1){ comentarios="("+numcomentarios+" comentarios)";}   
                    
                    zonas[i].infowindow += "<p>&raquo; <a href='zona/"+markers[i].getAttribute("enlace")+"'><strong>Ver ficha de la zona</strong></a><span style='color:#FF8000'> "+comentarios+"</span></p><br/><hr/>";
                    
                    
                    zonas[i].infowindow += "<p> <a href='javascript:zoom(" +markers[i].getAttribute("lat")+","+markers[i].getAttribute("lng")+")'>Ver de cerca</a> | ";
                    zonas[i].infowindow += " <a target=\'_blank\' href=\'http://maps.google.es/maps?saddr=&daddr="+ markers[i].getAttribute("lat") +","+ markers[i].getAttribute("lng") +"\'>¿Cómo llegar hasta aquí?</a></p>";
                    
                    zonas[i].infowindow += "<br/><br/></div>";

                    zonas[i].markerindex = i;
                    //markers[i].db_id = locations[i].getAttribute("id");
                    zonas[i].search_point=0;   //indico que no es una marca de un lugar
                  
                    
                
                  map.addOverlay(zonas[i]);
                  
                }
                HideStatus();
            });
        }
       
            
        function zoom(lat,lng){
            var point = new GLatLng(lat,lng);
            map.setCenter(point,16); 
        }    
        
        //************************************************************************************************************+
        function StatusControl() {
        }
            
        StatusControl.prototype = new GControl();
        // Creates a one DIV for each of the buttons and places them in a container DIV which is returned as our 
        //control element. We add the control to the map container and return the element for the map class to position properly.
        StatusControl.prototype.initialize = function() {
            var container = document.createElement("div");
            var statusDiv = document.createElement("div");
            statusDiv.id = 'status';
            this.setStyle_(statusDiv);
            container.appendChild(statusDiv);
            map.getContainer().appendChild(container);
            return container;
        }
        
        // By default, the control will appear in the top left corner of the map with 1 pixels of padding.
        StatusControl.prototype.getDefaultPosition = function() {
          return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(60, 1));
        }
        
        // Sets the proper CSS for the given button element.
        StatusControl.prototype.setStyle_ = function(bar) {
            bar.className = 'status';
        }
         function ShowStatus(str){
           //alert(str);
           // add status control if it does not exist
           if(!document.getElementById('status')){
              mapStatus = new StatusControl();
              map.addControl(mapStatus);
           }
           document.getElementById('status').innerHTML = str; //display error message in status bar
        }

        function HideStatus(){     
            map.removeControl(mapStatus);
        }
        //***************************************************************************************+***************
       
       
               
        
        
        //this function clears all markers and the array of plotted points upon user request via link
        function clearMarkers(){
            map.clearOverlays(); //remove all markers from map
            //places.length=0; //reset array of markers
            //sides.length=0;
        }
        
        
        
       
            
           
        
        
        function converter(lat,lon){
        
            //alert(lat+" , "+lon);
            
            var signlat=1;
            var signlon=1;
            
            //LAT
            if(lat < 0)  { signlat = -1; }
            latAbs = Math.abs( Math.round(lat * 1000000.));
            //Math.round is used to eliminate the small error caused by rounding in the computer:
            //e.g. 0.2 is not the same as 0.20000000000284
            //Error checks
            if(latAbs > (90 * 1000000)) { 
                alert(' Degrees Latitude must be in the range of -90. to 90. '); 
                latAbs=0; 
            }
            
            //LON
           if(lon < 0)  { signlon = -1; }
           lonAbs = Math.abs(Math.round(lon * 1000000.));
           //Math.round is used to eliminate the small error caused by rounding in the computer:
           //e.g. 0.2 is not the same as 0.20000000000284
           //Error checks
           if(lonAbs > (180 * 1000000)) {  
                alert(' Degrees Longitude must be in the range of -180 to 180. '); 
                lonAbs=0; 
           }
            
            
           //RESULTADO EN GRados minutos y segundos
                      
           deglat = (Math.floor(latAbs / 1000000) * signlat) + '° ' + Math.floor(  ((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60)  + '\' ';  
           tmp1 =( Math.floor(((((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60) - Math.floor(((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60)) * 100000) *60/100000 );
           tmp2 = new Number(tmp1);
           deglat = deglat + "" + tmp2.toFixed(2) + '"'  ;
           
//deglat = ((Math.floor(latAbs / 1000000) * signlat) + 'Â° ' + Math.floor(  ((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60)  + '\' ' +  ( Math.floor(((((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60) - Math.floor(((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60)) * 100000) *60/100000 ) + '"'  );
           
           
           deglon = (Math.floor(lonAbs / 1000000) * signlon) + '° ' + Math.floor(  ((lonAbs/1000000) - Math.floor(lonAbs/1000000)) * 60)  + '\' ';
           tmp1 = ( Math.floor(((((lonAbs/1000000) - Math.floor(lonAbs/1000000)) * 60) - Math.floor(((lonAbs/1000000) - Math.floor(lonAbs/1000000)) * 60)) * 100000) *60/100000 );
           tmp2 = new Number(tmp1); 
           deglon = deglon + "" + tmp2.toFixed(2) + '"'  ;
           
           
           
           signlat=1;
           signlon=1;
           
           
           
           return ("[ "+deglat + " ] &nbsp; [ " + deglon+" ]");
        
           
        }
        
/* ******* ESCUELAS **** */

function getEscuelas(){            
             
                
            GDownloadUrl("locations_escuelas.php", function(doc) {
                var xmlDoc = GXml.parse(doc);
                var markers = xmlDoc.documentElement.getElementsByTagName("marker");
    
                //ShowStatus('Cargando escuelas ...');
               
                for (var i = 0; i < markers.length; i++) {
                  
                  zonas[i] = new GMarker(new GLatLng(markers[i].getAttribute("lat"),markers[i].getAttribute("lng")));
                        
                    zonas[i].infowindow = "<div class='infowin infowinescuelas'>";
                    //zonas[i].infowindow += "<p>"+markers[i].getAttribute("lat")+", "+markers[i].getAttribute("lng")+"</p>";
                    
                    zonas[i].infowindow += "<p><a href='"+markers[i].getAttribute("enlace")+"' class='titulo'>"+markers[i].getAttribute("titulo")+"</a></p>";
                    
                    zonas[i].infowindow += "<p>"+markers[i].getAttribute("poblacion")+" - "+markers[i].getAttribute("provincia")+"</p>";
                    
                    zonas[i].infowindow += "<p><a href='"+markers[i].getAttribute("enlace")+"'><img src='http://www.volarenparamotor.com/images/escuelas/logos/"+markers[i].getAttribute("logo")+"' /></a></p>";
                    //zonas[i].infowindow += "<p class='cord'>" + converter(markers[i].getAttribute("lat"),markers[i].getAttribute("lng")) +"</p>";
                    //zonas[i].infowindow += "<p>&raquo; <a href='"+markers[i].getAttribute("enlace")+"'>Información de la escuela</a></p><br/><hr/>";
                    //zonas[i].infowindow += "<p> <a href='javascript:zoom(" +markers[i].getAttribute("lat")+","+markers[i].getAttribute("lng")+")'>Ver de cerca</a> | ";
                    //zonas[i].infowindow += " <a target=\'_blank\' href=\'http://maps.google.es/maps?saddr=&daddr="+ markers[i].getAttribute("lat") +","+ markers[i].getAttribute("lng") +"\'>¿Cómo llegar hasta aquí?</a></p>";
                    
                    zonas[i].infowindow += "<p>&nbsp;</p><br/></div>";

                    zonas[i].markerindex = i;
                    //markers[i].db_id = locations[i].getAttribute("id");
                    zonas[i].search_point=0;   //indico que no es una marca de un lugar
                  
                    
                
                  map.addOverlay(zonas[i]);
                  
                }
                //HideStatus();
            });
        }        
