﻿/*
Logic for importing featured locations list.
*/

var _featureArray = new Array();

/// <summary>
/// Call to load featured locations
/// </summary>
/// <returns>null</returns>
function LoadFeaturedLocations()
{
   FetchFeaturedLocations();
}


function FetchFeaturedLocations()
{
    var whereClause = "";  //Show All locations
    var returnGeometry = true;
    var outFields = [_featuredName,_featuredLocationDescription, _featuredLocationZoolLevel];
    var fc_QueryTask = new esri.arcgis.gmaps.QueryTask(GetArcGISServerRESTURL(_featuredLocationsLayerIndex));
    var fc_Query = new esri.arcgis.gmaps.Query();
    ExecuteFeaturedLocationsRESTQuery(returnGeometry, outFields, fc_Query, fc_QueryTask);
    
}


//Handles the JSON response
function ProcessFeaturedLocationsResults(featureSet)
{
    try
    {
        //If JSON error
        if(featureSet.code != null){
            ErrorAlert(FeaturedLocationsError);
            return DisplayFeaturedLocations(FetchCannedLocations());
        }
    
        if(featureSet)
        {
            var featureArray;
            var html = "";
            //loop thru and display locations.
            for(var i=0; i<featureSet.features.length;i++)
             {
                feature = new Array();
                feature[0] = featureSet.features[i].geometry[0].getLatLng().y; //Lat
                feature[1] = featureSet.features[i].geometry[0].getLatLng().x; //Lon
                feature[2] = (featureSet.features[i].attributes[_featuredLocationZoolLevel] == undefined)?(_defaultFeaturedLocationsZoom):(featureSet.features[i].attributes[_featuredLocationZoolLevel]); //Zoom Level
                feature[3] = featureSet.features[i].attributes[_featuredName]; //Location Name
                feature[4] = featureSet.features[i].attributes[_featuredLocationDescription]; //Description 
                _featureArray[i] = feature; //Add the array to a list of feature arrays
                html += BuildFeaturedLocationLink(i, featureSet.features[i].attributes[_featuredName], featureSet.features[i].attributes[_featuredLocationDescription]);
             }
             return DisplayFeaturedLocations(html);
        }
    }
    catch(ex)
    {
        //TODO:  Call Error on 0 featured locations
        //If all else fails:
        ErrorAlert(FeaturedLocationsError);
        return DisplayFeaturedLocations(FetchCannedLocations());
    }

}

/// <summary>
/// Fetches Canned Featured Locations in case dynamic link fails
/// </summary>
/// <returns>HTML formatted result string of featured locations</returns>
function FetchCannedLocations()
{

    // In retrospect this should probably be a loop... but then again this is all going to be overwritten by ESRI's service anyways.

    var element0 = new Array();
    var element1 = new Array();
    var element2 = new Array();
    var element3 = new Array();
    var element4 = new Array();
    
    element0[0] = 36.2072302311275;
    element0[1] = -77.02437400817871;
    element0[2] = 15;
    element0[3] = "Downtown Washington, DC";
    _featureArray[0] = element0;
    
    element1[0] = 36.53215713544697;
    element1[1] = -76.62084102630615;
    element1[2] = 15;
    element1[3] = "Baltimore, Maryland";
    _featureArray[1] = element1;
    
    element2[0] = 38.507118;
    element2[1] = -97.044029;
    element2[2] = 4;
    element2[3] = "United States";
    _featureArray[2] = element2;
    
    element3[0] = 39.09229375512589;
    element3[1] = 116.37130737304688;
    element3[2] = 8;
    element3[3] = "Beijing, China";
    _featureArray[3] = element3;
 
    element4[0] = 34.25161025351679;
    element4[1] = 103.50460968017578;
    element4[2] = 4;
    element4[3] = "China";
    _featureArray[4] = element4;

    //For now, grabs this canned list of locations.
    //In the future, we'll grab dynamically from Web Service.
    var html = '<a href="javascript:AddPlacemarkToMap(0, '+true+','+true+');" title=\"(Country Code: US)\">Downtown Washington, DC</a><br/>';
        html += '<a href="javascript:AddPlacemarkToMap(1, '+true+','+true+');" title=\"(Country Code: US)\">Baltimore, Maryland</a><br/>';
        html += '<a href="javascript:AddPlacemarkToMap(2, '+true+','+true+');" title=\"(Country Code: US)\">United States</a><br/>';
        html += '<a href="javascript:AddPlacemarkToMap(3, '+true+','+true+');" title=\"(Country Code: US)\">Beijing, China</a><br/>';
        html += '<a href="javascript:AddPlacemarkToMap(4, '+true+','+true+');" title=\"(Country Code: US)\">China</a><br/>'; 
    return html;
}

function BuildFeaturedLocationLink(featureIndex, featureName, featureDescription)
{
   return "&rsaquo; <a class=\"aFeaturedLocations\" href=\"javascript:ClearEverything();AddPlacemarkToMap("+ featureIndex + "," + true +","+true+");\" title=\"" + featureDescription + "\">" + featureName + "</a><br />";
}


/// <summary>
/// Writes content to featured locations panel
/// </summary>
/// <param name="htmlContent">The list of featured locations, in HTML format</param>
/// <returns>null</returns>
function DisplayFeaturedLocations(htmlContent)
{
    $('wrapper-featuredLocations').innerHTML = "<div id='wrapper-featuredLocationsInner'>" + htmlContent + "</div>";
}