<?php
  
  require_once("_app_init.php");
  
  // error checking - check required plugins installed and activated
  if (!@$GLOBALS['SIMPLECART_PLUGIN']) {
    die("You must activate the SimpleCart plugin before you can access this page.");
  }

  // load record from 'pages'
  list($pagesRecords, $pagesMetaData) = getRecords(array(
    'tableName'   => 'pages',
    'where'       => "`num` = '9'", //PAGE CONTENT http://freestylepools.com.au/cmsAdmin/admin.php?menu=pages
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $pagesRecord = @$pagesRecords[0]; // get first record
  if (!$pagesRecord) { dieWith404("Record not found!"); } // show error message if no record found


// load records from 'products_listing'
list($products_listingRecords, $products_listingMetaData) = getRecords(array(
  'tableName'   => 'products_listing',
  'loadUploads' => true,
  'allowSearch' => false,
));

// load variations
$variationRecords             = mysql_select('variations');
$variationProductNumToRecords = array_groupBy($variationRecords, 'products_listingNum', true);


// get the list of categories as array wherein the array key is the category value, and array value is the category label
// $productCategoriesValuesToLabels will be used for the left category list
$productCategoriesValuesToLabels = array_combine(array_pluck($products_listingRecords, 'category'), array_pluck($products_listingRecords, 'category:label'));

// $productsListsRecords will be used for the right sub-list
$productsListsRecords = array();
if (@$_REQUEST['category']){
  $productsListsRecords = array_groupBy($products_listingRecords, 'category', true);
  $productsListsRecords = @$productsListsRecords[$_REQUEST['category']];
}
else{
  $productsListsRecords = $products_listingRecords;
}

?><!DOCTYPE html>
<html>
<head>	
	<title><?php if ($productsRecord['title']): ?> - <?php echo $productsRecord['title'] ?><?php endif ?> -  <?php if ($productsRecord['category']): ?><?php echo $productsRecord['category'] ?> pool products<?php endif ?>- Freestyle Pool Products for Sale</title>

	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="google-site-verification" content="SOjxqoOtnXRreKuKReKD2oyxfPvv-aDed-wLMfYbuj0" />
	
	<meta name="keywords" content="
	<?php if ($pagesRecord['seo_keywords']): ?><?php echo $pagesRecord['seo_keywords']; ?>
	<?php endif ?> 
	" />
	<meta name="description" content="
	<?php if ($pagesRecord['seo_description']): ?><?php echo $pagesRecord['seo_description']; ?>
	<?php endif ?> 
	" />

	<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'>
	
	<link rel="stylesheet" type="text/css" href="<?php echo PREFIX_URL ?>/css/kube.css" /> 	
	<link rel="stylesheet" type="text/css" href="<?php echo PREFIX_URL ?>/css/master.css" />
	<link rel="stylesheet" type="text/css" href="<?php echo PREFIX_URL ?>/css/jquery-ui-1.10.3.custom.css" />

	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
	<script src="<?php echo PREFIX_URL ?>/js/jquery.cycle2.min.js"></script>
	
	
	<!--[if lt IE 9]>
	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->		

<link rel="stylesheet" type="text/css" href="<?php echo PREFIX_URL ?>/shadowbox/shadowbox.css">
<script type="text/javascript" src="<?php echo PREFIX_URL ?>/shadowbox/shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init();
</script>

</head>
<body>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-44006658-40', 'auto');
  ga('send', 'pageview');

</script>
<div id="page">

<header class="container" style="background:url(<?php shuffle($snippetsRecord['flowers']); ?><?php foreach ($snippetsRecord['flowers'] as $index => $upload): ?><?php echo $upload['urlPath'] ?><?php break ?><?php endforeach ?>) no-repeat right top;">
	<img class="logo" src="images/headerMainLogo.jpg" alt="<?php echo htmlencode($snippetsRecord['title']) ?>">
	<div id="contact"><a href="contact.php">Email Us</a></div>
</header>

<nav class="container">
	<ul id="mainNav">
      <li><a href="index.php" class="active">Home</a></li>
      <li><a href="products.php" class="active">Shop</a></li>
      <li><a href="galleries.php" class="active">Galleries</a></li>
      <li><a href="procedure.php" class="active">Procedure</a></li>
      <li><a href="about_design.php" class="active">About Design</a></li>
      <li><a href="useful_links.php" class="active">Useful Links</a></li>
      <li><a href="contact.php" class="active">Contact Us</a></li>
      <li><a href="faqs.php" class="active">FAQ's</a></li>
	</ul>
</nav>

<style>
  .productCategory li{
    list-style: none;
    background: #111;
    border: solid 1px #1f1f1f;
    padding: 6px 10px;
    margin-bottom: 6px;
  }
  .productCategory li:hover{
    border: solid 1px #333;
  }
  .productCategory li a{
    text-decoration: none;
  }
  
  .productList>li{
    margin: 0 0 20px 0; list-style: none; padding: 10px; border: solid 1px #1f1f1f; background: #111; background: white; color: black;
  }
  .productListLeft{
    width: 150px;
    height: 150px;
    float: left;
    margin: 10px 20px;
  }
  .productListLeft img{
    max-width: 150px;
    max-height: 150px;
  }
  .productListRight{
    width: 580px;
    float: right;
  }
  .productListVariation{
    margin: 0;
  }
  .productListVariation li{
    margin: 0 20px 10px 0;
    list-style: none;
    display: inline-block;
  }
</style>
<div class="row content">
<!-- 	PAGE CONTENT -->
	<?php echo $pagesRecord['content']; ?>
<!-- 	END PAGE CONTENT -->
</div>

<div class="row content">
	<div class="quarter">
		<!--left category list-->
		<ul class="productCategory">
          <li><a href="products.php">All</a></li>
		<?php if (@$productCategoriesValuesToLabels ): ?>
		  <?php foreach ($productCategoriesValuesToLabels  as $value => $label): ?>
		  <li><a href="?category=<?php echo htmlspecialchars($value); ?>"><?php echo htmlspecialchars($label); ?></a></li>
		  <?php endforeach; ?>
		<?php endif; ?>
		</ul>
	</div>
	
	<div class="threequarter">
		<!--right category list-->
		<ul class="productList">
		<?php if (@$productsListsRecords): ?>
		  <?php foreach ($productsListsRecords as $product): ?>
              <?php
                $cartItemParts = 
                  array(
                    'name'            => $product['title'],
                    'product_price'   => $product['price'] ? number_format($product['price'], 2) : 0.00,
                    'recordNum'       => $product['num'],
                    'tableOrTag'      => 'products_listing',
                  );
              ?>
              
              <li>
                  <div class="productListLeft">
                      <?php $image = @$product['image_s'][0]; // get the first image ?>
                      <?php if ($image): ?>
                          <img src="<?php echo htmlspecialchars($image['thumbUrlPath']); ?>">
                      <?php endif; ?>
                  </div>
                  <div class="productListRight">
                      <h3>
                      <a href="<?php echo htmlspecialchars($product['_link']); ?>">
                        <?php echo htmlspecialchars($product['title']); ?>
                      </a>
                      </h3>
                      
                      <?php echo $product['content']; ?>
                      
                      <?php if (@$variationProductNumToRecords[$product['num']]): ?>
                          <ul class="productListVariation">
                          <?php foreach($variationProductNumToRecords[$product['num']] as $variation): ?>
                              <?php
                                $variationPrice = $variation['additional_price'] ? number_format($variation['additional_price'], 2) : 0.00;
                                $unitPrice      = $product['price'] ? number_format($product['price'], 2) : 0.00;
                                $unitPrice      = $unitPrice + $variationPrice;
                                $unitPrice      = $unitPrice ? number_format($unitPrice, 2) : 0.00;
                                $cartItemParts  = array_merge($cartItemParts, 
                                  array(
                                    'unitPrice'                  => $unitPrice,
                                    'variation_num'              => $variation['num'],
                                    'variation_title'            => $variation['title'],
                                    'variation_additional_price' => $variation['additional_price'],
                                  )
                                );
                                $cartItems = array($cartItemParts);
                                $addLink   = sc_createAddLink($cartItems);
                              ?>
                              <li>
                                <?php echo htmlspecialchars($variation['title']); ?><br />
                                $ <?php echo htmlspecialchars($unitPrice); ?><br />
                                <a href="<?php echo $addLink; ?>">Add to Cart</a>
                              </li>
                          <?php endforeach; ?>
                          </ul>
                          
                      <?php else: ?>
                          <?php
                          $unitPrice      = $product['price'] ? number_format($product['price'], 2) : 0.00;
                          $cartItemParts  = array_merge($cartItemParts, array(
                                                                                'unitPrice' => $unitPrice,
                                                                              ));
                          $cartItems      = array($cartItemParts);
                          $addLink        = sc_createAddLink($cartItems);
                          ?>
                          $ <?php echo htmlspecialchars($unitPrice); ?><br />
                          <a href="<?php echo $addLink; ?>">Add to Cart</a>
                      <?php endif; ?>
                  </div>
                  <div style="clear: both"></div>
              </li>
		  <?php endforeach; ?>
		<?php endif; ?>
        </ul>
	</div>
</div>

<?php include("_bottom.php"); ?>
