if(detectBrowser.modernBrowser()){


Event.observe(window,'load',function()
{
    var selectVal=trimString(defaultVal);
   // alert("defaultVal:"+selectVal);

     if($('map_check_showAllEvents'))
	 {
		 if(selectVal == ''){
		    $('map_check_showAllEvents').checked=true;
		 }
	 }

    /* if($('map_check_show_Y2008_Events'))
	 {
    $('map_check_show_Y2008_Events').checked=true;
	 }
	  if($('map_check_show_Y2009_Events'))
	 {
    $('map_check_show_Y2009_Events').checked=true;
	 }*/

    var newElm=new Array();
	var i=0;
    locations.each(function(location){
	    if(location['type'] == '1'){
         // do nothing
		}else if(newElm.length==0 && location['type'] != '1'){
		  newElm[i]=trimString(location['type']);
		  i=i+1;
		} else {
            var type=trimString(location['type']);
			var x=0;	
			var elemExists=false;
			for (x in newElm)
			{
			  if(type==newElm[x]){
				  elemExists=true;
				  break;
			  }
			}
			if(!elemExists){
              newElm[i]=type;
			  i=i+1;
			}
           
		}
	});

    function trimString(sInString) {
	  sInString = sInString.replace( /^\s+/g, "" );// strip leading
	  return sInString.replace( /\s+$/g, "" );// strip trailing
	}
    //alert(newElm);
    newElm.sort();
    var yearElem=$('form_wrapper1').getElementsBySelector('ul')[0];
	//alert("yearElem:"+newElm.length);
	var x=0;
	for (x=0;x<newElm.length;x++)
	{
		//alert("x:"+x);
		liId='map_show_'+newElm[x]+'_Events';
		checkId='map_check_show_'+newElm[x]+'_Events';
		var  liElm= new Element('li',{id:liId});
       
		var  chckBoxElm;
		var checkedFlag=false;
		if(selectVal != ''){
			if(selectVal == newElm[x]){
			  chckBoxElm=new Element('input',{id:checkId,name:'map_check',type:'checkbox'});
			  checkedFlag=true;
			 }else{
              chckBoxElm=new Element('input',{id:checkId,name:'map_check',type:'checkbox'});
			 }
		}else{
			   chckBoxElm=new Element('input',{id:checkId,name:'map_check',type:'checkbox'});
			    checkedFlag=true;
		}
	  	var  anchorElm=new Element('a', { id:newElm[x], href: '#' }).update(newElm[x]);
		liElm.appendChild(chckBoxElm);
  	    liElm.appendChild(anchorElm);
		yearElem.appendChild(liElm);
	    if(checkedFlag){
			$(chckBoxElm).checked=true;
		}

	}
	/*var  liElm= new Element('li',{id:'map_show_Y2010_Events'});
	var  chckBoxElm=new Element('input',{id:'map_check_show_Y2010_Events',name:'map_check',type:'checkbox'}).update("&nbsp;");
	var  anchorElm=new Element('a', { href: '#' }).update("2010");
	alert("anchorElm:"+anchorElm.innerHTML);
	liElm.appendChild(chckBoxElm);
	liElm.appendChild(anchorElm);
	yearElem.appendChild(liElm);*/



	if(window['GBrowserIsCompatible']){
			
		function initialize() {
			map.clearOverlays();
			locations.each(function(location){
				identifyAddress(location,true);
			});
		}
	
	
   			function identifyAddress(id,dontClear,options) {
	 
        	var location;
			if(parseInt(id) > -1)
			{location = locations.find(function(item){ return item.id==id; });}
			else
			{
			   if(typeof(id)=='object' && parseInt(id['id']) > -1)
			   {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]);
				
                  
                
				
					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 html = (
						'<div id="info_window1">' +
							'<div id="image_wrapper1">' +
								'<img src="' + location['image'] + '" border="0" />' +
							'</div>' +
							'<div id="data_wrapper1">' +
								'<p id="info_window_title1">' + location['place_name'] + '</p>' +
								'<p id="info_window_description1">' + location['address'] + '</p>' +
								'<p class="info_window_link1"><b><a href="' + location['website'] +'" target="_blank">View web site</a></b></p>' +
								'<p class="info_window_link1"><b><a href="' + location['visitor'] +'" target="_blank">Visitors information</a></b></p>' +
								'<p class="info_window_link1 info_window_link_last1"><b><a href="' + location['driving'] +'" target="_blank">Driving instructions</a></b></p>' +
							'</div>' +
						'</div>'
					);
					GEvent.addListener(marker,"click", function() {
							map.openInfoWindowHtml(new GLatLng(lat,lng),html);
					});
				}
							
		}
	
	
	        
		function showAllEvents(ev) {
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input"){
				$('map_check_showAllEvents').checked = !$('map_check_showAllEvents').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}
	
	
		/*function showY2008Events(ev) {
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input"){
				$('map_check_show_2008_Events').checked = !$('map_check_show_2008_Events').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}
		
		function showY2009Events(ev) {
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input"){
				$('map_check_show_2009_Events').checked = !$('map_check_show_2009_Events').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}*/

		function showSpecificEvents(ev) {
			if(ev.target.nodeName!="INPUT" && ev.target.nodeName!="input"){
				//alert(ev.target.id);
				$('map_check_show_'+ev.target.id+'_Events').checked = !$('map_check_show_'+ev.target.id+'_Events').checked;
				ev.stop();
			}
			mapFilteredLocations();
			return false;
		}
	  	Event.observe($$('#map_showAllEvents a')[0],'click',showAllEvents);
		Event.observe($$('#map_check_showAllEvents')[0],'click',showAllEvents);
	
		var i=0;
		for (i=0;i<newElm.length;i++)
		{
			Event.observe($$('#map_show_'+newElm[i]+'_Events a')[0],'click',showSpecificEvents);
			Event.observe($$('#map_check_show_'+newElm[i]+'_Events')[0],'click',showSpecificEvents);
	
	    }

	  	/*Event.observe($$('#map_show_2008_Events a')[0],'click',showY2008Events);
		Event.observe($$('#map_check_show_2008_Events')[0],'click',showY2008Events);
	
	  	Event.observe($$('#map_show_2009_Events a')[0],'click',showY2009Events);
		Event.observe($$('#map_check_show_2009_Events')[0],'click',showY2009Events);*/
		
		/*function mapFilteredLocations()
		{
			map.clearOverlays();
			if($('map_check_showAllEvents').checked)
			{			
				locations.each(function(location){
				identifyAddress(location,true);
			});
				
			}
		}*/
		
		function mapFilteredLocations()
		{
            map.clearOverlays();
			$('locations_selector1').innerHTML="";
            var locationSelector1 = new Element("select");	
			
			locations.each(function(location){
			
				if($('map_check_showAllEvents').checked)
  			    {	
					identifyAddress(location,true);
				    var opElem = new Element("option",{'value':location['id']});
					opElem.update(location['place_name']);
					locationSelector1.appendChild(opElem);                 				
					
				}else{

                   var type=location['type'];
				   if(type != '1'){
					   idTarget='map_check_show_'+type+'_Events';
					  // alert("we are in :"+idTarget);
					   if($(idTarget).checked)
						{	
								identifyAddress(location,true);
								var opElem = new Element("option",{'value':location['id']});
								opElem.update(location['place_name']);
								locationSelector1.appendChild(opElem);                 										
						}
				   }
					/*if($('map_check_show_2008_Events').checked)
					{	
						if(location['type'] == '2008')
						{
							identifyAddress(location,true);
							var opElem = new Element("option",{'value':location['id']});
							opElem.update(location['place_name']);
							locationSelector1.appendChild(opElem);                 				
						}
					}
					
					if($('map_check_show_2009_Events').checked)
					{	
						if(location['type'] == '2009')
						{
							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_showAllEvents').checked){
				var skinnedDropper = new SkinnedSelect($$('#locations_selector1')[0],$$('#locations_selector1 select')[0],function(){
							identifyAddress($$('#locations_selector1 select')[0].value,false,{'zoom':14});
						},'','gray');
			}else{
				var j=0;
				for (j=0;j<newElm.length;j++)
				{
					if($('map_check_show_'+newElm[j]+'_Events').checked){
                         var skinnedDropper = new SkinnedSelect($$('#locations_selector1')[0],$$('#locations_selector1 select')[0],function(){
							identifyAddress($$('#locations_selector1 select')[0].value,false,{'zoom':14});
						},'','gray');
						break;
					}
				}
			}
			
			/*if( $('map_check_showAllEvents').checked ||  $('map_check_show_Y2008_Events').checked || $('map_check_show_Y2009_Events').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();
	  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);
			
			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_showAllEvents'))
	              {
                  $('map_check_showAllEvents').checked=false;
	              }
				identifyAddress($$('#locations_selector1 select')[0].value,false,{'zoom':14});
			},'','gray');
			//initialize();		
			mapFilteredLocations();
		}
		
		
		
	}
	
});

}


