﻿/*
catalogsearch.js handles the logic for executing catalog searches and processing the results
*/

var _extentToolOn = false; // boolean value to determine if the extent tool should be disabled or not.
var _placeNameSearchWindowVisible = true; //keeps track of whether the placename search result window is visible.  Once closed, set to false.

/// <summary>
/// Execute a Catalog Search
/// <param name="searchType">Types are {envelope, point, polygon, polyline}</param>
/// </summary>
/// <returns>bool.  Did REST Request get fired off?</returns>
function ExecuteCatalogSearch(searchGeometry)
{
    //Zoom is OK and Footprint count is OK
    //Execute the catalog search, and return the results to the map and to the grid.
    
    //Clear Results
    try 
    {
        _searchResultsGrid.Clear();
        
        //Visibly change button image
        $("mapPan").style.backgroundImage = "url(images/button-map-drag-disabled.gif)"; 
        $("mapMarker").style.backgroundImage = "url(images/button-map-pin-disabled.gif)";
        $("mapPoly").style.backgroundImage = "url(images/button-map-polygon-disabled.gif)"; 
        $("mapExtent").style.backgroundImage = "url(images/button-map-map-extent-disabled.gif)";
        
        //Turn on Freeze Div to disable application interaction while searching
        $("wrapper-searchingCover").style.display = "inline";
        
        //Display Loading image & text
        ShowLoadingDisplay(_searchGeometryObject.GeometryType);
        
        //Show Search Tab
        SwitchTab("search");
        
        //Gather Preferences from Prefs Object
        var whereClause = _searchPrefs.CreateWhereClause();
        var returnGeometry = true;
        var outFields = [_imageNameColumn,_dateColumn, _imageURL, _imageLL_Lat, _imageLL_Lon, _imageUR_Lat, _imageUR_Lon, _spacecraftColumn, _cloudCoverColumn, _dataOwnerColumn, _tiltAngleColumn, _resolutionColumn, _fullProductURL, _fullMetadataURL]; //Column names stored in preferences.js
        ExecuteRESTQuery(searchGeometry, whereClause, returnGeometry, outFields);
        return true;
    }
    catch(e) 
    {
        ErrorAlert(CatalogSearchError, true);
    }
}


//Global Search Geometry Object
//REST Request Query Task only likes GPoly, GLatLng, not a GMarker.
//So if it is a marker we're searching on, we need only to persist its GLatLng.
//This object will hold the geometry and geometry type to use for removing and adding search geometry
//and to use when updating serach preferences
function SearchGeometryObject()
{
    this.GeometryType;  //String - marker, polygon, polyline, envelope
    this.Geometry;  //Holds GMarker.GLatLng, GPolygon, GEnvelope (converts Markers to ESRI-REST-usable format.
    this.MapObject; //Holds the GPolygon, GMarker, GEnvelope (used to redraw AOI after search).
    this.AOIExists = false; //Whether or not there is an AOI
    
    this.SetGeometry = function (geometry, geometryType)
    {
        if(geometryType == "marker")
        {
            this.Geometry = geometry.getLatLng();
            this.MapObject = geometry;
            this.GeometryType = geometryType;
            this.AOIExists = true;
        }
        else
        {
            this.Geometry = geometry;
            this.MapObject = geometry;
            this.GeometryType = geometryType;
            this.AOIExists = true;
        }
    }
    
    this.Clear = function () 
    {
       this.GeometryType = "";
       this.MapObject = undefined;
       this.Geometry = undefined;
       this.AOIExists = false;
       _searchPrefs.DisableApplyAndRefreshCheckBox();
    }
}

/// <summary>
/// Depending on the geometry type, calculate bounds and/or area.
/// If area is less than Threshold execute the search, otherwise notify user
/// </summary>
/// <param name="geom">The geometry to search on</param>
/// <param name="geomType">The string containing the geometry type:  polyline, polygon, envelope</param>
/// <returns>N/A</returns>
function SearchByGeometry(geom) 
{    
    try
    {
        //See first if geometry is within threshold
        var appropriate = true;
        var errorMsg = "";
        if(geom.GeometryType == "polyline")
        {
            if (IsLengthAppropriate(GetKmLength(geom.Geometry)) == false)
            {
                appropriate = false;
                errorMsg = _mapPolyLineMsg;
            }
        }
        else if(geom.GeometryType == "polygon" || geom.GeometryType == "envelope")
        {
            if (IsAreaAppropriate(GetSquareKm(geom.Geometry)) == false)
            {
                appropriate = false;
                errorMsg = _mapPolygonMsg;
            }
        }
        
        //If is in threshold, execute search.
        if(appropriate)
        {
            ExecuteCatalogSearch(geom.Geometry);
            _searchGeometryObject.AOIExists = true;
        }
        else
        {
            MapAlert(errorMsg);
            _searchGeometryObject.AOIExists = false;
        }
        return appropriate;
    }
    catch (e)
    {
        ErrorAlert(CatalogSearchError, true);
    }
}