﻿ESRI.ADF.Graphics.GraphicFeatureGroup.prototype.add_coincidentFeaturesMouseOver = function(handler) {
    this.get_events().addHandler('coincidentFeaturesMouseOver', handler);
//    alert('this.getFeatureCount()'+this.getFeatureCount());
    if (!this._coincidentFeatureMouseOverEnabled)
    {
        for (var i = 0; i < this.getFeatureCount(); i++)
        {
            this.get(i).add_mouseOver(this._raiseCoincidentFeaturesEvent);
            this.get(i)._parentGroupID = this.get_id();
        }
        this._coincidentFeatureMouseOverEnabled = true;
    }
}
ESRI.ADF.Graphics.GraphicFeatureGroup.prototype.remove_coincidentFeaturesMouseOver = function(handler) {
    this.get_events().removeHandler('coincidentFeaturesMouseOver', handler);
    if (this._coincidentFeatureMouseOverEnabled)
    {

        for (var i = 0; i < this.getFeatureCount(); i++)
        {
            this.get(i).remove_mouseOver(this._raiseCoincidentFeaturesEvent);
        }
        this._coincidentFeatureMouseOverEnabled = false;
    }
}
ESRI.ADF.Graphics.GraphicFeatureGroup.prototype._raiseCoincidentFeaturesEvent = function(sender) {
 if (!sender._parentGroupID)
        return;
    
//    if (!ESRI.ADF.Geometries.Point.isInstanceOfType(sender.get_geometry()))
//        return;  //修改 98.3.25


    var graphicFeatureGroup = $find(sender._parentGroupID);
    var coincidentFeatures = graphicFeatureGroup.findCoincidentFeatures(sender);

        var eventArgs = { 'coincidentFeatures':coincidentFeatures }
        graphicFeatureGroup._raiseEvent('coincidentFeaturesMouseOver', eventArgs);
}

// Gets graphicFeatures from the passed-in graphicFeatureGroup that have centers that intersect the 
// symbol envelope of the passed-in graphicFeature
ESRI.ADF.Graphics.GraphicFeatureGroup.prototype.findCoincidentFeatures = function (graphicFeature) {
    var coincidentFeatures = new Array();

//    // Get the envelope of the graphic feature's symbol
   var graphicFeatureEnvelope = graphicFeature.getScreenEnvelope(true);   //原始98.3.25
//   var graphicFeatureEnvelope = graphicFeature.getScreenEnvelope(false);   //修改 98.3.25
//   var graphicFeatureEnvelope = graphicFeature.getEnvelope();  //修改 98.3.25

    if (!graphicFeatureEnvelope)
        return;                
    
    // Find the graphic features in the current graphicFeatureGroup that have centers within the
    // symbol envelope of the moused over feature
//      alert('graphicFeatureEnvelope=' + this.get(0).getScreenEnvelope(false));
//alert('graphicFeatureEnvelope=' + graphicFeatureEnvelope)
    for (var i = 0; i < this.getFeatureCount(); i++)
    {
        var targetGraphicFeature = this.get(i);
        
        // Get the envelope of the center of the loop feature    
        var targetGraphicEnvelope = targetGraphicFeature.getScreenEnvelope(false);  
//        var targetGraphicEnvelope = targetGraphicFeature.getEnvelope();                   
        
        // If the symbol envelope of the passed-in feature and the center envelope of the loop
        // feature intersect, add the loop feature to the array of coincident features
        if (graphicFeatureEnvelope.intersects(targetGraphicEnvelope))
        {
            if (targetGraphicFeature.get_id() == graphicFeature.get_id())
                coincidentFeatures.splice(0, 0, targetGraphicFeature);
            else
                coincidentFeatures.push(targetGraphicFeature);
        }
    }
   
    return coincidentFeatures;
}

// Retrieves an envelope of either the graphicFeature's symbol or its center.  
// The bounds of the envelope are in screen pixels.
ESRI.ADF.Graphics.GraphicFeature.prototype.getScreenEnvelope = function(getSymbolEnvelope)
{
    var xMin;
    var xMax;
    var yMin;
    var yMax;
    
    // Check whether the passed-in graphicFeature has a symbol defined.  If so, use it to determine the
    // envelope.  If not, use the graphicFeature's HTML DOM element.
    var symbol = this.get_symbol();
//    alert('symbol=' + symbol);
    if (symbol)
    {
        // use the graphic feature's ADF MarkerSymbol
        var symbolWidth = symbol.get_width();
        var symbolHeight = symbol.get_height();
        if (getSymbolEnvelope)
        {
            // Get the bounds for the graphic feature's symbol
            xMin = symbol.offsetX;
            xMax = symbol.offsetX + symbolWidth;
            yMin = symbol.offsetY;
            yMax = symbol.offsetY + symbolHeight;
        }
        else
        {
            // Get the graphic feature's center point
            xMin = symbol.offsetX + (symbolWidth / 2);
            xMax = symbol.offsetX + (symbolWidth / 2);
            yMin = symbol.offsetX + (symbolHeight / 2);
            yMax = symbol.offsetX + (symbolHeight / 2);
        }
    }
    else
    {
        // use the graphic feature's HTML DOM element
        var graphicElement = this.get_graphicReference();
        if (!graphicElement)
            return;
                                  
        var graphicElementWidth =  48;        //graphicElement.offsetWidth;  //配合手
        var graphicElementHeight = 48;      //graphicElement.offsetHeight;  //配合手
        
        if (getSymbolEnvelope)
        {
            // Get the bounds for the graphic feature's symbol
            xMin = graphicElement.offsetLeft;
            xMax = graphicElement.offsetLeft + graphicElementWidth;
            yMin = graphicElement.offsetTop;
            yMax = graphicElement.offsetTop + graphicElementHeight;
        }
        else
        {
            // Get the graphic feature's center point
            xMin = graphicElement.offsetLeft + (graphicElementWidth / 2);
            xMax = graphicElement.offsetLeft + (graphicElementWidth / 2);
            yMin = graphicElement.offsetTop + (graphicElementHeight / 2);
            yMax = graphicElement.offsetTop + (graphicElementHeight / 2);
//            alert('xMin ='+ xMin + ' xMax ='+ xMax + 'yMin ='+ yMin + 'yMax ='+ yMax);

        }
    }

    // Return an envelope with the calculated bounds
    return new ESRI.ADF.Geometries.Envelope(xMin, yMin, xMax, yMax);
}