﻿

// 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;

////////////////////////////
//
// zoomIn()
//
// This zooms the map in by 1 level
// It retrieves the current zoom level and subtracts 1 from it.
// maximum zoom level is 17; minimum zoom level is 0
//
////////////////////////////

function zoomIn() {
map.setZoom(map.getZoom() + 1);
}

//////////////////////////////
//
// zoomOut()
//
// This zooms the map out by 1 level
// It retrieves the current zoom level and adds 1 to it.
// maximum zoom level is 17; minimum zoom level is 0
//
////////////////////////////

function zoomOut() {
map.setZoom(map.getZoom() - 1);
}

/////////////////////////////
//
// setMapType()
//
// This sets the display type of the map.  The default map formats for standard Google Map are:
// G_NORMAL_MAP
// G_SATELLITE_MAP
// G_HYBRID_MAP
// One of these values needs to be passed into this function to set the map type.
// Custom map types can be created and called through the Google Maps API.
//
////////////////////////////

function setMapType(mapType) {
	map.setMapType(mapType);
}

///////////////////////////
//
// 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()));
}

function switchTabs(){
	var thisurl = String(document.location);
	thisurl = thisurl.replace(/submit=Go\#fragment-1/,"submit=Go");
	thisurl = thisurl.replace(/submit=Go\#fragment-2/,"submit=Go");
	thisurl = thisurl.replace(/submit=Go/,"submit=Go#fragment-2");
	window.location.href = thisurl;	
}