//<![CDATA[

function moveMapTo(map, lat, lng,zoom) {
	map.setCenter(new GLatLng(lat, lng), zoom);
		window.setTimeout(function() {
			map.panTo(new GLatLng(lat, lng));
	}, 100);
}

//show/hide the crosshair in center of map
function showCrossHair() {
	if(this.recording) { return; }
	var centerpoint = this.map.getCenter();
		//remove old icon
		if (this.centericon) {
			map.removeOverlay(centericon)
		}
		this.centericon = new GMarker(centerpoint, this.crosshairicon);
		map.addOverlay(centericon);
}

//called upon loading the page
function onLoad(skipWeather,skipMarkers,homePage) {

      if(skipWeather != true) {
		try {	   
			window.setTimeout(function() {
				loadWeather();
				}, 100);
		}
		catch(e){}
	}

	
	//load gmap
	if (GBrowserIsCompatible()) {

//ads
var adPos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7, 20));

var mapOptions = {
    googleBarOptions : {      style : "new",
      adsOptions: {        client: "pub-0412558566882978",
        channel: "3025713117",
        adsafe: "high",
        language: "en"      }  },

draggableCursor: 'crosshair',
draggingCursor: 'crosshair'
}

var publisher_id = 'pub-0412558566882978';
var adsManagerOptions = {  maxAdsOnMap : 2,  style: 'adunit', client:'partner-pub-0412558566882978'};


        var map = new GMap2(document.getElementById("map"),mapOptions);
        this.map = map;

adsManager = new GAdsManager(map, publisher_id, adsManagerOptions);
adsManager.enable();

        map.enableGoogleBar(); //ads!

	if(!homePage) {
		map.addMapType(G_PHYSICAL_MAP);

		addExtraMaps(map);
	}
	
	if(map != null) {
		
		//show lat & long on load
		GEvent.addListener(map, "load", function() {
			showCrossHair();
		});
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		map.addControl(new GScaleControl());
		
		map.disableScrollWheelZoom();
		//map.enableContinuousZoom();

		// create crosshair icon
		this.crosshairicon = new GIcon();
		crosshairicon.image = "/images/crosshair1.png";
		crosshairicon.shadow = "";
		crosshairicon.iconSize = new GSize(15, 15);
		crosshairicon.shadowSize = new GSize(0, 0);
		crosshairicon.iconAnchor = new GPoint(7, 7);
		crosshairicon.infoWindowAnchor = new GPoint(7, 7);
		
		//route recording
		initClickHandling(map);
		
		map.enableContinuousZoom();

		//will load route if passed in
		if(loadRouteFromURL(location.href)) {}
		else if(loadMapStateFromURL(location.href)) {
			window.setTimeout(function() {
			if(!skipMarkers) {loadRoutes();}
			}, 200);
	
			window.setTimeout(function() {
				if(!skipMarkers) {loadMarkers();}
			}, 300);

		}
		else { //default start location
			var z;	
			if(skipWeather){z =4;} else {z= 11;}
			moveMapTo(map, startLat, startLng, z);	

			window.setTimeout(function() {
				if(!skipMarkers) {loadRoutes();}
			}, 100);
	
			window.setTimeout(function() {
				if(!skipMarkers) {loadMarkers();}
				//else {newRoutesOverlay(map);} //show latest routes on map instead
			}, 100);
		}

		//show lat & long on move
		GEvent.addListener(map, "moveend", function() {
			var center = map.getCenter();
			document.getElementById("latLong").innerHTML = "latitude, longitude @ center: " + center.lat().toFixed(6) + ", " + center.lng().toFixed(6);
			
			showCrossHair();
		});

		var center = map.getCenter();
		if(center != null && center.lat) {
		document.getElementById("latLong").innerHTML = "latitude, longitude @ center of map: " + center.lat().toFixed(6) + ", " + center.lng().toFixed(6);
		}
		if(getElem("localonly") != null) {getElem("localonly").checked = true;}
		this.localSearch = true;
		if(!homePage) {
			document.getElementById("elevation").value = "-";
		}
	}//end if
		
	} //end gmap compat 
	else {	alert("Either your browswer can't handle Google Maps, or my google API key is wrong..."); }
	
}//end onLoad
 
function panToSite(point,zoom) {
	if(this.map == null) { GLog.write("ERROR! (map is null)"); }
	else 
	{
		map.setCenter(new GLatLng(point.y, point.x), zoom);
		window.setTimeout(function() {
		map.panTo(new GLatLng(point.y, point.x));
		}, 1000);
       }
}

function newRoutesOverlay() {

var geoXml = new GGeoXml("http://veloroutes.org/rss/geo.cgi?k=1");

 if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map")); 
    
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(42.496675,-62.65625), 3); 
    map.addControl(new GLargeMapControl());
    map.addOverlay(geoXml);
  }  


 /* var geoXml = new GGeoXml("http://veloroutes.org/rss/geo.cgi?k=1");
  map.addControl(new GLargeMapControl());
  map.setCenter(new GLatLng(49.496675,-102.65625), 3); 
  map.addOverlay(geoXml);*/
} 

//]]

