<?php
  
  include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/sessionStartGeneral.php");
?>
<?php
  $pageTitle = "Hot Deals";
  $pageDescription = "Check out our hot deals on your favorite grocery items. These won't last long so act fast.";
  $pageKeywords = "Hot Deals, advertisement, specials, deals";
?>
<?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php") ?>
<?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/mainNav.php")?>
    
    
    
    <section class="container">
      <div id="blankBuffer" class="carousel-buffer"></div>
      
      <section class="container" id="mainContent">
        <article class="row">
          <div class="span12" id="hotDealContainer">
            <h1>Hot Deals</h1>
            <?php
              include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.features.inc.php");
              $storeObj = new stores();
              $theDeals = $storeObj->getActiveDeals();
              if(count($theDeals) > 0){
                foreach($theDeals as $theDeal){
            ?>
                <div class="offsetPadLeft" id="oneDeal">
                  <h3><?php echo $theDeal['deal_name']; ?></h3>
                  <?php
                    if(substr($theDeal['deal_photo'], -3) != "pdf"){
                  ?>
                    <img src='/<?php echo $theDeal['deal_photo']; ?>' alt="<?php echo $theDeal['deal_name']; ?>">
                  <?php
                    }//end if(substr($theDeal['deal_photo'], -3) != "pdf")
                    else{
						if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/Kickstrap/img/dynamic")) {
							mkdir($_SERVER['DOCUMENT_ROOT'] . "/Kickstrap/img/dynamic", 0777, true);
						}
						
						$lastSlash = strrpos($theDeal['deal_photo'], "/");
						$fileName = substr($theDeal['deal_photo'], $lastSlash);
						$fileName = str_replace(".pdf", ".jpg", $fileName);
						if(!is_file($_SERVER['DOCUMENT_ROOT'] . "/Kickstrap/img/dynamic" . $fileName)){
						  $im = new Imagick($_SERVER['DOCUMENT_ROOT'] . "/" . $theDeal['deal_photo']);
						  $im->setIteratorIndex(0);
						  $im->setImageFormat('jpg');
						  $im->setImageCompression(imagick::COMPRESSION_JPEG);
						  $im->setImageCompressionQuality(40);
						  $im->writeImage($_SERVER['DOCUMENT_ROOT'] . "/Kickstrap/img/dynamic" . $fileName);
						}
						
                  ?>
					<a href="/<?php echo $theDeal['deal_photo']; ?>"><img src="/Kickstrap/img/dynamic<?php echo $fileName; ?>" width="250px" alt="<?php echo $theDeal['deal_name']; ?>"></a>
                    <a href="/<?php echo $theDeal['deal_photo']; ?>" class="btn">Download the File</a>
                  <?php
                    }
                  ?>
                </div>
                <br>
            <?php
                }//end foreach($theDeals as $theDeal)
              }//end if(count($theDeals) > 0)
              else{
            ?>
              <div id="noDeals">
                <p>We do not currently have any hot deals. Check back often for the best of the best deals.</p><br><br>
                <p>Click on a location below to see our weekly ad.</p>
                <p>
                  <a href="/weeklyAd" class="btn">See the Weekly Ad</a> &nbsp;
                </p>
              </div>
            <?php
              }
            ?>
          </div>
        </article>
      </section>
    </section>
      
    
    
    
    <?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/footer.php"); ?>
  
  
</body>
</html>
