// JavaScript Document

 var locations = [ 
{
  'id':'1',  
  'address':'Moffett Field, CA 94035',
  'place_name':'Ames Research Center',
  'description':'Ames Research Center',
  'website':'http://www.nasa.gov/centers/ames/home/index.html',
  'visitor':'http://www.nasa.gov/centers/ames/about/index.html',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=NASA+Ames+Research+Center,+Moffett+Field,+California+94035&amp;ie=UTF8&amp;ll=37.463959,-122.033043&amp;spn=0.10982,0.232086&amp;z=12&amp;iwloc=A&amp;om=1',
  'image':'../images/nasa_ARC_center_tn.gif',
  'altText':'Ames Research Center (ARC)',
  'title':'Ames Research Center (ARC)',
  'type':'1'
}

,

{
  'id':'2',  
  'address':'Lilly Dr Bldg 4800 Edwards, CA 93523',
  'place_name':'Dryden Flight Research Center',
  'description':'Dryden Flight Research Center',
  'website':'http://www.nasa.gov/centers/dryden/home/index.html',
  'visitor':'http://www.nasa.gov/centers/dryden/about/Dryden/tour.html',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=Lilly+Dr+Bldg+4800+Edwards,+CA+93523&amp;sll=38.887158,-77.015877&amp;sspn=0.01246,0.028925&amp;ie=UTF8&amp;ll=34.951277,-117.896261&amp;spn=0.00656,0.014462&amp;z=16&amp;iwloc=cent&amp;om=1',
  'image':'../images/nasa_DFRC_center2_tn.gif',
  'altText':'Dryden Flight Research Center',
  'title':'Dryden Flight Research Center',
  'type':'1'
}

,

{
  'id':'3',  
  'address':'21000 Brookpark Rd, Cleveland, OH 44135',
  'place_name':'Glenn Research Center',
  'description':'Glenn Research Center',
  'website':'http://www.nasa.gov/centers/glenn/home/index.html',
  'visitor':'http://www.nasa.gov/centers/glenn/about/visitgrc.html',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=21000+Brookpark+Rd,+Cleveland,+OH+44135&amp;sll=34.951277,-117.896261&amp;sspn=0.00656,0.014462&amp;ie=UTF8&amp;ll=41.422166,-81.851192&amp;spn=0.012003,0.028925&amp;z=15&amp;iwloc=addr&amp;om=1',
  'image':'../images/nasa_GRC_center_tn.gif',
  'altText':'Glenn Research Center (GRC)',
  'title':'Glenn Research Center (GRC)',
  'type':'1'
}

,

{
  'id':'4',  
  'address':'Greenbelt, MD 20771',
  'place_name':'Goddard Space Flight Center',
  'description':'Goddard Space Flight Center',
  'website':'http://www.nasa.gov/centers/goddard/home/index.html',
  'visitor':'http://www.nasa.gov/centers/goddard/visitor/home/index.html',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=Greenbelt,+MD+20771&amp;ie=UTF8&amp;ll=39.001843,-76.85194&amp;spn=0.02488,0.05785&amp;z=14&amp;iwloc=addr&amp;om=1',
  'image':'../images/nasa_GSFC_center_tn.gif',
  'altText':'Goddard Space Flight Center (GSFC)',
  'title':'Goddard Space Flight Center (GSFC)',
  'type':'1'
}

,

{
  //40.805437,-73.965629
  //34.19792,-118.175077
  //34.197876,-118.174664
  'id':'5',  
  'address':'4800 Oak Grove Drive, Pasadena, California 91109',
  'place_name':'Jet Propulsion Laboratory',
  'description':'Jet Propulsion Laboratory',
  'website':'http://www.nasa.gov/centers/jpl/home/index.html',
  'visitor':'http://www.jpl.nasa.gov/pso/pt.cfm',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=4800+Oak+Grove+Drive,+Pasadena,+California+91109&amp;ll=34.197876,-118.174664&amp;sspn=0.006578,0.014462&amp;ie=UTF8&amp;ll=34.188695,-118.174524&amp;spn=0.013241,0.028925&amp;z=15&amp;iwloc=cent&amp;om=1',
  'image':'../images/nasa_JPL_center_tn.gif',
  'altText':'Jet Propulsion Laboratory (JPL)',
  'title':'Jet Propulsion Laboratory (JPL)',
  'type':'1'
}

,

{
  'id':'6',  
  'address':'Houston, TX 77058',
  'place_name':'Johnson Space Center',
  'description':'Johnson Space Center',
  'website':'http://www.nasa.gov/centers/johnson/home/index.html',
  'visitor':'http://www.spacecenter.org/',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=Houston,+TX+77058&amp;ie=UTF8&amp;ll=29.5854,-95.099716&amp;spn=0.111363,0.2314&amp;z=12&amp;iwloc=addr&amp;om=1',
  'image':'../images/nasa_JSC_center_tn.gif',
  'altText':'Johnson Space Center (JSC)',
  'title':'Johnson Space Center (JSC)',
  'type':'1'
}

,

{
  'id':'7',  
  'address':'Florida 32899',
  'place_name':'Kennedy Space Center',
  'description':'Kennedy Space Center',
  'website':'http://www.nasa.gov/centers/kennedy/home/index.html',
  'visitor':'http://www.nasa.gov/centers/kennedy/about/visit/index.html',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=Florida+32899&amp;ie=UTF8&amp;ll=28.539555,-81.379509&amp;spn=0.007031,0.014462&amp;z=16&amp;iwloc=addr&amp;om=1',
  'image':'../images/nasa_Kennedy_Space_Center.jpg',
  'altText':'Kennedy Space Center',
  'title':'Kennedy Space Center',
  'type':'1'
}

,

{
  'id':'8',  
  'address':'Hampton, VA 23681',
  'place_name':'Langley Research Center',
  'description':'Langley Research Center',
  'website':'http://www.nasa.gov/centers/langley/home/index.html',
  'visitor':'http://www.vasc.org/',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=Hampton,+VA+23681&amp;ie=UTF8&amp;ll=37.031336,-76.340303&amp;spn=0.006389,0.014462&amp;z=16&amp;iwloc=addr&amp;om=1',
  'image':'../images/nasa_LARC_center_alt_tn.gif',
  'altText':'Langley Research Center (LaRC)',
  'title':'Langley Research Center (LaRC)',
  'type':'1'
}

,

{
  'id':'9',  
  'address':'Huntsville, Alabama 35812',
  'place_name':'Marshall Space Flight Center',
  'description':'Marshall Space Flight Center',
  'website':'http://www.nasa.gov/centers/marshall/home/index.html',
  'visitor':'http://www.nasa.gov/centers/marshall/about/visitor.html',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=Huntsville,+Alabama+35812&amp;ie=UTF8&amp;ll=34.731773,-86.586299&amp;spn=0.006578,0.014462&amp;z=16&amp;iwloc=addr&amp;om=1',
  'image':'../images/nasa_MSFC_center_tn.gif',
  'altText':'Marshall Space Flight Center (MSFC)',
  'title':'Marshall Space Flight Center (MSFC)',
  'type':'1'
}

,

{
  'id':'10',  
  'address':'300 E Street SW, Washington, DC 20546',
  'place_name':'NASA Headquarters',
  'description':'NASA Headquarters',
  'website':'http://www.nasa.gov/centers/hq/home/index.html',
  'visitor':'http://www.nasa.gov/centers/hq/about/map.html',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=300+E+Street+SW,+Washington,+DC+20546&amp;sll=37.425252,-122.058449&amp;sspn=0.050849,0.1157&amp;ie=UTF8&amp;ll=38.887158,-77.015877&amp;spn=0.01246,0.028925&amp;z=15&amp;iwloc=addr&amp;om=1',
  'image':'../images/nasa_AN_Structure_tn.gif',
  'altText':'NASA Headquarters',
  'title':'NASA Headquarters',
  'type':'1'
}

,

{
  'id':'11',  
  'address':'MS,39529',
  'place_name':'Stennis Space Center',
  'description':'Stennis Space Center',
  'website':'http://www.nasa.gov/centers/stennis/home/index.html',
  'visitor':'http://www.ssc.nasa.gov/public/visitors/',
  'driving':'http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=MS,39529&amp;ie=UTF8&amp;ll=30.448674,-89.577713&amp;spn=0.220795,0.462799&amp;z=11&amp;iwloc=addr&amp;om=1',
  'image':'../images/nasa_SSC_site_tn.jpg',
  'altText':'Stennis Space Center (SSC)',
  'title':'Stennis Space Center (SSC)',
  'type':'1'
}

];

Event.observe(window,'load',function()
{
     if (!(document.getElementById) && !(document.getElementsByTagName)) return;
	 
	 if($('map_check_centers'))
	 {
    $('map_check_centers').checked=true;
	 }
	
	
	  if($('map_check_facilities'))
	  {
	$('map_check_facilities').checked=true;
	  }
	
	if(window['GBrowserIsCompatible'])
	{
		var geocoder;
		
        function identifyAddress1(id,dontClear,options) {
			var location;
			if(parseInt(id) > -1){
				// this is a location id -- retrieve a location object from array
				location = locations.find(function(item){ return item.id==id; });
			} else {
				// not an integer. this might be a location
				if(typeof(id)=='object' && parseInt(id['id']) > -1){
					// it is a location
					location = id;
				} else {
					return false;
				}
			}
			if(dontClear!=true){
				map.clearOverlays();
			}
			
			
			     if($('map_check_centers'))
				 {
			  $('map_check_centers').checked=false;
			     }
				 
				 
			   if($('map_check_facilities'))
			   {
	          $('map_check_facilities').checked=false;
			   }
			
			geocoder.getLatLng(
				location['address'],
				function(point) {
				
				    //alert(point);
					// use optional zoom parameter if its there and is a number. Otherwise, default to zoom level 3
					if(options && typeof(parseInt(options['zoom']))=='number'){
						map.setCenter(point,options['zoom']);
					} else {
						map.setCenter(new GLatLng(39, -93), 3);
					}
					
					var marker = new GMarker(point,icon);
					map.addOverlay(marker);	
					var html = (
						'<div id="info_window">' +
							'<div id="image_wrapper">' +
								'<img src="' + location['image'] + '"alt="' + location['altText'] +
								 '"title="' + location['title'] +'" border="0" />' +
							'</div>' +
							'<div id="data_wrapper">' +
								'<p id="info_window_title">' + location['place_name'] + '</p>' +
								'<p id="info_window_description">' + location['address'] + '</p>' +
								'<p class="info_window_link"><b><a href="' + location['website'] +'" target="_blank">View web site</a></b></p>' +
								'<p class="info_window_link"><b><a href="' + location['visitor'] +'" target="_blank">Visitors information</a></b></p>' +
								'<p class="info_window_link info_window_link_last"><b><a href="' + location['driving'] +'" target="_blank">Driving instructions</a></b></p>' +
							'</div>' +
						'</div>'
					);
					GEvent.addListener(marker,"click", function() {
							map.openInfoWindowHtml(point,html);
					});
				}
			);				
		}
		
		
		// loads the map according to the one location selected
		function identifyAddress(id,dontClear,options)


		{
			
			if(dontClear!=true)
			{
				map.clearOverlays();
			}
			
							  
			
            for (i=0;i<points.length;i++)
			{
			
            // use optional zoom parameter if its there and is a number. Otherwise, default to zoom level 3
					if(options && typeof(parseInt(options['zoom']))=='number')
					{
						map.setCenter(points[i],options['zoom']);
					} else
					{
						map.setCenter(new GLatLng(39, -93), 3);
					}
				
					
					
					
					addEventListeners(i);
					
					
					

            }
			
		//End of Identify Adress
		}
	
	
	    function addEventListeners(i)
		{
		
		    		map.setCenter(new GLatLng(39, -93), 3);	
					 html = (
						'<div id="info_window">' +
							'<div id="image_wrapper">' +
								'<img src="' + locations[i]['image'] + '"alt="' + locations[i]['altText'] +
								 '"title="' + locations[i]['title'] +'" border="0" />' +
							'</div>' +
							'<div id="data_wrapper">' +
								'<p id="info_window_title">' + locations[i]['place_name'] + '</p>' +
								'<p id="info_window_description">' + locations[i]['address'] + '</p>' +
								'<p class="info_window_link"><b><a href="' + locations[i]['website'] +'" target="_blank">View web site</a></b></p>' +
								'<p class="info_window_link"><b><a href="' + locations[i]['visitor'] +'" target="_blank">Visitors information</a></b></p>' +
								'<p class="info_window_link info_window_link_last"><b><a href="' + locations[i]['driving'] +'" target="_blank">Driving instructions</a></b></p>' +
							'</div>' +
						'</div>'
					);
	                
					if ( i == 0)
					{
					html0=html;
					
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(37.414968,-122.051723),html0);
					});
					}
					
					if ( i == 1)
					{
					html1=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(34.951691,-117.891826),html1);
					});
					}
					
					if ( i == 2)
					{
					html2=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(41.418742,-81.854727),html2);
					});
					}
					
					if ( i == 3)
					{
					html3=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(38.997438,-76.853014),html3);
					});
					}
					
					if ( i == 4)
					{
					html4=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(34.197876,-118.174664),html4); //40.805437,-73.965629
					});
					}
					
					if ( i == 5)
					{
					html5=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(29.562688,-95.092172),html5);
					});
					}
					
					if ( i == 6)
					{
					html6=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(28.5382,-81.3795),html6);
					});
					}
					if ( i == 7)
					{
					html7=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(37.0301,-76.3403),html7);
					});
					}
					
					if ( i == 8)
					{
					html8=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(34.7305,-86.5863),html8);
					});
					}
					
					if ( i == 9)
					{
					html9=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(38.883212,-77.015982),html9);
					});
					}
					
					if ( i == 10)
					{
					html10=html;
					var marker = new GMarker(points[i],icon);
					map.addOverlay(marker);	
					GEvent.addListener(marker,"click", function()
					{
					       
							map.openInfoWindowHtml(new GLatLng(30.386707,-89.613906),html10);
					});
					}
					
	}				
	
		// brings all centers on click on link
		function allCenters(ev)
		{
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input")
			{
				$('map_check_centers').checked = !$('map_check_centers').checked;
				
				ev.stop();
     		}
			mapFilteredLocations();
			return false;
		}
	
	
		// brings all facilities on click on link
		function allFacilities(ev)
		{
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input")
			{
				$('map_check_facilities').checked = !$('map_check_facilities').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}
	
		Event.observe($$('#map_allcenters a')[0],'click',allCenters);
		Event.observe($$('#map_check_centers')[0],'click',allCenters);
		Event.observe($$('#map_allfacilities a')[0],'click',allFacilities);
		Event.observe($$('#map_check_facilities')[0],'click',allFacilities);
		
		
		
		
	
		function mapFilteredLocations()
		{
			map.clearOverlays();
			if($('map_check_centers').checked)
			{			
				for (var i = 0; i < locations.length;i++)
				{	
					if(locations[i]['type'] == '1')
					{
					addEventListeners(i);
					}	
				}
				
			}
			if($('map_check_facilities').checked)
			{
			for (i = 0; i < locations.length;i++)
				{				
					if(locations[i]['type'] == '2')
					{
						addEventListeners(i);
					}
				}
			}
		}
		
		
		
		if (GBrowserIsCompatible())
		{
		
		    var points = new Array();
            points[0] = new GLatLng(37.414968,-122.051723);
            points[1] = new GLatLng(34.951691,-117.891826);
            points[2] = new GLatLng(41.418742,-81.854727);
            points[3] = new GLatLng(38.997438,-76.853014);
            points[4] = new GLatLng(34.197876,-118.174664);
			//34.19792,-118.175077
           /* points[5] = new GLatLng(39.430687,-80.195721);
            points[6] = new GLatLng(34.184877,-118.173121);*/
            points[5] = new GLatLng(29.562688,-95.092172);
            points[6] = new GLatLng(28.5382,-81.3795);
            points[7] = new GLatLng(37.0301,-76.3403);
            points[8] =new GLatLng(34.7305,-86.5863);
            points[9] =new GLatLng(38.883212,-77.015982);
			points[10] =new GLatLng(30.386707,-89.613906);
            /*points[13] =new GLatLng(37.869123,-75.513978);
            points[14] =new GLatLng(32.456877,-106.613965);*/
            
         	var html,html0,html1,html2,html3,html4,html5,html6,html7,html8,html9,html10;	

		
		    var locationSelector = new Element("select");	
			locations.each(function(location)
			{
			    var opElem = new Element("option",{'value':location['id']});
				opElem.update(location['place_name']);
				locationSelector.appendChild(opElem);
			});
	
			$('locations_selector').appendChild(locationSelector);
			/*var skinnedDropper = new SkinnedSelect($$('#locations_selector')[0],$$('#locations_selector select')[0],function(){
				identifyAddress1($$('#locations_selector select')[0].value,false,{'zoom':14});
			},'','gray');*/
			
			
			Event.observe(
				locationSelector,
				'change',
				function(){
					identifyAddress1(this.value,false,{'zoom':14});
				});
			//Event.observe($$('#map_allcenters a')[0],'click',allCenters);
			//function identifyAddress(id,dontClear,options)
			//window.addEventListner('change', function(){alert'changed'});
			
			

			var map = new GMap2(document.getElementById("map_canvas"));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			//map.addControl(new GOverviewMapControl());
			map.enableScrollWheelZoom();
			geocoder = new GClientGeocoder();
			map.setCenter(new GLatLng(39, -93), 3);
			
			// Creates the  customized marker icon
			var icon = new GIcon();
			icon.image = "../images/blue_marker.png";
			icon.iconSize = new GSize(13, 27);
			icon.iconAnchor = new GPoint(8, 25);
			icon.infoWindowAnchor = new GPoint(5, 1);	
			identifyAddress(true);
				
	
		}
		
		
		
	}
	
});
