
// create array of locations
var locations = [
	// oak ridge
	{
		arrLngLat: [36.0097715,-84.2649279],
		strMsgHTML: '<p><strong>ORAU</strong><br />100 ORAU Way<br /> Oak Ridge, TN 37830</p>',
		strAddress: '100 ORAU Way Oak Ridge TN 37830',
		strAirport: 'McGhee Tyson Airport, Alcoa Highway, Knoxville, TN',
		arrAirportLngLat: [35.806355,-83.990682],
		eMapTrigger: 'map1',
		eDirTrigger: 'dir1'
	},
	// arvada
	{
		arrLngLat: [39.844625,-105.10986],
		strMsgHTML: '<p><strong>Arvada, CO, Worker Health Surveillance</strong><br />9950 W. 80th Avenue, Suite 17<br />Arvada, CO 80005</p>',
		strAddress: '9950 W. 80th Avenue Arvada, CO 80005',
		strAirport: 'Denver International Airport Denver CO',
		arrAirportLngLat: [],
		eMapTrigger: 'map2',
		eDirTrigger: 'dir2'
	},
	// washington1
	{
		arrLngLat: [38.880529,-77.112987],
		strMsgHTML: '<p><strong>National Security and Emergency Management</strong><br />4301 Wilson Boulevard Suite 300<br />Arlington, VA 22203</p>',
		strAddress: '4301 Wilson Boulevard Suite 300 Arlington VA 22203',
		strAirport: 'Ronald Reagan National Airport Arlington VA',
		arrAirportLngLat: [38.872592,-77.034073],
		strAltAirport: 'Dulles International Airport',
		arrAltAirportLngLat: [38.97702,-77.428058],
		//arrAltAirportLngLat: [38.924161,-77.044029],
		eMapTrigger: 'map3',
		eDirTrigger: 'dir3a'
	},	
	// washington2
	{
		arrLngLat: [38.880529,-77.112987],
		strMsgHTML: '<p><strong>National Security and Emergency Management</strong><br />4301 Wilson Boulevard Suite 300<br />Arlington, VA 22203</p>',
		strAddress: '4301 Wilson Boulevard Suite 300 Arlington VA 22203',
		strAirport: 'Dulles International Airport',
		arrAirportLngLat: [38.97702,-77.428058],
		//arrAirportLngLat: [38.924161, -77.044029],
		strAltAirport: '1 Aviation Circle Washington D.C. 20001',
		arrAltAirportLngLat: [38.924161,-77.044029],
		eMapTrigger: 'map3',
		eDirTrigger: 'dir6a'
	},
	// cincinnati
	{
		arrLngLat: [39.162472,-84.454374],
		strMsgHTML: '<p><strong>Cincinnati, Ohio Dose Reconstruction</strong><br />4850 Smith Road, Suite 200<br />Cincinnati, OH 45212</p>',
		strAddress: '4850 Smith Road Cincinnati, OH 45212',
		//strAirport: '3286 Loomis Road Cincinnati OH 45275', //Cincinnati/Northern Kentucky 
		strAirport: 'Cincinnati Northern Kentucky International Airport', //Cincinnati/Northern Kentucky 
		arrAirportLngLat: [39.055718,-84.649315],
		eMapTrigger: 'map4',
		eDirTrigger: 'dir4'	
	},
	//aberdeen
	{
		arrLngLat: [39.467492,-76.235842],
		strMsgHTML: '<p><strong>U.S. Army Research Participation Programs</strong><br />4692 Millennium Drive Suite 101<br />Belcamp, MD 21017</p>',
		//strAddress: '4692 Millennium Drive Belcamp MD',
		strAddress: 'ORAU Belcamp Maryland',
		strAirport: 'Baltimore-Washington International Airport Maryland', //Baltimore-Washington 
		arrAirportLngLat: [39.1855,-76.675107],
		eMapTrigger: 'map5',
		eDirTrigger: 'dir5'	
	},
	// stafford
	{
		arrLngLat: [38.354008,-77.512257],//(38.35355388466458, -77.51172065734863)
		strMsgHTML: '<p>5 Le Way Drive<br />Suite 125<br />Fredericksburg, VA 22406</p>',
		strAddress: '5 Le Way Drive Fredericksburg VA 22406',
		strAirport: 'Ronald Reagan National Airport, Arlington VA',
		arrAirportLngLat: [38.861298,-77.038622],
		eMapTrigger: 'map7',
		eDirTrigger: 'dir7a'
	},
	// sacramento
	{
		arrLngLat: [38.571253,-121.304512],
		strMsgHTML: '<p>3650 Schriever Ave.<br />Mather, California 95655</p>',
		strAddress: '3650 Schriever Ave. Mather, CA 95655',
		strAirport: 'Sacramento International Airport', // Airport Sacramento CA
		arrAirportLngLat: [38.691941,-121.583118],
		eMapTrigger: 'map8',
		eDirTrigger: 'dir8'
	}
];



// run when DOM is ready
	$(document).ready(function() {

	    // default values	
	    var eMap = document.getElementById('map');
	    var eDirections = document.getElementById('directions');

	    var objSearchOptions = {
	        resultList: eDirections,
	        searchFormHint: "Search nearby (i.e.; Hotels)",
	        suppressInitialResultSelection: true
	    };

	    // getMap
	    function getMap(objLocation) {
	        GEvent.addDomListener(document.getElementById(objLocation.eMapTrigger), 'click', function() {
	            var point = new GLatLng(objLocation.arrLngLat[0], objLocation.arrLngLat[1]);
	            var html = objLocation.strMsgHTML;
	            var marker = new GMarker(point);
	            var map = new GMap2(eMap);
	            map.setCenter(point, 15);
	            map.addControl(new GSmallMapControl());
	            map.addControl(new GMapTypeControl());
	            map.enableScrollWheelZoom();
	            map.addOverlay(marker);
	            var localSearch = new google.maps.LocalSearch(objSearchOptions);
	            map.addControl(localSearch);
	            GEvent.addListener(marker, "click", function() {
	                marker.openInfoWindowHtml(html);
	            });
	        });

	    }

	    // getDirections
	    function getDirections(objLocation) {
	        GEvent.addDomListener(document.getElementById(objLocation.eDirTrigger), 'click', function() {
	            eDirections.innerHTML = '';
	            var oMap = new GMap2(eMap);
	            var oDirections = new GDirections(oMap, eDirections);
				
				//error handler  
     			GEvent.addListener(oDirections, "error", // handle errors
					function handleErrors(){
			 			if (oDirections.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
						   alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + oDirections.getStatus().code);
						 else if (oDirections.getStatus().code == G_GEO_SERVER_ERROR)
						   alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + oDirections.getStatus().code);
						 else if (oDirections.getStatus().code == G_GEO_MISSING_QUERY)
						   alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + oDirections.getStatus().code);
						 else if (oDirections.getStatus().code == G_GEO_BAD_KEY)
						   alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + oDirections.getStatus().code);
						 else if (oDirections.getStatus().code == G_GEO_BAD_REQUEST)
						   alert("A directions request could not be successfully parsed.\n Error code: " + oDirections.getStatus().code);
						 else alert("An unknown error occurred.");
		 				}); 
				
	            oDirections.load("from: " + objLocation.strAirport + " to: " + objLocation.strAddress + "");
	        });
	    }
		
		

	    // initialize page view with Oak Ridge map
	    function initPageView() {
	        var point = new GLatLng(locations[0].arrLngLat[0], locations[0].arrLngLat[1]);
	        var html = locations[0].strMsgHTML;
	        var marker = new GMarker(point);
	        var map = new GMap2(eMap);
	        map.setCenter(point, 15);
	        map.addControl(new GSmallMapControl());
	        map.addControl(new GMapTypeControl());
	        map.enableScrollWheelZoom();
	        var localSearch = new google.maps.LocalSearch(objSearchOptions);
	        map.addControl(localSearch);
	        GEvent.addListener(marker, 'click', function() {
	            marker.openInfoWindowHtml(html);
	        });
	        map.addOverlay(marker);
	    }


	    function initialize() {
	        if (GBrowserIsCompatible()) {

	            // init page view with main campus location
	            initPageView();

	            // add click handlers to links for other maps
	            for (i = 0; i < locations.length; i++) {
	                var location = locations[i];
	                getMap(location);
	            };

	            // add  event listeners for directions links
	            for (i = 0; i < locations.length; i++) {
	                var location = locations[i];
	                getDirections(location);
	            };


	        } // end isCompatible
	    } // end initialize

	    google.setOnLoadCallback(initialize);

		// init locationsMenu accordion widget 
		$('#locationsMenu').accordion({
			active: true,
			alwaysOpen: false,
    		header: '.trigger', 
    		event: 'click', 
			autoHeight: false ,
			clearStyle: true,
			selectedClass: 'active'						  
		});
		//$('#locationsMenu').accordion("activate", 0);

	}); // end jQuery DOM ready	

	$(document.body).unload(function() {
	if (GBrowserIsCompatible()) {
	GUnload();
		}
	});
	
/*	
// Reverse GeoCoding Example

var map;
var geocoder;
var address;

function initialize() {
  map = new GMap2(document.getElementById("map_canvas"));
  map.setCenter(new GLatLng(40.730885,-73.997383), 15);
  map.addControl(new GLargeMapControl);
  GEvent.addListener(map, "click", getAddress);
  geocoder = new GClientGeocoder();
}

function getAddress(overlay, latlng) {
  if (latlng != null) {
    address = latlng;
    geocoder.getLocations(latlng, showAddress);
  }
}

function showAddress(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(
        '<b>orig latlng:</b>' + response.name + '<br/>' + 
        '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
        '<b>Status Code:</b>' + response.Status.code + '<br>' +
        '<b>Status Request:</b>' + response.Status.request + '<br>' +
        '<b>Address:</b>' + place.address + '<br>' +
        '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
  }
}*/
