/*
*   Helper functions for GMAP
*/

//clear map and sidebar
function clearMap()
{
	map.clearOverlays();
	sidebar.innerHTML = '';	
}

//Grab the checked categories and return a pipe delimited string
function getCheckedCategories()
{
	var sCategories = "";
	var aCatIds = Array( 'cat_quail', 'cat_deer', 'cat_turkey', 'cat_pheasant', 'cat_other' );
	
	for( xx = 0; xx <= aCatIds.length-1; xx++)
	{
		//checkbox id's = cat_name
		var thisCat = aCatIds[ xx ];
		//get checkbox
		var fld = document.getElementById( thisCat );
		//is category checked?
		if( fld.checked )
		{
			//do we need a delim?
			if( sCategories == "" )
			{
				sCategories = thisCat;
			}
			else
			{
				sCategories += "|" + thisCat;
			}
		}
	}
	
	return sCategories;
}


	function load() 
	{
	  if (GBrowserIsCompatible()) {
		geocoder = new GClientGeocoder();
	
		map = new GMap2( document.getElementById('map') );
	
		map.addControl(new GSmallMapControl());
	
		//map type chooser
		//map.addControl(new GMapTypeControl());
	
		//center it
		map.setCenter( new GLatLng( 32.9, -85 ), 6 );
		//set Terrain view
		map.setMapType( G_PHYSICAL_MAP );
		//hook up auto loading
		GEvent.addListener( map, 'load', getAllLodges() );
	  }
	}

	/*
		Map marker
	*/
    function createMarker( point, id, name, address ) 
	{
      var marker= new GMarker(point);
      var html	=	'<span id="bubble"><b>' + name + '</b>' +
	  				'<br/>' + address + '<br/>' +
					'<a href="lodge_details.php?lodge_id=' + id + '">Lodge Details</a></span>';

      GEvent.addListener(marker, 'mouseover', function() {
        marker.openInfoWindowHtml(html);
      });

      return marker;
    }
	
	/*
		This is the lodge listing on the left
	*/
    function createSidebarEntry(marker, id, name, address, distance, phone)
    {		
		var div = document.createElement('div');
		//TODO : get distance back in here
		var html =	'<b>' + name + '</b>' +
					'<br/>' + address;
					
		// don't add a blank phone
		if( phone )
		{
			if( phone.split(' ').join('') != "")
			{
				html += '<br/>' + phone;
			}
		}
		html +=		'<br/><a href="lodge_details.php?lodge_id=' + id + '">Lodge Details</a><br/>' +
					'<hr size="1" color="gray" noshade />';

      div.innerHTML = html;
      div.style.cursor = 'pointer';
      div.style.marginBottom = '5px';

	  //add events
      GEvent.addDomListener(div, 'mouseover', function() {
        div.style.backgroundColor = '#EFEFEF';
		GEvent.trigger( marker, 'mouseover' );
      });

      GEvent.addDomListener(div, 'mouseout', function() {
        div.style.backgroundColor = '#FFFFFF';
      });
	  
	  // NEW : onClick : goto lodge_details
	  GEvent.addDomListener(div, 'click', function() {
        document.location = "lodge_details.php?lodge_id=" + id;
      });

      return div;
    }