/**
 * Application "GWMFlashMx.mxml"
 */

/**
 * The "GWMFlashMx" javascript namespace. All the functions/variables you
 * have selected under the "GWMFlashMx.mxml" in the tree will be
 * available as static members of this namespace object.
 */
GWMFlashMx = {};
GWMFlashMx.ready = false;

/**
 * Listen for the instantiation of the Flex application over the bridge
 */
FABridge.addInitializationCallback("GWMFlashMx", GWMFlashMxReady);


/**
 * Hook here all the code that must run as soon as the "GWMFlashMx" class
 * finishes its instantiation over the bridge.
 *
 * For basic tasks, such as running a Flex method on the click of a javascript
 * button, chances are that both Ajax and Flex may well have loaded before the 
 * user actually clicks the button.
 *
 * However, using the "GWMFlashMxReady()" is the safest way, as it will 
 * let Ajax know that involved Flex classes are available for use.
 */
function GWMFlashMxReady() {
  // Initialize the "root" object. This represents the actual 
  // "GWMFlashMx.mxml" flex application.
  var flashroot = FABridge["GWMFlashMx"].root();
  var readyCallback = GWMFlashMx.readyCallback;
  GWMFlashMx = flashroot;
  GWMFlashMx.mapView = flashroot.getMapView();
  GWMFlashMx.mapView.redlineService = GWMFlashMx.mapView.getRedlineService();
  GWMFlashMx.mapView.geometryCaptureService = GWMFlashMx.mapView.getGeometryCaptureService();
  GWMFlashMx.mapView.transformationService = GWMFlashMx.mapView.getTransformationService();
  GWMFlashMx.legend = flashroot.getLegend();
  GWMFlashMx.selectSet = flashroot.getSelectSet();
  
  // event constants
  GWMFlashMx.MapViewEvent = {
    ViewRangeChange: "gwmMapViewRangeChange",
    CommandCancel: "gwmMapViewCommandCancel"
  };
  
  GWMFlashMx.MapViewMouseEvent = {
    Click: "gwmMapViewMouseClick",
    MouseMove: "gwmMapViewMouseMove"
  };
  
  GWMFlashMx.FeatureMouseEvent = {
    Click: "gwmFeatureMouseClick",
    DoubleClick: "gwmFeatureMouseDoubleClick",
    MouseOver: "gwmFeatureMouseOver",
    MouseOut: "gwmFeatureMouseOut"
  };
  
  GWMFlashMx.FeaturesEvent = {
    Append: "gwmFeaturesAppend",
    Remove: "gwmFeaturesRemove"
  };
  
  GWMFlashMx.FeatureEvent = {
    Selected: "gwmFeatureSelected",
    Unselected: "gwmFeatureUnselected",
    Fit: "gwmFeatureFit",
    Get: "gwmFeatureGet"
  };

  GWMFlashMx.RedlineEvent = {
    Capture: "gwmRedlineCapture",
    CaptureCancel: "gwmRedlineCaptureCancel",
    Remove: "gwmRedlineRemove",
    Get: "gwmRedlineGet", 
    Hide: "gwmRedlineHide"
  };

  GWMFlashMx.GeometryCaptureEvent = {
    Capture: "gwmGeometryCapture",
    CaptureCancel: "gwmGeometryCaptureCancel"
  };
  
  GWMFlashMx.ReaderEvent = {
    Success: "gwmReaderSuccess",
    Error: "gwmReaderError"
  };
  
  // redline constants
  GWMFlashMx.GeometryType = {
    Point: "point",
    Line: "line",
    Polygon: "polygon",
    Polyline: "polyline",   
    Circle: "circle",
    Ellipse: "ellipse",
    Rectangle: "rectangle",
    Text: "text",
    Callout: "callout"    
  };
  
  GWMFlashMx.DisplayMode = { on : 1, off : 2};
  GWMFlashMx.CoordinateUnits = { Readout: 0, Storage: 1, Distance: 2, Viewer: 100 };
  
  // method to convert a FeatureData object into the real ActionScript object
  GWMFlashMx.convertToFeature = function(stub){
    if (null != stub.legendEntry) {
      var le = GWMFlashMx.convertToLegendEntry(stub.legendEntry);
      var features = le.getFeatures();
      var feature = features.item(stub.id);
      features.release();
      return feature;
    }
    else {
      return null;
    }
  };
  
  // method to convert a LegendEntryData object into the real ActionScript object
  GWMFlashMx.convertToLegendEntry = function(stub) {
    var parent = null;
    if (null == stub.displayParent) {
      parent = GWMFlashMx.legend;
    }
    else {
      parent = GWMFlashMx.convertToLegendEntry(stub.displayParent);
    }
    var dispEntries = parent.getDisplayEntries();
    var le = dispEntries.item(stub.name);
    dispEntries.release();
    return le;
  }
  
  GWMFlashMx.ready = true;

  if(readyCallback)
  {
    readyCallback(GWMFlashMx);
  }
  
} //function GWMFlashMxReady()
