
var showTooltip;
var mymouseover;
var mymouseout;
var myclick;


function gloader(xmlfile, x, y, z, current_region, language) {

	var side_bar_html = "";
	var gmarkers = [];
	var htmls = [];
	var i = 0;
   
   
      // ====== This function displays the tooltip ======
      // it can be called from an icon mousover or a side_bar mouseover
       showTooltip = function(marker) {
      	tooltip.innerHTML = marker.tooltip;
		var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
		var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor=marker.getIcon().iconAnchor;
		var width=marker.getIcon().iconSize.width;
		var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x - 5 + width,- offset.y + point.y +anchor.y - 5)); 
		pos.apply(tooltip);
		tooltip.style.visibility="visible";
      }

      // ===== This function is invoked when the mouse goes over an entry in the side_bar =====
      // It launches the tooltip on the icon      
      mymouseover = function(x) {
        showTooltip(gmarkers[x])
      }
      // ===== This function is invoked when the mouse leaves an entry in the side_bar =====
      // It hides the tooltip      
      mymouseout = function() {
        tooltip.style.visibility="hidden";
      }

      // This function picks up the side_bar click and opens the corresponding info window
      myclick = function (x) {
        gmarkers[x].openInfoWindowHtml(htmls[x]);
      }
   
    if (GBrowserIsCompatible()) {
		icon = new GIcon();
		icon.image = "http://www.spanischakademie.de/spanischkurs/prave.png";
		
		icon.iconSize = new GSize(32, 32);
		icon.iconAnchor = new GPoint(16, 16);
		icon.infoWindowAnchor = new GPoint(16, 16);

    // A function to create the marker and set up the event window
      function createMarker(point,name,html,region) {
        var marker = new GMarker(point,icon);
        // === store the name so that the tooltip function can use it ===
        marker.tooltip = '<div class="tooltip">'+name+'</div>';
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        gmarkers[i] = marker;
        htmls[i] = html;
        if (region == current_region){ 
        	side_bar_html += '<a href="javascript:myclick(' + i + ')" onmouseover="mymouseover('+i+')" onmouseout="mymouseout()">' + name + '</a> ';
        } //i++;
        map.addOverlay(marker);

        //  ======  The new marker "mouseover" and "mouseout" listeners  ======
        GEvent.addListener(marker,"mouseover", function() {
          showTooltip(marker);
        });        
        GEvent.addListener(marker,"mouseout", function() {
		  tooltip.style.visibility="hidden"
        });        
      }
     
      // create the map
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(x, y), z);

      // ====== set up marker mouseover tooltip div ======
      var tooltip = document.createElement("div");
      document.getElementById("map").appendChild(tooltip);
      tooltip.style.visibility="hidden";

      // Read the data from INSERT NAME HERE
      var request = GXmlHttp.create();
      request.open("GET", xmlfile, true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = request.responseXML;
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          for (i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
			if (language == 'de'){
                var html = markers[i].getAttribute("html-de");
			}
			if (language == 'es'){
                var html = markers[i].getAttribute("html-es")
			}
			if (language == 'en'){
                var html = markers[i].getAttribute("html-en")
			}
            var label = markers[i].getAttribute("label");
            var region = markers[i].getAttribute("region");
            // create the marker
			
			if(html != null){
              var marker = createMarker(point,label,html,region);
			}
          }
		  
          // put the assembled side_bar_html contents into the side_bar div
          document.getElementById("side_bar").innerHTML = side_bar_html;
          
        }
      }
	  
      request.send(null);
    } else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
 
}

