<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  // load viewer library
  $libraryPath = 'cms/lib/viewer_functions.php';
  $dirsToCheck = array('/home/luxurychartergroup/luxurychartergroup.com/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
  
  // determine sort order (defined in the URL)
  $customOrderBy = getCustomOrderBy(@$_REQUEST['sort']);

  // load records from 'yachts'
  list($yachtsRecords, $yachtsMetaData) = getRecords(array(
    'tableName'   => 'yachts',
    'perPage'     => '20',
    'where'       => 'active ="1"' . ' AND hide ="0"',
    'loadUploads' => true,
    'allowSearch' => true,
    'useSeoUrls'    => true,
    'orderBy'     => $customOrderBy,
  ));
  
  if(!@$_REQUEST['destinations']){
       $_REQUEST['destinations']=-1;
    }

   if(!@$_REQUEST['type']){
       $_REQUEST['type']=-1;
    }
  list($destinationsRecords, $destinationsMetaData) = getRecords(array(
    'tableName'   => 'destinations',
    'where'       => "num=".intval(@$_REQUEST['destinations']),
    'limit'       => '1',
    'allowSearch' => false,

  ));
  $destinationsRecord = @$destinationsRecords[0]; // get first record
  
  list($yacht_typeRecords, $yacht_typeMetaData) = getRecords(array(
    'tableName'   => 'yacht_type',
    'where'       => "url ='".mysql_escape(@$_REQUEST['type'])."'",
    'limit'       => '1',
    'allowSearch' => false,

  ));
  $yacht_typeRecord = @$yacht_typeRecords[0]; // get first record

  list($settingsRecords, $settingsMetaData) = getRecords(array(
    'tableName'   => 'settings',
    'limit'       => '1',
    'allowSearch'   => false,
  ));
  $settingsRecord = @$settingsRecords[0]; // get first record

 $resultsText="";

 if($destinationsRecord){
   $resultsText.=$destinationsRecord['name'];
   
   if($yacht_typeRecord){
     $resultsText.=" and ".$yacht_typeRecord['name_plural'];
   }
 }
 elseif($yacht_typeRecord){
     $resultsText.=$yacht_typeRecord['name_plural'];
   }
 elseif(@$_REQUEST['yacht_name_query']){
     $resultsText.=$_REQUEST['yacht_name_query'];
 }

 ####
 // this function will determine what the URL is for each of the sorting links on the page
 // first click will give the order ASC
 // second click will give the order DESC
 // $sortingField options: rate, length, guests
 // usage: getSortingURL("rate");
 function getSortingURL($sortingField) {
      
      // sorting for "rate"
      if ($sortingField == "rate") {
            // first click -> set order to ASC
            if ( @$_REQUEST['sort'] == "rateASC" )                   { return array('param' => "sort=rateDESC", 'class' => "active sortdesc"); }
            // second click -> set order to DESC
            if ( @$_REQUEST['sort'] == "rateDESC" )                  { return array('param' => "sort=rateASC", 'class' => "active sortasc"); }

            if ( !@$_REQUEST['sort'] || @$_REQUEST['sort'] )         { return array('param' => "sort=rateASC", 'class' => ""); }
      }
      
      // sorting for "length"
      if ($sortingField == "length") {
            // first click -> set order to ASC
            if ( @$_REQUEST['sort'] == "lengthASC" )                 { return array('param' => "sort=lengthDESC", 'class' => "active sortdesc"); }
            // second click -> set order to DESC
            if ( @$_REQUEST['sort'] == "lengthDESC" )                { return array('param' => "sort=lengthASC", 'class' => "active sortasc"); }

            if ( !@$_REQUEST['sort'] || @$_REQUEST['sort'] )         { return array('param' => "sort=lengthASC", 'class' => ""); }
      }
      
      // sorting for "guests"
      if ($sortingField == "guests") {
            // first click -> set order to ASC
            if ( @$_REQUEST['sort'] == "guestsASC" )                 { return array('param' => "sort=guestsDESC", 'class' => "active sortdesc"); }
            // second click -> set order to DESC
            if ( @$_REQUEST['sort'] == "guestsDESC" )                { return array('param' => "sort=guestsASC", 'class' => "active sortasc"); }

            if ( !@$_REQUEST['sort'] || @$_REQUEST['sort'] )         { return array('param' => "sort=guestsASC", 'class' => ""); }
      }
      
      return "?";
 }
 
 function getCustomOrderBy($sort) {
      
      if (!@$sort)  { return ""; }
      
      switch ($sort) {
            case "rateASC":
                return "CAST(rate_high_season AS UNSIGNED) ASC";   
                break;
            case "rateDESC":
                return "CAST(rate_high_season AS UNSIGNED) DESC";
                break;
            case "lengthASC":
                return "yacht_length ASC";
                break;
            case "lengthDESC":
                return "yacht_length DESC";
                break;
            case "guestsASC":
                return "guests_night ASC";
                break;
            case "guestsDESC":
                return "guests_night DESC";
                break;
      }
      
      return "";
 }
 
?>
<?php include("includes/headers.php"); ?>
<!doctype html>
<html class="no-js" lang="en" >
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php if ($resultsText): ?>List of <?php echo $yachtsMetaData['totalRecords']; ?> Yachts for <?php echo $resultsText;?><?php else: ?>List of <?php echo $yachtsMetaData['totalRecords']; ?> Yachts (Page <?php echo $yachtsMetaData['page'] ?>)<?php endif ?> - <?php echo $settingsRecord['title'] ?></title>
<?php if ($yachtsMetaData['prevPage']): ?>
    <link rel="prev" href="<?php echo $yachtsMetaData['prevPageLink'] ?>" />
    <?php endif ?>
    <?php if ($yachtsMetaData['nextPage']): ?>
    <link rel="next" href="<?php echo $yachtsMetaData['nextPageLink'] ?>" />
    <?php endif ?>
    <link rel="canonical" href="<?php echo $settingsRecord['web'] ?>/search.php/page-<?php echo $yachtsMetaData['page'] ?>/" />


<?php include("includes/head.php"); ?>
  </head>
<body>

<div id="skip"><a href="#content">Skip to Main Content</a></div>

<div class="off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">

<?php include("includes/top.php"); ?>

<div id="content">

    <div class="content-body row-space-bottom-3">

        <form method="get" action="?">

        <div class="filters">

                <div class="row">
                    
                    <div class="small-12 column">
                        <h2>Yacht Charter Search</h2>
                        <?php if ($resultsText): ?><p>Our list of premium yachts for <b><?php echo $resultsText;?></b></p>
                        <?php else: ?>
                        <p>Use the options below to filter the list of yachts. Looking for destinations? <a href="/destinations.php">Click here</a></p>
                        <?php endif ?>
                    </div>
                    <div class="search-inline">
                        <div class="medium-4 column">
                            <select class="input-list input-large" id="destinations" name="destinations">
                                <option value="">Destination...</option>
                                <?php
                                  // load records from 'destinations' for top and footer nav
                                  list($search_destinationsRecords, $selectedDestinations) = getCategories(array(
                                    'tableName'            => 'destinations', //
                                    'defaultCategory'     => '', // Enter 'first', a category number, or leave blank '' for none
                                    'rootCategoryNum'     => '0', // Only categories _below_ this one will be shown (defaults to 0 for all)
                                    'categoryFormat'       => 'showall',  // showall, onelevel, twolevel, breadcrumb
                                    'useSeoUrls'    => true, 
                                  ));
                                ?>
                                <?php foreach ($search_destinationsRecords as $record): ?>
                                    <?php if($record['active']==1 && $record['hide']==0): ?>
                                    <option value="<?php echo $record['num'] ?>" <?php selectedIf( $record['num'],  @$_REQUEST['destinations']) ?> >
                                        <?php if ($record['depth'] =="1"): ?>- <?php endif ?>
                                        <?php if ($record['depth'] =="2"): ?>&nbsp;&nbsp;-- <?php endif ?>
                                        <?php echo htmlspecialchars($record['name']) ?>
                                    </option>
                                    <?php endif ?>
                                <?php endforeach ?>
                            </select>
                        </div>
                        <div class="medium-3 column">
                            <select class="input-list input-large" id="type" name="type">
                                <option value="">Yacht Type...</option>
                                <?php
                                  list($search_yacht_typeRecords, $yacht_typeMetaData) = getRecords(array(
                                  'tableName'   => 'yacht_type',
                                  'loadUploads' => '0',
                                  'allowSearch' => '0',
                                  'where'         => 'active = "1"',
                                  ));
                                ?>
                                <?php foreach ($search_yacht_typeRecords as $record): ?>
                                <option value="<?php echo $record['url'] ?>" <?php selectedIf( $record['url'],  @$_REQUEST['type']) ?> >- <?php echo htmlspecialchars($record['name_plural']) ?></option>
                                <?php endforeach ?>
                            </select>
                        </div>
                        <div class="medium-3 column">
                            <input class="input-search input-large" type="text" placeholder="Keyword..." name="yacht_title_query,meta_description_query,intro_query,yacht_name_query,keywords_query" value="<?php echo htmlspecialchars(@$_REQUEST['yacht_title_query,meta_description_query,intro_query,yacht_name_query,keywords_query']); ?>" />
                        </div>
                        <div class="medium-2 column">
                            <button type="submit" class="button postfix">Search</button>
                        </div>
                    </div>
                </div>

        </div>
        
        <?php // showme($yachtsMetaData); ?>

        <div class="row results-filter">
            <div class="filter-sort medium-8 column">
                <dl class="sub-nav">
                    <dt>Sort by: </dt>
                    <dd<?php if ( !@$_REQUEST['sort'] ): ?> class="active"<?php endif ?>><a href="?">Default</a> </dd>
                    <dd class="<?php echo getSortingURL('rate')['class']; ?>"><a href="?<?php echo preg_replace("/&sort=.*?(ASC|DESC)/", "", @$_SERVER['QUERY_STRING']); ?>&<?php echo getSortingURL('rate')['param']; ?>">Rate</a></dd>
                    <dd class="<?php echo getSortingURL('length')['class']; ?>"><a href="?<?php echo preg_replace("/&sort=.*?(ASC|DESC)/", "", @$_SERVER['QUERY_STRING']); ?>&<?php echo getSortingURL('length')['param']; ?>">Length</a></dd>
                    <dd class="hide-for-small-only <?php echo getSortingURL('guests')['class']; ?>">  <a href="?<?php echo preg_replace("/&sort=.*?(ASC|DESC)/", "", @$_SERVER['QUERY_STRING']); ?>&<?php echo getSortingURL('guests')['param']; ?>">Guests</a></dd>
                </dl>
            </div>
            <div class="results-total medium-4 column text-right">
                <p><b><?php echo $yachtsMetaData['totalRecords']; ?> results ( Page <?php echo $yachtsMetaData['page'] ?> of <?php echo $yachtsMetaData['totalPages'] ?> )</b></p>
            </div>
        </div>

        </form>

        <div class="row">
            <div class="small-12 column">

                <ul class="small-block-grid-2 medium-block-grid-3 large-block-grid-4">
                    
                    <?php foreach ($yachtsRecords as $record): ?>
                    <li>
                        <div class="card-wrap">
                            <div class="card-img">
                                <div class="card-media-cover">
                                <a href="<?php echo strtolower($record['_link']); ?>">
                                <?php foreach ($record['list_image'] as $index => $upload): ?>
                                <?php if ($index >= 1) { continue; } // limit uploads shown ?>
                                    <?php if ($upload['thumbUrlPath2']): ?>
                                    <img src="<?php echo $upload['thumbUrlPath2'] ?>" alt="Yacht <?php echo $record['yacht_name'] ?>" />
                                    <?php else: ?>
                                    <img src="<?php echo $upload['thumbUrlPath'] ?>" alt="Yacht <?php echo $record['yacht_name'] ?>" />
                                    <?php endif ?>
                                <?php endforeach ?>
                                <?php if (!$record['list_image']): ?>  
                                    <img src="/images/default-image.png" alt="Yacht <?php echo $record['yacht_name'] ?>" /><br/>
                                <?php endif ?>
                                </a>
                                </div>
                            </div>
                            <div class="card-section">
                                <div class="guests right">
                                    <span><?php echo $record['guests_night'] ?></span>
                                </div>
                                <a href="<?php echo strtolower($record['_link']); ?>"><h3><?php echo $record['yacht_name'] ?></h3></a>
                                <div class="card-listing-info xsmall">
                                    <span class="listing-length"><?php echo $record['yacht_length'] ?>m, <?php echo round($record['yacht_length'] * 3.2808399); ?>'</span>
                                    <?php if ($record['rate_low_season'] or $record['rate_high_season']): ?>
                                    <span class="listing-rate">Rate:
                                    <?php if ($record['rate_low_season'] && $record['rate_low_season'] != $record['rate_high_season']): ?>
                                        
                                    <?php if ($record['rate_currency'] == 'nz'): ?>NZ$<?php elseif ($record['rate_currency'] == 'euro'): ?>&euro;<?php elseif ($record['rate_currency'] == 'au'): ?>AU$<?php else: ?>US$<?php endif ?><?php echo $record['rate_low_season'] ?>
                                    
                                    <?php endif ?>
                                    
                                    <?php if ($record['rate_low_season'] && $record['rate_high_season'] && $record['rate_low_season'] != $record['rate_high_season']): ?> - <?php endif ?>
                                    
                                    <?php if ($record['rate_high_season']): ?>
                                        <?php if ($record['rate_currency'] == 'nz'): ?>NZ$<?php elseif ($record['rate_currency'] == 'euro'): ?>&euro;<?php elseif ($record['rate_currency'] == 'au'): ?>AU$<?php else: ?>US$<?php endif ?><?php echo $record['rate_high_season'] ?>
                                    <?php endif ?> pw
                                    </span>

                                    
                                    <?php endif ?>
                                </div>
                            </div>
                        </div>
                    </li>
                    <?php endforeach ?>

                </ul>

            </div>
        </div>

        <div class="row">
          <div class="small-12 column">
            <div class="results-footer">
                <div class="pagination-row">
                    <ul class="pagination clearfix">
                    <?php if ($yachtsMetaData['prevPage']): ?>
                      <li><a class="button button-secondary button-before" href="<?php echo $yachtsMetaData['prevPageLink'] ?>"><i class="font_lcg icon-caret-left"></i> Previous Page</a></li>
                    <?php endif ?>
                    <?php if ($yachtsMetaData['nextPage']): ?>
                      <li class="right"><a class="button button-secondary button-after" href="<?php echo $yachtsMetaData['nextPageLink'] ?>">Next Page <i class="font_lcg icon-caret-right"></i></a></li>
                    <?php endif ?>
                    </ul>
                </div>
            </div>
          </div>
        </div>
        
    </div>

</div><!-- /#content-->

<?php include("includes/footer.php"); ?>

<!-- close the off-canvas menu -->
<a class="exit-off-canvas"></a>

</div><!-- .inner-wrap -->
</div><!-- .off-canvas-wrap -->

<?php include("includes/footer-scripts.php"); ?>

</body>
</html>
