var geocoder, map;

function gmaps_iniciar () {
	
	// Iniciar Google Maps
	geocoder = new google.maps.Geocoder();
	var myOptions = {
		zoom: 16,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("mapa"), myOptions);
	gmaps_ubicar();
	setTimeout('gmaps_teclado();',6000);
}

function gmaps_ubicar() {
	var address = document.getElementById('ubicacion').firstChild.nodeValue;
	if (geocoder) {
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
						map: map, 
						position: results[0].geometry.location
				});
			} else {
				alert("Error en la localización: " + status);
			}
		});
	}
}

function gmaps_teclado() {
	var capaMapa = 'mapa' // cambiar por id de la capa en la que se inserta el mapa
	var button, button_style = 'width:100%;height:100%;padding:2px;margin:2px;background:transparent;border-width:0px;border-style:solid;cursor:pointer;overflow:hidden;text-indent:-100em;position:absolute;top:-2px;left:-2px;';
	var divs = document.getElementById(capaMapa).getElementsByTagName('div');
	for (var i = 0; i < (divs.length); i++) {
		if ( divs[i].getAttribute('title')) {
			// Se genera el button
			button = document.createElement("BUTTON");
			button.setAttribute('id','boton-mapa-'+i);
			button.setAttribute('value',divs[i].getAttribute('title'));
			
			// estilos...
			button.setAttribute('style',button_style); // Para navegadores de verdad
			button.style.cssText = button_style; // Para IE
			
			// Se añade el button
			divs[i].appendChild(button);
		}
	}
}

// Inicio de las funciones de mapa (no tiene por que hacerse así necesariamente)
window.onload = function () {gmaps_iniciar();}
