	var map; 
    var mgr;
    var icons = {};
    var clicked = false;
    
    function showLatLng(lat,lon,zoom){
      zoom = zoom || 14;
      map.setCenter(new GLatLng(lat,lon),zoom);
    }
    
   /* function GMap_MoveEnd() {
      var bounds = this.getBoundsLatLng();
      var numOverlays = this.overlays.length;
      for(var i=0; i<numOverlays; i++) {

        var pnt = this.overlays[i].point;

        if( pnt.x >< bounds.minX || pnt.y < bounds.minY || pnt.x > bounds.maxX || pnt.y > bounds.maxY )
            this.removeOverlay(this.overlays[i]);
        	numOverlays = this.overlays.length;
		
      }
    }*/
      
    function getIcon(images) {
      var icon = null;
      if (images) {
        // alert(images);
        if (icons[images[0]]) {
          icon = icons[images[0]];
        } else {
          icon = new GIcon();
          icon.image = "wp-content/themes/multiopticas/img/icons/" + images[0] + ".png";
          var size = iconData[images[0]];
          icon.iconSize = new GSize(size.width, size.height);
          icon.iconAnchor = new GPoint(13, size.height);
          icon.infoWindowAnchor = new GPoint(size.width >> 1, size.height >> 1);
          icon.shadow = "wp-content/themes/multiopticas/img/icons/" + images[1] + ".gif";
          size = iconData[images[1]];
          icon.shadowSize = new GSize(size.width, size.height);
          icons[images[0]] = icon;
        }
      }
      
      return icon;
    }
    
    function createMarker(point, title, icon, html, zoom) {
      var marker = new GMarker(point, {title: title, icon: icon});
      GEvent.addListener(marker, "click", function() {
        clicked = true;
        if (map.getZoom()<zoom) {

          map.setCenter(point, zoom);

        } else {

          map.panTo(point);

        }

        // window.setTimeout(marker.openInfoWindowHtml(html),0);

        window.setTimeout("clicked=false", 500);

        marker.openInfoWindowHtml(html);

      });

      return marker;

    }

    

    function showMarker(id){

      placeLayer.each( function(layer){

        layer.places.each( function(place){

          

          if (place["id"] == id){

            var icon = getIcon(place["icon"]);

            var posn = new GLatLng(place["posn"][0], place["posn"][1]);

            var text = "<div class=\"popUp\"><p>"+place["name"]+"</p><p>"+place["addr"]+"</p><p>";
            if (place["icon"][0]=="Microaudio"){
              text+="<strong>Audiolog&iacute;a.</strong>";
            } else if (place["icon"][0]=="Microaudio-Multiopticas") {
              text+="<strong>Servicios &Oacute;pticos y audiolog&iacute;a.</strong>";
            } else if (place["icon"][0]=="Multiopticas") {
              text+="<strong>Servicios &Oacute;pticos.</strong>";
            }
            text+="</p></div>";
            var zoom = place["zoom"];
            map.setCenter(posn, 14);
            map.openInfoWindowHtml(posn,text);
            // var marker = createMarker(posn, place["name"],icon, text, zoom);
            // map.addOverlay(marker);
            // marker.openInfoWindow(text);
            // alert(text);
          }
        });
      });
    }
  
    function setupPlaces() {
      map.clearOverlays();
      mgr = new GMarkerManager(map);
      placeLayer.each( function(layer){
        var markers = [];
        layer.places.each( function(place) {
          var icon = getIcon(place["icon"]);
          var posn = new GLatLng(place["posn"][0], place["posn"][1]);
          // if (map.getBounds().contains(posn)) {
            var text = "<div class=\"popUp\"><p>"+place["name"]+"</p><p>"+place["addr"]+"</p><p>";
            if (place["icon"][0]=="Microaudio"){
              text+="<strong>Audiolog&iacute;a.</strong>";
            } else if (place["icon"][0]=="Microaudio-Multiopticas") {
              text+="<strong>Servicios &oacute;pticos y audiolog&iacute;a.</strong>";
            } else if (place["icon"][0]=="Multiopticas") {
              text+="<strong>Servicios &oacute;pticos.</strong>";
            }
            text+="</p></div>";
            var zoom = place["zoom"];
            var marker = createMarker(posn, place["name"],icon, text, zoom);
            markers.push(marker);
          // }
        });
        mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
      });
      mgr.refresh();
    }
 
    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapa"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.addControl(new GOverviewMapControl());
        map.setCenter(new GLatLng(39.856777,-4.024475), 5 /*, G_HYBRID_MAP*/ );
        map.enableDoubleClickZoom();
        map.enableContinuousZoom();
        map.enableScrollWheelZoom();
        // GEvent.addListener(map, "moveend", function() {if (!clicked) {window.setTimeout(setupPlaces, 0);}});
        // GEvent.addListener(map, "zoomend", function() {if (!clicked){window.setTimeout(setupPlaces, 0);}});
        window.setTimeout(setupPlaces, 0);
        geocoder = new GClientGeocoder();
      }
      Event.observe('searchLink', 'click', function(e){ searchData(); Event.stop(e);}, false);
    }
    
    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " no encontrado");
            } else {
              act("" + point.y + "," + point.x);
              map.setCenter(point, 17);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }
    
    function act(text){
      document.getElementById("longitud").innerHTML = "[" + text + "]";
    }
 
    function searchData(){
      $('searchLink').blur();
      var url = 'wp-content/themes/multiopticas/searchLocations.php';
      var target = 'searchResults';
      var state = escape($F("state"));
      // todo: check state
      var zipCode = escape($F("zipCode"));
      // todo: check zipCode
      
          		var pars = 'state='+state+'&zipCode='+zipCode;
  		      var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, evalScripts: true});
      return false;
    }
    
    Ajax.Responders.register({
      onCreate: function() {
        $('searching').show();
      },
      onComplete: function() {
        $('searching').hide();
        $('searchResults').scrollTop=0;
        $('searchResults').scrollLeft=0;
      }
    });
    
    Event.observe(window, 'load', load, false);
    Event.observe(window, 'unload', GUnload, false);
