// JavaScript Document
// MAPAS Y COORDENADAS OBTENIDAS POR IRLANDA EN RED SL  - GRUPO MUNDO EN RED - PROHIBIDO REPRODUCIR SIN AUTORIZACION ESTE SCRIPT. //

    //<![CDATA[

    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(53.481508, -8.10791), 7);
	    map.addControl(new GMapTypeControl());
        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl());
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
		map.getContainer().style.overflow="hidden";
        map.addControl(new GOverviewMapControl());	
		map.setMapType(G_NORMAL_MAP);
		
		// Creamos el "MEC" marker icon
		var mecIcon = new GIcon();
		
		//GIcon mecIcon = new GIcon();
		mecIcon.image = "http://www.becasmec.info/images/mec.png";
		mecIcon.shadow = "http://www.becasmec.info/images/mec-sombra.png";
		mecIcon.iconSize = new GSize(48, 50);
		mecIcon.shadowSize = new GSize(48, 50);
		mecIcon.iconAnchor = new GPoint(15, 49);
		mecIcon.infoWindowAnchor = new GPoint(17, 35);
		markerOptions = { icon:mecIcon };
		
		/*GMarkerOptions mOpts = new GMarkerOptions();
		mOpts.icon = mecIcon;
		
		GMarker marker = new GMarker(latlng, mOpts);
		GMap1.addGMarker(marker);*/
		
		//Funcion de texto de escuelas MEC
		function addtag(point, address) {
        var marker = new GMarker(point, markerOptions);
        GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(address); } );
        return marker;
        }
		
		// inicio pruebas de api google
		
		// crea el buscador y lo aņade al mapa
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();
		var lsc = new google.maps.LocalSearch(); 
		map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(140,20)))	
		
		// fin pruebas de api google
		
		// IRLANDA
		
		//Dorset College (Dublin)
		var point = new GLatLng(53.359792,-6.261531);
        var address = '<b>D.C.D</b><br/><i>para cursos de ingles en Dublin</i><br /><a href="http://www.becasmec.info/detalles/66/dublin/dorset-college-dublin" title="CURSOS DE INGLES EN DUBLIN">ESCUELA DE DUBLIN DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		//Infinity Business School (Dublin)
		var point = new GLatLng(53.340329,-6.25872);
        var address = '<b>I. B. School</b><br/><i>para cursos de ingles en Dublin</i><br /><a href="http://www.becasmec.info/detalles/1/dublin/infinity-business-school" title="CURSOS DE INGLES EN DUBLIN">ESCUELA DE DUBLIN DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		/*NMLI Cork (Cork)*/
		var point = new GLatLng(51.897469,-8.469837);
        var address = '<b>NMLI Cork</b><br/><i>para cursos de ingles en Cork</i><br /><a href="http://www.becasmec.info/detalles/4/cork/nmli-cork" title="CURSOS DE INGLES EN CORK">ESCUELA DE CORK DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		//Atlantic language school
        var point = new GLatLng(53.274359,-9.045294);
        var address = '<b>A. L. S.</b><br/><i>para cursos de ingles en Galway</i><br /><a href="http://www.becasmec.info/detalles/6/galway/atlantic-language-school" title="CURSOS DE INGLES EN GALWAY">ESCUELA DE GALWAY DE NUESTRA RED</a>';
        var marker = addtag(point, address);
       	map.addOverlay(marker); 
		
		//ISI Ireland (Dublin)
		var point = new GLatLng(53.350551,-6.261864);
        var address = '<b>ISI IRELAND</b><br/><i>para cursos de ingles en Dublin</i><br /><a href="http://www.becasmec.info/detalles/2/dublin/isi-ireland" title="CURSOS DE INGLES EN DUBLIN">ESCUELA DE DUBLIN DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		//GALWAY CULTURAL (Galway)
		var point = new GLatLng(53.260416,-9.07545);
        var address = '<b>G.C.I. </b><br/><i>para cursos de ingles en Galway</i><br /><a href="http://www.becasmec.info/detalles/2/dublin/isi-ireland" title="CURSOS DE INGLES EN DUBLIN">ESCUELA DE DUBLIN DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker);
					
		//ATC (BRAY)
		var point = new GLatLng(53.198244,-6.093915);
        var address = '<b>ATC IRELAND</b><br/><i>para cursos de ingles en Bray</i><br /><a href="http://www.becasmec.info/detalles/112/bray/atc-language-travel" title="CURSOS DE INGLES EN BRAY">ESCUELA DE BRAY DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		//CEC (CORK)
		var point = new GLatLng(51.900782,-8.466658);
        var address = '<b>CEC</b><br/><i>para cursos de ingles en Dublin</i><br /><a href="http://www.becasmec.info/detalles/116/cork/cork-english-college" title="CURSOS DE INGLES EN CORK">ESCUELA DE CORK DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		//IH DUBLIN (Dublin)
		var point = new GLatLng(53.33504,-6.265308);
        var address = '<b>I.H.D.</b><br/><i>para cursos de ingles en Dublin</i><br /><a href="http://www.becasmec.info/detalles/123/dublin/international-house-dublin" title="CURSOS DE INGLES EN DUBLIN">ESCUELA DE DUBLIN DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		//IH Belfast (BELFAST)
		var point = new GLatLng(54.586107,-5.92804);
        var address = '<b>I.H.B</b><br/><i>para cursos de ingles en Belfast</i><br /><a href="http://www.becasmec.info/detalles/117/belfast/international-house-belfast" title="CURSOS DE INGLES EN BELFAST">ESCUELA DE BELFAST DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 

		
		/*Limerick Language Centre (Limerick)
		var point = new GLatLng(52.659075,-8.627529);
        var address = '<b>LIMERICK LANGUAGE CENTRE</b><br/><i>para cursos de ingles en Limerick</i><br /><a href="http://www.becasmec.info/detalles/3/limerick/limerick-language-centre" title="CURSOS DE INGLES EN LIMERICK">ESCUELA DE LIMERICK DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		
		// Galway cultural institute
	   	var point = new GLatLng(53.260349,-9.073044);
        var address = '<b>GALWAY CULTURAL INSTITUTE</b><br/><i>para cursos de ingles en Galway</i><br /><a href="http://www.becasmec.info/detalles/5/galway/galway-cultural-institute" title="CURSOS DE INGLES EN GALWAY">ESCUELA DE GALWAY DE NUESTRA RED</a>';
        var marker = addtag(point, address);
		map.addOverlay(marker); */

      }
    }
	
    var map;
    var gdir;
    var geocoder = null; // ?
    var addressMarker;   // ?


    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GMapTypeControl());
        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl());
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        GEvent.addListener(gdir, "addoverlay", onGDirectionsAddOverlay); // added to trigger marker swap
		map.setCenter(new GLatLng(0,0),0);	//Inital setCenter()  added. Esa.

        setDirections("Dublin Irlanda", "Galway, Irlanda", "es_SP");
      }
    }
    
    function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale , "getSteps":true});
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Estimado becario del MEC, google no reconoce esta direcci&oacute;n para dibujar el recorrido, pudiendo ser que la direcci&oacute;n es muy nueva. No dejes de probar en esta p&aacute;gina para calcular e imprimir tus recorridos por la zona, a tu alojamiento etc. aunque contrates tu curso en otro sitio, usa esta p&aacute;gina con toda libertad.\nCodigo de error: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Codigo de error: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Codigo de error: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Codigo de error: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Codigo de error: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("Una petici&oacute;n de direcci&oacute;n no se puede parsear con &eacute;xito.\n Codigo de error: " + gdir.getStatus().code);
	    
	   else alert("Ha ocurrido un error desconocido.");
	   
	}
	
	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load() results.
	}
	  
	  
///////////////////////////////////////////////////////////////////////
// The add-on code for draggable markers
// Esa 2008
//
	var newMarkers = [];
	var latLngs = [];
	var icons = [];
	
	// Note the 'addoverlay' GEvent listener added inside initialize() function

	function onGDirectionsAddOverlay(){ 
	// Remove the draggable markers from previous function call.
	for (var i=0; i<newMarkers.length; i++) 
	{map.removeOverlay(newMarkers[i]);
	}

	// Loop through the markers and create draggable copies
	for (var i=0; i<=gdir.getNumRoutes(); i++) 
		{
		var originalMarker = gdir.getMarker(i);
		latLngs[i] = originalMarker.getLatLng();
		icons[i] = originalMarker.getIcon();
		newMarkers[i] = new GMarker(latLngs[i],{icon:icons[i], draggable:true});
		map.addOverlay(newMarkers[i]);

		// Get the new waypoints from the newMarkers array and call loadFromWaypoints by dragend
		GEvent.addListener(newMarkers[i], "dragend", function()
		  {
		var points = [];
		for (var i=0; i<newMarkers.length; i++) 
			{
		points[i]= newMarkers[i].getLatLng();
			}
		gdir.loadFromWaypoints(points);
		  });

		//Bind 'click' event to original hidden marker
		copyClick(newMarkers[i],originalMarker);
		
		// hide or remove the original marker
		//originalMarker.hide();
		map.removeOverlay(originalMarker);
		}
		
		function copyClick(newMarker,oldMarker){
		GEvent.addListener(newMarker, 'click', function()
		  {GEvent.trigger(oldMarker,'click');
		  });
		}
// End of draggable markers code
// Esa 2008
///////////////////////////////////////////////////////////////////////
	}	
	
    //]]>
