﻿// ---------------------------------------------------------
// MAP GLOBALS
//----------------------------------------------------------

var _gmap;
var _centerMarker;
var _geocoder = new GClientGeocoder();


// ---------------------------------------------------------
//MAP INITS
//----------------------------------------------------------

function InitMapDiv(mapDiv, latitude, longitude, zoom) {
    //INITIALIZES MAP IN DIV
    //SETS DEFAULT LOCATION
    
    //get element
    var MapDiv = document.getElementById(mapDiv);
    
    //show map or message that browser doesn't support
    if (InitGoogleMap(MapDiv)) { 
        
        //add controls 
        _gmap.addControl(new GSmallZoomControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT,0));
        
        //center (map must have a valid center to add buildings)
        SetMapCenter(latitude, longitude, zoom);
        _gmap.savePosition();
        
        //info window close handler
        GEvent.addListener(_gmap, "infowindowclose",
                function() {
                    _gmap.returnToSavedPosition();
                }
            ); 
    }   
    else{
       dvMapDiv.innerHTML = "<br/><br/>We're sorry, but our map tool is experiencing problems or is not compatible with your web browser.";
    }
}

function InitGoogleMap(dvMap) {
    //CREATES NEW MAP INSTANCE
    
    if (GBrowserIsCompatible()) {
        _gmap = new GMap2(dvMap);
        return true
    }
}

        

// ---------------------------------------------------------
//MAP METHODS
//----------------------------------------------------------

function SetMapCenter(latitude, longitude, zoom) {

    //remove current center marker if one exists
    if (_centerMarker != null) {
        RemoveMarker(_centerMarker);
    }
    
    //calc new center and set map
    var center = new GLatLng(latitude, longitude);
    _gmap.setCenter(center, zoom);

    var blueIcon = new GIcon();
    blueIcon.iconSize = new GSize(32, 32);
    blueIcon.shadowSize = new GSize(56, 32);
    blueIcon.iconAnchor = new GPoint(16, 32);
    blueIcon.infoWindowAnchor = new GPoint(16, 0);
    var thisicon = new GIcon(blueIcon, "/images/site/blue-map-marker.png", null, "/images/site/blue-map-marker-shadow.png");
    markerOptions = { icon: thisicon, draggable: true };
    //markerOptions = { icon: thisicon};
    var marker = new GMarker(center, markerOptions);

    
    //add listeners for center pin
    GEvent.addListener(marker, "dragstart", function() {
    
    });

    GEvent.addListener(marker, "dragend", function() {
        //reset center of map
        var point = marker.getLatLng();
        _gmap.setCenter(point, 15);
    });

    //add center pin to map
    _gmap.addOverlay(marker);

    //set global
    _centerMarker = marker;
}

function RemoveMarker(marker) {
    if (marker) {
        _gmap.removeOverlay(marker);
    }
}

function CalculateViewPort(SmallLat, SmallLong, LargeLat, LargeLong, ZoomLevel){
   //calc new zoom
    var bound1 = new GLatLng(SmallLat, SmallLong);
    var bound2 = new GLatLng(LargeLat, LargeLong);
    var bounds = new GLatLngBounds(bound1, bound2);
    var NewZoomLevel = ZoomLevel
    if (NewZoomLevel ==0){
        NewZoomLevel = _gmap.getBoundsZoomLevel(bounds) -1; //subracts 1 to zoom out 1 extra layer
    }

    //set center
    _gmap.setCenter(new GLatLng((SmallLat+LargeLat)/2, (SmallLong+LargeLong)/2),NewZoomLevel);
}


function AddBuilding(Lat, Long, Number, HTML) {
    //add point
    var latlng = new GLatLng(Lat, Long);
    var marker = new GMarker(latlng);
    //var windowopts = new GInfoWindowOptions
    _gmap.addOverlay(marker);

    //add event listener
    if (HTML != '') {
        GEvent.addListener(marker, "click",
                        function() {
                            _gmap.savePosition();
                            _gmap.setCenter(latlng, 15);
                            _gmap.openInfoWindowHtml(latlng, HTML);
                        }
                    );
    }       
}

function AddAddress(address, zoomLevel, clearExisting) {
    //clear existing?
    if (clearExisting) {
        _gmap.clearOverlays();
    }
    
    //retrieve and add
    _geocoder.getLatLng(
    address,
    function(point) {
        if (!point) {
            
            //alert("The address you entered cannot be found with our mapping software. Please make sure it is correct.");
        } else {
            _gmap.setCenter(point, zoomLevel);
            var marker = new GMarker(point);
            _gmap.addOverlay(marker);
        }
    }
  );
}

function GotoAddress(address, zoomLevel, clearExisting) {
    _geocoder.getLatLng(
    address,
    function(point) {
        if (!point) {

            //alert("The address you entered cannot be found with our mapping software. Please make sure it is correct.");
        } else {
            _gmap.setCenter(point, zoomLevel);
        }
    }
  );
}

//function AddBuilding(Lat, Long, Number, HTML){
//    if(map != null){
//    
//        var MapIcon = new GIcon(G_DEFAULT_ICON);
//        MapIcon.image = "/images/search/mappoints/" + Number + ".jpg";
//        var MapIconSize = new GSize(18,18);
//        MapIcon.iconSize = MapIconSize;
//        MapIcon.shadow = "";
//        markerOptions = { icon:MapIcon };

//        //add point
//        var point = new GLatLng(Lat, Long);
//        var marker = new GMarker(point,markerOptions); 
//        map.addOverlay(marker);

//        //add event listener
//        if(HTML != ''){
//            GEvent.addListener(marker, "click", 
//                    function() {
//                        map.savePosition();
//                        map.setCenter(point, 15);
//                        map.openInfoWindowHtml(point, HTML);
//                    }
//                );        
//        }

//  
//    }
//}


function GotoPrevMapLocation() {
    _gmap.closeInfoWindow();
  //  map.returnToSavedPosition();
}    


//function ChangeMapArea(AreaID) {

//    if(map != null){
//        if (AreaID ==0) {    
//            SetDefaultLocation() 
//        }

//        if (AreaID ==1) {
//            map.setCenter(new GLatLng(43.052926035483600, -87.892325520515440),13);
//        
//        }
//        
//        if (AreaID ==2) {
//            map.setCenter(new GLatLng(43.040171295899200, -87.91444838047028),13);
//        
//        }    

//        if (AreaID ==3) {
//            map.setCenter(new GLatLng(43.087364492731740, -87.8874921798706),13);
//        
//        }    

//        if (AreaID ==4) {
//            map.setCenter(new GLatLng(43.111249992367680, -87.906954288482670),13);
//        
//        }   
//        
//        if (AreaID ==5) {
//            map.setCenter(new GLatLng(43.068027572780245, -88.007381558418270),13);
//        
//        }  

//    }
//     
//}

//function GotoPrevMapLocation() {
//    map.closeInfoWindow();
//    map.returnToSavedPosition();
//}    




// 
