﻿        var map = null;
        var geocoder = null;

        places = new Array();


        function load() {
          if (GBrowserIsCompatible()) {

            map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(39.97712,-3.054199), 6);
            map.addControl(new GLargeMapControl());
		    map.enableScrollWheelZoom();
		    // bind a search control to the map, suppress result list
            //map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));

            geocoder = new GClientGeocoder();
            
            var res = web.controles.google.cargarAsesoras();
            var res2 = res.value;
	        var total = res2.src.length;
            var res_ciu = web.controles.google.AsesorasDireccion();
            var res3 = res_ciu.value;
            
            for (i = 0; i < total; i++)
	        {
	            showAddress(res3.src[i], res2.src[i]);
	        }
       }
       
////////////////////// CREAR MARCADOR PASANDO LA DIRECCION A GOOGLE /////////////////////
        function showAddress(address, content) {
          geocoder.getLatLng(
            address,
            function(point) {
//              if (!point) {
//                alert(address + " not found");
//              } else {
//                map.setCenter(point, 13);
//                var marker = new GMarker(point);
//                map.addOverlay(marker);
//                marker.openInfoWindowHtml(content);
//              }
                createMarker(point, content);
            }
          );
        }



////////////////////// CREAR MARCADOR PASANDO LAS COORDENADAS A GOOGLE /////////////////////
  	    function showAddress2(lat,lon,content) {
		    var coords = new GLatLng(lat, lon);
            createMarker(coords, content);
        }

        function createMarker(point, content) {

		    var blueIcon = new GIcon(G_DEFAULT_ICON);

            blueIcon.image = "http://www.secretisima.com/imagenes/ico.png";
            blueIcon.shadow = "";
            blueIcon.iconSize = new GSize(30, 31);
		    blueIcon.shadowSize = new GSize(37, 34);
            blueIcon.iconAnchor = new GPoint(4,20);

		    // Set up our GMarkerOptions object
		    markerOptions = { icon:blueIcon };

            //markerOptions = { icon:letteredIcon };
            var marker = new GMarker(point, markerOptions);

		    GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(content);
            });
		    places.push(marker); 
		    map.addOverlay(marker);
	    }
            
    //  	  	var currentMarker = false;    	  
    //	    // Display the vcards on the map
    //	    jQuery('#padrinos .post').each(function() {
    //	        var vcard = jQuery(this);
    //	        var geo = vcard.find('.geo[title]');
    //	        if (geo.length && /;/.exec(geo.attr('title'))) {
    //	            var degrees = geo.attr('title').split(';');
    //	            var lat = parseFloat(degrees[0]);
    //	            var lon = parseFloat(degrees[1]);
    //	            vcard.css('cursor', 'pointer');
    //	            vcard.click(function() {
    //	                if (currentMarker) {
    //	                    map.removeOverlay(currentMarker);
    //	                }
    //	                jQuery('.current-vcard').removeClass('current-vcard');
    //	                vcard.addClass('current-vcard');
    //	                var point = new GLatLng(lat, lon);
    //					// If the vcard has an image, use it for the marker
    //					var image ="http://www.secretisima.com/imagenes/ico.png";
    //					if (image) {
    //						image = image.replace('gif','png');
    //	 				   	var icon = new GIcon();
    //					    icon.iconAnchor = new GPoint(9, 34);
    //					    icon.infoWindowAnchor = new GPoint(9, 2);
    //						icon.image = image;
    //		                var marker = new GMarker(point,icon);
    //					} else {
    //						var marker = new GMarker(point);
    //					}
    //	                currentMarker = marker;
    //	                GEvent.addListener(marker, "click", function() {
    //	                    marker.openInfoWindowHtml(vcard.html());
    //	                });
    //	                map.addOverlay(marker);
    //	                map.panTo(point,icon);
    //	                marker.openInfoWindowHtml(vcard.html());
    //	            });
    //	        }
    //	    });        
        }
        
	    function openWindow(x,y,postid){
		    var vcard = jQuery('#'+postid);
      	    var n=0;
      	    while(n < places.length){
          	    var latlong = places[n].getLatLng();
          	    lat = latlong.y;
          	    lon = latlong.x;
          	    /*x = Math.abs(Math.round(value=x * 1000000.));
          	    y = Math.abs(Math.round(value=y * 1000000.));
          	    lat = Math.abs(Math.round(value=latlong.y * 1000000.));
          	    lon = Math.abs(Math.round(value=latlong.x * 1000000.));*/
              	
                if((y == lat) && (x == lon)) break;
                //else alert(y + ' ' + lat + ' ' + lon + ' ' + x);
      		    n++;
      	    }
      	    //close info window if open
      	    if(!map.getInfoWindow().isHidden())	map.closeInfoWindow();
      	    places[n].openInfoWindowHtml(vcard.html());
          }