// http://gisweb.azdeq.gov/arcgis/emaps/
// ADEQ: Arizona Department of Environmental Quality

function ShowMessage(message)
{
    // used let user know something is going on
    var html = '<table width="92%">';
    html += '<tr><td align="left">';
    html += '<img src="images/close.gif" width="16" height="14" alt="Close - Show TOC" onclick="ShowTOC();">';
    html += '</td><td align="right">';
    html += '<img src="images/close.gif" width="16" height="14" alt="Close - Show TOC" onclick="ShowTOC();">';
    html += '</td></tr>';
    html += '<tr><td align="center" class="nowrap">';
    html += '<span class="title"> &nbsp; ' + _featureLayer + '</span><br><br>';
    html += '&nbsp; ' + message;
    html += '</td></tr></table>';
    document.getElementById("panel").innerHTML = html;
    document.getElementById("panel").style.backgroundColor = _backgroundColor;
}

function ShowData()
{
    var html = '<table width="92%">';
    html += '<tr><td align="left">';
    html += '<img src="images/close.gif" width="16" height="14" alt="Close - Show TOC" onclick="ShowTOC();">';
    html += '</td><td align="right">';
    html += '<img src="images/close.gif" width="16" height="14" alt="Close - Show TOC" onclick="ShowTOC();">';
    html += '</td></tr>';
    html += '<tr><td align="center" class="nowrap">';
    html += '<span class="title"> &nbsp; ' + _featureLayer + '</span><br><br>';
    html += '</td></tr>';
    if (_features.length >= 500)  // hit maximum returned features limit
    {
        // let user know we've returned a partial list of features
        html += '<tr><td>';
        html += 'Partial list returned.<br>Refine query to return fewer features.';
        html += '</td></tr>';
    }
    html += '<tr><td class="nowrap">';

    // sort features
    var sorted = new Array();
    for (var i = 0; i < _features.length; i++)
    {
        // get display field value
        for (attribute in _features[i].attributes)
        {
            if (attribute == _displayField)
            {
                sorted.push(_features[i].attributes[attribute] + ":" + i);
                break;
            }
        }
    }
    sorted.sort();
    sorted.reverse();
    
    // create feature links
    while (sorted.length > 0)
    {
        var entry = sorted.pop();
        var split = entry.split(":");
        var feature = split[0];
        var index = split[1];
        html += '&nbsp;<span class="link" onmouseover="this.style.color=\'red\';" onmouseout="this.style.color=\'blue\'" onclick="ZoomFeature(' + index + ');" title="Zoom to feature and show attributes">' + feature + '</span><br>';
    }
    
    html += '</td></tr></table>';

    // display feature list
    document.getElementById("panel").innerHTML = html;
    document.getElementById("panel").style.backgroundColor = _backgroundColor;
}

function ZoomFeature(index)
{
    if (_features[index].geometry.type == "point")
    {
        var extent = EnvelopeFromPoint(_features[index].geometry, 1000);
        // shift map up a bit since centered county label hides feature label
        extent.ymin -= 100;
        extent.ymax -= 100;
        ZoomExtent(extent);
    }
    else
    {
        var extent = _features[index].geometry.getExtent().expand(1.1);
        ZoomExtent(extent);
    }

    setTimeout("ShowFeature(" + index + ");", 2000);
    if (_currentTool == "hyperlink") ShowHyperlink(_features[index]);
}

function ShowFeature(index)
{
    _map.infoWindow.setTitle(_featureLayer);

    // show feature attributes
    var html = '<table cellspacing="0" cellpadding="0">';
    
    // get display field value
    for (attribute in _features[index].attributes)
    {
        if (attribute == _displayField)
        {
            html += '<tr><td align="right" class="comment">' + attribute + ':&nbsp;</td><td class="comment">' + _features[index].attributes[attribute] + '</td></tr>';
            break;
        }
    }

    for (attribute in _features[index].attributes)
    {
        switch (attribute)
        {
                case _displayField:
                case "FID":
                case "Shape":
                case "Shape_Length":
                case "Shape_Area":
                // omit
                break;
            default:
                html += '<tr><td align="right" class="comment">' + attribute + ':&nbsp;</td><td class="comment">' + _features[index].attributes[attribute] + '</td></tr>';
                break;
        }
    }
    html += '</table>';
    _map.infoWindow.setContent(html);
    
    // show window
    var geometry = _features[index].geometry;
    if (geometry.type != "point") geometry = geometry.getExtent().getCenter();
    var point = _map.toScreen(geometry);
    _map.infoWindow.show(point, _map.getInfoWindowAnchor(point));
}

function ShowHyperlink(feature)
{
    // display hyperlink popup page
    var index = HyperlinkIndex();
    if (index < 0) return;

    var url = _hyperlinks[index][HYPERLINK];
    var feature = feature.attributes[_hyperlinks[index][FIELD]];
    if (feature.length == 0)
    {
        alert("Hyperlink undefined for this feature");
        return;
    }
    
    window.open(url + feature);
}

