var myMap;
var infoWindow;

var strAllDate = "";
var strDate = "";
var strBeginDate = "";
var strEndDate = "";
var where = "new_england";
var when = "All";

var year;
var month;
var day;


//********************
function setWhere(loc)
{
	where = loc
}

//*************
function init()
{
	loadMap();
	mouseOutWhere(where);
	setWhat();
	resetWhen("All");
	onWhere(loc);
}

//*************
function initHomePage()
{
	//setWhat();
	
	showGardenTours = true;
	showGardenEvents = true;
	showGardenEducation = true;
	showGardenOther = true;
	
	when = "Date_Range";
	
year = '0';
	
	var newDate;
	var newDescriptiveEventTitle;
	var newState;
	var newCity;
	var rec;
	var tokens;
	var y;
	var m;
	var d;
	var content = document.getElementById('2week_schudule');
	var str = "";
	var bUL = false;
	var id;
	var coordinates;
	var point;
	var strContent;
	var eventType;
	var scheduleArray = new Array();

	// Update the global dates
	strBeginDate = getShorterFormatedDateX(new Date(), 0);
	strEndDate = getShorterFormatedDateX(new Date(), 14);

	// Create a new array with filtered dates (datesArray)
	var tempDateRec;
	var filteredDatesArray = new Array();
	var j = 0;
	for (var i = 0; i < datesArray.length; i++)
	{
		tempDateRec = datesArray[i];
		if (passWhen(tempDateRec))
		{
			filteredDatesArray[j] = tempDateRec;
			j++;
		}
	}

	// Create a new array with filtered what and where
	var tempEventRec;
	var filteredEventsArray = new Array();
	j = 0;
	for (var i = 0; i < eventsArray.length; i++)
	{
		tempEventRec = eventsArray[i];
		if (passWhat(tempEventRec) && passWhere(tempEventRec))
		{
			filteredEventsArray[j] = tempEventRec;
			j++;
		}
	}

	// Create the final scheduleArray.
	// For each rec in filteredDatesArray see if there is a rec in filteredEventsArray with the same id.
	// If so create a scheculeRec and add it to scheduleArray
	var eventId;
	j = 0;
	for (var i = 0; i < filteredDatesArray.length; i++)
	{
		tempDateRec = filteredDatesArray[i];
		eventId = tempDateRec.event_id;
		for (var k = 0; k < filteredEventsArray.length; k++)
		{
			tempEventRec = filteredEventsArray[k];
			if (eventId == tempEventRec.event_id)
			{
				scheduleArray[j] = new scheduleRec(eventId, tempDateRec.date, tempEventRec.descriptive_event_title, tempDateRec.start_time, tempDateRec.end_time, tempEventRec.city, tempEventRec.state, tempEventRec.start_location_coordinates, tempEventRec.event_type);
				j++;
				break;
			}
		}
	}

	var tempScheduleRec;
	for (i = 0; i < scheduleArray.length; i++)
	{
		tempScheduleRec = scheduleArray[i];
		newDate = tempScheduleRec.date;
		newState = tempScheduleRec.state;
		newDescriptiveEventTitle = tempScheduleRec.descriptiveEventTitle;
		newCity = tempScheduleRec.city;
		eventType = tempScheduleRec.eventType;
		id = tempScheduleRec.eventId;
		
		tokens = newDate.split('-');
		
		y = tokens[0];
		
		m = tokens[1];
		if (m[0] == "0")
		{
			m = m[1];
		}
		
		d = tokens[2];
		if (d[0] == "0")
		{
			d = d[1];
		}
	
		if ((y != year) || (m != month))
		{
			// add a new month, year header
			// add new day header
			str += "<h2>" + getFormatedMonth(m) + " " + y + "</h2>";
			str += "<h3 class='separator'>" + getFormatedDate(newDate) + "</h3>";
		}
		else if (d != day)
		{
			// add new day header
			str += "<h3 class='separator'>" + getFormatedDate(newDate) + "</h3>";
		}
		
		year = y;
		month = m;
		day = d;
		
		if (!bUL)
		{
			str += "<ul>";
			bUL = true;
		}
		// add tour/event to schedule
		var strTemp;
		if (newDescriptiveEventTitle != "")
		{
			strTemp = "<a href='JavaScript:void()' onClick='showDetails(" + id + ")'><span style='font-size: 110%;'><em>" + newDescriptiveEventTitle + "</em></span> - " + newCity + ", " + newState + "</a></li>";
		}
		else
		{
			strTemp = "<a href='JavaScript:void()' onClick='showDetails(" + id + ")'>" + newCity + ", " + newState + "</a></li>";
		}
		
		if (eventType == "garden_tour")
		{
			str += "<li class='garden_tour_list_item' >" + strTemp;
		}
		else if (eventType == "garden_event")
		{
			str += "<li class='garden_event_list_item' >" + strTemp;
		}
		else if (eventType == "garden_education")
		{
			str += "<li class='garden_education_list_item' >" + strTemp;
		}
		else
		{
			str += "<li class='garden_other_list_item' >" + strTemp;
		}
		
		if (bUL)
		{
			str += "</ul>";
			bUL = false;
		}
	}
	
	if (str == "")
	{
			str += "<h3 class='separator'>There are no events scheduled for " + strBeginDate + " - " + strEndDate + "</h3>";
	}
	
	content.innerHTML = str;


}

//****************
function loadMap()
{
	var mapDiv = document.getElementById('map_canvas');
	var latlng = new google.maps.LatLng(42.948381,-70.664062);
	var myOptions =
	{
		zoom: 7,
		center: latlng,
		mapTypeControl: false,
		scaleControl: true,
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	myMap = new google.maps.Map(mapDiv, myOptions);
	infoWindow = new google.maps.InfoWindow();
	google.maps.event.addListener(myMap, 'click', function()
		{
			infoWindow.close();
		});
}

// Fix a problem when the browser window is resized and we are on a different tab than the map.
//***************
function fixMap()
{
	google.maps.event.trigger(myMap, 'resize');
}

//***************************************
function getCoordinateTokens(coordinates)
{
	coordinates = coordinates.replace(')', '');
	
	coordinates = coordinates.replace(' ', '');
	coordinates = coordinates.replace('(', '');
	return coordinates.split(',');
}

//****************************
function getLatLng(strLatLng)
{
	var tokens = getCoordinateTokens(strLatLng);

	return new google.maps.LatLng(parseFloat(tokens[0]), parseFloat(tokens[1]));
}

//**************************
function getFormatedMonth(m)
{
	var m_names = new Array("January", "February", "March", 
		"April", "May", "June", "July", "August", "September", 
		"October", "November", "December");
	
	return m_names[m - 1];
}

// Takes a string in the format:
// 2010-07-24
//****************************
function getFormatedDate(date)
{
	if (date == "")
	{
		return "";
	}
	else
	{
		var tokens = date.split('-');
		var m = tokens[1];
		if (m[0] == "0")
		{
			m = m[1];
		}
		
		var x = tokens[2];
		if (x[0] == "0")
		{
			x = x[1];
		}

		var d = new Date(parseInt(tokens[0]), parseInt(m) - 1, parseInt(x));
		
		var d_names = new Array("Sunday", "Monday", "Tuesday",
		"Wednesday", "Thursday", "Friday", "Saturday");
		
		var m_names = new Array("January", "February", "March", 
		"April", "May", "June", "July", "August", "September", 
		"October", "November", "December");
		
		var curr_day = d.getDay();
		var curr_date = d.getDate();
		var curr_month = d.getMonth();
		var curr_year = d.getFullYear();
		
		return d_names[curr_day] + ", " + m_names[curr_month] + " " + curr_date  + ", " +  curr_year;
	}
}

// Takes a Date object and an offset number of days
//******************************************
function getShorterFormatedDateX(date, days)
{
	var d = date;
	if (days > 0)
	{
		d = new Date(Math.ceil(date.getTime() + days * 1000*60*60*24));
	}
	
	var d_names = new Array("Sunday", "Monday", "Tuesday",
		"Wednesday", "Thursday", "Friday", "Saturday");
		
	var m_names = new Array("Jan", "Feb", "Mar", 
		"Apr", "May", "Jun", "Jul", "Aug", "Sep", 
		"Oct", "Nov", "Dec");
		
	var curr_day = d.getDay();
	var curr_date = d.getDate();
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();
	
	return d_names[curr_day] + ", " + m_names[curr_month] + " " + curr_date  + ", " +  curr_year;
}

//***********************************
function getShorterFormatedDate(date)
{
	if (date == "")
	{
		return "";
	}
	else
	{
		var tokens = date.split('-');
		var m = tokens[1];
		if (m[0] == "0")
		{
			m = m[1];
		}
		
		var x = tokens[2];
		if (x[0] == "0")
		{
			x = x[1];
		}

		var d = new Date(parseInt(tokens[0]), parseInt(m) - 1, parseInt(x));
		
		return getShorterFormatedDateX(d, 0);
	}
}

var detailsWindow;
//**********************
function showDetails(id)
{
	winRef = window.open('http://www.gardentoursnewengland.com/details.php?id=' + id,'detailsWindow','width=656,height=600,scrollbars=yes');
	
	if (winRef != detailsWindow)
	{
		if (window.screen)
		{
			winRef.moveTo(10, 10);
		}
	}
	detailsWindow = winRef;

	if (window.focus)
	{
		{detailsWindow.focus()};
	}
}

//************************
//************************

//********************************************** Private
function scheduleRec(eventId, date, descriptiveEventTitle, startTime, endTime, city, state, startLocationCoordinates, eventType)
{
    this.eventId = eventId;
	this.date = date;
	this.descriptiveEventTitle = descriptiveEventTitle;
    this.startTime = startTime;
    this.endTime = endTime;
	this.city = city;
	this.state = state;
    this.startLocationCoordinates = startLocationCoordinates;
    this.eventType = eventType;
}

//********************************************** Private
function markerRec(eventId, dateArray, descriptiveEventTitle, city, state, startLocationCoordinates, eventType)
{
    this.eventId = eventId;
	this.dateArray = dateArray;
	this.descriptiveEventTitle = descriptiveEventTitle;
	this.city = city;
	this.state = state;
    this.startLocationCoordinates = startLocationCoordinates;
    this.eventType = eventType;
}

var showGardenTours;
var showGardenEvents;
var showGardenEducation;
var showGardenOther;
//****************
function setWhat()
{
	var el = document.getElementById('show_garden_tours');
	showGardenTours = el.checked;
	
	var el = document.getElementById('show_garden_events');
	showGardenEvents = el.checked;
	
	var el = document.getElementById('show_garden_education');
	showGardenEducation = el.checked;
	
	var el = document.getElementById('show_garden_other');
	showGardenOther = el.checked;
	
	// Show message in red if no checkboxes are checked
	var strContent;
	var content = document.getElementById('checkbox_warning');
	if (showGardenTours || showGardenEvents || showGardenEducation || showGardenOther)
	{
		strContent = '<p><span class="smaller">*Check one or more</span></p>';
	}
	else
	{
		strContent = '<p><span class="warning smaller">*Check one or more</span></p>';
	}
	
	content.innerHTML = strContent;
}

//***************
function onWhat()
{
	// Close the info window if it is open
	infoWindow.close();
	
	setWhat();
	
	filterSchedule();
	markersBestFit();
}

//************************
function mouseOverWhere(w)
{
	var el = document.getElementById(w);
	el.style.color = "#090";
}

//***********************
function mouseOutWhere(w)
{
	var el = document.getElementById(w);
	if (w == where)
	{
		el.style.color = "#090";
		el.style.fontSize = "120%";
	}
	else
	{
		el.style.color = "#004000";
		el.style.fontSize = "100%";
	}
}

//************************
function onWhere(newWhere)
{
	// Close the info window if it is open
	infoWindow.close();

	var els = document.getElementsByName("where_rollover");
	for (var i = 0;i < els.length; i++)
	{
		els[i].style.color = "#004000";
		els[i].style.fontSize = "100%";
	}
	
	var el = document.getElementById(newWhere);
	el.style.color = "#090";
	el.style.fontSize = "120%";
	
	// Map stuff
	var strCoordinates;
	var zoom;
	
	if (newWhere == 'new_england')
	{
		strCoordinates = "42.948381,-70.664062";
		zoom = 7;
	}
	else if (newWhere == 'connecticut')
	{
		strCoordinates = "41.543533,-72.726746";
		zoom = 9;
	}
	else if (newWhere == 'maine')
	{
		strCoordinates = "44.964798,-68.840332";
		zoom = 7;
	}
	else if (newWhere == 'massachusetts')
	{
		strCoordinates = "42.126747,-71.784668";
		zoom = 8;
	}
	else if (newWhere == 'new_hampshire')
	{
		strCoordinates = "43.620171,-71.499023";
		zoom = 8;
	}
	else if (newWhere == 'rhode_island')
	{
		strCoordinates = "41.765166,-71.336975";
		zoom = 9;
	}
	else if (newWhere == 'vermont')
	{
		strCoordinates = "43.620171,-71.691284";
		zoom = 8;
	}
	
	where = newWhere;

	myMap.setZoom(zoom);
	myMap.panTo(getLatLng(strCoordinates));

	filterSchedule();
	markersBestFit();
} 

//*************************
function resetWhen(newWhen)
{
	// Hide or show date fields as needed
	var elDate = document.getElementById("filter_date");
	var elDateLabel = document.getElementById("filter_date_label");
	elDate.style.display = 'none';
	elDateLabel.style.display = 'none';
	
	var elBeginDate = document.getElementById("filter_begin_date");
	var elBeginDateLabel = document.getElementById("filter_begin_date_label");
	elBeginDate.style.display = 'none';
	elBeginDateLabel.style.display = 'none';
	
	var elEndDate = document.getElementById("filter_end_date");
	var elEndDateLabel = document.getElementById("filter_end_date_label");
	elEndDate.style.display = 'none';
	elEndDateLabel.style.display = 'none';
		
	if (newWhen == "All")
	{
	}
	else if (newWhen == "Date")
	{
		elDateLabel.style.display = '';
		elDate.style.display = '';
	}
	else if (newWhen == "Date_Range")
	{
		elBeginDateLabel.style.display = '';
		elBeginDate.style.display = '';
		elEndDateLabel.style.display = '';
		elEndDate.style.display = '';
	}
}

//**********************
function onWhen(newWhen)
{
	// Close the info window if it is open
	infoWindow.close();
	
	resetWhen(newWhen);

	when = newWhen;
	
	filterSchedule();
	markersBestFit();
}

var markersArray = [];

// Removes the overlays from the map, but keeps them in the array
//**********************
function clearOverlays()
{
	if (markersArray)
	{
		for (i in markersArray)
		{
			markersArray[i].setMap(null);
		}
	}

	markersArray.length = 0;
}

//***********************
function markersBestFit()
{
	var mapBounds = myMap.getBounds();
	var markersBounds = null;
	var latlngTop;
	var latlngBottom;
	
	// We need to take the height of the marker in latitude into account
	var zoom = myMap.getZoom();
	// If the zoom level is not at it's onWhere zoom level, return.  Don't try to fit markers.
	if ((where == 'new_england') || (where == 'maine'))
	{
		if (zoom != 7)
		{
			return;
		}
	}
	else if ((where == 'massachusetts') || (where == 'new_hampshire') || (where == 'vermont'))
	{
		if (zoom != 8)
		{
			return;
		}
	}
	else if ((where == 'connecticut') || (where == 'rhode_island'))
	{
		if (zoom != 9)
		{
			return;
		}
	}

	var markerheight = 0;
	if (zoom == 7)
	{
		markerheight = 0.165398//0.264637; // Small/Large markers 20/32
	}
	else if (zoom == 8)
	{
		markerheight = 0.087425//0.1398807;
	}
	else if (zoom == 9)
	{
		markerheight = 0.038784//0.0620545;
	}

	if (mapBounds)
	{
		var bAllInBounds = true;
		// Check to see if all of the markers are visible.
		// They may be cut off on the left or right.  We can fix that if needed
		for (var i = 0; i < markersArray.length; i++)
		{
			// Get the latlng for the points at the top and bottom of the marker
			latlngBottom = markersArray[i].getPosition();
			latlngTop = new google.maps.LatLng(latlngBottom.lat() + markerheight, latlngBottom.lng(), true);

			if (markersBounds == null)
			{
				markersBounds = new google.maps.LatLngBounds(latlngBottom, latlngTop);
			}

			markersBounds.extend(latlngBottom);
			markersBounds.extend(latlngTop);

			if (!mapBounds.contains(latlngBottom))
			{
				bAllInBounds = false;
			}
			
			if (!mapBounds.contains(latlngTop))
			{
				bAllInBounds = false;
			}
		}

		// If all of the markers are not visible, pan to fit.
		if (!bAllInBounds)
		{
			// Figure out if we should panToBounds or fitBounds
			var mapNE = mapBounds.getNorthEast();
			var mapSW = mapBounds.getSouthWest();
			var mapHeight = mapNE.lat() - mapSW.lat();
			var mapWidth = mapNE.lng() - mapSW.lng();

			var markersNE = markersBounds.getNorthEast();
			var markersSW = markersBounds.getSouthWest();

			var markersHeight = markersNE.lat() - markersSW.lat();
			var markersWidth = markersNE.lng() - markersSW.lng();

			// Take into account the scale at the bottom of the page
			mapHeight *= 13 / 14;
			// Take into account the navigation control on the left of the page
			mapWidth *= 19 / 20;

			if ((mapHeight > markersHeight) && (mapWidth > markersWidth))
			{
				myMap.panToBounds(markersBounds);
			}
			else
			{
				myMap.fitBounds(markersBounds);
			}
		}
	}
}

//***********************
function filterSchedule()
{
	year = '0';
	
	var newDate;
	var newDescriptiveEventTitle;
	var newState;
	var newCity;
	var rec;
	var tokens;
	var y;
	var m;
	var d;
	var content = document.getElementById('schedule');
	var str = "";
	var bUL = false;
	var id;
	var coordinates;
	var point;
	var strContent;
	var eventType;
	var scheduleArray = new Array();
	var markerRecArray = new Array();

	// Update the global dates
	if (when == "All")
	{
		strAllDate = getShorterFormatedDateX(new Date(), 0);
	}
	else if (when == "Date")
	{
		strDate = document.getElementById('filter_date').value;
	}
	else if (when == "Date_Range")
	{
		strBeginDate = document.getElementById('filter_begin_date').value;
		strEndDate = document.getElementById('filter_end_date').value;
	}

	// Clear the map markers
	clearOverlays();

	// Create a new array with filtered dates (datesArray)
	var tempDateRec;
	var filteredDatesArray = new Array();
	var j = 0;
	for (var i = 0; i < datesArray.length; i++)
	{
		tempDateRec = datesArray[i];
		if (passWhen(tempDateRec))
		{
			filteredDatesArray[j] = tempDateRec;
			j++;
		}
	}

	// Create a new array with filtered what and where
	var tempEventRec;
	var filteredEventsArray = new Array();
	j = 0;
	for (var i = 0; i < eventsArray.length; i++)
	{
		tempEventRec = eventsArray[i];
		if (passWhat(tempEventRec) && passWhere(tempEventRec))
		{
			filteredEventsArray[j] = tempEventRec;
			j++;
		}
	}

	// Create the final scheduleArray.
	// For each rec in filteredDatesArray see if there is a rec in filteredEventsArray with the same id.
	// If so create a scheculeRec and add it to scheduleArray
	var eventId;
	j = 0;
	for (var i = 0; i < filteredDatesArray.length; i++)
	{
		tempDateRec = filteredDatesArray[i];
		eventId = tempDateRec.event_id;
		for (var k = 0; k < filteredEventsArray.length; k++)
		{
			tempEventRec = filteredEventsArray[k];
			if (eventId == tempEventRec.event_id)
			{
				scheduleArray[j] = new scheduleRec(eventId, tempDateRec.date, tempEventRec.descriptive_event_title, tempDateRec.start_time, tempDateRec.end_time, tempEventRec.city, tempEventRec.state, tempEventRec.start_location_coordinates, tempEventRec.event_type);
				j++;
				break;
			}
		}
	}

// Create the final markerRecArray.
// For each rec in filteredEventsArray find all of the eventIds in filteredDatesArray that match,
// create a markerRec, and add it to markerRecArray
var j = 0;
for (var i = 0; i < filteredEventsArray.length; i++)
{
	tempEventRec = filteredEventsArray[i];
	eventId = tempEventRec.event_id;
	var dateArray = new Array();
	var m = 0;
	for (var k = 0; k < filteredDatesArray.length; k++)
	{
		tempDateRec = filteredDatesArray[k];
		if (eventId == tempDateRec.event_id)
		{
			dateArray[m] = tempDateRec;
			m++;
		}
	}
	if (dateArray.length > 0) // We found some dates belonging to the event
	{
		markerRecArray[j] = new markerRec(eventId, dateArray, tempEventRec.descriptive_event_title, tempEventRec.city, tempEventRec.state, tempEventRec.start_location_coordinates, tempEventRec.event_type);
		j++;
	}
}

var tempScheduleRec;
for (i = 0; i < scheduleArray.length; i++)
{
	tempScheduleRec = scheduleArray[i];
	newDate = tempScheduleRec.date;
	newState = tempScheduleRec.state;
	newDescriptiveEventTitle = tempScheduleRec.descriptiveEventTitle;
	newCity = tempScheduleRec.city;
	eventType = tempScheduleRec.eventType;
	id = tempScheduleRec.eventId;
	
	tokens = newDate.split('-');
	
	y = tokens[0];
	
	m = tokens[1];
	if (m[0] == "0")
	{
		m = m[1];
	}
	
	d = tokens[2];
	if (d[0] == "0")
	{
		d = d[1];
	}

	if ((y != year) || (m != month))
	{
		// add a new month, year header
		// add new day header
		str += "<h2>" + getFormatedMonth(m) + " " + y + "</h2>";
		str += "<h3 class='separator'>" + getFormatedDate(newDate) + "</h3>";
	}
	else if (d != day)
	{
		// add new day header
		str += "<h3 class='separator'>" + getFormatedDate(newDate) + "</h3>";
	}
	
	year = y;
	month = m;
	day = d;
	
	if (!bUL)
	{
		str += "<ul>";
		bUL = true;
	}
	// add tour/event to schedule
	var strTemp;
	if (newDescriptiveEventTitle != "")
	{
		strTemp = "<a href='JavaScript:void()' onClick='showDetails(" + id + ")'><span style='font-size: 110%;'><em>" + newDescriptiveEventTitle + "</em></span> - " + newCity + ", " + newState + "</a></li>";
	}
	else
	{
		strTemp = "<a href='JavaScript:void()' onClick='showDetails(" + id + ")'>" + newCity + ", " + newState + "</a></li>";
	}
	
	if (eventType == "garden_tour")
	{
		str += "<li class='garden_tour_list_item' >" + strTemp;
	}
	else if (eventType == "garden_event")
	{
		str += "<li class='garden_event_list_item' >" + strTemp;
	}
	else if (eventType == "garden_education")
	{
		str += "<li class='garden_education_list_item' >" + strTemp;
	}
	else
	{
		str += "<li class='garden_other_list_item' >" + strTemp;
	}
	
	if (bUL)
	{
		str += "</ul>";
		bUL = false;
	}
}

if (str == "")
{
	if (when == "All")
	{
		str += "<h3 class='separator'>There are no events scheduled</h3>";
	}
	else if (when == "Date")
	{
		str += "<h3 class='separator'>There are no events scheduled for " + strDate + "</h3>";
	}
	else if (when == "Date_Range")
	{
		str += "<h3 class='separator'>There are no events scheduled for " + strBeginDate + " - " + strEndDate + "</h3>";
	}
}

content.innerHTML = str;

// Add the markers to the map
var tempMarkerRec;
var tempDateArray;
var strToolTipContent;
for (i = 0; i < markerRecArray.length; i++)
{
	tempMarkerRec = markerRecArray[i];
	tempDateArray = tempMarkerRec.dateArray;
	newState = tempMarkerRec.state;
	newDescriptiveEventTitle = tempMarkerRec.descriptiveEventTitle;
	newCity = tempMarkerRec.city;
	eventType = tempMarkerRec.eventType;
	id = tempMarkerRec.eventId;
	coordinates = tempMarkerRec.startLocationCoordinates;
	point = getLatLng(coordinates);
	
	strContent = '<p style="color: #004000;">';
	
	if (newDescriptiveEventTitle != "")
	{
		strContent += "<span style='font-size: 110%;'><em>" + newDescriptiveEventTitle + "</em></span><br />";
	}

	for (j = 0; j < tempDateArray.length; j++)
	{
			strContent += '&nbsp;&nbsp;' + getFormatedDate(tempDateArray[j].date) + '<br />';
	}
	strContent += newCity + ', ' + newState + '<br /><a style="color: #009900;" href="JavaScript:void()"  onClick="showDetails(' + id + ')">Details</a></p>';
	
	strToolTipContent = "";
	if (tempMarkerRec.descriptiveEventTitle != "")
	{
		strToolTipContent = tempMarkerRec.descriptiveEventTitle += '\n';
	}
	
	for (j = 0; j < tempDateArray.length; j++)
	{
		if (j > 0)
		{
			strToolTipContent += '\n';
		}
		strToolTipContent += getFormatedDate(tempDateArray[j].date);
	}

	// add tour/event marker to markersArray
	if (eventType == "garden_tour")
	{
		markerImage = new google.maps.MarkerImage("../images/mm_20_green.png");
	}
	else if (eventType == "garden_event")
	{
		markerImage = new google.maps.MarkerImage("../images/mm_20_yellow.png");
	}
	else if (eventType == "garden_education")
	{
		markerImage = new google.maps.MarkerImage("../images/mm_20_blue.png");
	}
	else
	{
		markerImage = new google.maps.MarkerImage("../images/mm_20_white.png");
	}

	markersArray[i] = new google.maps.Marker({
		icon: markerImage,
		position: point,
		map: myMap,
		bubbleText: strContent,
		title: strToolTipContent
	  });

	addMarkerClickHandler(markersArray[i]);
}

	//************************************
	function addMarkerClickHandler(marker)
	{
		google.maps.event.addListener(marker, 'click', function() {
			infoWindow.setContent(marker.bubbleText);
			infoWindow.open(myMap, marker);
		});
	}
}

//********************
function passWhat(rec)
{
	if (showGardenTours && (rec.event_type == "garden_tour"))
	{
		return true;
	}
	else if (showGardenEvents && (rec.event_type == "garden_event"))
	{
		return true;
	}
	
	else if (showGardenEducation && (rec.event_type == "garden_education"))
	{
		return true;
	}
	
	else if (showGardenOther && (rec.event_type == "garden_other"))
	{
		return true;
	}
	
	return false;
}

//*********************
function passWhere(rec)
{
	var recWhere;
	
	if (where == "new_england")
	{
		return true;
	}
	else
	{
		var recState = rec.state;
		var testWhere;
		switch (rec.state)
		{
			case "Connecticut":
				testWhere = "connecticut";
			break;
			case "Maine":
				testWhere = "maine";
			break;
			case "Massachusetts":
				testWhere = "massachusetts";
			break;
			case "New Hampshire":
				testWhere = "new_hampshire";
			break;
			case "Rhode Island":
				testWhere = "rhode_island";
			break;
			case "Vermont":
				testWhere = "vermont";
			break;
		}
		
		if (testWhere == where)
		{
			return true;
		}
	}
	
	return false;
}

//********************
function passWhen(rec)
{
	var recDate = rec.date;

	if (when == "All")
	{
		var testDate = getDbFormatedDate(strAllDate);

		if (recDate >= testDate)
		{
			return true;
		}

		return false;
	}
	else if (when == "Date")
	{
		var testDate = getDbFormatedDate(strDate);

		if (recDate == testDate)
		{
			return true;
		}

		return false;
	}
	else if (when == "Date_Range")
	{
		var testBeginDate = getDbFormatedDate(strBeginDate);
		var testEndDate = getDbFormatedDate(strEndDate);

		if ((recDate >= testBeginDate) && (recDate <= testEndDate))
		{
			return true;
		}

		return false;
	}
}

// Takes a date string in the format: Saturday, Aug 14, 2010
//*********************************
function getDbFormatedDate(strDate)
{
	tempDate = strDate.replace(/,/g, "");
	var tokens = tempDate.split(" ");

	// Get day
	var day = tokens[2];
	if (day.length == 1)
	{
		day = "0" + day;
	}
	
	// Get month
	var month;
	switch (tokens[1])
	{
		case "Jan":
			month = "01";
		break;
		case "Feb":
			month = "02";
		break;
		case "Mar":
			month = "03";
		break;
		case "Apr":
			month = "04";
		break;
		case "May":
			month = "05";
		break;
		case "Jun":
			month = "06";
		break;
		case "Jul":
			month = "07";
		break;
		case "Aug":
			month = "08";
		break;
		case "Sep":
			month = "09";
		break;
		case "Oct":
			month = "10";
		break;
		case "Nov":
			month = "11";
		break;
		case "Dec":
			month = "12";
		break;
	}
	
	return tokens[3] + "-" + month + "-" + day;
}

