
function load( )
{
	if( GBrowserIsCompatible( ) )
	{
		var map = new GMap2(document.getElementById("map"));
		map.enableDoubleClickZoom( );
		map.disableContinuousZoom( );
		map.addControl( new GMapTypeControl( ) );
		map.addControl( new GLargeMapControl( ) );
		map.addControl( new GScaleControl( ) );
		map.addControl( new GOverviewMapControl( ) );
		map.setCenter( new GLatLng( 40.0, -95 ), 4 );

		var icon = new GIcon( );
		icon.image = "http://www.lairdplastics.com/locations/images/GMarker.png";
		icon.shadow = "http://www.lairdplastics.com/locations/images/shadow50.png";
		icon.iconSize = new GSize( 20, 34 );
		icon.shadowSize = new GSize( 37, 34 );
		icon.iconAnchor = new GPoint( 9, 33 );
		icon.infoWindowAnchor = new GPoint( 9, 2 );

		GEvent.addListener( map, "click",	function( marker )
							{
								if( marker && "getTitle" in marker )
								{
									var id = marker.getTitle( );
									var point = marker.getPoint( );

									var HTML;

									HTML  = '<div class="infoWindow">';

									HTML += '<div id="mini' + BUnits[id]["num"] + '" class="miniMap"></div>';
									HTML += '<strong>' + id + '</strong><br/>';

									HTML += BUnits[id]["add1"] + '<br/>';
									if( BUnits[id]["add2"] )
										HTML += BUnits[id]["add2"] + '<br/>';
									if( BUnits[id]["add3"] )
										HTML += BUnits[id]["add3"] + '<br/>';
									if( BUnits[id]["add4"] )
										HTML += BUnits[id]["add4"] + '<br/>';

									HTML += BUnits[id]["city"] + ', ' + BUnits[id]["state"] + ' ' + BUnits[id]["zip"] + '<br/>';
									if( BUnits[id]["tol"] || BUnits[id]["fon"] || BUnits[id]["fax"] )
									{
										HTML += '<div class="phones">';
										if( BUnits[id]["tol"] != "/" && BUnits[id]["tol"] != "" )
											HTML += 'T: ' + BUnits[id]["tol"] + '<br/>';
										if( BUnits[id]["fon"] != "/" )
											HTML += 'L: ' + BUnits[id]["fon"] + '<br/>';
										if( BUnits[id]["fax"] != "/" )
											HTML += 'F: ' + BUnits[id]["fax"] + '<br/>';
										HTML += '</div>';
									}

									HTML += '<div class="links">';
									if( BUnits[id]["website"] )
										HTML += '<a href="http://' + BUnits[id]["website"] + '">Homepage</a> ';
									if( BUnits[id]["email"] )
										HTML += '<a href="mailto:' + BUnits[id]["email"] + '?subject=Location%20Inquiry">eMail</a> ';
									HTML += '<a href="#" id="zoom' + BUnits[id]["num"] + '">Zoom In</a>';
									HTML += '</div>';

									HTML += '</div>';

									marker.openInfoWindowHtml(	HTML );
									
									setTimeout( 
												function( )
												{
													document.getElementById( "zoom" + BUnits[id]["num"] ).onclick = function( ) { map.panTo( point ); map.setZoom( 17 ); return false; };
													
													var miniMap = new GMap2( document.getElementById( "mini" + BUnits[id]["num"] ) );
													miniMap.setCenter( point, 16 );
													miniMap.setMapType( G_HYBRID_MAP );
													miniMarker = new GMarker( point, icon );
													miniMap.addOverlay( miniMarker );
												}, 1000 );
								}

								return true;
							} );

		var markers = [];
		for( centerName in BUnits )
		{
			BUnits[centerName].point = new GLatLng( BUnits[centerName].lat, BUnits[centerName].lng );
			BUnits[centerName].marker = new GMarker( BUnits[centerName].point, { icon: icon, title: centerName } );
			map.addOverlay( BUnits[centerName].marker );
		}
		

		var nwPoint = new GLatLng( 46.255846818480315, -108.7646484375 );
		var swPoint = new GLatLng( 34.2, -105.5 );
		var nePoint = new GLatLng( 42, -79.3 );
		var sePoint = new GLatLng( 32.6, -88 );

		document.getElementById( "northwest_zoom" ).onclick = function( ) { map.setZoom( 5 ); map.panTo( nwPoint ); return false; };
		document.getElementById( "southwest_zoom" ).onclick = function( ) { map.setZoom( 5 ); map.panTo( swPoint ); return false; };
		document.getElementById( "northeast_zoom" ).onclick = function( ) { map.setZoom( 5 ); map.panTo( nePoint ); return false; };
		document.getElementById( "southeast_zoom" ).onclick = function( ) { map.setZoom( 5 ); map.panTo( sePoint ); return false; };
		var geocoder = new GClientGeocoder( );
		if( document.getElementById( "findlocation" ) )
			document.getElementById( "findlocation" ).onclick =
			function( )
			{
				geocoder.getLatLng( document.getElementById( "saddr" ).value, 
				function( point )
				{
					/* Initialize to headquarters in case geocoding doesn't pan out. */
					var closestBranch = "Corporate Headquarters";
					var closestBranchDistance = 3000;
					var branchDistance = 0;
	
					if( point )
					{
						var lat1 = point.lat( ) / 57.2958;
						var lat2 = 0;
						var lng1 = point.lng( ) / 57.2958;
						var lng2 = 0;

						for( centerName in BUnits )
							if( centerName != "Corporate Headquarters" )
							{

								lat2 = BUnits[centerName].lat / 57.2958;
								lng2 = BUnits[centerName].lng / 57.2958;

								branchDistance = Math.acos( Math.cos( lat1 ) * Math.cos( lng1 ) * Math.cos( lat2 ) * Math.cos( lng2 ) + Math.cos( lat1 ) * Math.sin( lng1 ) * Math.cos( lat2 ) * Math.sin( lng2 ) + Math.sin( lat1 ) * Math.sin( lat2 ) ) * 3963.189

								if( branchDistance < closestBranchDistance )
								{
									closestBranchDistance = branchDistance;
									closestBranch = centerName;
								}
							}
					}

					GEvent.trigger( map, "click", BUnits[closestBranch].marker );
				} ); // geocoder.getLatLng
				return false;
			}; // document.getElementById( "findlocation" ).onclick
	} // if( GBrowserIsCompatible( ) )
} // function load( )
