<?php
// ----------------------------------------------------------------------------
// Copyright (C) 2013, Vinos de Frutas Tropicales
// Created expressly for PB Publishing, xcountryski-vermont.com.
// ----------------------------------------------------------------------------
$sidebarLinks = '';
if (is_array($commonInfo['sidebar_links'])) {
  foreach ($commonInfo['sidebar_links'] as $currentSidebar) {
    $sidebarInfo = explode('|', $currentSidebar);
    if ($sidebarInfo[0] == '%%LINKS%%') {
      $sortedCenters = array ( 'northern' => array(), 'central' => array(), 'southern' => array() );
      foreach ($dbMain as $key => $values) {
        $sortedCenters[$values['region']][$key] = $values['name'];
      }
      unset ($key, $values);
      foreach ($sortedCenters as $region => $centerNames) {
        $sidebarLinks .= '        <li><a href="#">' . ucfirst($region) . ' Vermont Conditions</a><div><ul>' . "\n";
        foreach ($centerNames as $id => $name) {
          $sidebarLinks .= '          <li><a href="index.php?center=' . $id . '">' . $name . '</a>' . "\n";
        }
        $sidebarLinks .= '        </ul></div></li>' . "\n";
      }
    } else {
      $sidebarLinks .= '          <li><a href="' . $sidebarInfo[0] . '"' . (($sidebarInfo[2] == 'Y') ? ' target="_blank"' : '') . '>' . $sidebarInfo[1] . '</a></li>' . "\n";
    }
  }
}
unset($sidebarInfo, $currentSidebar);
/*
        <li><a href="#">Menu Item</a>
          <div>
            <ul>
              <li><a href="#">Menu Item</a></li>
              <li><a href="#">Menu Item</a></li>
              <li><a href="#">Menu Item</a></li>
              <li><a href="#">Menu Item</a></li>
            </ul>
          </div>
        </li>
        <li><a href="#">Menu Item</a>
          <div>
            <ul>
              <li><a href="#">Menu Item</a>
                <div>
                  <ul>
                    <li><a href="#">Menu Item</a>
                      <div>
                        <ul>
                          <li><a href="#">Menu Item</a></li>
                        </ul>
                      </div>
                    </li>
                  </ul>
                </div>
              </li>
            </ul>
          </div>
        </li>
        <li><a href="#">Menu Item</a></li>
        <li><a href="#">Menu Item</a></li>
        <li><a href="#">Menu Item</a></li>
        <li><a href="#">Menu Item</a></li>
*/
$main_template = ($current_page == 'page') ? PAGE_TEMPLATE : MAIN_TEMPLATE;
if (!@file_exists($main_template)) {
  general_dump2log('Error: Could not locate main template file!  Please contact the site administrator.', '', true);
  
} else {
  $mainTemplate = @file_get_contents($main_template);
  if ($mainTemplate === false) {
    general_dump2log('Error reading main template file!  Please contact the site administrator.', '', true);
    
  } else {
    $mainTemplate = str_replace ('%%METADESC%%', ($metaDescription == '') ? '' : '<meta content="' . $metaDescription . '" name="description" />', $mainTemplate);
    $mainTemplate = str_replace ('%%METAKEYWORDS%%', ($metaKeywords == '') ? '' : '<meta content="' . $metaKeywords . '" name="keywords" />', $mainTemplate);
    $mainTemplate = str_replace ('%%PAGETITLE%%', $pageTitle, $mainTemplate);
    $mainTemplate = str_replace ('%%CENTER_DROPDOWN%%', general_build_centers_dropdown($dbMain, $center), $mainTemplate);
    $mainTemplate = str_replace ('%%ADDITIONAL_STYLING%%', $additionalStyling, $mainTemplate);
    $mainTemplate = str_replace ('%%ADDITIONAL_SCRIPTS%%', $additionalScripts, $mainTemplate);
    $headerImage = (general_is_null($headerImage)) ? '&nbsp;' : "<img src=\"images/$headerImage\" alt=\"Xcountryski-vermont banner\" width=\"598\" height=\"103\" class=\"scaled\" />";
    $mainTemplate = str_replace ('%%HEADER_IMAGE%%', $headerImage, $mainTemplate);
    $mainTemplate = str_replace ('%%SIDEBAR_LINKS%%', $sidebarLinks, $mainTemplate);
    $mainTemplate = str_replace ('%%TOP_CONTENT%%', nl2br(html_entity_decode($top_content)), $mainTemplate);
    $mainTemplate = str_replace ('%%BANNER_AD_TOP%%', ($banner_ad_top == '') ? '&nbsp;' : ("\n" . html_entity_decode($banner_ad_top) . "\n"), $mainTemplate);
    $mainTemplate = str_replace ('%%PAGE_CONTENT%%', html_entity_decode($page_content), $mainTemplate);
    $mainTemplate = str_replace ('%%BANNER_AD_BOTTOM%%', ($banner_ad_bottom == '') ? '&nbsp;' : ("\n" . html_entity_decode($banner_ad_bottom) . "\n"), $mainTemplate);
    $mainTemplate = str_replace ('%%CURRENT_YEAR%%', date('Y'), $mainTemplate);
    $mainTemplate = str_replace ('%%PAGE_FILENAME%%', $page_filename, $mainTemplate);
    
    echo $mainTemplate;
  }
}