private function getClosestPage($entry_id = 0, $direction)
 {
     //###   Get EE Super Global   ###
     $this->EE =& get_instance();
     require_once APPPATH . 'third_party/structure/mod.structure.php';
     $Structure = new Structure();
     //###   Structure Class   ###
     //###   Get Site ID for SQL   ###
     $site_id = $this->EE->config->item('site_id');
     $output = '';
     $this->EE->load->library('typography');
     $this->EE->typography->initialize();
     $site_pages = $this->get_site_pages();
     if ($site_pages === false) {
         return;
     }
     //###   Get Entry ID Parameter   ###
     $param = $this->EE->TMPL->fetch_param('entry_id');
     if (empty($param)) {
         $param = $entry_id;
     }
     $entryID = preg_replace("/[^0-9]/", '', $param);
     if (empty($entryID)) {
         $entryID = 0;
     }
     //###   Get Status Parameter   ###
     $param = $this->EE->TMPL->fetch_param('status');
     $param = $this->EE->db->escape_str($param);
     $restrictStatus = $this->EE->security->xss_clean($param);
     //###   Convert html entities?   ###
     $convertHTML = false;
     if ($param = $this->EE->TMPL->fetch_param('convert_html')) {
         if (strtolower($param) === "true" || strtolower($param) === "t" || strtolower($param) === "yes" || strtolower($param) === "y" || strtolower($param) === "1") {
             $convertHTML = true;
         }
     }
     $param = $this->EE->TMPL->fetch_param('page_uri');
     $param = $this->EE->db->escape_str($param);
     $currentPageURI = $this->EE->security->xss_clean($param);
     //###   Get initial URL path (e.g. /index.php)   ###
     $tempIndexArray = explode("/", str_replace(array("http://", "https://"), "", $this->EE->functions->fetch_site_index()));
     array_shift($tempIndexArray);
     $siteIndex = implode("/", $tempIndexArray);
     if (substr($siteIndex, -1, 1) == "/") {
         $siteIndex = substr($siteIndex, 0, -1);
     }
     if (substr($siteIndex, 0, 1) != "/" && strlen($siteIndex) > 0) {
         $siteIndex = "/" . $siteIndex;
     }
     $actualURI = $siteIndex . "/" . $this->EE->uri->uri_string();
     if (empty($entryID) && empty($currentPageURI)) {
         $currentPageURI = $actualURI;
     }
     if (empty($currentPageURI)) {
         //###   Debug Output   ###
         $this->EE->TMPL->log_item("structure_entries (Next): No page specified and no URI found?!?: Actual URL=" . $actualURI);
         return;
     }
     $sqlStructure = $this->get_structure_data();
     $foundMatch = false;
     //###   Debug Output   ###
     $this->EE->TMPL->log_item("structure_entries (Next): Starting main loop through entries. Total rows from DB=" . $sqlStructure->num_rows());
     foreach ($sqlStructure->result_array() as $structureField) {
         $entry_id = $structureField['entry_id'];
         $status = $structureField['status'];
         $entryURL = $siteIndex . $site_pages['uris'][$entry_id];
         if (substr($entryURL, -1, 1) == "/") {
             $entryURL = substr($entryURL, 0, -1);
         }
         //###   Bug in Structure with ability to map an entry to be it's own parent   ###
         if ($structureField['parent_id'] == $structureField['entry_id']) {
             $structureField['parent_id'] = 0;
         }
         if (!empty($entry_id) && ($status != "closed" || $restrictStatus == "closed")) {
             //###   Get specific entry information   ###
             if (isset($this->EE->session->cache['structure_entries']['entryData' . $entry_id])) {
                 $sqlEntryResult = $this->EE->session->cache['structure_entries']['entryData' . $entry_id];
             } else {
                 $sql = "SELECT exp_channel_data.*, exp_channel_titles.*, exp_channels.channel_name\n\t\t\t\t\t\t\t  FROM exp_channel_data, exp_channel_titles, exp_channels\n\t\t\t\t\t\t\t WHERE exp_channel_data.entry_id = " . $entry_id . "\n\t\t\t\t\t\t\t   AND exp_channel_titles.entry_id = " . $entry_id . "\n\t\t\t\t\t\t\t   AND exp_channels.channel_id = exp_channel_titles.channel_id\n\t\t\t\t\t\t\t LIMIT 1";
                 $sqlEntryResult = $this->EE->db->query($sql);
                 $this->EE->session->cache['structure_entries']['entryData' . $entry_id] = $sqlEntryResult;
             }
             $EntryDetails = $sqlEntryResult->result_array();
             //###   Debug Output   ###
             $this->EE->TMPL->log_item("structure_entries (Next): entryURL=" . $entryURL . " actualURI=" . $actualURI);
             if ($entryURL == $actualURI) {
                 $foundMatch = true;
                 if ($direction == "previous" || $direction == "prev") {
                     $output .= $this->EE->TMPL->tagdata;
                     //###   Debug Output   ###
                     $this->EE->TMPL->log_item("structure_entries (previous): tagdata=" . $this->EE->TMPL->tagdata);
                     //###   Loop through all the single variables from template   ###
                     foreach ($this->EE->TMPL->var_single as $key => $fieldName) {
                         if ($fieldName == "page_uri") {
                             //###   This is the full URL to (and including) the page
                             $output = $this->EE->TMPL->swap_var_single($fieldName, $previousPageData["entryURL"], $output);
                         } else {
                             if ($fieldName == "page_url") {
                                 //###   This is the individual segment URL (not the full URL)
                                 $output = $this->EE->TMPL->swap_var_single($fieldName, $previousPageData["page_slug"], $output);
                             } else {
                                 if (isset($previousPageData[$fieldName])) {
                                     //###   Replace any built-in EE fields   ###
                                     if ($convertHTML) {
                                         $newContent = htmlentities($previousPageData[$fieldName], ENT_QUOTES, "UTF-8");
                                     } else {
                                         $newContent = $previousPageData[$fieldName];
                                     }
                                     $output = $this->EE->TMPL->swap_var_single($fieldName, $newContent, $output);
                                 }
                             }
                         }
                     }
                     //###   End of foreach
                     //###   Parse directory variables   ###
                     $output = $this->EE->typography->parse_file_paths($output);
                     return $output;
                 }
             } else {
                 if ($foundMatch) {
                     $output .= $this->EE->TMPL->tagdata;
                     //###   Debug Output   ###
                     $this->EE->TMPL->log_item("structure_entries (next): tagdata=" . $this->EE->TMPL->tagdata);
                     //###   Loop through all the single variables from template   ###
                     foreach ($this->EE->TMPL->var_single as $key => $fieldName) {
                         if ($fieldName == "page_uri") {
                             //###   This is the full URL to (and including) the page
                             $output = $this->EE->TMPL->swap_var_single($fieldName, $entryURL, $output);
                         } else {
                             if ($fieldName == "page_url") {
                                 //###   This is the individual segment URL (not the full URL)
                                 $output = $this->EE->TMPL->swap_var_single($fieldName, $Structure->page_slug($entry_id), $output);
                             } else {
                                 if (isset($EntryDetails[0][$fieldName])) {
                                     //###   Replace any built-in EE fields   ###
                                     if ($convertHTML) {
                                         $newContent = htmlentities($EntryDetails[0][$fieldName], ENT_QUOTES, "UTF-8");
                                     } else {
                                         $newContent = $EntryDetails[0][$fieldName];
                                     }
                                     $output = $this->EE->TMPL->swap_var_single($fieldName, $newContent, $output);
                                 }
                             }
                         }
                     }
                     //###   End of foreach
                     //###   Parse directory variables   ###
                     $output = $this->EE->typography->parse_file_paths($output);
                     if ($direction == "next") {
                         return $output;
                     }
                 }
             }
         }
         //###   End of Status conditional
         $previousPageData = $EntryDetails[0];
         $previousPageData["entryURL"] = $entryURL;
         $previousPageData["page_slug"] = $Structure->page_slug($entry_id);
     }
     //###   End of foreach ($sqlStructure->result)
 }