/**
 * External JS dependencies:
 * AjaxStreetLightSearch
 *
 * External var dependencies:
 * toolbar.reportErrorButton.noReportOnSwitchingCabinets
 * toolbar.reportErrorButton.confirmNewStreetlight
 * report.observation.over.limit
 * report.observation.limit
 * selectStreetlight.titleSL
 * observationform.title
 * layerIDStartTX
 * layerIDStartSL
 */

var reportErrorX;
var reportErrorY;

/**
 * Ajax callback-method for the calls that return a new map image URL that shall be updated.
 */
function showClickResultsReportError(result) {
    //alert('Result is ' + result.length + ' long: ' + result);

    if (result == null || result.length == 0 ||
            (result.length == 1 && result[0].idArray.length == 0) ) {
        var isSure = window.confirm(toolbar_reportErrorButton_confirmNewStreetlight);
        if (isSure) {
            getFrame('mapframe').initPopup(
                    observationform_title, // 'Rapporter feil på veilys som ikke er i Hafslunds register'
                    'observationform.htm?x_pos=' + reportErrorX + '&y_pos=' + reportErrorY, 'observationform');
        }
    } else {
        if (result.length == 1 && result[0].idArray.length == 1) {
            if (result[0].layerID.indexOf(layerIDStartSL) == 0) {
                getFrame('mapframe').initPopup(observationform_title, 'observationform.htm?streetlightid=' + result[0].idArray[0], 'observationform');
            } else if (result[0].layerID.indexOf(layerIDStartTX) == 0) {
                alert(toolbar_reportErrorButton_noReportOnSwitchingCabinets);
            } else {
                alert("Assertion error. layerID unknown: " + result[i].layerID);
            }
        } else {
            var streetLightIds = new Array();
            var foundSL = false;
            for (var i = 0; i < result.length; ++i) {
                // debug: alert(result[i].layerID + ": " + result[i].idArray + ".");
                if (result[i].layerID.indexOf(layerIDStartSL) == 0) {
                    streetLightIds = streetLightIds.concat(result[i].idArray);
                    foundSL = true;
                } else {
                    // Intentionally empty
                }
            }
            if (foundSL) {
                getFrame('mapframe').initPopup(selectStreetlight_titleSL,
                        'selectStreetlight.htm?' +
                        'forwardURLSL=' + 'observationform.htm' +
                        '&streetlightids=' + streetLightIds +
                        '&forwardTitleSL=' + observationform_title +
                        '&forwardTypeSL=' + 'observationform',
                        'selectStreetlight');
            } else {
                alert(toolbar_reportErrorButton_noReportOnSwitchingCabinets);
            }
        }
    }
}

/**
 * Enables the click tool, and sets up events for reportError when mouse is clicked in map
 */
function reportErrorOnButtonClick(e) {
    getFrame('mapframe').enableClickTool(reportErrorOnMouseClicked);
}

/**
 * Reads data for clicked item, and displays in popup
 */
function reportErrorOnMouseClicked(x1, y1) {

    var selectedScale = getFrame('mapframe').getSelectedScale();

    if (selectedScale != null && selectedScale > report_observation_limit) {
        alert(report_observation_over_limit + " (1:" + report_observation_limit + ")");
    } else {
        reportErrorX = x1;
        reportErrorY = y1;
        AjaxStreetLightSearch.doSearch(x1, y1, showClickResultsReportError);
    }
}

