rg.destination = {

points: [ ],
bounds: new GLatLngBounds(),

showControlsDelayed: function(show, delay) {
        var _this = this;
        clearTimeout(document._timeoutMapControls);
        document._timeoutMapControls = setTimeout(function() { _this.showControls(show) }, delay);
},

showControls: function(show) {
        if (show) {
                this.map.addControl(this.controls.zoom, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,40)));
                this.map.addControl(this.controls.type, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10)));
        } else {
                this.map.removeControl(this.controls.zoom);
                this.map.removeControl(this.controls.type);
        }
},

addMarker: function(x, y, title) {
        var pt = new GLatLng(y, x);
        pt._title = title;
        this.points.push(pt);
        this.bounds.extend(pt);
},

drawMarkers: function() {
        var icon = new GIcon();
        icon.iconSize = new GSize(16, 16);
        icon.iconAnchor = new GPoint(7, 7);

        for(var c = this.points.length - 1; c >= 0; c--) {
                var pt = this.points[c];
                icon.image = '/groups/rg/images/forum/trip_location_marker_' + (c+1) + '.png';
                var marker = new GMarker(pt, { title: pt._title, icon: icon });
                marker._num = c+1;
                this.map.addOverlay(marker);
        }

	if (this.arrow) this.map.addOverlay(this.arrow);
},

showArrow: function() {
        var icon = new GIcon();
        icon.image = 'http://maps.google.com/mapfiles/arrow.png';
        icon.iconSize = new GSize(39, 34);
        icon.iconAnchor = new GPoint(10, 34);
        icon.shadow = 'http://www.google.com/intl/en_us/mapfiles/arrowshadow.png';
        icon.shadowSize = new GSize(39, 34);
        this.arrow = new GMarker(this.points[this.points.length - 1], icon);
},

init: function(p) {
	document.body.onunload = function() { GUnload() };
	var map = this.map = new GMap2(document.getElementById('RGMapDiv'), { });

	var tc = new GMapTypeControl(true);	

	this.controls = { 
		zoom: new GSmallZoomControl(),
		type: tc
	};

	map.addMapType(G_PHYSICAL_MAP);

	map.enableContinuousZoom();
	map.enableDoubleClickZoom();

	map.setCenter(new GLatLng(0, 0), 0);

	var _this = this;
	GEvent.addListener(map, "mouseover", function(point) { _this.showControlsDelayed(true, 300) });
	GEvent.addListener(map, "mouseout", function(point) { _this.showControlsDelayed(false, 500) });
	GEvent.addListener(map, "click", function(marker) { 
		if (marker != null && marker._num) document.location.href = document.getElementById('RGMarker' + marker._num).href;
	});

},

zoomPos: function(x, y, z) {
	this.map.setCenter(new GLatLng(y, x), z);
},

zoomFit: function(extraBounds) {
	var bounds = new GLatLngBounds(this.bounds.getSouthWest(), this.bounds.getNorthEast());

	if (extraBounds) {
	        var m = extraBounds.match(/\((-?[0-9\.]+),(-?[0-9\.]+)\),\((-?[0-9\.]+),(-?[0-9\.]+)\)/);
	        var x2 = m[1];
	        var y2 = m[2];
	        var x1 = m[3];
	        var y1 = m[4];

		bounds.extend(new GLatLng(y1, x1));
		bounds.extend(new GLatLng(y2, x2));
	}

        var b2 = bounds.getNorthEast();
        var b1 = bounds.getSouthWest();

        var zoom = this.map.getBoundsZoomLevel(this.bounds);
        var proj = this.map.getCurrentMapType().getProjection();
        var p1 = proj.fromLatLngToPixel(b1, zoom);
        var p2 = proj.fromLatLngToPixel(b2, zoom);

	var point = p1.x == p2.x && p1.y == p2.y;

        var sx = p1.x <= p2.x ? 1 : -1;
        var sy = p1.y <= p2.y ? 1 : -1;
        p1.x -= 16*sx; p1.y -= 24*sy;
        p2.x += 16*sx; p2.y += 24*sy;

        var rBounds = new GLatLngBounds(proj.fromPixelToLatLng(p1, zoom), proj.fromPixelToLatLng(p2, zoom));
        this.map.setCenter(rBounds.getCenter(), point ? 7 : this.map.getBoundsZoomLevel(rBounds));

},

zoomBox: function(txt) {
	var m = txt.match(/\((-?[0-9\.]+),(-?[0-9\.]+)\),\((-?[0-9\.]+),(-?[0-9\.]+)\)/);
	var x2 = m[1];
	var y2 = m[2];
	var x1 = m[3];
	var y1 = m[4];

        var bounds = new GLatLngBounds(new GLatLng(y1, x1), new GLatLng(y2, x2));
        this.map.setCenter(bounds.getCenter(), this.map.getBoundsZoomLevel(bounds));
        var diff = y1 - this.map.getBounds().getSouthWest().y;
        //this.map.setCenter(new GLatLng(bounds.getCenter().y + diff, bounds.getCenter().x));
}


};
