var LocationMap;

var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;

// functions that open the directions forms
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function createMap()
{
	var MapTag = document.getElementById("GoogleMap"),
		MsgTag = document.getElementById("NoGoogleMap");
	if(GBrowserIsCompatible()) {
		var GoogleMap = new GMap2(MapTag);
		
		GoogleMap.addControl(new GLargeMapControl());
		GoogleMap.addControl(new GMapTypeControl());
		GoogleMap.addControl(new GScaleControl());
		return GoogleMap;
	} else {
		MapTag.style.height = '0px';
		MsgTag.style.display = 'none';
	}
	return false;
}

// Creates a marker at the given point with the given info
function createMarker(map, point, name, html, centre, icon, directions)
{
	if( icon )
	    var marker = new GMarker(point, icon);
	else
	    var marker = new GMarker(point);

    // The info window version with the "to here" form open
    to_htmls[i] = html + "<br />Directions: <b>To here</b> - <a href=\"javascript:fromhere(" + i + ")\">From here</a>" +
       "<br />Start address<form action=\"http://maps.google.co.uk/maps\" method=\"get\" target=\"_blank\">" +
       "<input type=\"text\" size=\"40\" maxlength=\"40\" name=\"saddr\" id=\"saddr\" value=\"\" /><br />" +
       "<input value=\"Get Directions\" type=\"submit\">" +
       "<input type=\"hidden\" name=\"daddr\" value=\"" + point.lat() + "," + point.lng() + 
               "(" + name + ")" + 
       "\"/>";
      
    // The info window version with the "to here" form open
    from_htmls[i] = html + "<br />Directions: <a href=\"javascript:tohere(" + i + ")\">To here</a> - <b>From here</b>" +
       "<br />End address<form action=\"http://maps.google.co.uk/maps\" method=\"get\"\" target=\"_blank\">" +
       "<input type=\"text\" size=\"40\" maxlength=\"40\" name=\"daddr\" id=\"daddr\" value=\"\" /><br />" +
       "<input value=\"Get Directions\" type=\"submit\">" +
       "<input type=\"hidden\" name=\"saddr\" value=\"" + point.lat() + "," + point.lng() +
               "(" + name + ")" + 
       "\"/>";

    // The inactive version of the direction info
    html = "<div class=\"clearFix\" style=\"margin-bottom:10px\">" + html;
    
	switch( directions )
	{
		case 0:
			break;

		case 1:
			html += "<br />Directions: <a href=\"javascript:tohere("+i+")\">To here</a> - <a href=\"javascript:fromhere("+i+")\">From here</a></div>";
		    break;

		default:
		case 2:
			html += "<br /><a target=\"_blank\" href=\"http://maps.google.co.uk/maps?f=q&hl=en&q=" + point.lat() + "," + point.lng() +
			"(" + name + ")" +
			"&ie=UTF8&om=1\" title=\"Get directions to " + name + "\"" +
			"onmouseover=\"window.status='Get directions to " + name + "';return true\" onmouseout=\"window.status='';return true\"" +
			">Directions</a>";
			break;
	}
 
    
    GEvent.addListener(marker, "click", function() {
      	marker.openInfoWindowHtml(html);
    });
//	GEvent.addListener(marker,"mouseover", function() {
//		marker.openInfoWindowHtml(html);
//  });
	
    gmarkers[i] = marker;
    htmls[i] = html;
    i++;
    
	if(centre)
		map.setCenter(point, 14, G_NORMAL_MAP);
	
	map.addOverlay(marker);
    return marker;
}