 import com.esri.ags.layers.WebTiledLayer;
 import com.esri.ags.layers.supportClasses.TileInfo;
 import com.esri.ags.portal.WebMapUtil;
+import com.esri.ags.portal.supportClasses.PortalItem;
 import com.esri.builder.eventbus.AppEvent;
 import com.esri.builder.model.ConfigLayer;
 import com.esri.builder.model.ConfigMap;
 import com.esri.builder.model.Model;
+import com.esri.builder.model.PortalLayer;
 import com.esri.builder.model.PortalModel;
+import com.esri.builder.supportClasses.IconFetchEvent;
 import com.esri.builder.supportClasses.LogUtil;
+import com.esri.builder.supportClasses.PortalLayerIconFetcher;
 import com.esri.builder.views.popups.NoticePopUp;
 
 import flash.display.DisplayObject;

import mx.logging.ILogger;
import mx.managers.PopUpManager;
import mx.resources.ResourceManager;
import mx.utils.ObjectUtil;

public final class SwitchingMapsController

            }
        }

        var basemapLayerItemId:String;

        if (event.itemData.baseMap)
        {
            var basemapLayersObjects:Array = event.itemData.baseMap.baseMapLayers;
            for each (var basemapLayerObject:Object in basemapLayersObjects)
            {
                if (basemapLayerObject.itemId && !basemapLayerObject.isReference)
                {
                    basemapLayerItemId = basemapLayerObject.itemId;
                    break;
                }
            }
        }

        if (basemapLayerItemId)
        {
            PortalModel.getInstance().portal.getItem(basemapLayerItemId, new Responder(getItem_resultHandler, getItem_faultHandler));

            function getItem_resultHandler(item:PortalItem):void
            {
                LOG.debug("fetched basemap item: {0}", basemapLayerItemId);
                var iconFetcher:PortalLayerIconFetcher = new PortalLayerIconFetcher();
                iconFetcher.addEventListener(IconFetchEvent.FETCH_COMPLETE, iconFetcher_fetchCompleteHandler);
                iconFetcher.fetchIconURL(new PortalLayer(item));

                function iconFetcher_fetchCompleteHandler(e:IconFetchEvent):void
                {
                    iconFetcher.removeEventListener(IconFetchEvent.FETCH_COMPLETE, iconFetcher_fetchCompleteHandler);

                    if (baseLayers.children().length() > 0 && e.iconPath)
                    {
                        //set the icon on the 1st basemap
                        baseLayers.child(0).@icon = e.iconPath;
                    }

                    carryOn();
                }
            }

            function getItem_faultHandler(fault:Fault):void
            {
                LOG.debug("could not fetch basemap item: {0}", fault.toString());
                carryOn();
            }
        }
        else
        {
            carryOn();
        }

        function carryOn():void
        {
            mapConfigXML.appendChild(baseLayers);

            if (opLayers.hasComplexContent())
            {
                mapConfigXML.appendChild(opLayers);
            }

            if (unsupportedLayers.length)
            {
                noticePopUp.isWaiting = false;
                showLayerNotAddedPopUp(unsupportedLayers);
            }
            else
            {
                saveNewMapConfig();
            }
        }
    }

