﻿// This determines how far the map will pan when the North, South, East, or West buttons are pressed.
// It will scroll the screen by the percentage below * the current screen width or height.
var panPercentage = 0.5;

///////////////////////////
// aroundTheWorldLngSpan()
//
// This function adjusts calculation of the visible longitudinal span when the view crosses the 
// +/-180 degree line.  It insures that the width of the view (in degrees) is calculated correctly.
// NE = the North-East corner of the span.
// SW = the South-West corner of the span.
////////////////////////////
function aroundTheWorldLngSpan(NE,SW) {
	if (SW > NE) {
		NE = NE + 360;			
	}
	return Math.abs(NE - SW);
}	

////////////////////////////
// aroundTheWorldLngPan()
//
// This function adjusts the calculation for the new view point when panning East or West across
// the +/-180 degree line.  It insures the map will pan correctly across this boundary.
////////////////////////////
function aroundTheWorldLngPan(lng) {
	if (lng < -180) {
		lng = lng + 360;			
	}
	else if (lng > 180) {
		lng = lng - 360;
	}
	return lng;
}	

////////////////////////////
// limitNS(lat)
//
// This ensures that the map does not scroll beyond +/- 85 degrees latitudinally.
// Using  +/- 90 degrees as a limit creates a problem.  The spherical coordinates
// become so flattened out at the extreme norhtern and southern boundaries
// it takes a lof of N-S movement to change 1 degree.  (At least this what I'm assuming
// the problem is.)
////////////////////////////
function limitNS(lat) {
	if (lat > 85) {
		lat = 85;
	}
	else if (lat < -85) {
		lat = -85;
	}
	return lat;
}

//////////////////////
// lngSpan()
//
// This calculates the latitudinal span of the current view.
// It takes into account calculating across the +/- 180 degree line.
////////////////////////////
function lngSpan() {
	var bounds = map.getBounds(); // the boundary coordinates for the current view
	var southWest = bounds.getSouthWest(); // the South-West corner of the boundary
	var northEast = bounds.getNorthEast(); // the North-East corner of the boundary
	return aroundTheWorldLngSpan(northEast.lng(), southWest.lng());
}

/////////////////////
// latSpan()
//
// This calculates the latitudinal span of the current view.
////////////////////////////
function latSpan() {
	var bounds = map.getBounds(); // the boundary coordinates for the current view
	var southWest = bounds.getSouthWest(); // the South-West corner of the boundary
	var northEast = bounds.getNorthEast(); // the North-East corner of the boundary
	return Math.abs(northEast.lat() - southWest.lat());		
}
	
///////////////////////
// panWest()
//
// This function will pan the map West by a set % of the current visible width.
// The percentage is set in the variable panPercentage.
// It takes the current visible longitudinal span, multiplies it by panPercentage, and pans the map to the new longitudinal location
// The pan will be smooth if the new location is in the current view.
//
// NOTE: When the map is zoomed nearly all the way out, the panWest() will actually appear to move East.
// This is presumable because when Google Maps receives the new coordinates for the center,
// It is actually shorter to pan to the East rather than to the West, even though the coordinates
// given are to the West.
////////////////////////////
function panWest () {
	map.panTo(new GLatLng(map.getCenter().lat(), aroundTheWorldLngPan(map.getCenter().lng() - (lngSpan() * panPercentage))));
}

/////////////////////////
// panEast()
//
// This function will pan the map East by a set % of the current visible width.
// The percentage is set in the variable panPercentage.
// It takes the current visible longitudinal span, multiplies it by panPercentage, and pans the map to the new longitudinal location
// The pan will be smooth if the new location is in the current view.
////////////////////////////
function panEast () {
	map.panTo(new GLatLng(map.getCenter().lat(), aroundTheWorldLngPan(map.getCenter().lng() + (lngSpan() * panPercentage))));
}

////////////////////////////////
// panNorth()
//
// This function will pan the map North by a set % of the current visible width.
// The percentage is set in the variable panPercentage.
// It takes the current visible latitudinal span, multiplies it by panPercentage, and pans the map to the new latitudinal location
// The pan will be smooth if the new location is in the current view.
////////////////////////////
function panNorth () {
	map.panTo(new GLatLng(limitNS(map.getCenter().lat() + (latSpan() * panPercentage)), map.getCenter().lng()));
}

//////////////////////////////////
// panSouth()
//
// This function will pan the map South by a set % of the current visible width.
// The percentage is set in the variable panPercentage.
// It takes the current visible latitudinal span, multiplies it by panPercentage, and pans the map to the new latitudinal location
// The pan will be smooth if the new location is in the current view.
////////////////////////////
function panSouth () {
	map.panTo(new GLatLng(limitNS(map.getCenter().lat() - (latSpan() * panPercentage)), map.getCenter().lng()));
}
