// Copyright © 2007 RVFamilyFun.com. All rights reserved.

var map;
var marker;
var directions;

var resource_id;
var name;
var lat;
var lng;
var location_verified;

var updatingCtrl;

var markerExt = "_marker.png"; // hack for IE 6

function onLoad()
{
	if( !GBrowserIsCompatible() )
	{
		alert( "This site relies heavily on Google Maps. Unfortunately Google Maps is not compatible with your browser.\nClick OK to see a list of compatible browsers." );
		window.location.assign( "http://local.google.com/support/bin/answer.py?answer=16532&topic=1499" );
	}

	onResize(); // hack for IE 6

	map = new GMap2( document.getElementById( "map" ) );
	map.addControl( new GLargeMapControl() );
	map.addControl( new GHierarchicalMapTypeControl() );
	map.addMapType( G_PHYSICAL_MAP ); 
	map.addControl( new GScaleControl() );
	map.addControl( new GOverviewMapControl() );
	map.enableDoubleClickZoom();
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();

	updatingCtrl = new UpdatingControl();

	var init = document.getElementById( "init" );

	resource_id = init.getAttribute( "resource_id" );
	name = init.getAttribute( "name" );
	
	lat = init.getAttribute( "lat" );
	lng = init.getAttribute( "lng" );
	var latLng = new GLatLng( parseFloat( lat ), parseFloat( lng ) );
	
	map.setCenter( latLng, 11 );
	
	marker = new GMarker( latLng );
	map.addOverlay( marker );

	location_verified = init.getAttribute( "location_verified" );

	if( location_verified != "1" )
		document.getElementById( "sidebar").innerHTML = '<div style="font-weight: bold; font-size: 14px; color: #ea7528">The location of this campground has not been verified by the campground management. We can not guarantee the accuracy of the directions.</div>';
}

function onResize() // hack for IE 6
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 )
	{
		markerExt = "_marker.gif";
		
		var element = document.getElementById( "form" );
		element.style.width = ( document.documentElement.clientWidth - parseInt( element.style.left ) - parseInt( element.style.right ) ) + 'px';
		element.style.overflow = 'hidden';
		
		var element = document.getElementById( "formAd" );
		element.style.width = ( document.documentElement.clientWidth - parseInt( element.style.left ) - parseInt( element.style.right ) ) + 'px';
		element.style.height = ( document.documentElement.clientHeight - parseInt( element.style.top ) - parseInt( element.style.bottom ) ) + 'px';
		
		element = document.getElementById( "sidebar" );
		element.style.bottom = '150px';
		element.style.height = ( document.documentElement.clientHeight - parseInt( element.style.top ) - parseInt( element.style.bottom ) ) + 'px';
		
		element = document.getElementById( "map" );
		element.style.bottom = '45px';
		element.style.width = ( document.documentElement.clientWidth - parseInt( element.style.left ) - parseInt( element.style.right ) ) + 'px';
		element.style.height = ( document.documentElement.clientHeight - parseInt( element.style.top ) - parseInt( element.style.bottom ) ) + 'px';
	}
}

function UpdatingControl()
{
	this.width = 126;
	this.height = 25;
	this.showing = false;
}
UpdatingControl.prototype = new GControl();

UpdatingControl.prototype.initialize = function( map )
{
	var img = document.createElement( "img" );
	img.src = "/images/Updating.gif";
	img.width = this.width;
	img.height = this.height;
	map.getContainer().appendChild( img );
	return img;
}

UpdatingControl.prototype.getDefaultPosition = function() 
{
	var size = map.getSize();

	return new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize( ( size.width - this.width ) / 2, ( size.height - this.height ) / 2 ) );
}

function getDirections()
{
	var from = document.getElementById( "from" ).value;
	if( from.length == 0 )
	{
		alert( "You must enter a From location" );
		return;
	}
	
	var sidebar = document.getElementById( "sidebar");
	
	if( directions == null )
	{
		directions = new GDirections( map, sidebar );  
		
		GEvent.addListener( directions, "load", function()
		{
			map.removeOverlay( marker );
			map.removeControl( updatingCtrl );
			sidebar.innerHTML = "";
		} );
		
		GEvent.addListener( directions, "addoverlay", function()
		{
			var html = "";
			
			if( location_verified != "1" )
				html += '<div style="font-weight: bold; font-size: 14px; color: #ea7528">The location of this campground has not been verified by the campground management. We can not guarantee the accuracy of the directions.</div>';

			html += '<div align="right"><a href="/servlet/DrivingDirectionsServlet?id=' + resource_id + '&from=' + encodeURIComponent( from ) + '&print=1" target="_blank"><img src="/images/bar_icon_print_2.gif" align="absmiddle" border="0"> Print</a></div>';

			var sidebarHtml = sidebar.innerHTML;
			var i = sidebarHtml.indexOf( lat.substr( 0, 5 ) );
			html += sidebarHtml.substring( 0, i ) + name + sidebarHtml.substring( sidebarHtml.indexOf( "<", i ) );
			
			sidebar.innerHTML = html;
		} );
		
		GEvent.addListener( directions, "error", function()
		{
			map.removeControl( updatingCtrl );
 			alert( "Could not find one of the specified locations. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code );
		} );
	}
	
	map.addControl( updatingCtrl );
	directions.load( "from: " + from + " to: " + lat + ", " + lng );
}
