var map;
function load(lat,lon,site){
if (!GBrowserIsCompatible()) {
        alert('Sorry, your browser is not compatible');
 }else{
        var point = new GLatLng(lat,lon);
	map = new GMap2(document.getElementById("gmap"));
        map = new GMap2(document.getElementById("gmap"));
        map.addMapType(G_PHYSICAL_MAP);
        map.addControl(new GSmallMapControl());
        map.removeMapType(G_NORMAL_MAP);
        map.setCenter(point, 10,G_PHYSICAL_MAP);
        GEvent.addListener(map, "click", function(overlay,point){
                if (point){
                        var list='?textField1='+point.y+'&textField2='+point.x+'&site='+site+'&smap=1';
                        parent.location.href='http://forecast.weather.gov/MapClick.php'+list;
                }
        });//end addListener function
        // put polygon on map (from gTopoMap.js)
        d=1.25;
        putPolygon(map,point,d);
	//addPolygons(map);
        }
}

function putPolygon(map,center,d){
var lat2;
var lat1;
var lat=center.lat();
var lon=center.lng();

var x=5;
var y=10;
var z=x+y;
var lat1=toRad(lat);
var lon1=toRad(lon);
//Find the 4 lat/lon points that are d = 1/2 the grid size N,S,E,W of the center point.
//This is needed for creating the right sized polygon.
var p1=findRLatLon(lat,lon,0,d);
var p2=findRLatLon(lat,lon,90,d);
var p3=findRLatLon(lat,lon,180,d);
var p4=findRLatLon(lat,lon,270,d);
//Now create the four corner points from the N,S,E,W points.
var pul=new GLatLng(p1.lat(),p4.lng());
var pur=new GLatLng(p1.lat(),p2.lng());
var plr=new GLatLng(p3.lat(),p2.lng());
var pll=new GLatLng(p3.lat(),p4.lng());
//Put all the points into an array.
var points = [];
points.push(pul);
points.push(pur);
points.push(plr);
points.push(pll);
points.push(pul);
//Make it all happen (well, let google do it).
var polygon = new GPolygon(points, "#f33f00",1,1,"#ff0000");
map.addOverlay(polygon);

var gridSize=d*2;
var tooltip
var text='The Forecast Displayed is for This '+gridSize+'km Area';

enableToolTip(polygon,text,tooltip);
}

function enableToolTip(polygon,text,tooltip){
	function placeTooltip(mousePosition){
	    currtype = map.getCurrentMapType().getProjection();
	    startPoint = currtype.fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(50, 70), true), map.getZoom());
	    var offset = currtype.fromLatLngToPixel(mousePosition, map.getZoom());
	    var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - startPoint.x, offset.y - startPoint.y));
	    pos.apply(tooltip);
	}
	tooltip=document.createElement("div");
	tooltip.className="tooltip";
	tooltip.style.width="100px";
	tooltip.style.fontSize="11px";
	tooltip.style.fontFamily="Arial";
	tooltip.style.color="black";
	tooltip.style.background="white";
	tooltip.innerHTML=text;
	tooltip.style.display="none";
	map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
	GEvent.addListener(map, 'mousemove', placeTooltip);

	var inPolys=false;
	GEvent.addListener(polygon, 'mouseover', function(){
		polygon.color="#ffff00";
		polygon.redraw(true);
		tooltip.style.display="block";
		inPolys=true;
	});
	GEvent.addListener(polygon, 'mouseout', function(){
		polygon.color="#f33f00";
		polygon.redraw(true);
		inPolys=false;
		if (!inPolys)
			tooltip.style.display="none";
	});
}

function toRad(latlon) {  // convert degrees to radians
  return latlon * Math.PI / 180;
}
function toDeg(latlon) {  // convert radians to degrees
  return latlon * 180 / Math.PI;
}

function findRLatLon(lat,lon,brng,dist){

var R = 6371;
var d = parseFloat(dist)/R
lat1=toRad(lat);
lon1=toRad(lon);
brng=toRad(brng);

  var lat2 = lat1 + d*Math.cos(brng);
  var dLat = lat2-lat1;
  var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
  var q = (Math.abs(dLat) > 1e-10) ? dLat/dPhi : Math.cos(lat1);
  var dLon = d*Math.sin(brng)/q;
  if (Math.abs(lat2) > Math.PI/2) lat2 = lat2>0 ? Math.PI-lat2 : -Math.PI-lat2;
  lon2 = (lon1+dLon+Math.PI)%(2*Math.PI) - Math.PI;
 
  if (isNaN(lat2) || isNaN(lon2)) return null;
  return new GLatLng(toDeg(lat2), toDeg(lon2));
}
