
//_____________________________________________________________________________
//RiverMap Object - constructor, sets up properties, event handlers
function RiverFinder ()
{
	this.IdahoCenterPoint = new GPoint('-114.67529296875', '45.66012730272194'); //intitial center point (centers idaho on the map)
	this.InitialZoomLevel = 11; //initial zoom level (shows all of idaho)
	this.Regions = new Array(); //contains MapRegion instances
	this.RiverRuns = new Array(); //contains RiverRun instances
	
	//create new instance of a google map
	this.map = new GMap(document.getElementById("map"));
	
	//initialize map, regions
	this.InitializeMap();
	
	//add event handlers
	GEvent.bind(this.map, "click", this, this.onMapClick);
	GEvent.bind(this.map, "zoom", this, this.onZoom);
	GEvent.bind(this.map, "moveend", this, this.onMoveEnd);
	
}

//_____________________________________________________________________________
//RiverMap Initialize Function, initialize map and region labels
RiverFinder.prototype.InitializeMap = function()
{
	//setup initial map
	this.map.centerAndZoom(this.IdahoCenterPoint, this.InitialZoomLevel);
	this.map.addControl(new GLargeMapControl());
	this.map.addControl(new GMapTypeControl());
	
	//instantiate regions
	this.Regions[this.Regions.length] = new MapRegion(0, new GPoint('-115.6640625', '47.55428670127958'), new GPoint('-116.455078125', '47.66538735632654'));
	this.Regions[this.Regions.length] = new MapRegion(1, new GPoint('-115.48828125', '45.390735154248894'), new GPoint('-115.1806640625', '45.259422036351694'));
	this.Regions[this.Regions.length] = new MapRegion(2, new GPoint('-115.64208984375', '43.48481212891603'), new GPoint('-115.64208984375', '43.48481212891603'));
	this.Regions[this.Regions.length] = new MapRegion(3, new GPoint('-112.34619140625', '43.37311218382002'), new GPoint('-112.34619140625', '43.37311218382002'));
	
	//add regions to map
	for(var regionid = 0; regionid < this.Regions.length; regionid++)
	{
		this.map.addOverlay(this.Regions[regionid].Marker);
	}
}

//_____________________________________________________________________________
//RiverMap AddRiverRun Function, add a river to the river runs array
RiverFinder.prototype.AddRiverRun = function(objRiverRun)
{
	this.RiverRuns[this.RiverRuns.length] = objRiverRun;
	return objRiverRun;
}

//_____________________________________________________________________________
//RiverMap ShowRegionalMarkers Function, displays region labels
RiverFinder.prototype.ShowRegionalMarkers = function()
{	
	//turn off all runs
	for(var run = 0; run < this.RiverRuns.length; run++)
	{
		this.RiverRuns[run].TurnOff();
	}
	
	//display region labels
	for(var regionid = 0; regionid < this.Regions.length; regionid++)
	{
		this.Regions[regionid].TurnOn();
	}
}

//_____________________________________________________________________________
//RiverMap  HideRegionalMakers Function, - removes region labels
RiverFinder.prototype.HideRegionalMarkers = function()
{
	//hide region labels
	for(var regionid = 0; regionid < this.Regions.length; regionid++)
	{
		this.Regions[regionid].TurnOff();
	}
}

//_____________________________________________________________________________
//RiverMap  FindRivers Function, finds rivers inside current bounds and shows
RiverFinder.prototype.FindRivers = function()
{
	//check each run to see if is in current bounds, turn on visible runs
	for(var run = 0; run < this.RiverRuns.length; run++)
	{
		if (this.IsInBounds(this.RiverRuns[run].MapPoint))
		{
			this.RiverRuns[run].TurnOn();
		}	
	}
}

//_____________________________________________________________________________
//RiverMap  IsInBounds Function, checks if point is within supplied bounds
RiverFinder.prototype.IsInBounds = function(point)
{
	var bounds = this.map.getBoundsLatLng();
	if (point.x >= bounds.minX &&
		point.x <= bounds.maxX &&
		point.y >= bounds.minY &&
		point.y <= bounds.maxY)
	{
		return true;
	}
	else
	{
		return false;
	}
		
}

//_____________________________________________________________________________
//RiverMap MoveEnd Event, find rivers if zoom level is low enough
RiverFinder.prototype.onMoveEnd = function()
{
	if (this.map.getZoomLevel() <= DetailLevel)
	{
		this.FindRivers();
	}
}

//_____________________________________________________________________________
//RiverMap Zoom Event, determine if region labels should be displayed
RiverFinder.prototype.onZoom = function(oldZoomLevel, newZoomLevel)
{	
	if (newZoomLevel == (DetailLevel + 1))
	{
		this.ShowRegionalMarkers();
	}
	else
	{
		this.HideRegionalMarkers();
	}
}

//_____________________________________________________________________________
//RiverMap MapClick Event, put coords in textboxes
RiverFinder.prototype.onMapClick = function(overlay, point)
{
	if (point) 
	{
		//document.getElementById("tbLat").value = point.x;
		//document.getElementById("tbLong").value = point.y;
	}
}

//_____________________________________________________________________________
//MapRegion - Defines a Region
function MapRegion(RegionalIndex, pCenterPoint, pMapPoint)
{
	this.CenterPoint = pCenterPoint;
	this.MapPoint = pMapPoint;
	this.RegionalZoomLevel = 10;
	this.IsOn = true;
	
	this.Icon = new GIcon(BaseRegionIcon);
	this.Icon.image = "assets/images/map/region"+ RegionalIndex + ".png";
	this.Marker = new GMarker(pMapPoint, this.Icon);
	
	GEvent.bind(this.Marker, "click", this, this.onMarkerClick);
}

//_____________________________________________________________________________
//MapRegion TurnOff Function, turns region label off
MapRegion.prototype.TurnOff = function()
{
	if (this.IsOn)
	{	
		objRiverFinder.map.removeOverlay(this.Marker);
		this.IsOn = false;
	}
}

//_____________________________________________________________________________
//MapRegion TurnOn Function, turns region label on
MapRegion.prototype.TurnOn = function()
{
	if (!this.IsOn)
	{	
		objRiverFinder.map.addOverlay(this.Marker);
		this.IsOn = true;
	}
}

//_____________________________________________________________________________
//MapRegion Click Event, zoom to region, find rivers
MapRegion.prototype.onMarkerClick = function()
{
	objRiverFinder.map.centerAndZoom(this.CenterPoint, this.RegionalZoomLevel);
	objRiverFinder.map.HideRegionalOverlays();
	objRiverFinder.map.FindRivers();
}

//_____________________________________________________________________________
//RiverRun - defines a river run
function RiverRun (pMapPoint, pRunName, pRiverName, pRunID)
{
	this.MapPoint = pMapPoint;
	this.RunName = pRunName;
	this.RiverName = pRiverName;
	this.RunID = pRunID;
	this.Marker = new GMarker(this.MapPoint, BaseRunIcon);
	this.IsOn = false;
	this.Region = "";
	this.Difficulty = "";
	this.TripLength = "";
	this.Season = "";
	this.IsMultiDay = false;
	GEvent.bind(this.Marker, "click", this, this.onMarkerClick);
}

//_____________________________________________________________________________
//RiverRun TurnOff Function, removes the run from the map
RiverRun.prototype.TurnOff = function()
{
	if (this.IsOn)
	{
		objRiverFinder.map.removeOverlay(this.Marker);
		this.IsOn = false;
	}
}

//_____________________________________________________________________________
//RiverRun TurnOn Function, add the run to the map
RiverRun.prototype.TurnOn = function()
{
	if (!this.IsOn)
	{
		objRiverFinder.map.addOverlay(this.Marker);
		this.IsOn = true;
	}
}

//_____________________________________________________________________________
//RiverRun Click Event - shows popup info window
RiverRun.prototype.onMarkerClick = function()
{
	this.Marker.openInfoWindowHtml('<div class="runinfo"><a href="riverdetail.aspx?runid=' + this.RunID + '">' + this.RunName + 
	'</a> - ' + this.Region + 
	'<br /><span>Difficulty: ' + this.Difficulty + '</span>' + 
	'<br /><span>Trip Length: ' + this.TripLength + '</span>' + 
	'<br /><span>Season: ' + this.Season + '</span></div>');
}


//_____________________________________________________________________________
//Setup global variables
var DetailLevel = 10;
var BaseRegionIcon;
var BaseRunIcon;
var objRiverFinder;

//_____________________________________________________________________________
//Instantiate Map

function LoadRiverMap()
{
	BaseRegionIcon = CreateRegionIcon();
	BaseRunIcon = new CreateRunIcon();
	objRiverFinder = new RiverFinder();
	LoadRuns();
}

function LoadRiverRunMap()
{
	BaseRegionIcon = CreateRegionIcon();
	BaseRunIcon = new CreateRunIcon();
	objRiverFinder = new RiverFinder();
	LoadRuns();
}

function GetCenterPoint()
{
	var CenterPoint = objRiverFinder.map.getCenterLatLng();
	document.getElementById("tbLat").value = CenterPoint.x;
	document.getElementById("tbLong").value = CenterPoint.y;	
}

//_____________________________________________________________________________
//CreateRunIcon
function CreateRunIcon()
{
	var BaseRunIcon = new GIcon();
	BaseRunIcon.shadow = "assets/images/map/spacer.gif";
	BaseRunIcon.image = "assets/images/map/rafters_trans.png";
	BaseRunIcon.iconSize = new GSize(30, 23);
	BaseRunIcon.shadowSize = new GSize(1, 1);
	BaseRunIcon.iconAnchor = new GPoint(15, 11);
	BaseRunIcon.infoWindowAnchor  = new GPoint(30, 23);
	return BaseRunIcon;
}

//_____________________________________________________________________________
//CreateRegionIcon
function CreateRegionIcon()
{
	var BaseRegionIcon = new GIcon();
	BaseRegionIcon.iconSize = new GSize(84, 20);
	BaseRegionIcon.shadowSize = new GSize(1, 1);
	BaseRegionIcon.iconAnchor = new GPoint(42, 11);
	BaseRegionIcon.infoWindowAnchor = new GPoint(84, 20);
	BaseRegionIcon.shadow = "assets/images/map/spacer.gif";
	return BaseRegionIcon;
}

