/****************************************
    Website Coded by Gelo Factory LLC
           All Rights Reserved
           (c)  Copyright 2007
         http://gelofactory.com/
****************************************/

if (GBrowserIsCompatible()) {
	var defaultLatitude = '40.755932';
	var defaultLongitude = '-73.986508';
	var defaultZoom = 14;
	var centerLatitude = defaultLatitude;
	var centerLongitude = defaultLongitude;
	var zoomLevel = defaultZoom;
	var startPointMarker = null;
	var endPointMarker = null;
	var starPointMarker = null;
	var routePolyLine = null;
	var bounds = new GLatLngBounds();

	var startIcon = new GIcon();
	startIcon.image = "http://www.gelofactory.com/sites/bridgerunners/_img/icon_start.png";
	startIcon.shadow = "http://www.gelofactory.com/sites/bridgerunners/_img/icon_flagshadow.png";
	startIcon.iconSize = new GSize(35, 35);
	startIcon.shadowSize = new GSize(46, 35);
	startIcon.iconAnchor = new GPoint(5, 34);
	startIcon.infoWindowAnchor = new GPoint(29, 5);

	var endIcon = new GIcon();
	endIcon.image = "http://www.gelofactory.com/sites/bridgerunners/_img/icon_end.png";
	endIcon.shadow = "http://www.gelofactory.com/sites/bridgerunners/_img/icon_flagshadow.png";
	endIcon.iconSize = new GSize(35, 35);
	endIcon.shadowSize = new GSize(46, 35);
	endIcon.iconAnchor = new GPoint(5, 34);
	endIcon.infoWindowAnchor = new GPoint(29, 5);

	var starIcon = new GIcon();
	starIcon.image = "http://www.gelofactory.com/sites/bridgerunners/_img/icon_star.png";
	starIcon.shadow = "http://www.gelofactory.com/sites/bridgerunners/_img/icon_flagshadow.png";
	starIcon.iconSize = new GSize(35, 35);
	starIcon.shadowSize = new GSize(46, 35);
	starIcon.iconAnchor = new GPoint(5, 34);
	starIcon.infoWindowAnchor = new GPoint(29, 5);

	function setupMap() {
		centerAndZoomMap();
	}

	function centerAndZoomMap(latitude, longitude, level, pan) {
		centerLatitude = (latitude) ? latitude : centerLatitude;
		centerLongitude = (longitude) ? longitude : centerLongitude;
		zoomLevel = (level) ? level : zoomLevel;
		pan = (typeof(pan) != undefined) ? pan : false;
		if (pan) {
			map.setZoom(zoomLevel);
			map.panTo(new GLatLng(centerLatitude, centerLongitude));
		} else {
			map.setCenter(new GLatLng(centerLatitude, centerLongitude), zoomLevel);
		}
	}

	function addRoutePoint(pointArray, isTrack) {
		var point = new GLatLng(pointArray.y, pointArray.x);
		var pointsLength = points.length;
		var pointExists = false;
		for (var i = 0; i < pointsLength; i++) if (!pointExists) pointExists = points[i].equals(point);
		if (!pointExists) {
			if (isTrack) {
				if (points.length == 1) removeLastRoutePoint(isTrack);
				addRouteStarPoint(point);
			} else {
				if (points.length == 0) addRouteStartPoint(point);
				if (points.length >= 1) addRouteEndPoint(point);
			}
			points.push(point);
			bounds.extend(point);
			drawRouteMap();
		}
	}

	function addRouteStartPoint(point) {
		startPointMarker = new GMarker(point, startIcon);
		map.addOverlay(startPointMarker);
	}

	function addRouteEndPoint(point) {
		if (endPointMarker) map.removeOverlay(endPointMarker);
		endPointMarker = new GMarker(point, endIcon);
		map.addOverlay(endPointMarker);
	}

	function addRouteStarPoint(point) {
		removeRouteStarPoint();
		starPointMarker = new GMarker(point, starIcon);
		map.addOverlay(starPointMarker);
	}

	function removeRouteStarPoint() {
		if (starPointMarker) map.removeOverlay(starPointMarker);
	}

	function drawRouteMap() {
		removeRouteMapOverlay();
		if (points.length) {
			routePolyLine = new GPolyline(points);
			map.addOverlay(routePolyLine);
		}
	}

	function removeRouteMapOverlay() {
		if (routePolyLine) map.removeOverlay(routePolyLine);
	}

	function centerMapOnRoute() {
		if (points.length) centerAndZoomMap(((bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2), ((bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2), map.getBoundsZoomLevel(bounds));
	}
} else {
	alert('Your browser is not compatible with Google Maps.');
}