var GMaps = {};
GMaps = 
{
    hnd: false,
    markers: [],
    marker_html: [],
    marker_name: [],
    points: [],
	marker_bgs : {
		'blue'  : '/resources/gmaps/gmap_icon_blue.png'
	},
	marker_overlays : {
		'arkenford' : '/resources/gmaps/arkenford_a.png',
		'train' : '/resources/gmaps/train.png'
	},
    num_markers: 0,
    compat_check: function () {
        return GBrowserIsCompatible();
    },
    initalize_map: function (map_div, center_lat, center_lng, zoom) {
        $map = document.getElementById(map_div);
		GMaps.hnd = new GMap2($map);
		$map.style.backgroundImage = "url(/resources/gmaps/map_bg.png)";
        GMaps.hnd.addControl(new GLargeMapControl());
        GMaps.hnd.addControl(new GMapTypeControl());
        GMaps.hnd.setCenter(new GLatLng(center_lat, center_lng), zoom);
	},
    directions_from_here: function (marker_i) {
        GMaps.markers[marker_i].openInfoWindowHtml(GMaps.show_directions_html(marker_i, 'saddr'));
    },
    directions_to_here: function (marker_i) {
        GMaps.markers[marker_i].openInfoWindowHtml(GMaps.show_directions_html(marker_i, 'daddr'));
    },
    show_directions_html: function (marker_i, type) {
        var html = GMaps.marker_html[marker_i];
        var opp_type = '';
        if (type === 'daddr') {
            html = html + '<br>Directions: <b>To here<\/b> - <a href="javascript:GMaps.directions_from_here(' + marker_i + ')">From here<\/a><br>Start address:';
            opp_type = 'saddr';
        }
        else {
            html = html + '<br>Directions: <a href="javascript:GMaps.directions_to_here(' + marker_i + ')">To here<\/a> - <b>From here<\/b><br>End address:';
            opp_type = 'daddr';
        }
        return html + '<form action="http://maps.google.com/maps" method="get" target="_blank"><input type="text" SIZE=40 MAXLENGTH=40 name="' + opp_type + '" id="' + opp_type + '" value="" /><br>' + '<INPUT value="Get Directions" TYPE="SUBMIT">' + '<input type="hidden" name="' + type + '" value="' + GMaps.marker_name[marker_i] + ' @' + GMaps.points[marker_i].lat() + ',' + GMaps.points[marker_i].lng() +
        // "(" + name + ")" + 
        '"/>';
    },
    create_marker: function (lat, lng, name, html, marker_overlay, marker_bg) {
        var marker_i = GMaps.num_markers;
		GMaps.points[marker_i] = new GLatLng(lat, lng);
		var mylabel = {"url":GMaps.marker_overlays[marker_overlay], "anchor":new GLatLng(4,4), "size":new GSize(12,12)};
        var Icon = new GIcon(G_DEFAULT_ICON, GMaps.marker_bgs[marker_bg], mylabel);
        var marker = new GMarker(GMaps.points[marker_i], Icon);
		GMaps.markers[marker_i] = marker;
        GMaps.marker_html[marker_i] = html;
        GMaps.marker_name[marker_i] = name;
        GEvent.addListener(GMaps.markers[marker_i], "click", function () {
            marker.openInfoWindowHtml(html + '<br>Directions: <a href="javascript:GMaps.directions_to_here(' + marker_i + ')">To here<\/a> - <a href="javascript:GMaps.directions_from_here(' + marker_i + ')">From here<\/a>');
        });
        GMaps.num_markers++;
        return marker_i;
    },
    add_marker: function (marker_i) {
        GMaps.hnd.addOverlay(GMaps.markers[marker_i]);
    }
};
