if(detectBrowser.modernBrowser()){

Event.observe(window,'load',function()
{

 
	 if($('map_check_first_stage'))
	 {
    $('map_check_first_stage').checked=true;
	 }
	 if($('map_check_upper_stage'))
	 {
    $('map_check_upper_stage').checked=true;
	 }
	 if($('map_check_upper_stage_engine'))
	 {
    $('map_check_upper_stage_engine').checked=true;
	 }
	 if($('map_check_vehicle_integration'))
	 {
    $('map_check_vehicle_integration').checked=true;
	 }
	 

	if(window['GBrowserIsCompatible']){
		var geocoder;
		
		
		function initialize() {
			map.clearOverlays();
			locations.each(function(location){
                identifyAddress(location,true);
			});
		}
	
	



    	// loads the map according to the one location selected
		function identifyAddress(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 (location['point'] != undefined && location['point'] != null && location['point'] != '' && location['point'] != eval(''))
				{
				
				var splitPoint = location['point'].split(",");
                var lat=parseFloat(splitPoint[0]);
	            var lng=parseFloat(splitPoint[1]);
				
				// 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(new GLatLng(lat,lng),options['zoom']);
					} else {
						map.setCenter(new GLatLng(39, -93), 3);
					}
					var marker = new GMarker(new GLatLng(lat,lng),icon);
					map.addOverlay(marker);
				
					var html1;
					if (location['website'] != undefined && location['website'] != '' && location['website'] != null && location['website'] != eval(''))
					{
					html1='<p class="info_window_link info_window_link_last"><b><a href="' + location['website'] +'" target="_blank">View web site</a></b></p>';
					}
					else
					{
					html1='';
					}
					
					var html2;
					if (location['image'] != undefined && location['image'] != '' && location['image'] != null && location['image'] != eval(''))
					{
					html2='<div id="image_wrapper1">'+'<img src="' + location['image'] + '"alt="' + location['altText'] +'"title="' + location['title'] +'" border="0" />'+'</div>';
					}
					else
					{
					html2='';
					}
					
					var html = (
						'<div id="info_window1">' +html2+
							'<div id="data_wrappe">' +
								 '<p><b>'+location['place_name'] + '</b></p>' +
								 '<p id="info_window_description">'+location['address'] +'</p>'+
								 '<p id="info_window_description"><b>' + location['description'] +'</b></p>'+
								'<p id="info_window_description"><b>' + location['visitor'] +'</b></p>' +
								'<p class="info_window_link"><b><a href="' + location['driving'] +'" target="_blank">Driving Instructions</a></b></p>' + html1 +
							'</div>' +
						'</div>'
					);
					
					
					
					GEvent.addListener(marker,"click", function() {
							map.openInfoWindowHtml(new GLatLng(lat,lng),html);
					});
					
				}
							
		}
	


	
	    function showFirstStage(ev) {
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input"){
				$('map_check_first_stage').checked = !$('map_check_first_stage').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}
		
		function showUpperStage(ev) {
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input"){
				$('map_check_upper_stage').checked = !$('map_check_upper_stage').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}
		
		function showUpperStageEngine(ev) {
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input"){
				$('map_check_upper_stage_engine').checked = !$('map_check_upper_stage_engine').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}
		
		function showVehicleIntegration(ev) {
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input"){
				$('map_check_vehicle_integration').checked = !$('map_check_vehicle_integration').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}
	
	
	    Event.observe($$('#map_first_stage a')[0],'click',showFirstStage);
		Event.observe($$('#map_check_first_stage')[0],'click',showFirstStage);
		Event.observe($$('#map_upper_stage a')[0],'click',showUpperStage);
		Event.observe($$('#map_check_upper_stage')[0],'click',showUpperStage);
		Event.observe($$('#map_upper_stage_engine a')[0],'click',showUpperStageEngine);
		Event.observe($$('#map_check_upper_stage_engine')[0],'click',showUpperStageEngine);
	    Event.observe($$('#map_vehicle_integration a')[0],'click',showVehicleIntegration);
		Event.observe($$('#map_check_vehicle_integration')[0],'click',showVehicleIntegration);
	
	
			function mapFilteredLocations()
		{
			map.clearOverlays();
			$('locations_selector1').innerHTML="";
            

			var locationSelector1 = new Element("select");	
			
			locations.each(function(location){
			
			if($('map_check_first_stage').checked)
			{			
			   	if(location['type'] == '1')
				{
				identifyAddress(location,true);
                var opElem = new Element("option",{'value':location['id']});
				opElem.update(location['place_name']);
				locationSelector1.appendChild(opElem);                 				
				}
			}
			
			if($('map_check_upper_stage').checked)
			{			
				if(location['type'] == '2')
				{
               	identifyAddress(location,true);
				var opElem = new Element("option",{'value':location['id']});
				opElem.update(location['place_name']);
				locationSelector1.appendChild(opElem);
				}
			}
			
			if($('map_check_upper_stage_engine').checked)
			{			
				if(location['type'] == '3')
				{
               	identifyAddress(location,true);
				var opElem = new Element("option",{'value':location['id']});
				opElem.update(location['place_name']);
				locationSelector1.appendChild(opElem);
				}
			}
			
			if($('map_check_vehicle_integration').checked)
			{			
				if(location['type'] == '4')
				{
               	identifyAddress(location,true);
				var opElem = new Element("option",{'value':location['id']});
				opElem.update(location['place_name']);
				locationSelector1.appendChild(opElem);
				
				}
			}
			});
			
				$('locations_selector1').appendChild(locationSelector1);
				if( $('map_check_first_stage').checked ||  $('map_check_upper_stage').checked || 
				$('map_check_upper_stage_engine').checked || $('map_check_vehicle_integration').checked)
				{
				var skinnedDropper = new SkinnedSelect($$('#locations_selector1')[0],$$('#locations_selector1 select')[0],function(){
			      		  
				identifyAddress($$('#locations_selector1 select')[0].value,false,{'zoom':14});
			},'','gray');
			    }
				
		}
	
		
	
	function sortByPlaceName(a, b) {
    var x = a.place_name.toLowerCase();
    var y = b.place_name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }	
	 
	 function sleep(time)
	  {
      time = time * 100;
      var sleeping = true;
      var now = new Date();
	  var alarm;
      var startingMSeconds = now.getTime();
	  //alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
      while(sleeping){
         alarm = new Date();
         alarmMSeconds = alarm.getTime();
         if(alarmMSeconds - startingMSeconds > time){ sleeping = false; }
      }      
      }
		
	  	
		
		
		
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map_canvas1"));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GOverviewMapControl());
			map.enableScrollWheelZoom();
			locations.sort(sortByPlaceName);
			
			
			// Creates the  customized marker icon
			var icon = new GIcon();
			icon.image = "/templateimages/redesign/modules/locations/blue_marker.png";
			icon.iconSize = new GSize(13, 27);
			icon.iconAnchor = new GPoint(8, 25);
			//icon.infoWindowAnchor = new GPoint(5, 1);	
	
			var locationSelector1 = new Element("select");	
			locations.each(function(location){
				var opElem = new Element("option",{'value':location['id']});
				opElem.update(location['place_name']);
				locationSelector1.appendChild(opElem);
			});
	
			$('locations_selector1').appendChild(locationSelector1);
			var skinnedDropper = new SkinnedSelect($$('#locations_selector1')[0],$$('#locations_selector1 select')[0],function(){
			      if($('map_check_first_stage'))
	              {
                  $('map_check_first_stage').checked=false;
	              }
				   if($('map_check_upper_stage'))
	              {
                  $('map_check_upper_stage').checked=false;
	              }
				   if($('map_check_upper_stage_engine'))
	              {
                  $('map_check_upper_stage_engine').checked=false;
	              }
				   if($('map_check_vehicle_integration'))
	              {
                  $('map_check_vehicle_integration').checked=false;
	              }
				  
				identifyAddress($$('#locations_selector1 select')[0].value,false,{'zoom':14});
			},'','gray');
			initialize();	
           		
		}
		
		
		
	}
	
});

}