<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/colourp1/public_html/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // load records
  list($categoriesRecords, $categoriesMetaData) = getRecords(array( 
    'tableName'   => 'categories', 
  )); 
  
  $categoriesRecord = @$categoriesRecords[0]; // get first record 
   
  $title = ""; 
  foreach ($categoriesRecords as $category) { 
    if($category['num'] == @$_REQUEST['main_category']) { 
      $title = $category['title']; 
    } 
  } 
  // show error message if no matching record is found
  if (!$categoriesRecord) {
    header("HTTP/1.0 404 Not Found");
    print "Record not found!";
    exit;
  }

  list($product_subcategoriesRecords, $product_subcategoriesMetaData) = getRecords(array(
    'tableName'   => 'product_subcategories',
	'orderBy'     => 'title',
  ));
  
  /* CLAIRE ADDED CODE HERE */
  
  $products_query = array('tableName'   => 'product_details');
  
  if(isset($_REQUEST['avail']) {
	  if($_REQUEST['avail'] == 'true') {
		  $products_query['where'] = 'availability = "In Stock Now"';
	  }
	  else if($_REQUEST['avail'] == 'false') {
		  $products_query['where'] = 'availability = "Unavailable"';
	  }
  }
  
  list($product_detailsRecords, $product_detailsMetaData) = getRecords($products_query);
  
  /* END ADDED CODE */

  list($slideshowRecords, $slideshowMetaData) = getRecords(array(
    'tableName'   => 'slideshow',
    'limit'       => '1',
  ));
  $slideshowRecord = @$slideshowRecords[0]; // get first record

  // show error message if no matching record is found
  if (!$slideshowRecord) {
    header("HTTP/1.0 404 Not Found");
    print "Record not found!";
    exit;
  }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE><?php echo $title; ?></TITLE>
<LINK HREF="styles.css" REL="stylesheet" TYPE="text/css">

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-22970572-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script></HEAD>

<BODY>
<TABLE WIDTH="1000" BORDER="0" ALIGN="CENTER" CELLPADDING="0" CELLSPACING="0">
  <TR>
    <TD COLSPAN="7" CLASS="tdWhite"><?php require_once('_include-masthead.php'); ?></TD>
  </TR>
  <TR>
    <TD CLASS="titleLower" COLSPAN="7" ALIGN="LEFT" VALIGN="TOP"><?php 
  list($randomImageRecord, $randomImageMetaData) = getRecords(array( 
         'tableName'   =>  'slideshow', 
	 'limit'       =>  1, 
	 'allowSearch' => false, 
       )); 
   
  $images = array(); 
   
  if ($randomImageRecord) { 
    $images = $randomImageRecord[0]['images']; 
  } 
?>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"><!--  
  
var image = new Array();  
 
<?php $counter = 0; ?> 
<?php foreach ($images as $image): ?> 
  image[<?php echo $counter++; ?>] = '<?php echo $image['urlPath'];?>' ;  
<?php endforeach ?> 
  
var index = Math.floor(Math.random() * image.length);  
document.write('<style type="text/css"> td.titleLower { background-image: url('+ image[index] +') } </style>');  
//-->  
</SCRIPT><TABLE WIDTH="1000" HEIGHT="170" BORDER="0" CELLPADDING="0" CELLSPACING="0">
      <TR>
        <TD CLASS="tdLowerHeadlineCell"><H1><?php echo $title; ?></H1></TD>
      </TR>
    </TABLE></TD>
  </TR>
  <TR>
    <TD CLASS="tdWhite"><IMG SRC="images/single-pixel.gif" WIDTH="20" HEIGHT="17"></TD>
    <TD CLASS="tdWhite"><IMG SRC="images/single-pixel.gif" WIDTH="176" HEIGHT="17"></TD>
    <TD CLASS="tdWhite"><IMG SRC="images/single-pixel.gif" WIDTH="20" HEIGHT="17"></TD>
    <TD CLASS="tdWhite"><IMG SRC="images/single-pixel.gif" WIDTH="499" HEIGHT="17"></TD>
    <TD CLASS="tdWhite"><IMG SRC="images/single-pixel.gif" WIDTH="20" HEIGHT="17"></TD>
    <TD CLASS="tdWhite"><IMG SRC="images/single-pixel.gif" WIDTH="245" HEIGHT="17"></TD>
    <TD CLASS="tdWhite"><IMG SRC="images/single-pixel.gif" WIDTH="20" HEIGHT="20"></TD>
  </TR>
  <TR>
    <TD CLASS="tdWhite">&nbsp;</TD>
    <TD CLASS="tdWhite"><?php require_once('_include-plants-nav.php'); ?>
<?php require_once('_include-search-nav.php'); ?></TD>
    <TD CLASS="tdWhite">&nbsp;</TD>
    <TD CLASS="tdWhite"><p class="pRule"><a href="javascript:history.go(-1);">&laquo;                 Previous page</a></p>
    
    <?php /* CLAIRE ADDED CODE HERE */ ?>
    
    <?php $currenturl = $_SERVER['REQUEST_URI'] . '?main_category=' . $categoriesRecord['num']; ?>
    <a href="<?php echo $currenturl; ?>">All Products</a> | <a href="<?php echo $currenturl; ?>&avail=true">In Stock</a> | <a href="<?php echo $currenturl; ?>&avail=false">Unavailable</a>
    
    <?php /* END ADDED CODE */ ?>
    
      <?php foreach ($product_subcategoriesRecords as $record): ?>
		  <table width="499" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td>
        <?php foreach ($record['main_image'] as $upload): ?>
<table width="160" border="0" cellpadding="0" cellspacing="0"><tr><td class="tdInsetBG">          
          <?php if ($upload['hasThumbnail']): ?>
            <a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>""" alt="" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?> border=" border="0" class="imgPadTop"0 /></a>

          <?php elseif ($upload['isImage']): ?>
          <?php else: ?>
          <?php endif ?>
</td></tr></table>      
        <?php endforeach ?>
          <td>&nbsp;</td>
          <td>      <h2><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h2>
<?php 
      list($product_detailsRecords, $product_detailsMetaData) = getRecords(array( 
        'tableName'   => 'product_details', 
	'allowSearch' =>  false, 
	'where'       => "subcategory = '". intval($record['num'])."'", 
      )); 
    ?> 
    <ul><?php foreach ($product_detailsRecords as $detailRecord): ?> 
     <li> 
       <a href="<?php echo $detailRecord['_link'] ?>"><?php echo $detailRecord['title'] ?></a> 
             <?php endforeach ?> 
          
              <?php if (!$product_detailsRecords): ?> 
       </li> 
    <?php endif ?></ul></td>
        </tr>
        <tr>
          <td><img src="images/single-pixel.gif" width="160" height="10" /></td>
          <td><img src="images/single-pixel.gif" width="10" height="10" /></td>
          <td><img src="images/single-pixel.gif" width="329" height="10" /></td>
        </tr>
      </table>    <?php endforeach ?>

    <?php if (!$product_subcategoriesRecords): ?>
      <p>No records were found! Please choose a different category.</p>
    <?php endif ?>

</TD><TD CLASS="tdWhite">&nbsp;</TD>
    <TD COLSPAN="2" CLASS="tdWhite"><?php require_once('_include-right-column.php'); ?></TD>
  </TR>
  <TR>
    <TD CLASS="tdWhite">&nbsp;</TD>
    <TD COLSPAN="5" CLASS="tdWhiteRuleTop"><?php require_once('_include-footer.php'); ?></TD>
    <TD CLASS="tdWhite">&nbsp;</TD>
  </TR>
</TABLE>
</BODY>
</HTML>
