//use the following var to remember an image's original src value
//the imageRoll function will constantly update this value, allowing the imageOut function to use its current value
var imageSourceValue = '';
function imageRoll(imageObject)
{
	//store the current src value
	imageSourceValue = imageObject.src;
	//grab the directory from the src attribute
	var folderIndex = imageObject.src.lastIndexOf("/");
	var theDirectory = imageObject.src.substring(0,folderIndex+1);//returns the DIR in the form http://domain.com/folder
	//test for image file type, jpg v. gif
	var theFileType = imageObject.src.substring(imageObject.src.length-3);//grab the last 3 characters, they represent the file type

	imageObject.src=theDirectory+imageObject.name+'-on.'+theFileType;
}
function imageOut(imageObject)
{
	//retrieve the original image src attribute
	imageObject.src = imageSourceValue;
}


var map = 0;
var sectionBounds = 0;
var currentSectionID = 0;
var currentServiceTypeID = 0;

var typeObjectArray = new Array();

function addServiceTypeID(filename,id)
{
	var newServiceTypeObject = new Object();
	newServiceTypeObject.id = id;
	//if(filename == 'library') filename = 'museums';
	newServiceTypeObject.iconName = filename;
	typeObjectArray.push(newServiceTypeObject);
}

/*
When section is first loaded, this function call stores the GLLBounds object and sets the view of the map
*/
function storeSectionData(arrayGLL,sectionID)
{
	currentSectionID = sectionID;
	
	var minLat = 90;
	var maxLat = -90;
	var minLon = 180;
	var maxLon = -180;
	for(var k=0; k < arrayGLL.length;k++)
	{
		if(arrayGLL[k].lat() < minLat){minLat = arrayGLL[k].lat();}
		if(arrayGLL[k].lat() > maxLat){maxLat = arrayGLL[k].lat();}
		if(arrayGLL[k].lng() < minLon){minLon = arrayGLL[k].lng();}
		if(arrayGLL[k].lng() > maxLon){maxLon = arrayGLL[k].lng();}
	}
	
	//store this GLLBounds object for resetting the map view
	sectionBounds = new GLatLngBounds(new GLatLng(minLat,minLon),new GLatLng(maxLat,maxLon));
	map.setCenter(sectionBounds.getCenter(),map.getBoundsZoomLevel(sectionBounds));
	
}
function initMap(mapDivTag)
{
	//initialize map and call populate func
	map = new GMap2(mapDivTag);
	map.addMapType(G_PHYSICAL_MAP);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	var point = new GLatLng(45.56021795715051,-71.60888671875);
	map.setCenter(point,7);
}


function centerAndZoomTheMap(lat,lon,z)
{
	map.setCenter(new GLatLng(lat,lon),z);
}

function zoomToStreet(lat,lng)
{
	centerAndZoomTheMap(lat,lng,15);
}

function closeInfoWindows()
{
	//close any open windows
	map.closeInfoWindow();
}

function hideAllMarkers()//trip planner markers and service markers
{
	removeTPMarkers();
	hideServiceMarkers();
}
/*
given an array of GLatLng objects, center and zoom the map
*/

function zoomToBestView(arrayGLL)
{
	if(!arrayGLL.length)
	{
		if(sectionBounds)//make sure the best map view has been stored
		{
			map.setCenter(sectionBounds.getCenter(),map.getBoundsZoomLevel(sectionBounds));
		}
	}
	else
	{
		var minLat = 90;
		var maxLat = -90;
		var minLon = 180;
		var maxLon = -180;
		for(var k=0; k < arrayGLL.length;k++)
		{
			if(arrayGLL[k].lat() < minLat){minLat = arrayGLL[k].lat();}
			if(arrayGLL[k].lat() > maxLat){maxLat = arrayGLL[k].lat();}
			if(arrayGLL[k].lng() < minLon){minLon = arrayGLL[k].lng();}
			if(arrayGLL[k].lng() > maxLon){maxLon = arrayGLL[k].lng();}
		}
		
		var GLLBounds = new GLatLngBounds(new GLatLng(minLat,minLon),new GLatLng(maxLat,maxLon));
		
		map.setCenter(GLLBounds.getCenter(),map.getBoundsZoomLevel(GLLBounds));
	}
	
}


//called by various functions to refresh the view
function reloadPage()
{
	document.location = "index.cfm?action=loadSection&sectionID="+currentSectionID+"&currentServiceTypeID="+currentServiceTypeID;
}
