protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
        {
            if (Gui2UtilConnector.Instance.IsBusy || BufferingPlayerFactory != null) return; // wait for any background action e.g. dynamic category discovery to finish
            if (control == GUI_facadeView)
            {
                if (actionType == Action.ActionType.ACTION_SELECT_ITEM)
                {
                    currentFilter.Clear();
                    GUIPropertyManager.SetProperty("#OnlineVideos.filter", string.Empty);
                    switch (CurrentState)
                    {
                        case State.groups:
                            selectedSitesGroup = GUI_facadeView.SelectedListItem as OnlineVideosGuiListItem;
                            if (selectedSitesGroup.Item is SitesGroup)
                                DisplaySites();
                            else
                            {
                                SelectedSite = selectedSitesGroup.Item as Sites.SiteUtilBase;
                                DisplayCategories(null, true);
                            }
                            break;
                        case State.sites:
                            if (GUI_facadeView.SelectedListItem.Label == "..")
                            {
                                ShowPreviousMenu();
                            }
                            else
                            {
                                SelectedSite = (GUI_facadeView.SelectedListItem as OnlineVideosGuiListItem).Item as Sites.SiteUtilBase;
                                DisplayCategories(null, true);
                            }
                            break;
                        case State.categories:
                            if (GUI_facadeView.SelectedListItem.Label == "..")
                            {
                                ShowPreviousMenu();
                            }
                            else
                            {
                                Category categoryToDisplay = (GUI_facadeView.SelectedListItem as OnlineVideosGuiListItem).Item as Category;
                                if (categoryToDisplay is NextPageCategory)
                                {
                                    DisplayCategories_NextPage(categoryToDisplay as NextPageCategory);
                                }
                                else if (categoryToDisplay is Sites.FavoriteUtil.FavoriteCategory)
                                {
                                    Gui2UtilConnector.Instance.ExecuteInBackgroundAndCallback(delegate()
                                    {
                                        var favCat = categoryToDisplay as Sites.FavoriteUtil.FavoriteCategory;
                                        if (favCat.SiteCategory == null) favCat.DiscoverSiteCategory();
                                        return favCat;
                                    },
                                    delegate(bool success, object result)
                                    {
                                        if (success)
                                        {
                                            var favCat = result as Sites.FavoriteUtil.FavoriteCategory;
                                            if (favCat != null && favCat.SiteCategory != null)
                                            {
                                                currentNavigationContextSwitch = new NavigationContextSwitch()
                                                {
                                                    ReturnToUtil = SelectedSite,
                                                    ReturnToCategory = categoryToDisplay.ParentCategory,
                                                    GoToUtil = favCat.Site,
                                                    GoToCategory = favCat.SiteCategory,
                                                    BridgeCategory = favCat
                                                };
                                                SelectedSite = currentNavigationContextSwitch.GoToUtil;
                                                if (currentNavigationContextSwitch.GoToCategory.HasSubCategories)
                                                    DisplayCategories(currentNavigationContextSwitch.GoToCategory, true);
                                                else
                                                    DisplayVideos_Category(currentNavigationContextSwitch.GoToCategory, false);
                                            }
                                            else
                                            {
                                                GUIDialogNotify dlg_error = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
                                                if (dlg_error != null)
                                                {
                                                    dlg_error.Reset();
                                                    dlg_error.SetImage(SiteImageExistenceCache.GetImageForSite("OnlineVideos", type: "Icon"));
                                                    dlg_error.SetHeading(PluginConfiguration.Instance.BasicHomeScreenName);
                                                    dlg_error.SetText(string.Format("{0}: {1}", Translation.Instance.Error, Translation.Instance.CategoryNotFound));
                                                    dlg_error.DoModal(GUIWindowManager.ActiveWindow);
                                                }
                                            }
                                        }
                                    },
                                    Translation.Instance.GettingDynamicCategories, true);
                                }
                                else if (categoryToDisplay.HasSubCategories)
                                {
                                    DisplayCategories(categoryToDisplay, true);
                                }
                                else
                                {
                                    DisplayVideos_Category(categoryToDisplay, false);
                                }
                            }
                            break;
                        case State.videos:
                            ImageDownloader.StopDownload = true;
                            if (GUI_facadeView.SelectedListItem.Label == "..")
                            {
                                ShowPreviousMenu();
                            }
                            else if (GUI_facadeView.SelectedListItem.Label == Translation.Instance.NextPage)
                            {
                                DisplayVideos_NextPage();
                            }
                            else
                            {
                                selectedVideo = (GUI_facadeView.SelectedListItem as OnlineVideosGuiListItem).Item as VideoInfo;
                                if (SelectedSite is IChoice && selectedVideo.HasDetails)
                                {
                                    // show details view
                                    DisplayDetails();
                                }
                                else if (SelectedSite is Sites.FavoriteUtil && selectedVideo.HasDetails &&
                                    (selectedCategory is Sites.FavoriteUtil.FavoriteCategory && (selectedCategory as Sites.FavoriteUtil.FavoriteCategory).Site is IChoice))
                                {
                                    SelectedSite = (selectedCategory as Sites.FavoriteUtil.FavoriteCategory).Site;
                                    // show details view
                                    DisplayDetails();
                                }
                                else
                                {
                                    //play the video
                                    currentPlaylist = null;
                                    currentPlayingItem = null;

                                    Play_Step1(new PlayListItem(null, null)
                                            {
                                                Type = MediaPortal.Playlists.PlayListItem.PlayListItemType.VideoStream,
                                                Video = selectedVideo,
                                                Util = selectedSite is Sites.FavoriteUtil ? OnlineVideoSettings.Instance.SiteUtilsList[(selectedVideo as FavoriteDbVideoInfo).SiteName] : selectedSite
                                            }, true);
                                }
                            }
                            break;
                    }
                }
                else if (CurrentState == State.videos && !(SelectedSite is IChoice && selectedVideo.HasDetails) &&
                    (actionType == Action.ActionType.ACTION_MUSIC_PLAY || actionType == Action.ActionType.ACTION_PLAY))
                {
                    VideoInfo videoPressedPlayOn = (GUI_facadeView.SelectedListItem as OnlineVideosGuiListItem).Item as VideoInfo;
                    if (videoPressedPlayOn != null)
                    {
                        ImageDownloader.StopDownload = true;

                        currentFilter.Clear();
                        GUIPropertyManager.SetProperty("#OnlineVideos.filter", string.Empty);

                        selectedVideo = videoPressedPlayOn;

                        //play the video
                        currentPlaylist = null;
                        currentPlayingItem = null;

                        Play_Step1(new PlayListItem(null, null)
                        {
                            Type = MediaPortal.Playlists.PlayListItem.PlayListItemType.VideoStream,
                            Video = selectedVideo,
                            Util = selectedSite is Sites.FavoriteUtil ? OnlineVideoSettings.Instance.SiteUtilsList[(selectedVideo as FavoriteDbVideoInfo).SiteName] : selectedSite
                        }, true, true);
                    }
                }
                else if (CurrentState == State.videos && actionType == Action.ActionType.ACTION_SHOW_INFO)
                {
                    // toggles showing detailed info about a selectected video - is automatically reset when leaving videos view
                    ExtendedVideoInfo = !ExtendedVideoInfo;
                }
            }
            else if (control == GUI_infoList && CurrentState == State.details &&
                (actionType == Action.ActionType.ACTION_SELECT_ITEM || actionType == Action.ActionType.ACTION_MUSIC_PLAY || actionType == Action.ActionType.ACTION_PLAY))
            {
                ImageDownloader.StopDownload = true;
                if (GUI_infoList.SelectedListItemIndex == 0)
                {
                    ShowPreviousMenu();
                }
                else
                {
                    selectedClipIndex = GUI_infoList.SelectedListItemIndex;
                    //play the video
                    currentPlaylist = null;
                    currentPlayingItem = null;
                    Play_Step1(new PlayListItem(null, null)
                    {
                        Type = MediaPortal.Playlists.PlayListItem.PlayListItemType.VideoStream,
                        Video = (GUI_infoList.SelectedListItem as OnlineVideosGuiListItem).Item as VideoInfo,
                        Util = selectedSite is Sites.FavoriteUtil ? OnlineVideoSettings.Instance.SiteUtilsList[(selectedVideo as FavoriteDbVideoInfo).SiteName] : selectedSite
                    }, true, actionType != Action.ActionType.ACTION_SELECT_ITEM);
                }
            }
            else if (control == GUI_btnViewAs)
            {
                ToggleFacadeViewMode();
                // store as preferred layout in DB
                if (SelectedSite != null && PluginConfiguration.Instance.StoreLayoutPerCategory && (currentState == State.categories || currentState == State.videos))
                {
                    FavoritesDatabase.Instance.SetPreferredLayout(SelectedSite.Settings.Name, selectedCategory, (int)currentView);
                }
            }
            else if (control == GUI_btnMaxResult)
            {
                GUIControl.SelectItemControl(GetID, GUI_btnMaxResult.GetID, GUI_btnMaxResult.SelectedItem);
            }
            else if (control == GUI_btnOrderBy)
            {
                GUIControl.SelectItemControl(GetID, GUI_btnOrderBy.GetID, GUI_btnOrderBy.SelectedItem);
                if (CurrentState == State.sites) PluginConfiguration.Instance.siteOrder = (PluginConfiguration.SiteOrder)GUI_btnOrderBy.SelectedItem;
            }
            else if (control == GUI_btnTimeFrame)
            {
                GUIControl.SelectItemControl(GetID, GUI_btnTimeFrame.GetID, GUI_btnTimeFrame.SelectedItem);
            }
            else if (control == GUI_btnUpdate)
            {
                GUIControl.UnfocusControl(GetID, GUI_btnUpdate.GetID);
                switch (CurrentState)
                {
                    case State.sites: DisplaySites(); break;
                    case State.videos: DisplayVideos_Filter(); break;
                }
            }
            else if (control == GUI_btnSearchCategories)
            {
                GUIControl.SelectItemControl(GetID, GUI_btnSearchCategories.GetID, GUI_btnSearchCategories.SelectedItem);
            }
            else if (control == GUI_btnSearch)
            {
                Display_SearchResults();
            }
            else if (control == GUI_btnEnterPin)
            {
                string pin = String.Empty;
                if (GetUserInputString(ref pin, true))
                {
                    if (pin == PluginConfiguration.Instance.pinAgeConfirmation)
                    {
                        OnlineVideoSettings.Instance.AgeConfirmed = true;
                        GUIControl.UnfocusControl(GetID, GUI_btnEnterPin.GetID);
                        if (CurrentState == State.groups) DisplayGroups();
                        else DisplaySites();
                    }
                }
            }
            else if (control == GUI_btnCurrentDownloads)
            {
                // go to current downloads
                Sites.SiteUtilBase aSite = null;
                if (OnlineVideoSettings.Instance.SiteUtilsList.TryGetValue(Translation.Instance.DownloadedVideos, out aSite))
                {
                    Gui2UtilConnector.Instance.ExecuteInBackgroundAndCallback(delegate()
                    {
                        if (!aSite.Settings.DynamicCategoriesDiscovered)
                        {
                            Log.Instance.Info("Looking for dynamic categories on Site '{0}'", aSite.Settings.Name);
                            int foundCategories = aSite.DiscoverDynamicCategories();
                            Log.Instance.Info("Found {0} dynamic categories on Site '{1}'", foundCategories, aSite.Settings.Name);
                        }
                        return aSite.Settings.Categories;
                    },
                    delegate(bool success, object result)
                    {
                        if (success)
                        {
                            Category aCategory = aSite.Settings.Categories.FirstOrDefault(c => c.Name == Translation.Instance.Downloading);
                            if (aCategory != null)
                            {
                                SelectedSite = aSite;
                                selectedCategory = aCategory;
                                DisplayVideos_Category(aCategory, true);
                            }
                        }
                    },
                    Translation.Instance.GettingDynamicCategories, true);
                }
            }
            base.OnClicked(controlId, control, actionType);
        }
        void GUIWindowManager_OnThreadMessageHandler(object sender, GUIMessage message)
        {
            if (message.Message == GUIMessage.MessageType.GUI_MSG_GOTO_WINDOW &&
                message.TargetWindowId == 0 && message.TargetControlId == 0 && message.SenderControlId == 0 &&
                message.SendToTargetWindow == false && message.Object == null && message.Object2 == null &&
                message.Param2 == 0 && message.Param3 == 0 && message.Param4 == 0 &&
                (message.Param1 == (int)GUIWindow.Window.WINDOW_HOME || message.Param1 == (int)GUIWindow.Window.WINDOW_SECOND_HOME)
                )
            {
                if (CurrentState != State.groups && GroupsEnabled)
                {
                    // prevent message from beeing sent to MP core
                    message.SendToTargetWindow = true;
                    message.TargetWindowId = GetID;
                    message.Param1 = GetID;
                    message.Message = GUIMessage.MessageType.GUI_MSG_HIDE_MESSAGE;

                    // reset to groups view
                    SelectedSite = null;
                    selectedCategory = null;
                    selectedVideo = null;
                    currentVideoList = new List<VideoInfo>();
                    currentTrailerList = new List<DetailVideoInfo>();
                    currentNavigationContextSwitch = null;
                    loadParamInfo = null;
                    DisplayGroups();
                }
                else if (CurrentState != State.sites && !GroupsEnabled)
                {
                    // prevent message from beeing sent to MP core
                    message.SendToTargetWindow = true;
                    message.TargetWindowId = GetID;
                    message.Param1 = GetID;
                    message.Message = GUIMessage.MessageType.GUI_MSG_HIDE_MESSAGE;

                    // reset to sites view
                    selectedCategory = null;
                    selectedVideo = null;
                    currentVideoList = new List<VideoInfo>();
                    currentTrailerList = new List<DetailVideoInfo>();
                    currentNavigationContextSwitch = null;
                    loadParamInfo = null;
                    DisplaySites();
                }
            }
        }
        private void ShowPreviousMenu()
        {
            ImageDownloader.StopDownload = true;

            if (CurrentState == State.sites)
            {
                if (GroupsEnabled)
                {
                    // if plugin was called with loadParameter set to the current group and return locked -> go to previous window 
                    if (loadParamInfo != null && loadParamInfo.Return == LoadParameterInfo.ReturnMode.Locked && loadParamInfo.Group == selectedSitesGroup.Label)
                        OnPreviousWindow();
                    else
                        DisplayGroups();
                }
                else
                    OnPreviousWindow();
            }
            else if (CurrentState == State.categories)
            {
                if (selectedCategory == null)
                {
                    // if plugin was called with loadParameter set to the current site and return locked -> go to previous window 
                    if (loadParamInfo != null && loadParamInfo.Return == LoadParameterInfo.ReturnMode.Locked && loadParamInfo.Site == selectedSite.Settings.Name)
                        OnPreviousWindow();
                    else
                        DisplaySites();
                }
                else
                {
                    // if plugin was called with loadParameter set to the current site and return locked and currently displaying subcategories of category from loadParam -> go to previous window 
                    if (loadParamInfo != null && loadParamInfo.Return == LoadParameterInfo.ReturnMode.Locked && loadParamInfo.Site == selectedSite.Settings.Name && loadParamInfo.Category == selectedCategory.Name)
                        OnPreviousWindow();
                    else
                    {
                        if (currentNavigationContextSwitch != null && currentNavigationContextSwitch.GoToCategory == selectedCategory)
                        {
                            SelectedSite = currentNavigationContextSwitch.ReturnToUtil;
                            selectedCategory = currentNavigationContextSwitch.BridgeCategory;
                            var categoryToReturnTo = currentNavigationContextSwitch.ReturnToCategory;
                            currentNavigationContextSwitch = null;
                            DisplayCategories(categoryToReturnTo, false);
                        }
                        else
                        {
                            DisplayCategories(selectedCategory.ParentCategory, false);
                        }
                    }
                }
            }
            else if (CurrentState == State.videos)
            {
                videosVKfilter = string.Empty;
                // if plugin was called with loadParameter set to the current site with searchstring and return locked and currently displaying the searchresults or videos for the category from loadParam -> go to previous window 
                if (loadParamInfo != null && loadParamInfo.Return == LoadParameterInfo.ReturnMode.Locked && loadParamInfo.Site == selectedSite.Settings.Name &&
                    (currentVideosDisplayMode == VideosMode.Search ||
                    (currentVideosDisplayMode == VideosMode.Category && selectedCategory != null && loadParamInfo.Category == selectedCategory.Name))
                   )
                    OnPreviousWindow();
                else
                {
                    if (currentNavigationContextSwitch != null && currentNavigationContextSwitch.GoToCategory == selectedCategory)
                    {
                        SelectedSite = currentNavigationContextSwitch.ReturnToUtil;
                        selectedCategory = currentNavigationContextSwitch.BridgeCategory;
                        var categoryToReturnTo = currentNavigationContextSwitch.ReturnToCategory;
                        currentNavigationContextSwitch = null;
                        DisplayCategories(categoryToReturnTo, false);
                    }
                    else
                    {
                        if (selectedCategory == null || selectedCategory.ParentCategory == null) DisplayCategories(null, false);
                        else DisplayCategories(selectedCategory.ParentCategory, false);
                    }
                }
            }
            else if (CurrentState == State.details)
            {
                if (selectedCategory is Sites.FavoriteUtil.FavoriteCategory && !(SelectedSite is Sites.FavoriteUtil))
                {
                    SelectedSite = (selectedCategory as Sites.FavoriteUtil.FavoriteCategory).FavSite;
                }
                GUIControl.UnfocusControl(GetID, GUI_infoList.GetID);
                GUI_infoList.Focus = false;
                selectedClipIndex = 0;
                SetVideosToFacade(currentVideoList, currentVideosDisplayMode);
            }
        }
 public void ResetToFirstView()
 {
     selectedSitesGroup = null;
     SelectedSite = null;
     selectedCategory = null;
     selectedVideo = null;
     currentVideoList = new List<VideoInfo>();
     currentTrailerList = new List<DetailVideoInfo>();
     currentNavigationContextSwitch = null;
     currentPlaylist = null;
     currentPlayingItem = null;
     CurrentState = State.groups;
 }
        private void DisplayVideos_Category(Category category, bool displayCategoriesOnError)
        {
            Gui2UtilConnector.Instance.ExecuteInBackgroundAndCallback(delegate()
            {
                return SelectedSite.GetVideos(category);
            },
            delegate(bool success, object result)
            {
                Category categoryToRestoreOnError = selectedCategory;
                selectedCategory = category;
                if (!success || !SetVideosToFacade(result as List<VideoInfo>, VideosMode.Category))
                {
                    selectedCategory = categoryToRestoreOnError;

                    // reset a navigation context switch if it was set for this operation
                    if (currentNavigationContextSwitch != null && currentNavigationContextSwitch.GoToCategory == category)
                    {
                        SelectedSite = currentNavigationContextSwitch.ReturnToUtil;
                        selectedCategory = currentNavigationContextSwitch.ReturnToCategory;
                        currentNavigationContextSwitch = null;
                    }

                    if (displayCategoriesOnError)// an error occured or no videos were found -> return to the category selection if param was set
                    {
                        DisplayCategories(category.ParentCategory, false);
                    }
                }
            },
            Translation.Instance.GettingCategoryVideos, true);
        }