//Author: Ryan Villnow
//Company: Vivid Image
	
	var map;
    var container;
    var opacity = 0.3;
    var circle;
    var centerMarker;
    var circleUnits;
    var circleRadius;
    var zoom = 10;
    var centerPoint = new GLatLng(45.377201,-94.725952);

    function load() {
        doLoad();
    }

    /*code courtesy of http://maps.forum.nu/gm_sensitive_circle2.html*/
    function doLoad() {
        if (GBrowserIsCompatible()) {
            container = document.getElementById("map");
            map = new GMap2(container);
            map.setCenter(centerPoint, zoom);
            map.setMapType(G_HYBRID_MAP ); 
            map.addControl(new GScaleControl());
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.enableScrollWheelZoom();
            var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 620));
            distributorClick(-1)
        }
    }

    var center
    var bounds    

    /*function distributorClick(distributorID,distributorName)*/
    function distributorClick(distributorID)
    {
      //  var distributorID
        //var distributorName
        //distributorID = ddlDistributors.options[ddlDistributors.selectedIndex].value;
        /*distributorName = ddlDistributors.options[ddlDistributors.selectedIndex].text;
        document.getElementById("DistributorName").innerHTML = distributorName;*/

map.clearOverlays();
bounds = new GLatLngBounds();
if (distributorID ==1 || distributorID==-1) {centerPoint = new GLatLng(45.377201,-94.725952); map.setCenter(centerPoint)}
center = map.getCenter(); circleRadius = 11; circleUnits = 'MI';

//BEGIN MARKER LOCATIONS

if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,44.8358412,-94.3380344,'Hutchinson');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,44.5434712,-94.4454539,'Gibbon');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.5843007,-95.3233063,'Sedan');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,46.0890027,-94.8737129,'Browerville');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,44.7304149,-94.0382951,'Glencoe South');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.7900618,-95.0882817,'West Union');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.7067048,-95.5171323,'Lowry');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,46.1683780,-95.3575921,'Parkers Prairie');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.4444906,-94.0021539,'Clear Lake');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.5666835,-94.9498822,'Elrosa');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,44.8568676,-94.1541504,'Glencoe North');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.963765,-95.6710691,'Evansville');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.5016068,-95.1236031,'Brooten');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.0781036,-94.1952653,'Cokato');}
if (distributorID ==1 || distributorID==-1) {doDrawCircle(circleRadius,45.7787074,-94.2294872,'Rice');}

//END MARKER LOCATIONS


map.setZoom(map.getBoundsZoomLevel(bounds));
}


 function doDrawCircle(radius, latitude, longitude, towerLocation) {
        var circlePoints = Array();

        with (Math) {
            if (circleUnits == 'KM') {
                var d = circleRadius / 6378.8; // radians
            }
            else { //miles
                var d = circleRadius / 3963.189; // radians
            }

            /*
            var lat1 = (PI / 180) * center.lat(); // radians
            var lng1 = (PI / 180) * center.lng(); // radians
            */
            var lat1 = (PI / 180) * latitude; // radians
            var lng1 = (PI / 180) * longitude; // radians
            for (var a = 0; a < 21; a++) {
                var tc = (PI / 180) * a * 18;
                var y = asin(sin(lat1) * cos(d) + cos(lat1) * sin(d) * cos(tc));
                var dlng = atan2(sin(tc) * sin(d) * cos(lat1), cos(d) - sin(lat1) * sin(y));
                var x = ((lng1 - dlng + PI) % (2 * PI)) - PI; // MOD function
                var point = new GLatLng(parseFloat(y * (180 / PI)), parseFloat(x * (180 / PI)));
                circlePoints.push(point);
                bounds.extend(point);
            }


 var blueIcon = new GIcon(G_DEFAULT_ICON);
        blueIcon.image = "";
		
		// Set up our GMarkerOptions object
		markerOptions = { icon:blueIcon };

            var centerpoint = new GLatLng(latitude,longitude);
            //var marker = createMarker(centerpoint, "asdf","asdf","asdf");
            var marker = new GMarker(centerpoint,markerOptions)
            GEvent.addListener(marker, "click", function() {   
                marker.openInfoWindow(towerLocation);   
            });             
            map.addOverlay(marker);
            //map.addOverlay(new GMarker(centerpoint))

 



            if (d < 1.5678565720686044) {
                circle = new GPolygon(circlePoints, '#000000', 2, 1, '#000000', 0.25);
            }
            else {
                circle = new GPolygon(circlePoints, '#000000', 2, 1);
            }
            map.addOverlay(circle);

            
        }
    }

