<?php

$search = $_GET['search'];
$pageOffset = 7;
$anchor = $_GET['wine'];

$tableName="Wines2";


//$happyhourprice = 14;

date_default_timezone_set("America/Belize"); 


$variable = $_GET['WineID'];
$listType = $_GET['listType'];
$listName = $_GET['listName'];
$linkCount = $_GET['linkCount'];
$callingType = $_GET['identifier'];
$returnPage = $_GET['returnPage'];

$food1 = trim($_GET['food1']);
$food2 = trim($_GET['food2']);
$food3 = trim($_GET['food3']);
$food4 = trim($_GET['food4']);
$wineNumber = 0;
$screenWidth = 1000;

if(!empty($search)){
  $listType = "bySearch";
}
$singlePage="single-item/";


if(wonderplugin_is_device('iPhone')){
$screenWidth = 400;
$device = "iPhone";
}
elseif(wonderplugin_is_device('Windows')){
$screenWidth = 1000;
$device = "Windows";
}
elseif(wonderplugin_is_device('iPad')){
$screenWidth = 1000;
$device = "iPad";
}
elseif(wonderplugin_is_device('Mac')){
$screenWidth = 1000;
$device = "Mac";

}
elseif(wonderplugin_is_device('Android')){
$screenWidth = 1000;
$device = "Android";
}

//echo "XXXX".$device,"XXXX";

//$screenWidth=1000;
//$device = "iPad";

echo "<style>";
echo "img {display: block; margin: auto;}";
echo ".top { vertical-align: top; }";
echo ".bottom { vertical-align: bottom; }";
echo "</style>";




//echo"<meta name='viewport' id='viewport' content='width=500'/>";


if($food1!=""){
$xfood1=substr($food1, (strlen($food1)-2)*-1);
}
if($food2!=""){
$xfood2=substr($food2, (strlen($food2)-2)*-1);
}
if($food3!=""){
$xfood3=substr($food3, (strlen($food3)-2)*-1);
}
if($food4!=""){
$xfood4=substr($food4, (strlen($food4)-2)*-1);
}


   $con = mysqli_connect('localhost', 'mayabeac_wine', '3]eGo-r(Scru',"mayabeac_WineList");
        if (mysqli_connect_errno()) {
            echo"
                Failed to connect to the MySQL DB:".mysqli_connect_error();
            }
           else{


}

if (wonderplugin_is_device('iPhone')){
$singlePage="single-item/";
}
else{
$singlePage="single-item/";
}


mysqli_query($con, "SET NAMES 'utf8'");
$sql="SELECT * FROM settings";
$records3 = mysqli_query($con, $sql);
while($wines=mysqli_fetch_assoc($records3)){
    $happyHourStart = $wines['HappyHourStart'];
    $happyHourEnd = $wines['HappyHourEnd'];
    $happyhourprice = $wines['HappyHourPrice'];
    $happyHourShowStart = strtotime('-'.$wines['ShowBeforeHH'].' minutes', strtotime($happyHourStart));
    $happyHourShowStop = strtotime('+'.$wines['ShowAfterHH'].' minutes', strtotime($happyHourEnd));
    $happyHourOffer = $wines['HappyHourOffer'];
}

    if (time() >= $happyHourShowStart AND time() <= $happyHourShowStop){
    $happyhour = True;
    }
    else{
    $happyhour = False;
    }

if($listType=="bySearch"){

     $sql="SELECT * FROM ".$tableName." WHERE (WineName Like '%".$search."%' OR WineRegion Like '%".$search."%' OR WineCountry Like '%".$search."%' OR WineNationality Like '%".$search."%' OR WineSubRegion Like '%".$search."%' OR Category Like '%".$search."%' OR Category2 Like '%".$search."%' OR Category3 Like '%".$search."%'  OR  Desc1 Like '%".$search."%' OR  Desc2 Like '%".$search."%' OR  Desc3 Like '%".$search."%' OR  Desc4 Like '%".$search."%' OR  Eyes Like '%".$search."%' OR  Nose Like '%".$search."%' OR  Mouth Like '%".$search."%' OR  WineVarietal Like '%".$search."%' OR  WineProfile1 Like '%".$search."%' OR  WineProfile2 Like '%".$search."%' OR  PairingSuggestion Like '%".$search."%' OR  WineVintage Like '%".$search."%') AND WineType != 'Glass' AND QtyInStock > 0 AND AvailableForSale = -1 ORDER BY WinePrice";

// Does not include Desc1 'From the Winemaker'


// echo $sql;
//$records1=mysqli_query($con, $sql);
//       while ($row1 = mysqli_fetch_assoc($records1)) {
//       echo $row1['WineName'],"<br>";
//}
}
elseif($listType=="byVarietal"){
      $sql="SELECT * FROM ".$tableName." WHERE (OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 AND (Category = '".$listName."' OR Category2 = '".$listName."' OR Category3 = '".$listName."' OR Category4 = '".$listName."' OR Category5 = '".$listName."' OR Category6 = '".$listName."') AND ((QtyInStock > 0 AND WineType != 'Glass') OR (SoldAs = 1 AND WineType2 = 6))) ORDER BY WinePrice";
}
elseif($listType=="byCountry"){

      $sql="SELECT * FROM ".$tableName." WHERE (OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 AND WineCountry = '".$listName."' AND ((QtyInStock > 0 AND WineType != 'Glass'  AND WineType != 'Beer') OR (SoldAs = 1 AND WineType2 = 6))) ORDER BY CASE 
               WHEN WineType = 'Sparkling' THEN 1
               WHEN WineType = 'White' THEN 2
               WHEN WineType = 'Rose' THEN 3
               WHEN WineType = 'Red' THEN 4
               ELSE 5
         END,  WinePrice";
          

}
elseif($listType=="byProfile"){

      if($listName=="WhitesAZ"){
            $sql="SELECT * FROM ".$tableName." WHERE WineType = 'White' AND QtyInStock > 0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY WineName";
            $showName="All Whites A - Z";
            $listTypeName="White Wines";
      }
       elseif($listName=="WhitePrice"){
            $sql="SELECT * FROM ".$tableName." WHERE WineType = 'White' AND QtyInStock > 0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY WinePrice";
            $showName="All Whites by Price";
            $listTypeName="White Wines";
      }
      elseif($listName=="RedsAZ"){
            $sql="SELECT * FROM ".$tableName." WHERE WineType = 'Red' AND QtyInStock > 0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY WineName";
            $showName="All Reds A - Z";
            $listTypeName="Red Wines";
      }
      elseif($listName=="RedPrice"){
            $sql="SELECT * FROM ".$tableName." WHERE WineType = 'Red' AND QtyInStock > 0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY WinePrice";
            $showName="All Reds by Price";
            $listTypeName="Red Wines";
      }  
      elseif($listName=="Inventory"){
            $sql="SELECT * FROM ".$tableName." WHERE WineType != 'Glass' AND WineType != 'Dessert' AND QtyInStock > 0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY WineType, WineName";
            $showName="All Wines A-Z";
            $listTypeName="All Wines";
      }

elseif($listName=="All"){
            $sql="SELECT * FROM ".$tableName." WHERE WineType != 'Glass' AND WineType != 'Dessert' AND QtyInStock > 0 AND OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 ORDER BY CASE 
               WHEN WineType = 'Sparkling' THEN 1
               WHEN WineType = 'White' THEN 2
               WHEN WineType = 'Rose' THEN 3
               WHEN WineType = 'Red' THEN 4
               ELSE 5
         END,  WinePrice";
            $showName="All Wines A-Z";
            $listTypeName="All Wines";
      }  
  
      elseif($listName=="Dessert"){
             $sql="SELECT * FROM ".$tableName." WHERE (OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 AND (WineProfile1 = '".$listName."' OR WineProfile2 = '".$listName."') AND (QtyInStock > 0 AND WineType != 'Glass' OR (SoldAs = 1 AND WineType2 = 6))) ORDER BY WinePrice";
            $showName="";

       }
       else{
      
            $sql="SELECT * FROM ".$tableName." WHERE (OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 AND (WineProfile1 = '".$listName."' OR WineProfile2 = '".$listName."') AND (QtyInStock > 0 AND WineType != 'Glass' OR (SoldAs = 1 AND WineType2 = 6))) ORDER BY WinePrice";
     $showName="";
      } 
}
elseif($listType=="byPerson"){

  $sql="SELECT * FROM ".$tableName." WHERE (OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 AND WhosFavorite = '".$listName."' AND ((QtyInStock > 0 AND WineType != 'Glass'  AND WineType != 'Beer') OR (SoldAs = 1 AND WineType2 = 6))) ORDER BY CASE 
               WHEN WineType = 'Sparkling' THEN 1
               WHEN WineType = 'White' THEN 2
               WHEN WineType = 'Rose' THEN 3
               WHEN WineType = 'Red' THEN 4
               ELSE 5
         END,  WinePrice";

}

elseif($listType=="byGlass"){

 // $sql="SELECT * FROM ".$tableName." WHERE AvailableForSale = -1  AND WineType = 'Glass' AND AvailableToday = -1 ORDER BY WineType2, WinePrice";           

if($happyhour==True){

$sql="SELECT * FROM ".$tableName." WHERE  OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0  AND ((WineType = 'Glass' AND (AvailableToday = -1 OR AvailableHappyHour = -1)) OR (WineType!='Glass' AND ShowOnGlassList = -1 AND QtyInStock > 0)) AND WineName!='Mimosa'  ORDER BY SoldAs DESC, WineType2, WinePrice";
            $showName="Wines by Glass";
            $listTypeName="Glass";
}
else{

$sql="SELECT * FROM ".$tableName." WHERE OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 AND  ((WineType = 'Glass' AND AvailableToday = -1) OR (WineType!='Glass' AND ShowOnGlassList = -1 AND QtyInStock > 0)) AND WineName!='Mimosa' ORDER BY SoldAs DESC, WineType2, WinePrice";
            $showName="Wines by Glass";
            $listTypeName="Glass";
}

     
}
elseif($listType=="byFood"){

$foodList = "('".$food1."', '".$food2."', '".$food3."', '".$food4."')";

$sql="SELECT * FROM ".$tableName." WHERE (OutOfStock=0 AND Archived=0 AND LeaveOffMenu=0 AND ((WineType != 'Glass' AND QtyInStock>0) OR (WineType = 'Glass' AND AvailableToday = -1))  AND (FoodPairing1 IN ".$foodList." OR FoodPairing2 IN ".$foodList." OR FoodPairing3 IN ".$foodList." OR FoodPairing4 IN ".$foodList." OR FoodPairing5 IN ".$foodList." OR FoodPairing6 IN ".$foodList." OR FoodPairing7 IN ".$foodList." OR FoodPairing8 IN ".$foodList." OR FoodPairing9 IN ".$foodList." OR FoodPairing10 IN ".$foodList."  OR FoodPairing11 IN ".$foodList."  OR FoodPairing12 IN ".$foodList."  OR FoodPairing13 IN ".$foodList."  OR FoodPairing14 IN ".$foodList."  OR FoodPairing15 IN ".$foodList."))";

$showName="Food Pairings";
$listTypeName="Food Matches";
}



// Read all wines to determine length of list.
// Need to sort items based on match score (desc) then price (asc). Wines by the glass will appear first as they have 6 added
// to the base score whereas other wines start at 0

       $IDarray=array();
       $wineCount=0;
      $foodWines = array();      //two dimensional array 1= name, 2 = food1, 3 = food2, 4 = food3, 5 = food4, 6 = score, 7 = country, 8 = varietal, 9 = btl or glass, 10 = WineID, 11 = Price. 12 = Desc1, 13 = Desc2, 14= Desc3, 15 = Desc 4, 16 = Image1, 17= Image2, 18 = QtyInStock, 19 = Eyes, 20 = Nose, 21 = Mouth, 22 = Vintage, 23 = Alcohol Content, 24 = Pairing Suggestion , 25=Cases Produced, 26 = WineRegion, 27 = Wine Subregion, 28 = Nationality


       $records1=mysqli_query($con, $sql);
       while ($row1 = mysqli_fetch_assoc($records1)) {
            
      $wineCount = $wineCount+1;

      $foodWines[$wineCount][1] = $row1['WineName'];
      $foodWines[$wineCount][6] = 0;
      $foodWines[$wineCount][7]  = $row1['WineCountry'];
      $foodWines[$wineCount][8] = $row1['WineVarietal'];
      $foodWines[$wineCount][10] = $row1['ID'];
      $foodWines[$wineCount][11] = $row1['WinePrice'];
      //$foodWines[$wineCount][12] = $row1['PairingValue1'];
      //$foodWines[$wineCount][13] = $row1['PairingValue2'];
      //$foodWines[$wineCount][14] = $row1['PairingValue3'];
      //$foodWines[$wineCount][15] = $row1['PairingValue4'];
      $foodWines[$wineCount][32] = $row1['Desc1'];
      $foodWines[$wineCount][33] = $row1['Desc2'];
      $foodWines[$wineCount][34] = $row1['Desc3'];
      $foodWines[$wineCount][35] = $row1['Desc4'];
      $foodWines[$wineCount][16] = $row1['Image1'];
      $foodWines[$wineCount][17] = $row1['Image2'];
      $foodWines[$wineCount][18] = $row1['QtyInStock'];
      $foodWines[$wineCount][19] = $row1['Eyes'];
      $foodWines[$wineCount][20] = $row1['Nose'];
      $foodWines[$wineCount][21] = $row1['Mouth'];
      $foodWines[$wineCount][22] = $row1['WineVintage'];
      $foodWines[$wineCount][23] = $row1['AlcoholContent'];
      $foodWines[$wineCount][24] = $row1['PairingSuggestion'];
      $foodWines[$wineCount][25] = $row1['CasesProduced'];
      $foodWines[$wineCount][26] = $row1['WineRegion'];
      $foodWines[$wineCount][27] = $row1['WineSubRegion'];
      $foodWines[$wineCount][28] = $row1['WineNationality'];  
      $foodWines[$wineCount][29] = $row1['WineType']; 
      $foodWines[$wineCount][30] = $row1['WineSize']; 
      $foodWines[$wineCount][36] = $row1['AvailableToday'];
      $foodWines[$wineCount][37] = $row1['AvailableHappyHour'];
      $foodWines[$wineCount][38] = $row1['AvailableOnRequest'];  
       $foodWines[$wineCount][39] = $row1['SoldAs'];
      $foodWines[$wineCount][51] = $row1['FoodPairing1'];
      $foodWines[$wineCount][52] = $row1['FoodPairing2'];
      $foodWines[$wineCount][53] = $row1['FoodPairing3'];
      $foodWines[$wineCount][54] = $row1['FoodPairing4'];
      $foodWines[$wineCount][55] = $row1['FoodPairing5'];
      $foodWines[$wineCount][56] = $row1['FoodPairing6'];
      $foodWines[$wineCount][57] = $row1['FoodPairing7'];
      $foodWines[$wineCount][58] = $row1['FoodPairing8'];
      $foodWines[$wineCount][59] = $row1['FoodPairing9'];
      $foodWines[$wineCount][60] = $row1['FoodPairing10'];
      $foodWines[$wineCount][61] = $row1['FoodPairing11'];
      $foodWines[$wineCount][62] = $row1['FoodPairing12'];
      $foodWines[$wineCount][63] = $row1['FoodPairing13'];
      $foodWines[$wineCount][64] = $row1['FoodPairing14'];
      $foodWines[$wineCount][65] = $row1['FoodPairing15'];
      $foodWines[$wineCount][66] = $row1['WineryWebsite'];
      $foodWines[$wineCount][67] = $row1['CellarLocation'];
      $foodWines[$wineCount][101] = $row1['WhosFavorite'];
      $foodWines[$wineCount][102] = $row1['FavoriteImage'];
      $foodWines[$wineCount][103] = $row1['WineMaker'];
      $foodWines[$wineCount][104] = $row1['AverageYield'];
      $foodWines[$wineCount][105] = $row1['MainClones'];
      $foodWines[$wineCount][106] = $row1['pH'];
      $foodWines[$wineCount][107] = $row1['Barrels'];
      $foodWines[$wineCount][108] = $row1['TA'];
      $foodWines[$wineCount][109] = $row1['Winery'];


      if( $foodWines[$wineCount][29] == "Glass"){
          $foodWines[$wineCount][6] = 100;
          $foodWines[$wineCount][9] = 0;
          }
      else{
            $foodWines[$wineCount][9] = 1;
            $foodWines[$wineCount][6] = 0;
      }
if($listType=='bySearch'){
for($x = 1; $x <= 40; $x++){
if($x!=16 AND $x!=17){
$foodWines[$wineCount][$x]= preg_replace("/".$search."/i", "<span style='background-color: #FFFF00;'>\$0</span>",$foodWines[$wineCount][$x]);
}
}
}

// preg_replace("/the/i", "<b>\$0</b>", $original);



// $foodWines[$wineCount][1] = "<span style="background-color: #FFFF00">".$foodWines[$wineCount][1]."</span>" 

if($listType == 'byFood'){

// only sort the array if this is a food match selection

$minPairing = 0;

for($x = 1; $x <= 15; $x++){

if($food1!=""){
         if($row1['FoodPairing'.$x]==$food1){
          
          $foodWines[$wineCount][12] = $row1['PairingValue'.$x] * -1 + 6; 
          if($foodWines[$wineCount][12]==6){
           $foodWines[$wineCount][12]=5;
          } 
          if($foodWines[$wineCount][12]==0 OR is_null($foodWines[$wineCount][12])){
             $foodWines[$wineCount][12]=5; 
          }
         $foodWines[$wineCount][6] =$foodWines[$wineCount][6]+$foodWines[$wineCount][12];
         }   
}

if($food2!=""){
         if($row1['FoodPairing'.$x]==$food2){
         $foodWines[$wineCount][13] = $row1['PairingValue'.$x] * -1 + 6; 
         if($foodWines[$wineCount][13]==6){
           $foodWines[$wineCount][13]=5;
          } 
         if($foodWines[$wineCount][13]==0 OR is_null($foodWines[$wineCount][13])){
             $foodWines[$wineCount][13]=1; 
          }
         $foodWines[$wineCount][6] =$foodWines[$wineCount][6]+$foodWines[$wineCount][13];
         }   
}

if($food3!=""){
         if($row1['FoodPairing'.$x]==$food3){
         $foodWines[$wineCount][14] = $row1['PairingValue'.$x] * -1 + 6; 
          if($foodWines[$wineCount][14]==6){
           $foodWines[$wineCount][14]=5;
          } 
         if($foodWines[$wineCount][14]==0 OR is_null($foodWines[$wineCount][14])){
             $foodWines[$wineCount][14]=1; 
          }
        $foodWines[$wineCount][6] =$foodWines[$wineCount][6]+$foodWines[$wineCount][14];
         }   
}
if($food4!=""){
         if($row1['FoodPairing'.$x]==$food4){
         $foodWines[$wineCount][15] = $row1['PairingValue'.$x] * -1 + 6;
          if($foodWines[$wineCount][15]==6){
           $foodWines[$wineCount][15]=5;
          } 
          if($foodWines[$wineCount][15]==0 OR is_null($foodWines[$wineCount][15])){
             $foodWines[$wineCount][15]=1; 
          }
         $foodWines[$wineCount][6] =$foodWines[$wineCount][6]+$foodWines[$wineCount][15];
         }   
   }   

}  

//echo $foodWines[$wineCount][1]." ".$foodWines[$wineCount][12]." ".$foodWines[$wineCount][13]." ".$foodWines[$wineCount][14]." ".$foodWines[$wineCount][15]."<br>";

if($foodWines[$wineCount][12] <3  AND  $foodWines[$wineCount][13] < 3 AND $foodWines[$wineCount][14] < 3 AND $foodWines[$wineCount][15] < 3){
//echo "Skipping".$foodWines[$wineCount][1]."<br>";
$wineCount=$wineCount - 1;

}

}
       
}

//sort Array
if($listType=='byFood'){
foreach ($foodWines as $key => $row) {
    $score[$key]  = $row[6];
    $winePrice[$key] = $row[11];
}

// With the use of the column keys you can now sort entire array by these columns and keep the row index value intact if they are non-numeric. This is good if your using some kind of hashed index.

array_multisort($score, SORT_DESC, $winePrice, SORT_DESC, $foodWines);
}


     // echo $wineCount;
       
if($listType == 'byFood'){
$xstart = 0;
$totalWines = $wineCount-1;
}
else{
$xstart = 1;
$totalWines = $wineCount;

}


if($totalWines!=0){
 for($x = $xstart; $x <= $totalWines; $x++){
if($x<$pageOffset){
$Offset = "";
}
else{
$temp = $x-$pageOffset;
$Offset = "#wine".$temp;
}
if($listType == 'byFood'){
$y=$x+1;
}
else{
$y=$x;
}       

   if($listType=="byCountry"){

               $listTypeName=$foodWines[$x][28];
          

         }
         elseif($listType=="byPerson"){
        $listTypeName=$foodWines[$x][101]."'s Favorites";
        $listName=$foodWines[$x][101];
}


elseif($listType=="byProfile" AND $listTypeName!=""){


         }
        elseif($listType=="byProfile" AND $listTypeName==""){

          $listTypeName=$listName;
         }
        elseif($listType=="byVarietal"){
          $listTypeName=$listName;
        }

        if($listTypeName=="Rose"){
         $listTypeName="Rosé";
         }

        if($listType=='bySearch'){
        $browsing=$y." of ".$wineCount."&nbsp;<b>Results for '".ucwords($search)."'</b>";
       }
       
        elseif($listType=='byPerson'){

          $browsing=$y." of ".$wineCount."&nbsp;<a href='/".$listType."/?Listing=".$listName."&identifier=".$callingType."&listType=".$returnPage.$Offset."'><b>".$listTypeName."</b></a>";  

        }

        elseif($listType=='byCountry'){

              $browsing=$y." of ".$wineCount."&nbsp;<a href='/".$listType."/?Listing=".$listName.$Offset."'><b>".$listTypeName." Wines</b></a>";


           
             if($foodWines[$x][29]=='Rose'){
             $showName=$listName."&nbsp;(Rosé)";
             }
             else{
             $showName=$listName."&nbsp;(".$foodWines[$x][29]. " Wines)";
             }
        }
        elseif($listType=='byVarietal'){
		$listName1=str_replace("&","%26",$listName); 
                  $browsing=$y." of ".$wineCount."&nbsp;<a href='/".$listType."/?Listing=".$listName1.$Offset."'><b>".$listTypeName."</b></a>";

                  $showName=$listName;
         }
         elseif($listType=='byProfile'){
            $browsing=$y." of ".$wineCount."&nbsp;<a href='/".$listType."/?Listing=".$listName.$Offset."'><b>".$listTypeName." </b></a>";

            $showName=$listName;
         }
         elseif($listType=="byGlass"){
         $browsing=$y." of ".$wineCount."&nbsp;<a href='/".$listType."/?Listing=".$listName.$Offset."'><b> Wines By The Glass</b></a>";

         }
         
         elseif($listType=="byFood"){

           $browsing=$y." of ".$wineCount."&nbsp;<a href='/foodmatches/?food1=".$food1."&food2=".$food2."&food3=".$food3."&food4=".$food4."'><b> Food Matches</b></a>";
           
          }

if($browsing==""){

        $browsing=$y." of ".$wineCount."&nbsp;<a href='/".$listType."/?Listing=".$listName.$Offset."'><b>".$listTypeName."</b></a>";
}

        
    echo "<body>";



//set to 500 now so it does not change - effectively disabling

        if(strlen($foodWines[$x][1]."&emsp".$foodWines[$x][22])>500){
         $nameFont="1.1em";
         }
        else{
        $nameFont="1.35em";
        }

  if($foodWines[$x][29] == 'Glass' OR $foodWines[$x][39] == 1){
        
          if($foodWines[$x][45] == 5 OR $foodWines[$x][45] == 0){
                  if($happyhour==True AND $foodWines[$x][37] == -1){
                        if($happyHourOffer=='Fixed Price'){
                              $happyHourFinal=$happyhourprice;
                        }
                        else if($happyHourOffer=='Amount Off'){
                        $happyHourFinal=$foodWines[$x][11] - $happyhourprice;
                        }
                        else if($happyHourOffer=='Percent Off'){
                        $happyHourFinal=round($foodWines[$x][11] * (1-$happyhourprice/100));
                        }
                      $winePrice = "<b><del>$".$foodWines[$x][11]."</del>&emsp;<font color='red'>$".$happyHourFinal."/glass</font></b>";
                  }
            else{
            $winePrice = "<b>$".$foodWines[$x][11]."/glass</b>";
            }
             
          }
          else{
           $winePrice = "<b>$".$foodWines[$x][11]."/glass (".$foodWines[$x][45]."oz)</b>";
           }
         }        
        else{
        $winePrice= "<b>$".$foodWines[$x][11]."</b>";
        }

      if($foodWines[$x][30]<750 AND $foodWines[$x][30]>50){

      if($foodWines[$x][30]==375){
        $sizeInfo = " (half bottle)";
       } 
       else{
        $sizeInfo = " (small bottle)";
        }

        }
        else{
         $sizeInfo = "";
         }  
         
        
      if($listType=='byPerson'){
         $favorite="";
      }

      elseif(is_null($foodWines[$x][101])){
         	$favorite="";
          	}   
      else{
          $favorite="<a href='/byperson/?WineID=".$foodWines[$x][10]."&Listing=".$foodWines[$x][101]."&listType=".$listType."&identifier=".$listName."&wine=".$x."'>".$foodWines[$x][102]."</a>";
         


 //$favorite="<a href='/byperson/?Listing=".$foodWines[$x][101]."'>".$foodWines[$x][102]."</a>";
          	}

echo "<a style='display:block;  height:200px;  margin-top:-200px; visibility:hidden;' id='wine".$y."'></a>";



if ($device == 'Windows' OR $device == 'Mac' OR $device == 'Linux'){
        $bodytext="justify";
        
       echo "<table  cellspacing='0' cellpadding='0'>";
       echo "<colgroup style='vertical-align: top;'>";
        echo "<col span='1' style='width: 15%; vertical-align: top;'>";
        echo "<col span='1' style='width: 84%;'>";
        echo "</colgroup>";


      


  echo "<tr><td style='text-align: center; font-size: 1.5em;  font-weight: bold;'>".$winePrice."</td><td style='text-align: left; font-size: 1.0em;  font-weight: normal;'>".$browsing."</td></tr>";

echo"<tr style='vertical-align: top;'>";

if(!empty($foodWines[$x][17])){


//$tempstr = "changeImage(".$foodWines[$x][10].", &#039".$foodWines[$x][16]."&#039, &#039".$foodWines[$x][17]."&#039)";



echo "<td style='vertical-align: top;'><div style='width:image width px; font-size:80%; text-align:center;'><a style='width: 100%; height: 100%' href='#' data-featherlight='".$foodWines[$x][17]."'><img id=".$foodWines[$x][10]." style='width: 28vw; height: 60vh; object-fit: contain;' src='".$foodWines[$x][16]."' alt='WineImage'>Click Image for Front / Back View</div></td>";

}
else{
  echo "<td style='vertical-align: top;'><img id=".$foodWines[$x][10]." style='width: 28vw; height: 60vh; object-fit: contain;' src='".$foodWines[$x][16]."' alt='WineImage'></td>";

}

    

         if($foodWines[$x][22] != 0){
  	         echo "<td style='vertical-align:top; font-weight: normal; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size: 1.35em'><span style='font-weight: bold'>".$foodWines[$x][1]."&emsp; ".$foodWines[$x][22].$sizeInfo."&emsp;".$favorite."</span><br>";
        }
        else{
               echo "<td style='vertical-align:top; font-weight: normal; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size: 1.35em'><span style='font-weight: bold'>".$foodWines[$x][1].$sizeInfo."&emsp;".$favorite."</span><br>";
         }


        }

elseif($device == 'iPad' OR $device == 'Android'){
   
  $bodytext="left";

      echo "<table >";

       echo "<colgroup>";
        echo "<col span='1' style='width: 30%;'>";
        echo "<col span='1' style='width: 69%;'>";
        echo "</colgroup>";


  
  echo "<tr><td style='text-align: center; font-size: 1.5em;  font-weight: normal;'>".$winePrice."</td><td style='text-align: left; font-size: 1.0em;  font-weight: normal;'>".$browsing."</td></tr>";

 echo"<tr>";
if(!empty($foodWines[$x][17])){


$tempstr = "changeImage(".$foodWines[$x][10].", &#039".$foodWines[$x][16]."&#039, &#039".$foodWines[$x][17]."&#039)";


echo "<td style='vertical-align: top; text-align: center;'><div style='width:image width px; font-size:80%; text-align:center;'><a href='#' data-featherlight='".$foodWines[$x][17]."'><img id=".$foodWines[$x][10]." style=' width: 37vw; height: 65vh; object-fit: contain;' src='".$foodWines[$x][16]."' alt='WineImage'>Click Image for Front / Back View</div></td>";

}
else{
  echo "<td style='vertical-align: top; text-align: center;'><img style='width: 37vw; height: 65vh; object-fit: contain;' src='".$foodWines[$x][16]."' alt='WineImage'></td>";

}

            

     if($foodWines[$x][22] != 0){
  	         echo "<td style='vertical-align:top; font-weight: normal; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size: 1.35em'><span style='font-weight: bold'>".$foodWines[$x][1]."&emsp; ".$foodWines[$x][22].$sizeInfo."&emsp;".$favorite."</span><br>";
        }
        else{
               echo "<td style='vertical-align:top; font-weight: normal; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size: 1.35em'><span style='font-weight: bold'>".$foodWines[$x][1].$sizeInfo."&emsp;".$favorite."</span><br>";
         }


        }

      elseif($device == 'iPhone'){

   $bodytext="left";
   


 
 //echo "<table>";

//echo "<tr><td style='display:block-inline; text-align: center; font-size: 1.1em;  font-weight: normal;'>".do_shortcode($prevWine)."&emsp;".$browsing."&emsp;".do_shortcode($nextWine)."</td></tr></table>";

 
      
      echo "<table>";
       echo "<colgroup>";
        echo "<col span='1' style='width: 35%;'>";
        echo "<col span='1' style='width: 64%;'>";
        echo "</colgroup>";  
 echo "<tr><td style='text-align: center; font-size: 1.5em;  font-weight: normal;'>".$winePrice."</td><td style='text-align: left; font-size: 1.0em;  font-weight: normal;'>".$browsing."</td></tr>";
         echo"<tr>";
   
if(!empty($foodWines[$x][17])){
$tempstr = "changeImage(".$foodWines[$x][10].", &#039".$foodWines[$x][16]."&#039, &#039".$foodWines[$x][17]."&#039)";

echo "<td style='vertical-align: top; text-align: center;'><div style='width:image width px; font-size:80%; text-align:center;'><img id=".$foodWines[$x][10]." onclick='".$tempstr."' style='width: 30vw; height: 70vh; object-fit: contain;' src='".$foodWines[$x][16]."' alt='WineImage'>Click Image for Front / Back View</div></td>";
}
else{
  echo "<td style='vertical-align: top; text-align: center;'><img style='width: 30vw; height: 70vh; object-fit: contain;' src='".$foodWines[$x][16]."' alt='WineImage'></td>";

}
         

     if($foodWines[$x][22] != 0){
  	         echo "<td style='vertical-align:top; font-weight: normal; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size: 1.35em'><span style='font-weight: bold'>".$foodWines[$x][1]."&emsp; ".$foodWines[$x][22].$sizeInfo."&emsp;".$favorite."</span><br>";
        }
        else{
               echo "<td style='vertical-align:top; font-weight: normal; text-align: ".$bodytext."; line-height: 1.25em; padding-right: 20px; font-size: 1.35em'><span style='font-weight: bold'>".$foodWines[$x][1].$sizeInfo."&emsp;".$favorite."</span><br>";
         }


        }
         

         //  ///Common Code Here
        

         
  	if($listType=="byFood"){

 $foodarray = array($foodWines[$x][51],$foodWines[$x][52],$foodWines[$x][53],$foodWines[$x][54],$foodWines[$x][55],$foodWines[$x][56],$foodWines[$x][57],$foodWines[$x][58],$foodWines[$x][59],$foodWines[$x][60],$foodWines[$x][61],$foodWines[$x][62],$foodWines[$x][63],$foodWines[$x][64],$foodWines[$x][65]);
         $goodfoods = "";
         $badfoods = "";

          if(in_array($food1, $foodarray)) {
           
                $goodfoods = $goodfoods.$xfood1;
           }
           elseif($food1!='none'){
                  $badfoods = $badfoods.", ".$xfood1;
            }

           if(in_array($food2, $foodarray)) {
                if($goodfoods!=""){
                $goodfoods = $goodfoods.", ".$xfood2;
                }
                else{
                $goodfoods = $goodfoods.$xfood2;
                 }
           }
           elseif($food2!='none'){
                  $badfoods = $badfoods.", ".$xfood2;
            }

   if(in_array($food3, $foodarray)) {
                if($goodfoods!=""){
                $goodfoods = $goodfoods.", ".$xfood3;
                }
                else{
                $goodfoods = $goodfoods.$xfood3;
                  }
           }
           elseif($food3!='none'){
                  $badfoods = $badfoods.", ".$xfood3;
            }

   if(in_array($food4, $foodarray)) {
                if($goodfoods!=""){
                $goodfoods = $goodfoods.", ".$xfood4;
                 }
                 else{
                 $goodfoods = $goodfoods.$xfood4;
                 }
           }
           elseif($food4!='none'){
                  $badfoods = $badfoods.", ".$xfood4;
            }
            
       // if($goodfoods!=""){
       // $goodfoods=substr($goodfoods, (strlen($goodfoods)-2)*-1);
        // echo "<font size='+1'><b>We suggest with: </b>".$goodfoods."<br><font>";
        //}

//alternate good food list - show all food listed as pairing with this wine
      





        if($badfoods!=""){
        $badfoods=substr($badfoods, (strlen($badfoods)-2)*-1);
 // leave off for now    echo "<font size='+1'><b>Mabye not so good with: </b>".$badfoods."<br><font>";
        }


          

         }
       
        if(!empty($foodWines[$x][109])){
   
   // leave off for now echo "<span style='font-weight: normal font-size: 1.25em'>".$foodWines[$x][109]."</span><br>";
}

        $varietalText=$foodWines[$x][8];
        if ($varietalText != ""){
            if (strpos($foodWines[$x][1], $varietalText) > 0){
                $varietalText="";
            }
        }

            // echo $foodWines[$x][8]."<br>";

         if($varietalText != ""){
               echo "<span style='font-weight: normal font-size: 1.25em'>".$varietalText."</span>";
               echo "<br>";
         }
   
 

$wineCountry = $foodWines[$x][7];
if(!empty($foodWines[$x][26])){

$wineCountry=$wineCountry.">".$foodWines[$x][26];

if(!empty($foodWines[$x][27])){
$wineCountry=$wineCountry.">".$foodWines[$x][27];

}

}    
   
        echo "<p><span style='font-weight: normal font-size: 1.25em'>".$wineCountry."</span></p>";
 

// echo "<span style='font-weight: normal font-size: 1.25em'>".$foodWines[$x][7].">".$foodWines[$x][26]."</span><br><br>";
    
//if($foodWines[$x][29]!='Glass' AND $foodWines[$x][18] > 0){   
// echo "<font size='+1'><span style='font-weight: bold'>In our Cellar: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][18]."</span><br><font>";
//}

if ($listType=="byFood"){

       echo "<font size='+1'><span style='font-weight: bold'>Goes with your selections:</span><span style='font-weight: normal'>".$goodfoods."</span><br><font>";


}
    if (!empty($foodWines[$x][19])) {
       echo "<font size='+1'><span style='font-weight: bold'>Eyes: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][19]."</span><br><font>";
      }
  
  	if (!empty($foodWines[$x][20])) {
       echo "<font size='+1'><span style='font-weight: bold'>Nose: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][20]."</span><br><font>";
      }
  
  	if (!empty($foodWines[$x][21])) {
       echo "<font size='+1'><span style='font-weight: bold'>Mouth: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][21]."</span><br><font>";
      }
   
      if (!empty($foodWines[$x][23]) AND $foodWines[$x][23]!=0) {
       echo "<font size='+1'><span style='font-weight: bold'>Alcohol Content: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][23]."%</span><br><font>";
      }
  	if (!empty($foodWines[$x][34])) {
       echo "<font size='+1'><span style='font-weight: bold'>Ratings: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][34]."</span><br><font>";
      }
    
  	if (!empty($foodWines[$x][33])) {
       echo "<font size='+1'><text align='justify'><span style='font-weight: bold'>Why we like it: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][33]."</span><br><font>";
      }
   if($screenWidth > 10000){     
  	if (!empty($foodWines[$x][24])) {
       echo "<br><font size='+1'><span style='font-weight: bold'>Pairs with: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][24]."</span><br><font>";
      }
}

$pairsString="";
   
if($screenWidth > 500){
   
     for($y = 51; $y <= 55; $y++){

      if(!empty($foodWines[$x][$y])){
      
      if($y==51){
            $pairsString = substr($foodWines[$x][$y], (strlen($foodWines[$x][$y])-2)*-1);
      }
      else{
            $pairsString=$pairsString.", ".substr($foodWines[$x][$y], (strlen($foodWines[$x][$y])-2)*-1);
      }
      }      
      }
   if(!empty($pairsString)){
    echo "<br><font size='+1'><span style='font-weight: bold'>Pairs with:&nbsp;</span><span style='font-weight: normal'>".$pairsString."</span><br><font>";
   }
   }

 


if($screenWidth > 500){    
       if (!empty($foodWines[$x][103])) {
       $winemakertext="From the Winemaker:";         

//$winemakertext="From the Winemaker (".$foodWines[$x][103]."):";
        }
        else{
        $winemakertext="From the Winemaker:";
         }
  	if (!empty($foodWines[$x][32])) {
       
        //add in carriage returns because this does not seem to do it automatically
        $newstring = nl2br($foodWines[$x][32]); 

echo "<br><font size='+1'><span style='font-weight: bold'>",$winemakertext." &nbsp;</span><span style='font-weight: normal'>".$newstring."</span><br><font>";
      }

if (!empty($foodWines[$x][103])) {
       echo "<br><font size='+1'><span style='font-weight: bold'>Winemaker: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][103]."</span><font>";
      }
if (!empty($foodWines[$x][25])) {
       echo "<br><font size='+1'><span style='font-weight: bold'>Cases Produced: &nbsp;</span><span style='font-weight: normal'>".number_format($foodWines[$x][25])."</span><font>";
      }

if (!empty($foodWines[$x][107])) {
       echo "<br><font size='+1'><span style='font-weight: bold'>Winemaking / Barrels / Aging: &nbsp;</span><span style='font-weight: normal'>".nl2br($foodWines[$x][107])."</span><font>";
      }

if (!empty($foodWines[$x][104])) {
       echo "<br><font size='+1'><span style='font-weight: bold'>Average Yield: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][104]."</span><font>";
      }


if(!empty($foodWines[$x][106]) AND !empty($foodWines[$x][108])){
       echo "<br><font size='+1'><span style='font-weight: bold'>pH: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][106]."</span><span style='font-weight: bold'>&emsp; Total Acidity: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][108]."g/L</span> <font>";

}

elseif (!empty($foodWines[$x][106])) {
       echo "<br><font size='+1'><span style='font-weight: bold'>pH: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][106]."</span> <font>";
      }
elseif (!empty($foodWines[$x][108])) {
       echo "<br><font size='+1'><span style='font-weight: bold'>Total Acidity: &nbsp;</span><span style='font-weight: normal'>".$foodWines[$x][108]."g/L</span> <font>";
      }










  	if (!empty($foodWines[$x][66])) {
       echo "<br><font size='+1'><span style='font-weight: bold'>Winery Website: &nbsp;</span><span style='font-weight: normal'><a href='".$foodWines[$x][66]."'  
 target='_blank'>Click Here</a> </span><br><font>";
      }



}
      echo "<br><font size='-1'><span style='font-weight: normal'>All prices are in Belize Dollars and include 12.5% GST (Sales Tax). BZ$2 = US$1</span><font>";
      
   if($foodWines[$x][18]<3 AND $foodWines[$x][39]==0){
   if($foodWines[$x][18]==1){
echo "<br><font size='-1'><span style='font-weight: bold'>Only 1 Bottle Left in Our Cellar</span><font>";
}
else{
    echo "<br><font size='-1'><span style='font-weight: bold'>Only 2 Bottles Left in Our Cellar</span><font>";
}
}

if(!empty($foodWines[$x][67]))  {

echo "<br><font size='-1'><span style='font-weight: bold'>".$foodWines[$x][67]."</span><font>";
}   

echo "</td>";

    // echo "<td valign='top' align='left'>";
    // echo do_shortcode($nextWine);
    // echo "</td>";

  	echo "</tr>";
	echo "</table>";
     echo "<br><br><br><br>";
}
echo "</body>";
}
else{
echo "<h5>No Matching Results for  '".$search."'</h5>";
}