 function createMarker(point, color, desc, links, court_id) 
    {
    	var icon = new GIcon();
		icon.image = baseHrefPath + "/images/map/ico_" + color + ".png";
		icon.shadow = baseHrefPath + "/images/map/ico_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);

		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml(desc + links);
			currentMarker = marker;
			currentIcon = icon;
			currentPoint = point;
			currentDesc = desc;
			currentCourtId = court_id;
			currentLinks = links;
			// select on the list
			deselect_all_courts(window.parent.document.time.elements['fav[]']);
			select_court(window.parent.document.time.elements['fav[]'], court_id);
		});

		GEvent.addListener(marker,"mouseover", function() {
        	  showTooltip(marker);
        });        
	        
        GEvent.addListener(marker,"mouseout", function() {
			tooltip.style.visibility="hidden"
        });   

		return marker;
	}
    
    function createSimpleMarker(point, desc, links, court_id) 
    {
    	var icon = new GIcon();
		icon.image = baseHrefPath + "/images/map/ico_green.png";
		icon.shadow = baseHrefPath + "/images/map/ico_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);

		marker = new GMarker(point, icon);

		GEvent.addListener(marker,"mouseover", function() {
        	  showTooltip(marker);
        });        
	        
        GEvent.addListener(marker,"mouseout", function() {
			tooltip.style.visibility="hidden"
        });   

		return marker;
	}
    
    function switchMarker(marker, add, court_name, court_id, access, member)
    {
    	if (currentMarker) {
    		map.removeOverlay(currentMarker);
    	}

   		tt = marker.tooltip;

    	var desc = currentDesc;
    	if (add == 1) // adding court
    	{
    		var links = "<div class='small-txt'><span style='text-decoration: underline; cursor: pointer;' onclick='mapRemove(window.parent.document.time.elements[\"fav\[\]\"],\""+court_name+"\","+court_id+","+access+","+member+");'>Remove from my list</span></div>";
    		var color = 'red';
    		
    	} else  // removing court
    	{
    		if (access < 4) // add as a member
				var links = "<div class='small-txt'><span style='text-decoration: underline; cursor: pointer;' onclick='mapAdd(window.parent.document.time.elements[\"fav\[\]\"],\""+court_name+"\","+court_id+","+access+",0);'>Add (not a member)</span> <span style='text-decoration: underline; cursor: pointer;' onclick='mapAdd(window.parent.document.time.elements[\"fav\[\]\"],\""+court_name+"\","+court_id+","+access+",1);'>Add (member)</span></div>";
			else
				var links = "<div class='small-txt'><span style='text-decoration: underline; cursor: pointer;' onclick='mapAdd(window.parent.document.time.elements[\"fav\[\]\"],\""+court_name+"\","+court_id+","+access+",2);'>Add to my list</span></div>";
    		var color = 'green';
    	}
    	var marker = createMarker(currentPoint, color, desc, links, court_id);
    	marker.tooltip = tt;
    	map.addOverlay(marker);
	    currentLinks = links;
	    return marker;
    }
    
   function mapRemove(where, court_name, court_id, access, member)
    {
    	select_court(where,court_id);
    	remove_court(where);
    	switchMarker(currentMarker, 0, court_name, court_id, access, member);
    	map.closeInfoWindow();
    }
    
    function mapAdd(where, court_name, court_id, access, member)
    {
    	add_court(court_name, court_id, where, member);
    	switchMarker(currentMarker, 1, court_name, court_id,access, member);
    	map.closeInfoWindow();
    }
    
    function changeMarkerColor(marker,color)
    {
    	marker.iconImage.src = baseHrefPath + "/images/map/ico_" + color + ".png"; 
    }
	
	function addMapPoint(court_id, x, y, color, desc, links, title, access)
	{

		if (courts[court_id]==null) {
				
			courts[court_id]=new Array;
		    courts[court_id][0]=x;
		    courts[court_id][1]=y;
		    courts[court_id][2]=color;
		    courts[court_id][3]=desc;
		    courts[court_id][4]=links;
		    courts[court_id][5]=marker;
		    courts[court_id][6]=access;
		    courts[court_id][7]=title;
		    courts[court_id][8]=court_id; 
		    courts[court_id][9]=false; // was this court displayed? 
		}

		sw = mapRange.getSouthWest()
		ne = mapRange.getNorthEast()
		
		if (!courts[court_id][9] && x>sw.lng() && x<ne.lng() && y>sw.lat() && y<ne.lat()) {
		    point = new GPoint(x, y);	
		    marker = createMarker(point, color,desc, links, court_id);
		
			marker.tooltip = '<div class="tooltip">'+title+'</div>';
	
		    map.addOverlay(marker);
		    
			courts[court_id][9]=true;
			courts[court_id][5]=marker;
		} 
		
	}
	
	  // ====== This function displays the tooltip ======
      // it can be called from an icon mousover or a sidebar mouseover
      function showTooltip(marker) {
    
      	tooltip.innerHTML = marker.tooltip;
		var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
		var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor=marker.getIcon().iconAnchor;
		var width=marker.getIcon().iconSize.width;
		var height=tooltip.clientHeight;
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
		pos.apply(tooltip);
		tooltip.style.visibility="visible";
      }
	
	function showInfo(court_id)
	{
		window.open(baseHref + '?choice=view_court&suppressAll&court_id=' + court_id,'myWin','resizable,scrollbars,width=500,height=400');
		return false;
	}
	
	
	