/*
This Javascript is based on code provided by the
Blackpool Community Church Javascript Team
http://www.commchurch.freeserve.co.uk/ 
http://econym.googlepages.com/index.htm
*/

var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;
var lastlinkid;
var to_htmls = [];
var from_htmls = [];
var gdir;


function createMarker(point,name,info) {
	var letter = String.fromCharCode("A".charCodeAt(0) + i);
	var myIcon = new GIcon(G_DEFAULT_ICON, "http://www.google.com/mapfiles/marker" + letter + ".png");
	myIcon.printImage = "http://maps.google.com/mapfiles/marker"+letter+"ie.gif"
	myIcon.mozPrintImage = "http://maps.google.com/mapfiles/marker"+letter+"ff.gif"
	var marker = new GMarker(point, {icon:myIcon});
	var linkid = "link"+i;
	var html = "<b>" + letter + ": " + name + "</b";
	to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
	'<br>Start address:<form action="javascript:getDirections()">' +
	'<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
	'<INPUT value="Get Directions" TYPE="SUBMIT">' +
	'<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
	'"/>';
	from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
	'<br>End address:<form action="javascript:getDirections()">' +
	'<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
	'<INPUT value="Get Directions" TYPE="SUBMIT">' +
	'<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
	'"/>';
	html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';		
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
		document.getElementById(linkid).style.background="#ffff00";
		lastlinkid=linkid;
	});
	GEvent.addListener(marker, "mouseover", function() {
		marker.setImage("http://media.sjnp.net/images/highlight.png");
		document.getElementById(linkid).style.background="#ffff00";
	});
	GEvent.addListener(marker, "mouseout", function() {
		marker.setImage("http://www.google.com/mapfiles/marker" + letter + ".png");
		document.getElementById(linkid).style.background="#ffffff";
	});
	gmarkers[i] = marker;
	htmls[i] = html;
	side_bar_html += '<li id="'+linkid+'" onMouseOver="this.style.background=\'#ffff00\';gmarkers['+i+'].setImage(\'http://media.sjnp.net/images/highlight.png\')" onMouseOut="this.style.background=\'#fff\';gmarkers['+i+'].setImage(\'http://www.google.com/mapfiles/marker' + letter + '.png\')" ><div class="name"><a href="javascript:myclick(' + i + ')" onMouseOver="" onMouseOut="">' + name + '</a></div>' + info + '</li>';
	i++;
	return marker;
}

function mouseOver (id) {
	
}

function getDirections() {
	var saddr = document.getElementById("saddr").value
	var daddr = document.getElementById("daddr").value
	gdir.load("from: "+saddr+" to: "+daddr,{preserveViewport:true});
}

function myclick(i) {
	GEvent.trigger(gmarkers[i], "click");
	location.href='#map';
}

function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
	document.getElementById(lastlinkid).style.background="#ffff00";
}

function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
	document.getElementById(lastlinkid).style.background="#ffff00";
}

function createMap() {
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	mapCenter(map);
	gdir=new GDirections(map, document.getElementById("directions"));
	var reasons=[];
	reasons[G_GEO_SUCCESS]            = "Success";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
	reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
	reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
	reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
	GEvent.addListener(gdir, "error", function() {
		var code = gdir.getStatus().code;
		var reason="Code "+code;
		if (reasons[code]) {
			reason = reasons[code]
		} 
		alert("Failed to obtain directions, "+reason);
	});
	addPoints(map);
	document.getElementById("sponsors").innerHTML = side_bar_html;
	GEvent.addListener(map,"infowindowclose", function() {
		document.getElementById(lastlinkid).style.background="#ffffff";
	});
}