internal static bool GetEpisodeFromTvDb(string seriesName, string seriesID, TVDBLib tvdb, int seasonNumber,
                                               int episodeNumber, out ITVDBEpisode episode)
        {
            try
            {
                Debugger.LogMessageToFile("Getting information for Season " + seasonNumber + ", Episode " +
                                          Convert.ToString(episodeNumber) + " of " + seriesName + "...");
                
                MainImportingEngine.ThisProgress.Progress(MainImportingEngine.CurrentProgress,
                                                          "Getting information for Season " + seasonNumber + ", Episode " +
                                                          Convert.ToString(episodeNumber) + " of " + seriesName + "...");
            
                episode = tvdb.GetEpisode(seriesID, seasonNumber, episodeNumber, TVDBLib.EpisodeOrderEnum.Default);
           
            }
            catch
            {
                try
                {
                    MainImportingEngine.ThisProgress.Progress(MainImportingEngine.CurrentProgress,
                                                              "TVdb did not respond. Retrying...");

                    episode = tvdb.GetEpisode(seriesID, seasonNumber,
                                              episodeNumber, TVDBLib.EpisodeOrderEnum.Default);
             
                }
                catch (Exception e)
                {

                    Debugger.LogMessageToFile("Unable to connect to TVdb. Cannot download Episode information.");
                    
                    StatusForm.statusForm.TrayIcon.ShowBalloonTip(2000, "Unable to connect to TVdb",
                                                                  "MediaFairy was unable to contact TVdb database in order to download episode information. " +
                                                                  "Please check your internet connection availability, " +
                                                                  "otherwise the online database may be temporarily unavailable.",
                                                                  ToolTipIcon.Info);

                    Thread.Sleep(2000);

                    episode = null;
                    
                    return true;
                
                }

            }

            return false;
        
        }
        internal static void PopulateEpisodeMetadata(IMLItem item, bool fileServerIsOnline, bool isUNC, string mdfSettingsb,
                                                    IBaseSystem iBaseSystem, int seasonNumber, WebClient client,
                                                    ITVDBSeries series, ITVDBEpisode episode)
        {
            if (episode == null)
                return;

            if (String.IsNullOrEmpty(episode.ID.Trim()) ||
                episode.ID == null) 
                return;

            Debugger.LogMessageToFile("Found episode named " +
                                      episode.EpisodeName + "with ID " +
                                      episode.ID + ".");

            Debugger.LogMessageToFile("Writing series tags...");

            item.Tags["SeriesName"] = series.SeriesName;
            item.Tags["SeriesIMDbID"] = series.IMDb_ID;
            item.Tags["SeriesID"] = series.ID;


            GetEpisodeDetailsAndImages(item, fileServerIsOnline, isUNC,
                                                                             mdfSettingsb, iBaseSystem, client,
                                                                             seasonNumber, episode, series);


        }
        internal static bool GetSeriesAndEpisodeFromTVDb(string seriesName, string seriesID, TVDBLib tvdb, int episodeNumber,
                                                        int seasonNumber, out ITVDBEpisode episode, out ITVDBSeries series)
        {
            if (GetSeriesFromTVDb(seriesName, seriesID, tvdb, out series))
            {
                episode = null;
                return true;
            }

            return GetEpisodeFromTvDb(seriesName, seriesID, tvdb,
                                      seasonNumber, episodeNumber, out episode);

        }
        internal static void GetEpisodeThumbnail(IMLItem item, bool fileServerIsOnline, bool isUNC, string mdfSettingsb,
                                                 IBaseSystem iBaseSystem, ITVDBEpisode episode, WebClient client)
        {

            string imageFile = TVShows.CreateEpisodeThumbsFilePath
                (item, mdfSettingsb, ".jpg", iBaseSystem);


            string imagelocation = imageFile;

            if (File.Exists(imagelocation))
            {
                item.ImageFile = imagelocation;
                item.SaveTags();
                return;
            }

            if (!Settings.WantEpisodeThumbnails)
                return;

            if (String.IsNullOrEmpty(episode.ImageFile))
                return;

            if (!fileServerIsOnline && isUNC)
                return;


            MainImportingEngine.ThisProgress.Progress(MainImportingEngine.CurrentProgress,
                                                      "Downloading episode thumbnail...");

            try
            {
                client.DownloadFile(episode.ImageFile, imagelocation);
                item.ImageFile = imagelocation;
                item.SaveTags();
            }
            catch
            {
                try
                {
                    client.DownloadFile(episode.ImageFile, imagelocation);
                    item.ImageFile = imagelocation;
                    item.SaveTags();
                }
                catch
                {
                }

            }

        }
        internal static void GetEpisodeDetailsAndImages(IMLItem item, bool fileServerIsOnline, bool isUNC, string mdfSettingsb,
                                                       IBaseSystem iBaseSystem, WebClient client, int seasonNumber,
                                                       ITVDBEpisode episode, ITVDBSeries series)
        {

            PopulateEpisodeDetails
                (item, episode, series);

            GetEpisodeThumbnail(item, fileServerIsOnline, isUNC, mdfSettingsb,
                                iBaseSystem, episode, client);

            GetSeasonPoster
                (item, mdfSettingsb, iBaseSystem, episode, seasonNumber);

        }
        internal static void GetSeasonPoster(IMLItem item, string mdfSettingsb, IBaseSystem iBaseSystem, ITVDBEpisode episode,
                                            int seasonNumber)
        {

            if (!Settings.WantSeasonImages) 
                return;

            string imageFile = TVShows.CreateSeasonPostersFilePath
                (item, mdfSettingsb, ".jpg", iBaseSystem);
            
            TvSeriesOperations.DownloadSeriesBanner
                (item, "SeasonImage", seasonNumber, episode.SeasonID, imageFile);
        
        }
        internal static void PopulateEpisodeDetails(IMLItem item, ITVDBEpisode episode, ITVDBSeries series)
        {

            Debugger.LogMessageToFile("Populating episode details...");

            item.Name = series.SeriesName + " " +
                        episode.SeasonNumber + "x" +
                        episode.EpisodeNumber + " - " +
                        episode.EpisodeName;


            item.Tags["SeasonID"] = episode.SeasonID;
            item.Tags["SeriesID"] = episode.SeriesID;
            item.Tags["TVDbID"] = episode.ID;

            item.Tags["EpisodeNumber"] = Convert.ToString(episode.EpisodeNumber);
            item.Tags["SeasonNumber"] = Convert.ToString(episode.SeasonNumber);
            item.Tags["EpisodeName"] = episode.EpisodeName;

            if (!Settings.WantEpisodeDetails)
                return;

            item.Tags["Director"] = episode.Director;
            item.Tags["Overview"] = episode.Overview;
            item.Tags["SeriesOverview"] = series.Overview;
            item.Tags["SeriesActors"] = series.Actors;
            item.Tags["Rating"] = episode.Rating;
            item.Tags["Writer"] = episode.Writer;
            item.Tags["GuestStars"] = episode.GuestStars;
            item.Tags["DVD_Chapter"] = episode.DVD_Chapter;
            item.Tags["DVD_Disc_ID"] = episode.DVD_Disc_ID;
            item.Tags["DVD_Episode_Number"] = episode.DVD_Episode_Number;


        }