private void Finish_writing_html(SobekCM_Item CurrentItem, Page_TreeNode CurrentPage, string Filename, string TextFileLocation )
        {
            bool textSearchable = CurrentItem.Behaviors.Text_Searchable;
            CurrentItem.Behaviors.Text_Searchable = false;
            if (staticSobekcmLocation.Length > 0)
                SobekCM_Library_Settings.Base_Directory = staticSobekcmLocation;

            // Get the skin
            if ((CurrentItem.Behaviors.Web_Skin_Count > 0) && ( !CurrentItem.Behaviors.Web_Skins.Contains( defaultSkin.ToUpper())))
                currentMode.Skin = CurrentItem.Behaviors.Web_Skins[0];

            // Get the skin object
            SobekCM_Skin_Object skinObject = skinsCollection[currentMode.Skin];
            if (skinObject == null)
            {
                skinObject = assistant.Get_HTML_Skin(currentMode, skinsCollection, false, null);
                skinsCollection.Add(skinObject);
            }

            // Create the HTML writer
            Item_HtmlSubwriter itemWriter = new Item_HtmlSubwriter(CurrentItem, CurrentPage, null, codeManager, translations, true, true, currentMode, null, String.Empty, null, tracer) { Mode = currentMode, Skin = skinObject };
            SobekCM_Library_Settings.Base_SobekCM_Location_Relative = currentMode.Base_URL;
            if ((SobekCM_Library_Settings.Base_SobekCM_Location_Relative.Length == 0) || (SobekCM_Library_Settings.Base_SobekCM_Location_Relative.Contains("localhost")))
            {
                SobekCM_Library_Settings.Base_SobekCM_Location_Relative = primaryWebServerUrl;
                currentMode.Base_URL = SobekCM_Library_Settings.Base_SobekCM_Location_Relative;
            }

            // Now that the item viewer is built, set the robot flag to suppress some checks
            currentMode.Is_Robot = true;

            // Create the TextWriter
            StreamWriter writer = new StreamWriter(Filename, false, Encoding.UTF8);

            writer.WriteLine("<!DOCTYPE html>");
            writer.WriteLine("<html>");
            writer.WriteLine("<head>");
            writer.WriteLine("  <title>" + CurrentItem.Bib_Info.Main_Title + "</title>");
            writer.WriteLine();
            writer.WriteLine("  <!-- " + SobekCM_Library_Settings.System_Name + " : SobekCM Digital Repository -->");
            writer.WriteLine();
            writer.WriteLine("  <link href=\"" + SobekCM_Library_Settings.System_Base_URL + "default/SobekCM.min.css\" rel=\"stylesheet\" type=\"text/css\" />");
            writer.WriteLine("  <script type=\"text/javascript\" src=\"" + SobekCM_Library_Settings.System_Base_URL + "default/scripts/jquery/jquery-1.10.2.min.js\"></script>");
            writer.WriteLine("  <script type=\"text/javascript\" src=\"" + SobekCM_Library_Settings.System_Base_URL + "default/scripts/sobekcm_full.min.js\"></script>");
            writer.WriteLine("  <link href=\"" + SobekCM_Library_Settings.System_Base_URL + "default/SobekCM_Item.min.css\" rel=\"stylesheet\" type=\"text/css\" />");

            writer.WriteLine("  <meta name=\"robots\" content=\"index, follow\" />");
            if (skinObject.CSS_Style.Length > 0)
            {
                writer.WriteLine("  <link href=\"" + SobekCM_Library_Settings.System_Base_URL + skinObject.CSS_Style + "\" rel=\"stylesheet\" type=\"text/css\" />");
            }

            string image_src = currentMode.Base_URL + "/" + CurrentItem.Web.AssocFilePath + "/" + CurrentItem.Behaviors.Main_Thumbnail;
            writer.WriteLine("  <link rel=\"image_src\" href=\"" + image_src.Replace("\\", "/").Replace("//", "/").Replace("http:/", "http://") + "\" />");

            writer.WriteLine("</head>");
            writer.WriteLine("<body>");

            // Is this item DARK or PRIVATE
            if ((CurrentItem.Behaviors.Dark_Flag) || ( CurrentItem.Behaviors.IP_Restriction_Membership < 0 ))
            {
                writer.WriteLine("THIS ITEM IS CURRENTLY DARK OR PRIVATE");
                writer.WriteLine("</body>");
                writer.WriteLine("</html>");
                writer.Flush();
                writer.Close();
                return;
            }

            // Add the header
            Display_Header(writer, itemWriter.Skin, CurrentItem);

            // Begin to write the item view
            itemWriter.Write_HTML(writer, tracer);

            if ((CurrentItem.Behaviors.Wordmark_Count > 0) || ((CurrentItem.Web.Static_PageCount > 1) && (CurrentItem.Web.Static_Division_Count > 1)))
            {
                writer.WriteLine("<nav id=\"sbkIsw_Leftnavbar\" style=\"padding-top:3px\">");

                // Write the table of contents as static HTML, rather than the TreeView web control
                if ((CurrentItem.Web.Static_PageCount > 1) && (CurrentItem.Web.Static_Division_Count > 1))
                {
                    writer.WriteLine("  <div class=\"sbkIsw_ShowTocRow\">");
                    writer.WriteLine("    <div class=\"sbkIsw_UpToc\">HIDE TABLE OF CONTENTS</div>");
                    writer.WriteLine("  </div>");

                    writer.WriteLine("<div class=\"sbkIsw_TocTreeView\">");

                    // load the table of contents in the tree
                    TreeView treeView1 = new TreeView();
                    itemWriter.Create_TreeView_From_Divisions(treeView1);

                    // Step through all the parent nodes
                    writer.WriteLine("  <table cellspacing=\"4px\">");
                    foreach (TreeNode thisNode in treeView1.Nodes)
                    {
                        writer.WriteLine("    <tr><td width=\"9px\">&nbsp;</td><td>" + thisNode.Text.Replace("sbkIsw_SelectedTocTreeViewItem", "sbkIsw_TocTreeViewItem") + "</td></tr>");
                    }
                    writer.WriteLine("  </table>");
                    writer.WriteLine("</div>");
                }

            }

            itemWriter.Write_Additional_HTML(writer, tracer);

            //Literal citationLiteral = (Literal)placeHolder.Controls[0];
            //writer.WriteLine(citationLiteral.Text);
            //placeHolder.Controls.Clear();

            writer.WriteLine("<!-- COMMENT HERE -->");

            // Close out this tables and form
            writer.WriteLine("       </tr>");

            // If this is IP restricted, show nothing else
            if (CurrentItem.Behaviors.IP_Restriction_Membership == 0)
            {
                // Add the download list if there are some
                if (CurrentItem.Divisions.Download_Tree.Has_Files)
                {
                    writer.WriteLine("       <tr>");
                    // Create the downloads viewer to ouput the html
                    Download_ItemViewer downloadViewer = new Download_ItemViewer {CurrentItem = CurrentItem, CurrentMode = currentMode};

                    // Add the HTML for this now
                    downloadViewer.Write_Main_Viewer_Section(writer, tracer);
                    writer.WriteLine("       </tr>");
                }

                // If there is a table of contents write it again, this time it will be complete
                // and also show a hierarchy if there is one
                if ((CurrentItem.Web.Static_PageCount > 1) && (CurrentItem.Web.Static_Division_Count > 1))
                {
                    writer.WriteLine("       <tr>");
                    writer.WriteLine("         <td align=\"left\"><span class=\"SobekViewerTitle\">Table of Contents</span></td>");
                    writer.WriteLine("       </tr>");

                    writer.WriteLine("       <tr>");
                    writer.WriteLine("          <td>");
                    writer.WriteLine("            <div class=\"sbkCiv_Citation\">");

                    foreach (abstract_TreeNode treeNode in CurrentItem.Divisions.Physical_Tree.Roots)
                    {
                        recursively_write_toc(writer, treeNode, "&nbsp; &nbsp; ");
                    }

                    writer.WriteLine("            </div>");
                    writer.WriteLine("          </td>");
                    writer.WriteLine("       </tr>");
                }

                // Is the text file location included, in which case any full text should be appended to the end?
                if ((TextFileLocation.Length > 0) && (Directory.Exists(TextFileLocation)))
                {
                    // Get the list of all TXT files in this division
                    string[] text_files = Directory.GetFiles(TextFileLocation, "*.txt");
                    Dictionary<string, string> text_files_existing = new Dictionary<string, string>();
                    foreach (string thisTextFile in text_files)
                    {
                        string text_filename = (new FileInfo(thisTextFile)).Name.ToUpper();
                        text_files_existing[text_filename] = text_filename;
                    }

                    // Are there ANY text files?
                    if (text_files.Length > 0)
                    {
                        // If this has page images, check for related text files
                        List<string> text_files_included = new List<string>();
                        bool started = false;
                        if (CurrentItem.Divisions.Physical_Tree.Has_Files)
                        {
                            // Go through the first 100 text pages
                            List<abstract_TreeNode> pages = CurrentItem.Divisions.Physical_Tree.Pages_PreOrder;
                            int page_count = 0;
                            foreach (Page_TreeNode thisPage in pages)
                            {
                                // Keep track of the page count
                                page_count++;

                                // Look for files in this page
                                if (thisPage.Files.Count > 0)
                                {
                                    bool found_non_thumb_file = false;
                                    foreach (SobekCM_File_Info thisFile in thisPage.Files)
                                    {
                                        // Make sure this is not a thumb
                                        if (thisFile.System_Name.ToLower().IndexOf("thm.jpg") < 0)
                                        {
                                            found_non_thumb_file = true;
                                            string root = thisFile.File_Name_Sans_Extension;
                                            if (text_files_existing.ContainsKey(root.ToUpper() + ".TXT"))
                                            {
                                                string text_file = TextFileLocation + "\\" + thisFile.File_Name_Sans_Extension + ".txt";

                                                // SInce this is marked to be included, save this name
                                                text_files_included.Add(root.ToUpper() + ".TXT");

                                                // For size reasons, we only include the text from the first 100 pages
                                                if (page_count <= 100)
                                                {
                                                    if (!started)
                                                    {
                                                        writer.WriteLine("       <tr>");
                                                        writer.WriteLine("         <td align=\"left\"><span class=\"SobekViewerTitle\">Full Text</span></td>");
                                                        writer.WriteLine("       </tr>");
                                                        writer.WriteLine("       <tr>");
                                                        writer.WriteLine("          <td>");
                                                        writer.WriteLine("            <div class=\"sbkCiv_Citation\">");

                                                        started = true;
                                                    }

                                                    try
                                                    {
                                                        StreamReader reader = new StreamReader(text_file);
                                                        string text_line = reader.ReadLine();
                                                        while (text_line != null)
                                                        {
                                                            writer.WriteLine(text_line + "<br />");
                                                            text_line = reader.ReadLine();
                                                        }
                                                        reader.Close();
                                                    }
                                                    catch
                                                    {
                                                        writer.WriteLine("Unable to read file: " + text_file);
                                                    }

                                                    writer.WriteLine("<br /><br />");
                                                }
                                            }

                                        }

                                        // If a suitable file was found, break here
                                        if (found_non_thumb_file)
                                            break;
                                    }
                                }
                            }

                            // End this if it was ever started
                            if (started)
                            {
                                writer.WriteLine("            </div>");
                                writer.WriteLine("          </td>");
                                writer.WriteLine("       </tr>");
                            }
                        }

                        // Now, check for any other valid text files
                        List<string> additional_text_files = text_files_existing.Keys.Where(ThisTextFile => (!text_files_included.Contains(ThisTextFile.ToUpper())) && (ThisTextFile.ToUpper() != "AGREEMENT.TXT") && (ThisTextFile.ToUpper().IndexOf("REQUEST") != 0)).ToList();

                        // Now, include any additional text files, which would not be page text files, possiblye
                        // full text for included PDFs, Powerpoint, Word Doc, etc..
                        started = false;
                        foreach (string thisTextFile in additional_text_files)
                        {
                            if (!started)
                            {
                                writer.WriteLine("       <tr>");
                                writer.WriteLine("         <td align=\"left\"><span class=\"SobekViewerTitle\">Full Text</span></td>");
                                writer.WriteLine("       </tr>");
                                writer.WriteLine("       <tr>");
                                writer.WriteLine("          <td>");
                                writer.WriteLine("            <div class=\"sbkCiv_Citation\">");

                                started = true;
                            }

                            string text_file = TextFileLocation + "\\" + thisTextFile;

                            try
                            {

                                StreamReader reader = new StreamReader(text_file);
                                string text_line = reader.ReadLine();
                                while (text_line != null)
                                {
                                    writer.WriteLine(text_line + "<br />");
                                    text_line = reader.ReadLine();
                                }
                                reader.Close();
                            }
                            catch
                            {
                                writer.WriteLine("Unable to read file: " + text_file);
                            }

                            writer.WriteLine("<br /><br />");
                        }

                        // End this if it was ever started
                        if (started)
                        {
                            writer.WriteLine("            </div>");
                            writer.WriteLine("          </td>");
                            writer.WriteLine("       </tr>");
                        }
                    }
                }
            }

            writer.WriteLine("      </table>");
            writer.WriteLine("      </div>");

            // Write the footer
            Display_Footer(writer, itemWriter.Skin);

            writer.WriteLine("</body>");
            writer.WriteLine("</html>");

            writer.Flush();
            writer.Close();

            // Restore the text searchable flag and robot flag
            currentMode.Is_Robot = false;
            CurrentItem.Behaviors.Text_Searchable = textSearchable;
        }
        private void Finish_writing_html(SobekCM_Item currentItem, Page_TreeNode currentPage, string filename, string text_file_location )
        {
            string bibid = currentItem.BibID;
            currentItem.Behaviors.Text_Searchable = false;

            // Create the HTML writer
            Item_HtmlSubwriter itemWriter = new Item_HtmlSubwriter(currentItem, currentPage, null, codeManager, translations, true, true, currentMode, null, String.Empty, null, tracer)
                                                 {Mode = currentMode, Skin = ufdcInterface};
            SobekCM_Library_Settings.Base_SobekCM_Location_Relative = currentMode.Base_URL;
            if ((SobekCM_Library_Settings.Base_SobekCM_Location_Relative.Length == 0) || (SobekCM_Library_Settings.Base_SobekCM_Location_Relative.Contains("localhost")))
            {
                SobekCM_Library_Settings.Base_SobekCM_Location_Relative = primaryWebServerUrl;
                if (bibid.IndexOf("CA") == 0)
                {
                    currentMode.Skin = "dloc";
                    itemWriter.Skin = dlocInterface;
                    SobekCM_Library_Settings.Base_SobekCM_Location_Relative = "http://www.dloc.com/";
                }
                currentMode.Base_URL = SobekCM_Library_Settings.Base_SobekCM_Location_Relative;
            }

            // Now that the item viewer is built, set the robot flag to suppress some checks
            currentMode.Is_Robot = true;

            // Create the TextWriter
            StreamWriter writer = new StreamWriter(filename, false, Encoding.UTF8);

            // Add the header
            Display_Header(writer, itemWriter.Skin);

            // Begin to write the item view
            itemWriter.Write_HTML(writer, tracer);

            // Write the table of contents as static HTML, rather than the TreeView web control
            if ((currentItem.Web.Static_PageCount > 1) && (currentItem.Web.Static_Division_Count > 1))
            {
                writer.WriteLine("        <ul class=\"SobekNavBarMenu\">" + Environment.NewLine + "          <li class=\"SobekNavBarHeader\"> TABLE OF CONTENTS </li>" + Environment.NewLine + "        </ul>" + Environment.NewLine  +
                                 "        <div class=\"HideTocRow\">" + Environment.NewLine + "          <img src=\"" + SobekCM_Library_Settings.Base_SobekCM_Location_Relative + "design/skins/" + itemWriter.Skin.Skin_Code + "/tabs/cLG.gif\" border=\"0\" alt=\"\" /><img src=\"" + SobekCM_Library_Settings.Base_SobekCM_Location_Relative + "design/skins/" + itemWriter.Skin.Skin_Code + "/tabs/AU.gif\" border=\"0\" alt=\"\" /><span class=\"tab\">HIDE</span><img src=\"" + SobekCM_Library_Settings.Base_SobekCM_Location_Relative + "design/skins/" + itemWriter.Skin.Skin_Code + "/tabs/cRG.gif\" border=\"0\" alt=\"\" />" + Environment.NewLine + "        </div>");
                writer.WriteLine("<div class=\"SobekTocTreeView\">");

                // load the table of contents in the tree
                TreeView treeView1 = new TreeView();
                itemWriter.Create_TreeView_From_Divisions(treeView1);

                // Step through all the parent nodes
                writer.WriteLine("<table cellspacing=\"4px\" >");
                foreach (TreeNode thisNode in treeView1.Nodes)
                {
                    writer.WriteLine("  <tr><td width=\"9px\">&nbsp;</td><td>" + thisNode.Text.Replace("ufdcSelectedTocTreeViewItem", "ufdcTocTreeViewItem") + "</td></tr>");
                }
                writer.WriteLine("</table>");
                writer.WriteLine("</div>");
            }

            itemWriter.Write_Additional_HTML(writer, tracer);
            PlaceHolder placeHolder = new PlaceHolder();
            itemWriter.PageViewer.Add_Main_Viewer_Section(placeHolder, tracer);
            Literal citationLiteral = (Literal)placeHolder.Controls[0];
            writer.WriteLine(citationLiteral.Text);
            placeHolder.Controls.Clear();

            writer.WriteLine("<!-- COMMENT HERE -->");

            // Close out this tables and form
            writer.WriteLine("       </tr>");

            // Add the download list if there are some
            if ( currentItem.Divisions.Download_Tree.Has_Files )
            {
                writer.WriteLine("       <tr>");
                // Create the downloads viewer to ouput the html
                Download_ItemViewer downloadViewer = new Download_ItemViewer
                                                         {CurrentItem = currentItem, CurrentMode = currentMode};

                // Add the HTML for this now
                downloadViewer.Add_Main_Viewer_Section(placeHolder, tracer);
                Literal downloadLiteral = (Literal)placeHolder.Controls[0];
                writer.WriteLine(downloadLiteral.Text);
                writer.WriteLine("       </tr>");
            }

            // If there is a table of contents write it again, this time it will be complete
            // and also show a hierarchy if there is one
            if ((currentItem.Web.Static_PageCount > 1) && (currentItem.Web.Static_Division_Count > 1))
            {
                writer.WriteLine("       <tr>");
                writer.WriteLine("         <td align=\"left\"><span class=\"SobekViewerTitle\">Table of Contents</span></td>");
                writer.WriteLine("       </tr>");

                writer.WriteLine("       <tr>");
                writer.WriteLine("          <td>");
                writer.WriteLine("            <div class=\"SobekCitation\">");

                foreach (abstract_TreeNode treeNode in currentItem.Divisions.Physical_Tree.Roots)
                {
                    recursively_write_toc(writer, treeNode, "&nbsp; &nbsp; ");
                }

                writer.WriteLine("            </div>");
                writer.WriteLine("          </td>");
                writer.WriteLine("       </tr>");
            }

            // Is the text file location included, in which case any full text should be appended to the end?
            if ((text_file_location.Length > 0) && ( Directory.Exists(text_file_location)))
            {
                // Get the list of all TXT files in this division
                string[] text_files = Directory.GetFiles(text_file_location, "*.txt");
                Dictionary<string, string> text_files_existing = new Dictionary<string, string>();
                foreach (string thisTextFile in text_files)
                {
                    string text_filename = (new FileInfo(thisTextFile)).Name.ToUpper();
                    text_files_existing[text_filename] = text_filename;
                }

                // Are there ANY text files?
                if (text_files.Length > 0)
                {
                    // If this has page images, check for related text files
                    List<string> text_files_included = new List<string>();
                    bool started = false;
                    if (currentItem.Divisions.Physical_Tree.Has_Files)
                    {
                        // Go through the first 100 text pages
                        List<abstract_TreeNode> pages = currentItem.Divisions.Physical_Tree.Pages_PreOrder;
                        int page_count = 0;
                        foreach (Page_TreeNode thisPage in pages)
                        {
                            // Keep track of the page count
                            page_count++;

                            // Look for files in this page
                            if (thisPage.Files.Count > 0)
                            {
                                bool found_non_thumb_file = false;
                                foreach (SobekCM_File_Info thisFile in thisPage.Files)
                                {
                                    // Make sure this is not a thumb
                                    if (thisFile.System_Name.ToLower().IndexOf("thm.jpg") < 0)
                                    {
                                        found_non_thumb_file = true;
                                        string root = thisFile.File_Name_Sans_Extension;
                                        if (text_files_existing.ContainsKey(root.ToUpper() + ".TXT"))
                                        {
                                            string text_file = text_file_location + "\\" + thisFile.File_Name_Sans_Extension + ".txt";

                                            // SInce this is marked to be included, save this name
                                            text_files_included.Add(root.ToUpper() + ".TXT");

                                            // For size reasons, we only include the text from the first 100 pages
                                            if (page_count <= 100)
                                            {
                                                if (!started)
                                                {
                                                    writer.WriteLine("       <tr>");
                                                    writer.WriteLine("         <td align=\"left\"><span class=\"SobekViewerTitle\">Full Text</span></td>");
                                                    writer.WriteLine("       </tr>");
                                                    writer.WriteLine("       <tr>");
                                                    writer.WriteLine("          <td>");
                                                    writer.WriteLine("            <div class=\"SobekCitation\">");

                                                    started = true;
                                                }

                                                try
                                                {
                                                    StreamReader reader = new StreamReader(text_file);
                                                    string text_line = reader.ReadLine();
                                                    while (text_line != null)
                                                    {
                                                        writer.WriteLine(text_line + "<br />");
                                                        text_line = reader.ReadLine();
                                                    }
                                                    reader.Close();
                                                }
                                                catch
                                                {
                                                    writer.WriteLine("Unable to read file: " + text_file);
                                                }

                                                writer.WriteLine("<br /><br />");
                                            }
                                        }

                                    }

                                    // If a suitable file was found, break here
                                    if (found_non_thumb_file)
                                        break;
                                }
                            }
                        }

                        // End this if it was ever started
                        if (started)
                        {
                            writer.WriteLine("            </div>");
                            writer.WriteLine("          </td>");
                            writer.WriteLine("       </tr>");
                        }
                    }

                    // Now, check for any other valid text files
                    List<string> additional_text_files = text_files_existing.Keys.Where(thisTextFile => (!text_files_included.Contains(thisTextFile.ToUpper())) && (thisTextFile.ToUpper() != "AGREEMENT.TXT") && (thisTextFile.ToUpper().IndexOf("REQUEST") != 0)).ToList();

                    // Now, include any additional text files, which would not be page text files, possiblye
                    // full text for included PDFs, Powerpoint, Word Doc, etc..
                    foreach (string thisTextFile in additional_text_files)
                    {
                        if (!started)
                        {
                            writer.WriteLine("       <tr>");
                            writer.WriteLine("         <td align=\"left\"><span class=\"SobekViewerTitle\">Full Text</span></td>");
                            writer.WriteLine("       </tr>");
                            writer.WriteLine("       <tr>");
                            writer.WriteLine("          <td>");
                            writer.WriteLine("            <div class=\"SobekCitation\">");

                            started = true;
                        }

                        string text_file = text_file_location + "\\" + thisTextFile;

                        try
                        {

                            StreamReader reader = new StreamReader(text_file);
                            string text_line = reader.ReadLine();
                            while (text_line != null)
                            {
                                writer.WriteLine(text_line + "<br />");
                                text_line = reader.ReadLine();
                            }
                            reader.Close();
                        }
                        catch
                        {
                            writer.WriteLine("Unable to read file: " + text_file);
                        }

                        writer.WriteLine("<br /><br />");
                    }
                }
            }

            writer.WriteLine("      </table>");
            writer.WriteLine("    </td>");
            writer.WriteLine("  </tr>");
            writer.WriteLine("</table>");

            // Write the footer
            Display_Footer(writer, itemWriter.Skin);

            writer.Flush();
            writer.Close();

            // Restore the text searchable flag and robot flag
            currentMode.Is_Robot = false;
            currentItem.Behaviors.Text_Searchable = false;
        }