// JavaScript Document
// MAPAS Y COORDENADAS OBTENIDAS POR IRLANDA EN RED SL  - GRUPO MUNDO EN RED - PROHIBIDO REPRODUCIR SIN AUTORIZACION ESTE SCRIPT. //
// UNA COPIA DE LAS PRINCIPALES FUNCIONES DE ESTE SCRIPT Y UNO DE EJEMPLO HAN SIDO REGISTRADAS EN SUS DERECHOS DE PROPIEDAD INTELECTUAL EN 2009 EN LA OFICINA DE PATENTES Y MARCAS DE MADRID, Paseo de la Castellana, 75 - 28046 MADRID //
// LA REPRODUCCIÓN TOTAL Y PARCIAL DE ESTE SCRIPT POR PARTE DE EMPRESAS DE LA COMPETENCIA DIRECTA, SERÁ DENUNCIADA SIN REMISIÓN POR SUS DERECHOS DE PROPIEDAD INTELECTUAL, ENFRENTANDOSE A UNA ELEVADA COMPENSACIÓN ECONOMICA //
// ESTE SCRIPT HA SIDO DESARROLLADO POR EL PROPIO EQUIPO DE DESARROLLO DE GRUPO MUNDO EN RED, UTILIZANDO EL API DE GOOGLE, DE USO LIBRE Y GRATUITO. A LAS EMPRESAS DE LA COMPETENCIA LES ROGAMOS QUE SE DESARROLLEN USTEDES SUS PROPIOS SCRIPTS //

    //<![CDATA[

    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(46.747389,3.081665), 5);
	    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.becasfrances.info/images/mec.png";
		mecIcon.shadow = "http://www.becasfrances.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;
        }
				
		// fin pruebas de api google
		
		// FRANCIA
		
		// ESCUELA DE FRANCES 1
		var point = new GLatLng(48.870954,2.346354);
        var address = '<b>ESCUELA MEC A. P. </b><br/><i>Oferta de curso de frances - BECA MEC - Paris</i><br /><a href="http://www.becasfrances.info/detalles/26/paris/accord-paris" title="CURSO MEC EN PARIS, FRANCIA">CURSO MEC EN PARIS, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		// ESCUELA DE FRANCES 1 
		var point = new GLatLng(43.607923,3.880153);
        var address = '<b>ESCUELA MEC A.F.M. </b><br/><i>Oferta de Curso de Franc&eacute;s - BECA MEC - Montpellier</i><br /><a href="http://www.becasfrances.info/detalles/68/montpellier/accent-franais-montpellier" title="CURSO MEC EN MONTPELLIER, FRANCIA">CURSO MEC EN MONTPELLIER, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 
		
		// ESCUELA DE FRANCES 1
		var point = new GLatLng(45.763564,4.84202);
        var address = '<b>ESCUELA MEC L.B.I. </b><br/><i>Oferta de Curso de Franc&eacute;s - BECA MEC - Lyon</i><br /><a href="http://www.becasfrances.info/detalles/72/lyon/lyon-bleu-international" title="CURSO MEC EN LYON, FRANCIA">CURSO MEC EN LYON, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 

		// ESCUELA DE FRANCES 1
		var point = new GLatLng(43.703188,7.277378);
        var address = '<b>ESCUELA MEC I.H.N. </b><br/><i>Oferta de Curso de Franc&eacute;s - BECA MEC - Niza</i><br /><a href="http://www.becasfrances.info/detalles/73/nice/international-house-nice" title="CURSO MEC EN NIZA, FRANCIA">CURSO MEC EN NIZA, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 

		// ESCUELA DE FRANCES 1
		var point = new GLatLng(43.582992,7.127312);
        var address = '<b>ESCUELA MEC C.I.D.A. </b><br/><i>Oferta de Curso de Frances - BECA MEC - Antibes</i><br /><a href="http://www.becasfrances.info/detalles/75/antibes/centre-international-dantibes" title="CURSO MEC EN ANTIBES, FRANCIA">CURSO MEC EN ANTIBES, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 

		// ESCUELA DE FRANCES 1
		var point = new GLatLng(43.47062,-1.56022);
        var address = '<b>ESCUELA MEC B.F.C.I.. </b><br/><i>Oferta de Curso de Frances - BECA MEC - Biarritz</i><br /><a href="http://www.becasfrances.info/detalles/74/biarritz/biarritz-french-course-institute" title="CURSO MEC EN BIARRITZ, FRANCIA">CURSO MEC EN BIARRITZ, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 

		// ESCUELA DE FRANCES 1
		var point = new GLatLng(49.430243,1.090211);
        var address = '<b>ESCUELA MEC F.I.N. </b><br/><i>Oferta de Curso de Franc&eacute;s - BECA MEC - Rouen</i><br /><a href=http://www.becasfrances.info/detalles/128/rouen/french-in-normandy" title="CURSO MEC EN ROUEN, FRANCIA">CURSO MEC EN ROUEN, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 

		// ESCUELA DE FRANCES 1
		var point = new GLatLng(48.554477,7.745388);
        var address = '<b>ESCUELA MEC C.I.E.L. Estrasburgo </b><br/><i>Oferta de Curso de Franc&eacute;s - BECA MEC - Estrasburgo</i><br /><a href="http://www.becasfrances.info/detalles/131/estrasburgo/centre-international-etudes-de-langues-strasbourg" title="CURSO MEC EN ESTRASBURGO, FRANCIA">CURSO MEC EN ESTRASBURGO, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 

		// ESCUELA DE FRANCES 1 
		var point = new GLatLng(48.397292,-4.382094);
        var address = '<b>ESCUELA MEC C.I.E.L. Brest </b><br/><i>Oferta de Curso de Franc&eacute;s - BECA MEC - Brest</i><br /><a href="http://www.becasfrances.info/detalles/132/brest/centre-international-etudes-de-langues-brest" title="CURSO MEC EN BREST, FRANCIA">CURSO MEC EN BREST, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 

		// ESCUELA DE FRANCES 1 
		var point = new GLatLng(43.600381,1.447073);
        var address = '<b>ESCUELA MEC L.O. Toulouse </b><br/><i>Oferta de Curso de Franc&eacute;s - BECA MEC - Toulouse</i><br /><a href="http://www.becasfrances.info/detalles/133/toulouse/langue-onze" title="CURSO MEC EN TOULOUSE, FRANCIA">CURSO MEC EN TOULOUSE, FRANCIA</a>';
		var marker = addtag(point, address);
		map.addOverlay(marker); 



//PRUEBAS DE JAIME 2009 - INSERTAR OTRO ICONO CON LA DIRECCION DEL ALOJAMIENTO:
				
				// Creamos el "HOUSE" marker icon
				var houseIcon = new GIcon();
				
				//GIcon houseIcon = new GIcon();
				houseIcon.image = "http://www.becasfrances.info/images/edificio.png";
				houseIcon.shadow = "http://www.becasfrances.info/images/edificio-sombra.png";
				houseIcon.iconSize = new GSize(32, 32);
				houseIcon.shadowSize = new GSize(56, 32);
				houseIcon.iconAnchor = new GPoint(16, 32);
				houseIcon.infoWindowAnchor = new GPoint(16, 0);
				markerOptions = { icon:houseIcon };

				function addtag(point, address) {
				var marker = new GMarker(point, markerOptions);
				GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(address); } );
				return marker;
				}
						
				//var point = new GLatLng(40.70549,-73.801501);
				//var address = '<b>RESIDENCIA YMCA QUEENS</b><br/><i>Alojamiento secundario de nuestro curso MEC en Nueva York</i><br /><a href="http://www.becasfrances.info/detalles/16/nueva-york/american-language-communication-centre" title="ALOJAMIENTO DE CURSO MEC EN NUEVA YORK">RESIDENCIA CURSO MEC EN NUEVA YORK</a>';
				//var marker = addtag(point, address);
				//map.addOverlay(marker); 
				
				// FIN DE LA PARTE DE ALOJAMIENTOS DE ESTA ESCUELA


      }
    }
	
    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("229 West 36th Street, New York, NY 10018 USA", "5 W 63rd St, New York, NY 10023", "en_US");
      }
    }
    
    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("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + 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 Error code: " + 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 Error code: " + 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 Error code: " + 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 Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}
	
	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
///////////////////////////////////////////////////////////////////////
	}	
	
    //]]>