 
 package spark.primitives
 {
+
 import flash.display.Bitmap;
 import flash.display.BitmapData;
 import flash.display.DisplayObject;
+import flash.display.DisplayObjectContainer;
 import flash.display.Graphics;
+import flash.display.Loader;
+import flash.display.LoaderInfo;
 import flash.display.Sprite;
+import flash.events.Event;
+import flash.events.HTTPStatusEvent;
+import flash.events.IOErrorEvent;
+import flash.events.ProgressEvent;
+import flash.events.SecurityErrorEvent;
 import flash.geom.Matrix;
 import flash.geom.Point;
 import flash.geom.Rectangle;
-
+import flash.net.URLRequest;
+import flash.system.ApplicationDomain;
+import flash.system.LoaderContext;
+
+import mx.core.mx_internal;
 import mx.graphics.BitmapFillMode;
-
-import spark.primitives.supportClasses.GraphicElement; 
+import mx.graphics.BitmapScaleMode;
+import mx.graphics.BitmapSmoothingQuality;
+
+import spark.core.contentLoader.ContentRequest;
+import spark.core.contentLoader.IContentLoader;
+import spark.core.DisplayObjectSharingMode;
+import spark.layouts.HorizontalAlign;
+import spark.layouts.VerticalAlign;
+import spark.primitives.supportClasses.GraphicElement;
+
+use namespace mx_internal;
+
+//--------------------------------------
+//  Events
+//--------------------------------------
+
+/**
+ *  Dispatched when content loading is complete.
+ *
+ *  @eventType flash.events.Event.COMPLETE
+ *  
+ *  @langversion 3.0
+ *  @playerversion Flash 10
+ *  @playerversion AIR 2.0
+ *  @productversion Flex 4.5
+ */
+[Event(name="complete", type="flash.events.Event")]
+
+/**
+ *  Dispatched when a network request is made over HTTP 
+ *  and Flash Player or AIR can detect the HTTP status code.
+ * 
+ *  @eventType flash.events.HTTPStatusEvent.HTTP_STATUS
+ *  
+ *  @langversion 3.0
+ *  @playerversion Flash 10
+ *  @playerversion AIR 2.0
+ *  @productversion Flex 4.5
+ */
+[Event(name="httpStatus", type="flash.events.HTTPStatusEvent")]
+
+/**
+ *  Dispatched when an input/output error occurs.
+ *  @see flash.events.IOErrorEvent
+ *
+ *  @eventType flash.events.IOErrorEvent.IO_ERROR
+ *  
+ *  @langversion 3.0
+ *  @playerversion Flash 10
+ *  @playerversion AIR 2.0
+ *  @productversion Flex 4.5
+ */
+[Event(name="ioError", type="flash.events.IOErrorEvent")]
+
+/**
+ *  Dispatched when content is loading.
+ *
+ *  <p><strong>Note:</strong> 
+ *  The <code>progress</code> event is not guaranteed to be dispatched.
+ *  The <code>complete</code> event may be received, without any
+ *  <code>progress</code> events being dispatched.
+ *  This can happen when the loaded content is a local file.</p>
+ *
+ *  @eventType flash.events.ProgressEvent.PROGRESS
+ *  
+ *  @langversion 3.0
+ *  @playerversion Flash 10
+ *  @playerversion AIR 2.0
+ *  @productversion Flex 4.5
+ */
+[Event(name="progress", type="flash.events.ProgressEvent")]
+
+/**
+ *  Dispatched when a security error occurs.
+ *  @see flash.events.SecurityErrorEvent
+ *
+ *  @eventType flash.events.SecurityErrorEvent.SECURITY_ERROR
+ *  
+ *  @langversion 3.0
+ *  @playerversion Flash 10
+ *  @playerversion AIR 2.0
+ *  @productversion Flex 4.5
+ */
+[Event(name="securityError", type="flash.events.SecurityErrorEvent")]
 
 /**
  *  A BitmapImage element defines a rectangular region in its parent element's 
- *  coordinate space, filled with bitmap data drawn from a source file.
+ *  coordinate space, filled with bitmap data drawn from a source file or
+ *  source URL.
  *  
  *  @includeExample examples/BitmapImageExample.mxml
  *  
  *  @langversion 3.0
  *  @playerversion Flash 10
- *  @playerversion AIR 1.5
- *  @productversion Flex 4
+ *  @playerversion AIR 2.0
+ *  @productversion Flex 4.0
  */
 public class BitmapImage extends GraphicElement
 {
     include "../core/Version.as";
-
+        
     //--------------------------------------------------------------------------
     //
     //  Constructor
     //
     //--------------------------------------------------------------------------
-
+    
     /**
      *  Constructor. 
      *  

                // Generate the 16 points of the source (unscaled) grid
                cachedSourceGrid = [];
                cachedSourceGrid.push([new Point(0, 0), new Point(_scaleGridLeft, 0), 
                                new Point(_scaleGridRight, 0), new Point(_bitmapData.width, 0)]);
                cachedSourceGrid.push([new Point(0, _scaleGridTop), new Point(_scaleGridLeft, _scaleGridTop), 
                                new Point(_scaleGridRight, _scaleGridTop), new Point(_bitmapData.width, _scaleGridTop)]);
                cachedSourceGrid.push([new Point(0, _scaleGridBottom), new Point(_scaleGridLeft, _scaleGridBottom), 
                                new Point(_scaleGridRight, _scaleGridBottom), new Point(_bitmapData.width, _scaleGridBottom)]);
                cachedSourceGrid.push([new Point(0, _bitmapData.height), new Point(_scaleGridLeft, _bitmapData.height), 
                                new Point(_scaleGridRight, _bitmapData.height), new Point(_bitmapData.width, _bitmapData.height)]);                         
            }
            
            if (cachedDestGrid == null || 

                {
                    repeatBitmap = true;
                }    
                break;
            
            case BitmapFillMode.SCALE:
                if (_bitmapData)
                {
                    fillScaleX = adjustedWidth / _bitmapData.width;
                    fillScaleY = adjustedHeight / _bitmapData.height;
                }
                break;
            
            case BitmapFillMode.CLIP:
                if (_bitmapData)
                {
                    fillWidth = Math.min(adjustedWidth, _bitmapData.width);
                    fillHeight = Math.min(adjustedHeight, _bitmapData.height);
                }
                break;
        }
        
        // If no scaleGrid is defined or if fillMode != SCALE, just draw 
        // the entire rect
        if (_fillMode != BitmapFillMode.SCALE ||
            isNaN(_scaleGridTop) ||
            isNaN(_scaleGridBottom) ||
            isNaN(_scaleGridLeft) ||
            isNaN(_scaleGridRight))
        {
            var sampledScale:Boolean = _smooth && _smoothingQuality == BitmapSmoothingQuality.HIGH;
            var b:BitmapData = sampledScale ? resample(_bitmapData, fillWidth, fillHeight) : _bitmapData;
            
            if (sampledScale && _fillMode == BitmapFillMode.SCALE)
            {
                fillScaleX = adjustedWidth / b.width;
                fillScaleY = adjustedHeight / b.height;
            }
            
            var offsetX:Number = 0;
            var offsetY:Number = 0;
            
            if (maintainAspectRatio)
            {
                var cHeight:Number = b.height * fillScaleX;
                var cWidth:Number = b.width * fillScaleY;
                
                if (unscaledHeight > cHeight)
                    roundedDrawY = roundedDrawY + Math.floor((unscaledHeight - cHeight) 
                        * getVerticalAlignValue());

                if (unscaledWidth > cWidth)
                    roundedDrawX = roundedDrawX + Math.floor((unscaledWidth - cWidth) 
                        * getHorizontalAlignValue());
            }
            
            matrix.identity();
            if (!(sampledScale && maintainAspectRatio)) 
                matrix.scale(fillScaleX, fillScaleY);
            matrix.translate(roundedDrawX, roundedDrawY);  
            g.beginBitmapFill(b, matrix, repeatBitmap, _smooth);
            g.drawRect(roundedDrawX, roundedDrawY, fillWidth, fillHeight);
            g.endFill();
        }
        else
        {   
            // If we have scaleGrid, we draw 9 sections, each with a different scale factor based 
            // on the grid region.
            
