// © 2009 Eric P. Grove. All rights reserved.

var map;
var privateIcon;
var publicIcon;
var bounds;
var markers;
var reqId = 0;
var statusControl;

function onLoad()
{
	if( GBrowserIsCompatible() )
	{
		onResize();
		
		map = new GMap2( document.getElementById( "map" ) );
		map.addControl( new GLargeMapControl() );
		map.addControl( new GMapTypeControl() );
		map.addControl( new GScaleControl() );
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();

		var icon = new GIcon();
		icon.shadow = "/CampgroundMap/images/marker_shadow.png";
		icon.iconSize = new GSize( 35, 35 );
		icon.shadowSize = new GSize( 52, 35 );
		icon.iconAnchor = new GPoint( 17, 35 );
		icon.infoWindowAnchor = new GPoint( 18, 1 );
		privateIcon = new GIcon( icon );
		privateIcon.image = "/CampgroundMap/images/private_marker.png";
		publicIcon = new GIcon( icon );
		publicIcon.image = "/CampgroundMap/images/public_marker.png";
		
		statusControl = new StatusControl();

		GEvent.addListener( map, "moveend", function()
		{
			var center = map.getCenter();
			document.getElementById( "link" ).href = "http://www.epgsoft.com/servlet/epgSoft.CampgroundMap.BookmarkServlet?lat=" + center.lat() + "&lng=" + center.lng() + "&zoom=" + map.getZoom();
			
			if( map.getZoom() < 10 )
			{
				statusControl.setText( "Zoom in to see campgrounds" );
				map.addControl( statusControl );
				map.clearOverlays();
				bounds = null;
				markers = null;
				return;
			}

			if( bounds != null && bounds.containsBounds( map.getBounds() ) )
				return;

			statusControl.setText( "Updating..." );
			map.addControl( statusControl );

			bounds = map.getBounds();
			span = bounds.toSpan();
			southWest = bounds.getSouthWest();
			northEast = bounds.getNorthEast();
			southWest = new GLatLng( southWest.lat() - ( span.lat() / 2 ), southWest.lng() - ( span.lng() / 2 ) );
			northEast = new GLatLng( northEast.lat() + ( span.lat() / 2 ), northEast.lng() + ( span.lng() / 2 ) );
			bounds.extend( southWest );
			bounds.extend( northEast );

			reqId++;
			GDownloadUrl( "/servlet/epgSoft.CampgroundMap.CampgroundsServlet?reqId=" + reqId + "&minLat=" + southWest.lat() + "&minLng=" + southWest.lng() + "&maxLat=" + northEast.lat() + "&maxLng=" + northEast.lng(), function( data, responseCode )
			{
				if( bounds == null )
					return;
					
				var xml = GXml.parse( data );
				
				if( xml.documentElement.getAttribute( "reqId" ) != reqId )
					return;

				var campgrounds = xml.documentElement.getElementsByTagName( "campground" );
				var newMarkers = new Array( campgrounds.length );

				for( var i = 0; i < campgrounds.length; i++ )
				{
					var lat = parseFloat( campgrounds[ i ].getAttribute( "lat" ) );
					var lng = parseFloat( campgrounds[ i ].getAttribute( "lng" ) );

					if( markers != null )
					{
						var j;
						for( j = 0; j < markers.length; j++ )
						{
							if( markers[ j ] != null && markers[ j ].lat == lat && markers[ j ].lng == lng )
								break;
						}
						if( j < markers.length )
						{
							newMarkers[ i ] = markers[ j ];
							markers[ j ] = null;
							continue;
						}
					}

					newMarkers[ i ] = new Object();
					newMarkers[ i ].lat = lat;
					newMarkers[ i ].lng = lng;
					newMarkers[ i ].marker = addCampground( campgrounds[ i ], lat, lng );
				}

				if( markers != null )
				{
					for( var j = 0; j < markers.length; j++ )
					{
						if( markers[ j ] != null )
							map.removeOverlay( markers[ j ].marker );
					}
				}

				markers = newMarkers;

				map.removeControl( statusControl );
			  } );
		} );

		var init = document.getElementById( "init" );
		map.setCenter( new GLatLng( parseFloat( init.getAttribute( "lat" ) ), parseFloat( init.getAttribute( "lng" ) ) ), parseInt( init.getAttribute( "zoom" ) ) );
	}
}

function onResize()
{
	var element = document.getElementById( "map" );
	element.style.width = Math.max( ( ( window.innerWidth != undefined ) ? window.innerWidth : document.body.clientWidth ) - 420, 522 ) + "px";
	element.style.height = Math.max( ( ( window.innerHeight != undefined ) ? window.innerHeight : document.body.clientHeight ) - 240, 400 ) + "px";
}

function StatusControl()
{
}

StatusControl.prototype = new GControl();

StatusControl.prototype.setText = function( text )
{
	this.text = text;
}

StatusControl.prototype.initialize = function( map )
{
	var div = document.createElement( "div" );
	div.style.backgroundColor = "#FF0000";
	div.style.color = "#FFFFFF";
	div.style.fontWeight = "bold";
	div.style.padding = "4px 8px";
	div.appendChild( document.createTextNode( this.text ) );
	map.getContainer().appendChild( div );
	return div;
}

StatusControl.prototype.getDefaultPosition = function() 
{
	var size = map.getSize();
	return new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize( ( size.width ) / 2, ( size.height ) / 2 ) );
}

function addCampground( campground, lat, lng )
{
	var marker = new GMarker( new GLatLng( lat, lng ), ( campground.getAttribute( "type" ) == "private" ) ? privateIcon : publicIcon );
	map.addOverlay( marker );

	GEvent.addListener( marker, "click", function()
	{
		var html = '<div style="xwhite-space:nowrap;">';
		var name = campground.getAttribute( "name" );
		html += '<span class="name">' + name + '</span>';
		var street = campground.getAttribute( "street" );
		if( street != null )
			html += '<br>' + street;
		var city = campground.getAttribute( "city" );
		var state = campground.getAttribute( "state" );
		if( city != null && state != null )
		{
			html += '<br>' + city + ', ' + state;
			var zip = campground.getAttribute( "zip" );
			if( zip != null )
				html += ' ' + zip;
		}
		var phone = campground.getAttribute( "phone" );
		if( phone != null )
			html += '<br>' + phone;
		var email = campground.getAttribute( "email" );
		if( email != null )
			html += '<br><a href="mailto:' + email + '">' + email + '</a>';
		var website = campground.getAttribute( "website" );
		if( website != null )
			html += '<br><br><a href="http://' + website + '" target="_blank">' + name + ' Website</a>';
		html += '<br>'
		var rvparkreviews = campground.getAttribute( "rvparkreviews" );
		if( rvparkreviews != null )
			html += '<br><a href="http://www.rvparkreviews.com/regions/' + rvparkreviews + '" target="_blank">RV Park Reviews</a>';
		html += '<br><a href="http://www.rv.net/forum/index.cfm/fuseaction/search/parms/sw{' + name + '}|km{exact}|kl{m}|fm{29}|pd{365}|ma{}/sr/1.cfm" target="_blank">RV.Net Open Roads Forum Search</a>';
		html += '<br><a href="http://www.google.com/search?q=' + name + ' ' + state + '" target="_blank">Google Search</a>';
		html += '<br><br>'
		html += '<span style="font-size: 11px">' + lat + ', ' + lng + '</span>';
		html += '</div>';

		marker.openInfoWindowHtml( html );
	} );

	return marker;
}

function search( location )
{
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng( location, function( point )
	{
		if( point == null )
		{
			statusControl.setText( "Sorry, can't find location." );
			map.addControl( statusControl );
			return;
		}

		map.setCenter( point, 12 );
	} );
}

function showHelp()
{
	document.getElementById( "help" ).style.display = "inline";
}

function hideHelp()
{
	document.getElementById( "help" ).style.display = "none";
}