//***********************************************************
// Trail Locator Project
// The Trans Canada Trail
// http://www.tctrail.ca
// 
// Coding by Stephane Simard
// ssoutbox-tctrail@yahoo.com
//************************************************************


// Variable global
var map;
var CanadianView = new GLatLng(49.88669246863498, -97.13087797164917);
var mapIsDblclick = false;
var globalValue;


//-------------------------------------------------------------------------------------------
// OnLoad the page - Setting google map & interface

function load()
{
	var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
	map = new GMap2(document.getElementById("mapDiv"));
	map.setCenter(CanadianView, mapZoom.getZoom(), G_NORMAL_MAP);
	map.addControl(new GScaleControl(), topRight);
	map.disableDoubleClickZoom();
		
	loadTLocatorEvent();
	loadInterface();
	loadProvincesFromXml();
	loadTrailsFromXml();
	loadExtendFromXml();
	loadCompactFromXml();
	
	updateZoomMenu();
	trailsListView();
	showTrailsByProvinces();
}


//-------------------------------------------------------------------------------------------
// MAP Event

function loadTLocatorEvent()
{
	GEvent.addListener(map, "moveend", function() 
	{   
		if (mapZoom.actualZoom >= mapZoom.extendZoom && mapZoom.actualZoom < 4)
			perform("mapControl()")	// If Extend Zoom - Call mapControl with perform function to show Loading message
		else
			mapControl();
		
		checkBounds();
	}); 
	
	GEvent.addListener(map, "dblclick", function()
	{
		mapIsDblclick = true;
		showLoadingMsg();
	});
	
	GEvent.addListener(map, "click", function(overlay, point) 
	{
		if(document.getElementById("distanceDiv").style.display == "block")
			clickDistanceEvent(overlay, point);
	});
}


//-------------------------------------------------------------------------------------------
// Tlocator Control - CallBack Function

function mapControl()
{	
	// if dblclick then Zoom in
	if(mapIsDblclick == true)
	{
		mapIsDblclick = false;
		zoomIn();
		return;
	}
	
	// Do not display Canada MAP if zoom ! minZoom(3)
	if (mapZoom.actualZoom != mapZoom.minZoom) 
		document.getElementById("canadaDiv").style.display = "none";
	
	// If MAP CANADA
	if (mapZoom.actualZoom == mapZoom.minZoom) 
	{
		showTrailsByProvinces();
		resetExtendOverlay();
	}
	
	// Display Google MAP compact trail de minZoom a extendZoom
	else if (mapZoom.actualZoom > mapZoom.minZoom && mapZoom.actualZoom < mapZoom.extendZoom)
	{
		if(isCompact == false)
		{
			resetExtendOverlay();
			isCompact = true;
			compactTrail();
		}
		showTrailsByProvinces();
	}
	
	// Display Google MAP extend trail de extendZoom a maxZoom
	else if (mapZoom.actualZoom >= mapZoom.extendZoom && mapZoom.actualZoom <= mapZoom.maxZoom)
	{
		resetCompactOverlay();
		isCompact = false;
		extendTrail();
		showTrailsByPosition();
	}
	
	updateZoomMenu();
	hideLoadingMsg();
	inputFocus.focus();
}


//-------------------------------------------------------------------------------------------
// ShowLoading MSG & Execute parameter action whit Settimeout

function perform(action)
{
	showLoadingMsg();
	setTimeout(action, 1);
}


//-------------------------------------------------------------------------------------------
// If the map position is out of Canada, move it back

function checkBounds()
{
	// Bounds for zoom 0 & 1
	var allowedBounds = new GLatLngBounds(new GLatLng(36.07895, -157.14651), new GLatLng(83.03412, -36.63919));
	
	// Bounds for zoom 2
	if(mapZoom.actualZoom == 2)
		allowedBounds = new GLatLngBounds(new GLatLng(40.67964, -145.10275), new GLatLng(83.02624, -49.15428));
	
	// Bounds for zoom 3 & more
	else if(mapZoom.actualZoom >= 3)
		allowedBounds = new GLatLngBounds(new GLatLng(41.63279, -142.01121), new GLatLng(83.26742, -51.46393));
		
	// Perform the check and return if in Canada
	if (allowedBounds.contains(map.getCenter()))
	  return;
	
	// It`s not OK, 
	alert (lang(4));	//msg Out of canada trail
	
	// Find the nearest allowed point and move there
	if(mapZoom.actualZoom == 1)
	{
		var distanceGetBackY = 11.0;
		var distanceGetBackX = 20.0;
		
		// Find the nearest place to getBack
		var C = map.getCenter();
		var X = C.lng();
		var Y = C.lat();
		
		var neY = allowedBounds.getNorthEast().lat();
		var neX = allowedBounds.getNorthEast().lng();
		var swY = allowedBounds.getSouthWest().lat();
		var swX = allowedBounds.getSouthWest().lng();

		if (Y > neY) {Y = neY  - distanceGetBackY;}
		if (X > neX) {X = neX  - distanceGetBackX;}
		if (Y < swY) {Y = swY  + distanceGetBackY;}
		if (X < swX) {X = swX  + distanceGetBackX;}
					
		// Save value for perform function
		globalValue = new GLatLng(Y,X);
		perform("map.setCenter(globalValue)");
	
	}else{	// Show zoom MAP CANADA
			mapZoom.actualZoom = mapZoom.minZoom;		
			document.getElementById("canadaDiv").style.display = "block"; 
			perform("map.setCenter(CanadianView, mapZoom.getZoom())");
	}
}
