/**
 * @author Michal Cap
 */

  var map;
  var markers = [];
  var collaborativeMode = false;
  
  function Marker(fLabel, lat, lng, fAreaId, logoUrl, shadowUrl, logoSize)
  {
     this.label = fLabel;
     this.pos = new GLatLng(lat, lng);
     this.areaId = fAreaId;
     this.icon = null;       
     
     var marker = this;       
     
     // Get the icon image
     
     this.icon = new GIcon(G_DEFAULT_ICON);
     
     this.icon.image = logoUrl;
     this.icon.shadow	=  shadowUrl;      
        
     this.icon.iconSize = new GSize(logoSize, logoSize);
     this.icon.shadowSize = new GSize(logoSize*2, logoSize);
     this.icon.iconAnchor = new GPoint(0, logoSize);           
  }   
    
  function mapsLoad() {
    if (GBrowserIsCompatible()) {  
      if (document.getElementById("map") != null) {
  			// There is a map container on the page
  			map = new GMap2(document.getElementById("map"));
  			
  			map.addControl(new GSmallMapControl());
  			map.addControl(new GMapTypeControl());
  			map.enableDoubleClickZoom();
  			map.setCenter(new GLatLng(50, 0), 2);			
  			
  			// Get the markers to display - defined on the respective team, match or competition page
  			loadMarkers(markers);
  			resetView();
	    }
    }
  }
    	
  function mapsUnload()
  {
  	GUnload();
  }	    
    
  /* Places markers on the map, assigns viewport */
  function resetView()
  {
  	map.clearOverlays()    	
  	
    // Place the markers on the map
    setTimeout('addMarkersToMapOnBackground(0)', 500);
    
    // Adjust viewport
    // Show entire world
    map.setCenter(GLatLng(0, 0), 2);    
   	map.setMapType(G_HYBRID_MAP);	 	
  }
    
  function addMarkerToMap(marker)
  {    	
  	if (marker.pos != null) {
  		// Add to the map
  		//alert(document.getElementById("popup-" + marker.areaId));
  		var gMarker = new GMarker(marker.pos, {icon:marker.icon})
  		map.addOverlay(gMarker);  
  		
    	// Set onClick action       
      GEvent.addListener(gMarker, "click", function() {
        gMarker.openInfoWindow(document.getElementById("popup-" + marker.areaId).cloneNode(true));
      });
        		 
  	}	     	
  }  
    
  /**
   * Recursively calls itself resulting in slow adding of Markers. 
   * Waits 100ms between adding two markers.
   * 
   * @param int currentIndex
   */
  
  function addMarkersToMapOnBackground(currentIndex)
  {    	
    // If the marker image is not defined, do not display the marker
    if (markers[currentIndex].icon.image != null)
      addMarkerToMap(markers[currentIndex]);
    
    // Schedule adding of the next one
    if (currentIndex+1 < markers.length)
      setTimeout('addMarkersToMapOnBackground(' + (parseInt(currentIndex) + 1) + ')',25);
    
    map.setCenter(GLatLng(40, 40), 2);    
  }       