﻿// Output (map) display functions

function HOW() { }

HOW.Init = function() {
    HOW.IsLoaded = false;
    HOW.IsValid = false;
    HOW.MapType = "pin";
    HOW.MapTypeDisplay = "Pin Mapping";
};

HOW.DefineLiterals = function(data, status) {
    //alert("HOW.DefineLiterals = " + status);

    HOW.PinMapChoice = data.PinMapChoice;

    HOW.RepeatCallsChoice = data.RepeatCallsChoice;
    HOW.RepeatCallsHeading = data.RepeatCallsHeading;
    HOW.RepeatCallsInfoHeading = data.RepeatCallsInfoHeading;
    HOW.RepeatCallsInfoContent = data.RepeatCallsInfoContent;

    HOW.DensityMapChoice = data.DensityMapChoice;
    HOW.DensityMapHeading = data.DensityMapHeading;
    HOW.DensityMapInfoHeading = data.DensityMapInfoHeading;
    HOW.DensityMapInfoContent = data.DensityMapInfoContent;

    var url = "Data.aspx?page=how";
    var target = document.getElementById("how-content");

    //alert("Show How = " + url);
    _dynoLoader._loadXmlDocument(url, target, HOW.Load, null, false);
};

HOW.Load = function() {
    HOW.IsLoaded = true;
    HOW.QueryLayerID = "";
    HOW.QueryLayerName = "";
    HOW.RepeatPoints = 2;
    HOW.OnlyIncludeTop = true;
    HOW.GeographyLayerID = "";
    HOW.GeographyLayerName = "";
    HOW.UseBoundarySubset = false;
    HOW.Classify = "Density";

    //alert("HOW.Load = " + HOW.RepeatCallsHeading);
    if (UIHelper.DensityMapEnabled) $("#how-density-map-button").show();
    if (UIHelper.RepeatCallsEnabled) $("#how-repeat-calls-button").show();

    HOW.MapType = ""; // Make sure the function doesn't exit early
    $('input[name="how-type"]')[0].checked = true;
    HOW.TypeSelected("pin", false);

    $("input[name*='how-type']").click(function() {
        HOW.TypeSelected($(this).val(), true);
    });

    $("input[name*='how-classify']").click(function() {
        HOW.ClassifySelected($(this).val());
    });

    HOW.DefineQueryLayers();

    UIHelper.HideStatusMessage(true);
};

HOW.DefineQueryLayers = function() {
    var s, idx;
    var crntID = HOW.QueryLayerID;
    var vals = WHAT.GetLayersInUse().split("|");
    //alert("HOW.DefineQueryLayers - QueryLayersInUse = " + WHAT.GetLayersInUse());

    HOW.QueryLayerID = "";

    // Query layers
    if (vals[0] < 2) {
        s = "Query layer: <span class='how-single-layer'>" + vals[2] + "</span>";
    } else {
        s = "Query Layer: ";
        s += "<select id='lstHowQueryLayers' onchange='HOW.QueryLayerChanged();'>"
        for (idx = 1; idx < vals[0] * 2; idx += 2) {
            s += "<option value='" + vals[idx] + "'";
            if (vals[idx] == crntID) {
                s += " selected='selected'";
                HOW.QueryLayerID = vals[idx];
                HOW.QueryLayerName = vals[idx + 1];
            }
            s += ">" + vals[idx + 1] + "</option>";
        }
        s += "</select>";

    }
    $("#how-query-layers-content").html(s);

    // Set the current layer
    if (HOW.QueryLayerID == "") {
        HOW.QueryLayerID = vals[1];
        HOW.QueryLayerName = vals[2];
    }

    if (WHERE.IsValid) {
        $("#how-density-boundary-subset").show();
        HOW.UseBoundarySubset = $('input[name=how-use-boundary-subset]').attr('checked');
    } else {
        $("#how-density-boundary-subset").hide();
        HOW.UseBoundarySubset = false;
    }
};

HOW.TypeSelected = function(type, indicatePending) {
    //alert("HOW.TypeSelected = " + $("#how-common-container h3").html());
    if (HOW.MapType == type) return;

    HOW.MapType = type;

    switch (type) {
        case "pin":
            $("#how-information-container").hide();
            $("#how-repeat-container").hide();
            $("#how-density-container").hide();
            $("#how-common-container").hide();
            $("#how-pin-container").show();

            UIHelper.QueryIcon = "images/UI/ico-pin-map.jpg";
            UIHelper.QuerySummary = UIHelper.PinMapSummary;

            HOW.MapTypeDisplay = HOW.PinMapChoice;
            break;

        case "repeat":
            $("#how-common-container h3").html(HOW.RepeatCallsHeading);
            $("#how-information-container h3").html(HOW.RepeatCallsInfoHeading);
            $("#how-information-content").html(HOW.RepeatCallsInfoContent);
            $("#how-pin-container").hide();
            $("#how-density-container").hide();
            $("#how-common-container").show();
            $("#how-repeat-container").show();
            $("#how-information-container").show();

            UIHelper.QueryIcon = "images/UI/ico-repeat-calls.jpg";
            UIHelper.QuerySummary = UIHelper.RepeatCallsSummary;

            HOW.MapTypeDisplay = HOW.RepeatCallsChoice;
            break;

        case "density":
            $("#how-common-container h3").html(HOW.DensityMapHeading);
            $("#how-information-container h3").html(HOW.DensityMapInfoHeading);
            $("#how-information-content").html(HOW.DensityMapInfoContent);
            $("#how-pin-container").hide();
            $("#how-repeat-container").hide();
            $("#how-common-container").show();
            $("#how-density-container").show();
            $("#how-information-container").show();

            UIHelper.QueryIcon = "images/UI/ico-density-map.jpg";
            UIHelper.QuerySummary = UIHelper.DensityMapSummary;

            HOW.MapTypeDisplay = HOW.DensityMapChoice;
            HOW.GeographyLayerID = $("#lstHowGeographyLayers").val();
            HOW.GeographyLayerName = $("#lstHowGeographyLayers :selected").text();
            break;
    }

    // Set the main summary image and description
    $("#analysis-summary-image").attr({ src: UIHelper.QueryIcon });
    $("#analysis-summary-paragraph").html(UIHelper.QuerySummary);

    CVS.BuildSummaryDisplay();
    if (indicatePending) UIHelper.IndicatePendingChanges(true);
};

HOW.QueryLayerChanged = function(type) {
    HOW.QueryLayerID = $("#lstHowQueryLayers").val();
    HOW.QueryLayerName = $("#lstHowQueryLayers :selected").text();

    CVS.BuildSummaryDisplay();
    UIHelper.IndicatePendingChanges(true);
};

HOW.RepeatPointChanged = function() {
    HOW.RepeatPoints = $("#lstHowRepeatPoints").val();
    //alert("HOW.RepeatPoints = " + HOW.RepeatPoints);

    CVS.BuildSummaryDisplay();
    UIHelper.IndicatePendingChanges(true);
};

HOW.OnlyIncludeTopChanged = function() {
    HOW.OnlyIncludeTop = $('input[name=how-only-include-top]').attr('checked');
    //alert("HOW.OnlyIncludeTop = " + HOW.OnlyIncludeTop);

    CVS.BuildSummaryDisplay();
    UIHelper.IndicatePendingChanges(true);
};

HOW.GeographyLayerChanged = function() {
    HOW.GeographyLayerID = $("#lstHowGeographyLayers").val();
    HOW.GeographyLayerName = $("#lstHowGeographyLayers :selected").text();

    CVS.BuildSummaryDisplay();
    UIHelper.IndicatePendingChanges(true);
};

HOW.BoundarySubsetChanged = function() {
    HOW.UseBoundarySubset = $('input[name=how-use-boundary-subset]').attr('checked');

    CVS.BuildSummaryDisplay();
    UIHelper.IndicatePendingChanges(true);
};

HOW.ClassifySelected = function(type) {
    //alert("HOW.ClassifySelected");
    if (HOW.Classify == type) return;

    HOW.Classify = type;

    CVS.BuildSummaryDisplay();
    UIHelper.IndicatePendingChanges(true);
};

HOW.GetSummary = function() {
    var s = "<tr><td class=\"field\">Display:</td><td>" + HOW.MapTypeDisplay + "</td></tr>";

    if (HOW.MapType == "density") {
        s += "<tr><td class=\"field\">Query Layer:</td><td>" + HOW.QueryLayerName + "</td></tr>";
        s += "<tr><td class=\"field\">Boundary Subset:</td><td>" + HOW.UseBoundarySubset + "</td></tr>";
        s += "<tr><td class=\"field\">Geography Layer:</td><td>" + HOW.GeographyLayerName + "</td></tr>";
        s += "<tr><td class=\"field\">Classify By:</td><td>" + HOW.Classify + "</td></tr>";
    }

    if (HOW.MapType == "repeat") {
        s += "<tr><td class=\"field\">Query Layer:</td><td>" + HOW.QueryLayerName + "</td></tr>";
        s += "<tr><td class=\"field\">Boundary Subset:</td><td>" + HOW.UseBoundarySubset + "</td></tr>";
        s += "<tr><td class=\"field\">Minimum Points:</td><td>" + $("#lstHowRepeatPoints :selected").text() + "</td></tr>";
        s += "<tr><td class=\"field\">Top 10 Only:</td><td>" + HOW.OnlyIncludeTop + "</td></tr>";
    }

    return s;
};
