//Funcion para clave de google
var clavegoogle;

function googlekey(){
	var documento = self.location.href.match( /\/([^/]+)$/ )[1];	
	//clave internet: ABQIAAAA1wZlYkax2JKK22rEo9f05BQqGCbc7qQy6ZQSllCU2Ch7W9BCehTfigLO-F8C8EF4gMicFNJ_KA5YRA -->
	//clave oficina: ABQIAAAA1wZlYkax2JKK22rEo9f05BQ6ohNN26FcST7tEOUPMBoLJK9hYxQp3Aal94HJfX78ha8Y3-0c4AIF9w -->
	if (document.location.href == 'http://servidor.agroquimicoscespedes/localizacion.aspx')
	{
		clavegoogle = 'ABQIAAAAsJ6L0kwNtVxYVONAHv4rmhR-fKiY_rH6k9Vm_ANkaLKqt5jxHBS70vhatWhf8kF5ke9wH6ft4I9w_A';
	}
	else
	{
		if (document.location.href == 'http://www.agroquimicoscespedes.com/localizacion.aspx');
		{
			clavegoogle= 'ABQIAAAA1wZlYkax2JKK22rEo9f05BQqGCbc7qQy6ZQSllCU2Ch7W9BCehTfigLO-F8C8EF4gMicFNJ_KA5YRA';
		}
	}
}



var puntomapa = [
{nombre: 'Agroqu&iacute;micos C&eacute;spedes<br />(+34) 950 56 81 62',
calle: '',
ciudad: 'Adra',
foto: './imagenes/alma1.jpg',
telefono: '',
lat: 36.748616178507895, lng: -3.02274227142334},
{nombre: 'SUMINISTROS AGR&Iacute;COLAS C&Eacute;SPEDES CASTELL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
calle: '',
ciudad: 'Castell de Ferro',
foto: './imagenes/alma5.jpg',
telefono: '(+34) 958 65 62 65',
lat: 36.72488568514154, lng: -3.367137908935547},

{nombre: 'SUMINISTROS AGR&Iacute;COLAS C&Eacute;SPEDES CALAHONDA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
calle: '',
ciudad: 'Calahonda',
foto: './imagenes/alma7.jpg',
telefono: '(+34) 958 62 43 57',
lat: 36.70162956685203, lng: -3.4167909622192383}];

/**
 * The map object, null until script loads in.
 */
var map = null;  

/**
 * The bounds of the markers once loaded in.
 */
var bounds = null;

/**
 * The marker with currently opened info window.
 */
var currentMarker = null;

/**
 * Position of mouse click (clientX) on map div when in static mode.
 */
var clickedX = 0;

/**
 * Position of mouse click (clientY) on map div when in static mode.
 */
var clickedY = 0;

/**
 * Called after script is asynchronously loaded in.
 * Creates the GMap2, GMarker objects and performs actions according to 
 * what the user did to trigger the map load (search, zoom, click etc).
 */
function loadMap() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(mapDiv, {logoPassive: true});
    bounds = new GLatLngBounds();
    for (var i = 0; i < puntomapa.length; i++) {
      bounds.extend(new GLatLng(puntomapa[i].lat, puntomapa[i].lng));
    }
    var latSpan = bounds.toSpan().lat();
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(36.79169061907076,-2.867431640625), 9);
	//map.setMapType(G_MAP_TYPE);
	map.addControl(new GMapTypeControl());
    // The static map server gives markers more space when calculating
    // bounds and zoom level, so sometimes the API will give a higher
    // zoom level than was used by the static map server.
    // The .98 value is just a guess right now, may need tweaking.
    var newBounds = map.getBounds();
    var newLatSpan = newBounds.toSpan().lat();
    if (latSpan/newLatSpan > .90) { map.zoomOut(); }

    for (var i = 0; i < puntomapa.length; i++) {
      var marker = createMarker(i);
      var latlng = marker.getLatLng();
      var pixel = map.fromLatLngToDivPixel(latlng);
      if (Math.abs(pixel.x - clickedX) < 12 && Math.abs(pixel.y - clickedY) < 20) {
        //GEvent.trigger(marker, 'click');
      }
      map.addOverlay(marker);
    }
  }
}



/**
 * Creates a marker for the given direccion.
 */
function createMarker(ind) {
  var direccion = puntomapa[ind];
  var marker = new GMarker(new GLatLng(direccion.lat, direccion.lng));
  GEvent.addListener(marker, 'click', function() {
    marker.html = ['<div style="width:300px; height:110px; float:left;><div style="width:300px; height:110px; float:left;"><img src="',direccion.foto,'" alt="" align="left" border="0" style="border:1px solid #666666; margin-bottom:5px; margin-right:5px;"/><b>', direccion.nombre, '</b><br>', 
	direccion.calle, '<br /> ', direccion.ciudad,'<br>',
	direccion.telefono, '<br /><br /><br />', 'Obtener indicaciones: ', 
	'<b><a target="_blank" class="enlace_pagina"',
	'href="http://maps.google.com/maps?saddr=&daddr=',
	formateodedireccion(direccion), '">Hasta aqu&iacute;</a></b> - ', 
	'<b><a target="_blank" class="enlace_pagina" ',
	'href="http://maps.google.com/maps?saddr=',
	formateodedireccion(direccion), '&daddr=', '"> Desde aqu&iacute; <br /></a></b></div>'].join('');
    currentMarker = marker;
    marker.openInfoWindowHtml(marker.html);
  });
  return marker;
}

/**
 * formateo de la direccion para consultarla en google maps
 */
function formateodedireccion(direccion) {
  var address = direccion.calle + ' ' + direccion.ciudad;
  return escape(address.replace(' ', '+'));
}


/**
 * Cargamos el mapa asincronamente y luego lo mostramos
 */
function cargadordemapa() {
 	mapDiv = document.getElementById('map');
    isLoaded = true;
    var div = document.createElement('div');
    div.className = 'message';
    div.innerHTML = 'Cargando informacion ...';
    mapDiv.appendChild(div);
    var script = document.createElement('script');
    script.type = 'text/javascript';
	googlekey();
    script.src = 'http://maps.google.com/maps?file=api&v=2.x' + 
                 '&async=2&callback=loadMap&key='+ clavegoogle +'';
    document.body.appendChild(script);
}