
// starting point zoom
var sp_zoom = 15;
// starting points
var sp_bristol = new GLatLng(51.45449496, -2.59759068);
var sp_chichester = new GLatLng(50.83737692, -0.77903152); 
var sp_hereford = new GLatLng(52.05229255, -2.71038294); 
//var sp_london = new GLatLng(51.51573334, -0.08400679);
var sp_london = new GLatLng(51.521635, -0.081947);
var sp_loughton = new GLatLng(51.643809, 0.05181);
var sp_redruth = new GLatLng(50.23356360, -5.23076892);
var sp_stroud = new GLatLng(51.744848, -2.219152);
var sp_taunton = new GLatLng(51.01704173, -3.10042977);
var sp_weston = new GLatLng(51.35028933, -2.97956944);

var this_branch = "";
var chosen_postcode = "";

function load(which_branch) {
	this_branch = which_branch;
	var chosen_point = eval(which_branch);
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl (new GLargeMapControl());
		map.setCenter(chosen_point, sp_zoom);
	}	
	icon = new GIcon();
	icon.image = '../_assets/img/maps/map_icon.gif';
	icon.iconSize = new GSize(41,27);
	icon.iconAnchor = new GPoint(16,16);
	icon.shadow = "../_assets/img/maps/map_shadow.png";
	icon.shadowSize = new GSize(51, 37);
	map.clearOverlays();
	var marker = new GMarker(chosen_point,icon);
	gmarkers = marker;
    map.addOverlay(marker);
}

function convert_postcode(postcode_in) {
	var postcode_out = "";
	for (i = 0; i < postcode_in.length; i++) { if (postcode_in.charAt(i) != " ") { postcode_out += postcode_in.charAt(i); } else { postcode_out += "+"; } }
	return postcode_out;
}

function get_directions() {
	
	if (this_branch == "sp_bristol") { chosen_postcode = "BS1 4RD"; }
	if (this_branch == "sp_chichester") { chosen_postcode = "PO19 1LB"; }
	if (this_branch == "sp_hereford") { chosen_postcode = "HR1 2NX"; }
	if (this_branch == "sp_london") { chosen_postcode = "EC2A 2EA"; }
	if (this_branch == "sp_loughton") { chosen_postcode = "IG10 4LT"; }
	if (this_branch == "sp_redruth") { chosen_postcode = "TR15 2BY"; }
	if (this_branch == "sp_stroud") { chosen_postcode = "GL5 3AR"; }
	if (this_branch == "sp_taunton") { chosen_postcode = "TA1 1RT"; }
	if (this_branch == "sp_weston") { chosen_postcode = "BS23 1HQ"; }
	
	var converted_chosen_postcode = convert_postcode(chosen_postcode);
	var converted_entered_postcode = convert_postcode(document.directions.postcode.value);
	if (converted_entered_postcode == "" || converted_chosen_postcode == converted_entered_postcode) {
		var oWin = window.open("http://maps.google.co.uk/maps?q="+converted_chosen_postcode, "_blank");
		if (oWin) { if (oWin.focus) oWin.focus(); return false; }
		oWin = null;	
	} else {	
		var oWin = window.open("http://maps.google.com/maps?saddr="+ converted_entered_postcode + "&daddr="+ converted_chosen_postcode +"&hl=en", "_blank");
		if (oWin) { if (oWin.focus) oWin.focus(); return false; }
		oWin = null;
	}		
}
