// Needs the GoogleMap scripts to be loaded first!
if (typeof(GMap2) !== 'undefined') {
    var GLocMap = function() {
        this.map = null;
        this.markerFieldId = null;
        this.addressFieldId = null;
        this.latLngSeparator = null;
        this.marker = null;
    }

    GLocMap.prototype = new Object();

    // args:
    // divId: id of the div container for the map
    // verticesFieldId: id of the element which contains the polygon vertices
    // zoom: initial zoom level of the map
    // title: title to show on hover over marker
		// prem: whether premium or not
    // 
    GLocMap.prototype.initialize = function(args) {
        if (!GBrowserIsCompatible()) 
            return;
        
        this.markerFieldId = args.markerFieldId;
        this.addressFieldId = args.addressFieldId;
        this.latLngSeparator = args.latLngSeparator;
        
        this.map = new GMap2($get(args.divId));
        this.map.addMapType(G_PHYSICAL_MAP);
        this.map.setCenter(toGLatLng($get(this.markerFieldId).value, this.latLngSeparator), args.zoom, G_PHYSICAL_MAP);
        this.map.addControl(new GSmallMapControl());
        this.map.addControl(new GMapTypeControl());
        this.map.addControl(new GOverviewMapControl());
        
        this.marker = new GMarker(this.map.getCenter(), {title: args.title, icon: createIcon(args.prem)});
        this.map.addOverlay(this.marker);
        //this.map.addOverlay(new GMarker(this.map.getCenter())); // helps center the campsiteIcon
        GEvent.addListener(this.marker, "mouseover", function () { this.setImage(this.getIcon().hoverImg); });
        GEvent.addListener(this.marker, "mouseout", function () { this.setImage(this.getIcon().image); });
    }
    
    function createIcon(isPrem) {
        var icon = new GIcon();
        icon.image = isPrem
					? "/style/img/camping-map-icon-premium.gif"
					: "/style/img/camping-map-icon-current.gif";
        icon.iconAnchor = new GPoint(9, 11);
        icon.hoverImg = icon.image.replace(/.gif/, '-on.gif');
        return icon;
    }
    
    function toGLatLng(latLngStr, separator) {
        var latlng = latLngStr.split(separator);
	    if (latlng && latlng.length == 2)
            return new GLatLng(latlng[0], latlng[1]);
        return new GLatLng(0, 0);
    }
}
