var map;
var marcador;

function loadg() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    var longitud = $('#longitud').val();
    var latitud = $('#latitud').val();
    map.setCenter(new GLatLng(latitud, longitud), 15);
    map.addControl(new GMapTypeControl());
    add_marker();
    marcador.setLatLng(new GLatLng(latitud, longitud), 15);
    /*
    $.ajax({
	   type: "POST",
	   url: "/inc/files/rpc.php",
	   dataType: "json",
	   data: "action=getloc&suc="+$('#suc_id').val(),
	   success: function(msg){
	     if(typeof msg.error != 'undefined') {
	     	alert('ERROR: '+msg.error.msg);
	     } else {
	     	marcador.setLatLng(new GLatLng(msg.resultado.latitud, msg.resultado.longitud), 15);
	     	map.setCenter(new GLatLng(msg.resultado.latitud, msg.resultado.longitud), 15);
	     }
	   }
    });
    */
  }
}


function add_marker() {
	var normalIcon = new GIcon();
	var gicons = [];
	normalIcon.iconSize = new GSize(80,80);
	normalIcon.shadowSize = new GSize(82,82);
	normalIcon.iconAnchor = new GPoint(40,40);
	normalIcon.infoWindowAnchor = new GPoint(40,40);
	
	gicons["influencia"] = new GIcon(normalIcon, "/img/marker.png" );
	gicons["indicador"] = new GIcon(normalIcon, "/img/indicador_mapa.png" );

	if(typeof marcador == 'undefined') {
		marcador = new GMarker(map.getCenter(), {
			draggable : false,
			icon: gicons["indicador"]
		});
		/*GEvent.addListener(marcador, "dragstart", function() {
			map.closeInfoWindow();
		});
		GEvent.addListener(marcador, "dragend", function() {
			marcador.openInfoWindowHtml("<strong>Nueva latitud:</strong> "
					+ marcador.getPoint().y + "<br /><strong>Nueva longitud:</strong> "
					+ marcador.getPoint().x
			);
			$(":input[name=frm_agencia_lat]").val(marcador.getPoint().y);
			$(":input[name=frm_agencia_long]").val(marcador.getPoint().x);
			setTimeout(function() {
				marcador.closeInfoWindow();
			}, 3000);
		});
		GEvent.addListener(marcador, "click", function() {
			marcador.openInfoWindowHtml("<strong>Latitud:</strong> "
					+ marcador.getPoint().x + "<br /><strong>Longitud:</strong> "
					+ marcador.getPoint().y);
			$(":input[name=frm_agencia_lat]").val(marcador.getPoint().y);
			$(":input[name=frm_agencia_long]").val(marcador.getPoint().x);					
			setTimeout(function() {
				marcador.closeInfoWindow();
			}, 5000);					
		});*/
		map.addOverlay(marcador);
	} else {
		map.setCenter(new GLatLng(marcador.getPoint().y, marcador.getPoint().x), 11);
	}
}

function remove_elem(obj) {
	map.removeOverlay(obj);
}
