﻿/// <reference path="SearchResults.js" />
/// <reference path="SearchFunctions.js" />
/// <reference path="SearchSitesResultsBuilder.js" />

var _mapBar;
var _propertyMarkers = [];
var _progressBar;
var _bounds;
var _popupTracker = new GISPlanning_PopupTracker();
var _GISP_Filtering_Poly_Overlay;


function load() {
    //determine if any local layers are to be shown. Note, you must add the project ID here
    var myLocaLayersAreShown = true;

    //TWW Create Map object
    var myMapOptions = {
        MapBarColor: '#c0c0c0',
        SearchBar: true,
        StreetView: true,
        Satellite: true,
        Terrain: true,
        Earth: true,
        PolySelect: true,
        Identify: true,
        LocalData: myLocaLayersAreShown,
        Pinpoint: true,
        Distance: true,
        DragZoom: true,
        PolyOptions: {},
        GMapOptions: null,
        ScrollWheelZoom: false,
        StreetOptions: {
            PanoDiv: document.getElementById('pano'),
            ShowFunction: function() {
                $("#pano-wrapper").animate({ top: "450px" }, 1000);
                $("#pano-wrapper").show("fast");
                $("#mapCell").animate({ height: "750px" }, 1000);
            },
            HideFunction: function() {
                $("#pano-wrapper").animate({ top: "150px" }, 500);
                $("#pano-wrapper").hide("fast");
                $("#mapCell").animate({ height: "450px" }, 500);
            }
        },
        ZoomControls: GISP_ZOOMCONTROLS_LARGE,
        GGoogleBarOptions: { showOnLoad: false },
        ClickHandler: null,
        PolyStartedHandler: function() {
            RemovePoly();
        },
        PolySelectedHandler:
					function(markers, poly) {
					    var polyPointsString = '';
					    var numVertex = poly.getVertexCount();
					    for (i = 0; i < numVertex; i++) {
					        var vertex = poly.getVertex(i);
					        var lat = vertex.lat().toString().substr(0, vertex.lat().toString().indexOf('.', 0) + 5);
					        var lng = vertex.lng().toString().substr(0, vertex.lng().toString().indexOf('.', 0) + 5)
					        polyPointsString += lat + ":" + lng;

					        if (i < numVertex - 1) {
					            polyPointsString += ",";
					        }; //end if not last point
					    }; //end for each point in polygon

					    document.getElementById('hfldPolyPoints').value = polyPointsString;
					    AddPersistantPolygonInidicatingSearchArea();
					    DisplaySearch(true);
					},
        LocalOptions: { LoadedHandler: LayerControlLoaded, IsOpenByDefault: true }
    };





    _mapBar = new MapBar(document.getElementById('map'), myMapOptions);


    _progressBar = new ProgressbarControl(_mapBar.map, { width: 150 });
    _mapBar.map.setCenter(new GLatLng(35.97092116254337, -86.63801193237304), 7, G_PHYSICAL_MAP);

    JT_init();

    //Setup autocomplete extenders (Remove for safari for now, doesnt work
    jQuery.each(jQuery.browser, function(i) {
        if ($.browser.safari) {
            $(".nonSafari").css("visibility", "hidden");
        };//end if safari
    });


}; //end function load

function unloadMap() {
    _mapBar.googleEarth = null;
    _mapBar = null;
    GUnload();
};

function LayerControlLoaded(sender) {
    var myAirports = new GGeoXml("http://www.tvasites.com/kml/tvaairports.kml");
    var myColleges = new GGeoXml("http://www.tvasites.com/kml/tvacolleges.kml");

    sender.AddLayer(new GISPlanning_MapUtilities_LocalLayer("AIRPORTS", "Airports", myAirports, "KML", 75, false, '#37a0a2', { Tooltip: "Airports in the region" }, [{}]));
    sender.AddLayer(new GISPlanning_MapUtilities_LocalLayer("RAILROADS", "Railroads", null, "DYNAMIC", .8, false, '#3f3f3f', { Tooltip: "Show Railroads" }, [new GISPlanning_MapUtilities_LocalService("MAP", "DYNAMIC", "http://gpvm002.gisplanning.net/ArcGIS/rest/services/tva1/MapServer", { LayerID: 2 })]));
    sender.AddLayer(new GISPlanning_MapUtilities_LocalLayer("COLLEGE", "Colleges", myColleges, "KML", 75, false, '#fa7f23', { Tooltip: "Colleges in the area" }, [{}]));
    sender.AddLayer(new GISPlanning_MapUtilities_LocalLayer("Cities", "Cities", null, "DYNAMIC", .8, false, '#f0d187', { Tooltip: "Show city boundaries" }, [new GISPlanning_MapUtilities_LocalService("MAP", "DYNAMIC", "http://gpvm002.gisplanning.net/ArcGIS/rest/services/tva1/MapServer", { LayerID: 4 })]));
    sender.AddLayer(new GISPlanning_MapUtilities_LocalLayer("COUNTIES", "Counties", null, "DYNAMIC", .8, false, '#aa2524', { Tooltip: "Show county boundaries" }, [new GISPlanning_MapUtilities_LocalService("MAP", "DYNAMIC", "http://gpvm002.gisplanning.net/ArcGIS/rest/services/tva1/MapServer", { LayerID: 5 })]));
    sender.AddLayer(new GISPlanning_MapUtilities_LocalLayer("STATES", "States", null, "DYNAMIC", .8, false, '#5a5c57', { Tooltip: "Show state boundaries" }, [new GISPlanning_MapUtilities_LocalService("MAP", "DYNAMIC", "http://gpvm002.gisplanning.net/ArcGIS/rest/services/tva1/MapServer", { LayerID: 9 })]));
    sender.AddLayer(new GISPlanning_MapUtilities_LocalLayer("ZIPCODES", "Zip Codes", null, "DYNAMIC", .8, false, '#f4da03', { Tooltip: "Show ZIP Codes" }, [new GISPlanning_MapUtilities_LocalService("MAP", "DYNAMIC", "http://gpvm002.gisplanning.net/ArcGIS/rest/services/tva1/MapServer", { LayerID: 3 })]));
    sender.AddLayer(new GISPlanning_MapUtilities_LocalLayer("RIDAS", "Regions", null, "DYNAMIC", .8, false, '#5d2279', { Tooltip: "Show Regions" }, [new GISPlanning_MapUtilities_LocalService("MAP", "DYNAMIC", "http://gpvm002.gisplanning.net/ArcGIS/rest/services/tva1/MapServer", { LayerID: 6 })]));
   


    LoadLayersBasedOnSubsite(sender);
};




function RemoveMarkers(pMap, pMarkers) {
    for (var i = 0; i < pMarkers.length; i++) {
        pMap.removeOverlay(pMarkers[i]);
    }; //end for

    //reset the property markers
}; //end method

function ZoomToProperty(pPropertyID, REF_bounds) {
    var myMarker = FindPropertyMarker(pPropertyID);
    var myBounds = new GLatLngBounds();
    if (myMarker != null) {
        myBounds.extend(myMarker.getLatLng());
        _mapBar.map.setMapType(G_NORMAL_MAP);
        _mapBar.map.setZoom(_mapBar.map.getBoundsZoomLevel(myBounds) - 4);
        _mapBar.map.setCenter(myBounds.getCenter());
        myMarker.openInfoWindow(myMarker.HTML);
    }; //end if marker found

    REF_bounds = myBounds;
};

function ZoomToPropertyModal(pPropertyID) {
    tb_show("Interactive Map", '#TB_inline?height=460&width=960&inlineId=mapContainer');
    ZoomToProperty(pPropertyID);
}; //end function



function GetStaticMapURL(pLat, pLng, pOptions) {

    var myLink = "http://maps.google.com/staticmap?size=@WIDTHx@HEIGHT&maptype=@TYPE&key=@KEY&sensor=false&markers=@LAT,@LNG,@COLOR&zoom=@ZOOM";
    myLink = myLink.replace("@LAT", pLat);
    myLink = myLink.replace("@LNG", pLng);

    var myOptions = {
        Width: 300,
        Height: 190,
        Type: 'Roadmap',
        Key: 'ABQIAAAA28LOldUcLokeHYCqt4GgFRRdDQ78FEzvVkwOFsGJy0Vi0IxKMhRjLv8v3juUArx14qMorPG9clQ-7w',
        Color: 'green',
        Zoom: 12
    };

    //get the passed in option, or use the default and update the link
    for (var o in myOptions) {
        if (typeof (pOptions[o]) != "undefined") {
            myOptions[o] = pOptions[o];
        }; //end if option was passed in
        myLink = myLink.replace("@" + o.toUpperCase(), myOptions[o]);

    }; //end for each option

    return myLink;

};
function RemoveProgressBar() {if (_progressBar != null) {_progressBar.remove();}}