<?php
// ----------------------------------------------------------------------------
// Copyright (C) 2013-2016, Vinos de Frutas Tropicales
// Created expressly for PB Publishing, xcountryski-vermont.com.
// ----------------------------------------------------------------------------
require('home.php');
$pageTitle = $dbMain[$center]['name'] . ' Ski Conditions :: Reported by XCountrySki-Vermont';
$metaDescription = $dbMain[$center]['meta_description'];
$metaKeywords = $dbMain[$center]['meta_keywords'];
$headerImage = $commonInfo['logo_' . $region];
$top_content = ((general_is_null($dbMain[$center]['special_content'])) ? $commonInfo['content_common_top'] : $dbMain[$center]['special_content']);
$banner_ad_top = (general_is_null($dbMain[$center]['banner_ad_top'])) ? $banner_ad_top : html_entity_decode($dbMain[$center]['banner_ad_top']);
$banner_ad_bottom = (general_is_null($dbMain[$center]['banner_ad_bottom'])) ? $banner_ad_bottom : html_entity_decode($dbMain[$center]['banner_ad_bottom']);

if (!isset($dbCenter['new_from'])) {
  $dbCenter['new_from'] = 0;
}
if (!isset($dbCenter['new_to'])) {
  $dbCenter['new_to'] = 0;
}

if (!@file_exists(CENTER_TEMPLATE)) {
  general_dump2log('Error: Could not locate center template file!  Please contact the site administrator.', '', true);
  
} else {
  $page_content = @file_get_contents(CENTER_TEMPLATE);
  if ($page_content === false) {
    general_dump2log('Error reading center template file!  Please contact the site administrator.', '', true);
    
  } else { 
    $centerVarsMap = array ( 
         'km_open' => '%%KM_OPEN%%',
         'surface_type' => '%%SURFACE%%',
         'new_from' => '%%NEW_FROM%%',
         'new_to' => '%%NEW_TO%%',
         'base_from' => '%%BASE_FROM%%',
         'base_to' => '%%BASE_TO%%',
         'comment' => '%%COMMENTS%%',
         'description' => '%%DESC%%',
         'timestamp' => '%%UPDATED%%',
         'lessons' => '%%LESSONS%%',
         'rentals' => '%%RENTALS%%',
         'snowshoe' => '%%SNOWSHOE%%',
         'backcountry' => '%%BACKCOUNTRY%%',
         'km_track' => '%%KM_TRACK%%',
         'km_skate' => '%%KM_SKATE%%',
         'grooming' => '%%GROOMING%%',
         'surface_comment' => '',
        );
                          
    $surfaceTypes = array ( 'P'   => 'Packed',
                            'PP'  => 'Packed Powder',
                            'C'   => 'Corn',
                            'LG'  => 'Loose Granular',
                            'FG'  => 'Frozen Granular',
                            'LFG' => 'Loose Frozen Granular',
                            'S'   => 'Spring',
                            'MG'  => 'Machine Groomed',
                            'VC'  => 'Variable Conditions',
                            'WT'  => 'Wet',
                            'WFG' => 'Wet Frozen Granular',
                            'ICY' => 'Icy',
                            'PO'  => 'Powder',
                            'HP'  => 'Hard Pack',
                          );
    $centerName = $dbMain[$center]['name'];
    $website = (trim($dbMain[$center]['website']) != '') ? ('<a href="' . $dbMain[$center]['website'] . '" target="_blank"><img src="./images/bluebutton.gif" width="15" height="15" alt="Click to view this website" /> ' . $centerName . '</a>') : '';
    $page_content = str_replace('%%WEBSITE%%', $website, $page_content);
    $page_content = str_replace('%%NAME%%', (($website == '') ? $centerName : ''), $page_content);

    $center_address = ($dbMain[$center]['address'] != '' && strpos($dbMain[$center]['address'], ', VT') === false) ? (trim($dbMain[$center]['address']) . ', VT') : $dbMain[$center]['address'];
    $page_content = str_replace('%%CITY%%', $center_address, $page_content);
    $page_content = str_replace('%%PHONE%%', ($dbMain[$center]['phone'] == '') ? '' : ('Tel: ' . $dbMain[$center]['phone']), $page_content);
    $page_content = str_replace('%%LOGO%%', (($dbMain[$center]['logo'] == '') ? 'no_picture.gif' : $dbMain[$center]['logo']), $page_content);
    $page_content = str_replace('%%EMAIL%%', ($dbMain[$center]['email'] == '') ? '' : (sprintf('Email: <a href="mailto:%s">%s</a>', $dbMain[$center]['email'], $dbMain[$center]['email'])), $page_content);
    
    // -----
    // Create map-related script if the center has an address set.
    //
    if ($center_address != '' && $dbMain[$center]['longitude'] != 0 && $dbMain[$center]['latitude'] != 0) {
      $latitude = $dbMain[$center]['latitude'];
      $longitude = $dbMain[$center]['longitude'];
      
      $additionalScripts .= '<script src="https://maps.googleapis.com/maps/api/js"></script>' . "\n";
      $additionalScripts .= '<script>' . "\n";
      $additionalScripts .= '  function initialize() {' . "\n";
      $additionalScripts .= '    var mapCanvas = document.getElementById(\'the-map\');' . "\n";
      $additionalScripts .= '    var theLatLng = new google.maps.LatLng(' . $latitude . ', ' . $longitude . ');' . "\n";
      $additionalScripts .= '    var mapOptions = {' . "\n";
      $additionalScripts .= '      center: theLatLng,' . "\n";
      $additionalScripts .= '      zoom: 10,' . "\n";
      $additionalScripts .= '      mapTypeId: google.maps.MapTypeId.ROADMAP' . "\n";
      $additionalScripts .= '    }' . "\n";
      $additionalScripts .= '    var map = new google.maps.Map(mapCanvas, mapOptions);' . "\n";
      
      $additionalScripts .= '    var image = \'images/nordicski.png\';' . "\n";
      $additionalScripts .= '    var marker = new google.maps.Marker({' . "\n";
      $additionalScripts .= '      position: theLatLng,' . "\n";
      $additionalScripts .= '      map: map,' . "\n";
      $additionalScripts .= '      draggable:false,' . "\n";
      $additionalScripts .= '      icon: image,' . "\n";
      $additionalScripts .= '      animation: google.maps.Animation.DROP,' . "\n";
      $additionalScripts .= '      title:"' . $centerName . '"' . "\n";
      $additionalScripts .= '    });' . "\n";
      $additionalScripts .= '    google.maps.event.addListener(marker, \'click\', toggleBounce);' . "\n";
      $additionalScripts .= '    function toggleBounce() {' . "\n";
      $additionalScripts .= '      if (marker.getAnimation() != null) {' . "\n";
      $additionalScripts .= '        marker.setAnimation(null);' . "\n";
      $additionalScripts .= '      } else {' . "\n";
      $additionalScripts .= '        marker.setAnimation(google.maps.Animation.BOUNCE);' . "\n";
      $additionalScripts .= '      }' . "\n";
      $additionalScripts .= '    }' . "\n";
      $additionalScripts .= '  }' . "\n";
      $additionalScripts .= '  google.maps.event.addDomListener(window, \'load\', initialize);' . "\n";
      $additionalScripts .= '</script>' . "\n";

    }
    
    $extraFields = '';
    if (is_array($dbCenter['extra_fields'])) {
      foreach ($dbCenter['extra_fields'] as $currentField) {
        $fieldInfo = explode('|', $currentField);
        if ($fieldInfo[0] != '' && isset($fieldInfo[1]) && $fieldInfo[1] != '') {
          $extraFields .= '          <div class="w100"><strong>' . $fieldInfo[0] . '</strong> ' . $fieldInfo[1] . '</div>' . "\n";
        }
      }
    }
    $page_content = str_replace('%%EXTRA_FIELDS%%', $extraFields, $page_content);
    unset($extraFields, $currentField, $fieldInfo);
        
    $extraFields = '';
    if (is_array($dbCenter['extra_facilities'])) {
      foreach ($dbCenter['extra_facilities'] as $currentField) {
        $fieldInfo = explode('|', $currentField);
        if ($fieldInfo[0] != '' && isset($fieldInfo[1]) && $fieldInfo[1] != '') {
          $extraFields .= '          <div class="w100"><strong>' . $fieldInfo[0] . '</strong> ' . $fieldInfo[1] . '</div>' . "\n";
        }
      }
    }
    $page_content = str_replace('%%EXTRA_FACILITIES%%', $extraFields, $page_content);
    unset($extraFields, $currentField, $fieldInfo);
    
    foreach ($centerVarsMap as $key => $key2) {
      $currentValue = $dbCenter[$key];
      switch ($key) {
        case 'surface_type':
          $surfaces = explode(':', $currentValue);
          $surfaceText = '';
          if (isset($surfaces[0]) && array_key_exists($surfaces[0], $surfaceTypes)) {
            $surfaceText = $surfaceTypes[$surfaces[0]];
            if (isset($surfaces[1]) && array_key_exists($surfaces[1], $surfaceTypes)) {
              $surfaceText .= ', ' . $surfaceTypes[$surfaces[1]];
            }
          }
          if ($dbCenter['surface_comment'] != '') {
            $surfaceText .= ($surfaceText == '') ? '' : ' ';
            $surfaceText .= '(' . $dbCenter['surface_comment'] . ')';
          }
          $page_content = str_replace($key2, $surfaceText, $page_content);
          break;
        case 'base_from':
          $base = ($currentValue != '0') ? general_number_to_inches($currentValue) : 'N/A';
          $page_content = str_replace($key2, $base, $page_content);
          break;
        case 'new_from':
          $new = ($currentValue != '0') ? general_number_to_inches($currentValue) : '(none)';
          $page_content = str_replace($key2, $new, $page_content);
        case 'base_to':
        case 'new_to':
          $value_to   = ($currentValue != '0') ? (' - ' . general_number_to_inches($currentValue)) : '';
          $page_content = str_replace($key2, $value_to, $page_content);
          break;
        case 'timestamp':
          $page_content = str_replace($key2, date('H:i:s - l M d, Y', $currentValue), $page_content);
          break;

        case 'km_open':
        case 'comment':
        case 'description':
        case 'lessons':
        case 'rentals':
        case 'snowshoe':
        case 'backcountry':
        case 'km_track':
        case 'km_skate':
        case 'grooming':
          $page_content = str_replace($key2, $currentValue, $page_content);
          break;
        default:
          break;
      }
    }
  }
}