/*
         * public static DownloadWindow Register(WebClient client, String downloadLocation)
         * {
         *  DownloadWindow downloadWindow;
         *
         *  if (instances == null)
         *  {
         *      downloadWindow = new DownloadWindow(client, downloadLocation);
         *      instances = new List<DownloadWindow>();
         *      instances.Add(downloadWindow);
         *      return downloadWindow;
         *  }
         *  else if ((downloadWindow = instances.Find(c => c.client == client && c.downloadLocation == downloadLocation)) != null)
         *      return downloadWindow;
         *  else
         *  {
         *      downloadWindow = new DownloadWindow(client, downloadLocation);
         *      instances.Add(downloadWindow);
         *      return downloadWindow;
         *  }
         * }
         */

        public void Download(/*TaggedWebClient client, */ FileExplorerWindow.LinkInfo linkInfo, CancellationTokenSource cts, bool open)
        {
            string path = System.Web.HttpUtility.UrlDecode((Path.Combine(downloadLocation, linkInfo.Url.Substring(1).Replace("/", "\\"))));

            Directory.CreateDirectory(Path.GetDirectoryName(path));

            if (linkInfo.Size != null && linkInfo.Size.Equals("Directory"))
            {
                List <FileExplorerWindow.LinkInfo> directoryFiles = FileExplorerWindow.GetDirectoryFiles((new FileExplorerWindow.HttpInfo(httpClient, linkInfo, user, cts)));

                foreach (FileExplorerWindow.LinkInfo newLinkInfo in directoryFiles)
                {
                    Download(/*FileExplorerWindow.CreateClient(linkInfo.BaseAddress, user), */ newLinkInfo, cts, false);
                }
            }
            else
            {
                TaggedWebClient client = FileExplorerWindow.CreateClient(linkInfo.BaseInfo.BaseAddress, user);

                clients.Add(client);
                FileDownloadForm Form2 = new FileDownloadForm(client, linkInfo);
                //client.Tag = Form2;
                client.Tag      = path;
                Form2.MdiParent = this;

                flowLayoutPanel1.Controls.Add(Form2);
                Form2.Show();

                client.DownloadFileCompleted += Client_DownloadFileCompleted;
                client.DownloadFileAsync(new Uri(linkInfo.FullAddress), path, open);

                downloadCount++;
                this.Show();
            }
        }