private IList <CycloMediaLayer> GetCurrentLayers()
        {
            IList <CycloMediaLayer> result = new List <CycloMediaLayer>();
            IMap map = ArcUtils.Map;

            if (map != null)
            {
                // ReSharper disable UseIndexedProperty
                var    layers = map.get_Layers();
                ILayer layer;

                while ((layer = layers.Next()) != null)
                {
                    CycloMediaLayer thisLayer = AllLayers.Aggregate <CycloMediaLayer, CycloMediaLayer>
                                                    (null, (current, checkLayer) => (checkLayer.Name == layer.Name) ? checkLayer : current);

                    if (thisLayer != null)
                    {
                        result.Add(thisLayer);
                    }
                }

                // ReSharper restore UseIndexedProperty
            }

            return(result);
        }
        // =========================================================================
        // Event handlers
        // =========================================================================
        private void OnContentChanged()
        {
            foreach (var layer in AllLayers)
            {
                if (!Layers.Aggregate(false, (current, visLayer) => current || (visLayer == layer)))
                {
                    layer.IsVisible = true;
                    layer.Visible   = false;
                }
            }

            CycloMediaLayer changedLayer = Layers.Aggregate <CycloMediaLayer, CycloMediaLayer>
                                               (null, (current, layer) => (layer.IsVisible && (!layer.Visible)) ? layer : current);
            CycloMediaLayer refreshLayer = null;

            foreach (var layer in Layers)
            {
                bool visible = ((changedLayer == null) || (layer == changedLayer)) && layer.IsVisible;
                refreshLayer    = (layer.IsVisible != visible) ? layer : refreshLayer;
                layer.IsVisible = visible;
                layer.Visible   = layer.IsVisible;
            }

            if (refreshLayer != null)
            {
                IMxDocument document   = ArcUtils.MxDocument;
                IActiveView activeView = ArcUtils.ActiveView;

                if (document != null)
                {
                    // ReSharper disable UseIndexedProperty
                    IContentsView contentView = document.get_ContentsView(0);
                    // ReSharper restore UseIndexedProperty

                    if (contentView != null)
                    {
                        contentView.Refresh(_groupLayer);
                    }
                }

                if (activeView != null)
                {
                    activeView.Refresh();
                }
            }
        }
        public CycloMediaLayer AddLayer(string name)
        {
            CycloMediaLayer thisLayer = null;

            if (!Layers.Aggregate(false, (current, cycloLayer) => (cycloLayer.Name == name) || current))
            {
                thisLayer = AllLayers.Aggregate <CycloMediaLayer, CycloMediaLayer>
                                (null, (current, checkLayer) => (checkLayer.Name == name) ? checkLayer : current);

                if (thisLayer != null)
                {
                    thisLayer.AddToLayers();
                }
            }

            return(thisLayer);
        }
        public void RemoveLayer(string name)
        {
            CycloMediaLayer layer = Layers.Aggregate <CycloMediaLayer, CycloMediaLayer>
                                        (null, (current, checkLayer) => (checkLayer.Name == name) ? checkLayer : current);

            if (layer != null)
            {
                Layers.Remove(layer);
                layer.Dispose();
                IActiveView activeView = ArcUtils.ActiveView;

                if (activeView != null)
                {
                    activeView.ContentsChanged();
                    activeView.Refresh();
                }
            }
        }
        public string GetFeatureFromPoint(int x, int y, out CycloMediaLayer layer)
        {
            string result = string.Empty;

            layer = null;

            foreach (var layert in Layers)
            {
                if (string.IsNullOrEmpty(result))
                {
                    if ((layert.IsVisible) && (!(layert is WfsLayer)))
                    {
                        layer  = layert;
                        result = layer.GetFeatureFromPoint(x, y);
                    }
                }
            }

            return(result);
        }
        public void Dispose()
        {
            int i = 0;

            while (Layers.Count > i)
            {
                CycloMediaLayer layer = Layers[i];
                layer.Dispose();

                if (layer is WfsLayer)
                {
                    Layers.Remove(layer);
                }
                else
                {
                    i++;
                }
            }

            IActiveView activeView = ArcUtils.ActiveView;
            IMap        map        = ArcUtils.Map;

            if ((activeView != null) && (map != null))
            {
                var layer = GroupLayer as ILayer;

                if (layer != null)
                {
                    map.DeleteLayer(layer);
                }

                activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }

            var avEvents = ArcUtils.ActiveViewEvents;

            if (avEvents != null)
            {
                avEvents.ContentsChanged -= OnContentChanged;
            }
        }