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



$connection = mysql_connect('localhost', 'root', 'admin');
mysql_query("SET NAMES 'utf8'");
mysql_select_db('WineList'); 
mysqli_set_charset($connection, 'utf8');

if($listType=="byVarietal"){
      $sql="SELECT * FROM Wines WHERE Category = '".$listName."' AND QtyInStock>0 AND WineType != 'Glass' ORDER BY WinePrice";
}
elseif($listType=="byCountry"){

      $sql="SELECT * FROM Wines WHERE WineCountry = '".$listName."' AND QtyInStock>0 AND WineType != 'Glass' 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 Wines WHERE WineType = 'White' AND QtyInStock>0 ORDER BY WineName";
      }
       elseif($listName=="WhitePrice"){
            $sql="SELECT * FROM Wines WHERE WineType = 'White' AND QtyInStock>0 ORDER BY WinePrice";
      }
      elseif($listName=="RedsAZ"){
            $sql="SELECT * FROM Wines WHERE WineType = 'Red' AND QtyInStock>0 ORDER BY WineName";
      }
      elseif($listName=="RedPrice"){
            $sql="SELECT * FROM Wines WHERE WineType = 'Red' AND QtyInStock>0 ORDER BY WinePrice";
      }
      else{
      
            $sql="SELECT * FROM Wines WHERE (WineProfile1 = '".$listName."' OR WineProfile2 = '".$listName."') AND QtyInStock>0 AND WineType != 'Glass' ORDER BY WinePrice";

      } 
}

// Now read all the wines using $sql and determine which is the previous and which is the next. These will be the url for the prev and 
// next buttons. Also need to add the listType and listName variables to the url so that this page can know how and what to display.
       $IDarray=array();
       $wineCount=0;
      
       // echo $sql;
       $records1=mysql_query($sql);
       while ($row1 = mysql_fetch_assoc($records1)) {
             
             $wineCount = $wineCount+1;
             $IDarray[$wineCount]=$row1['ID'];
                  if($IDarray[$wineCount]==$variable){
                          $offset= $wineCount;        // need to know how many to go back in browser history
                  }
             // echo $IDarray[$wineCount].", ".$variable."<br>";
             

       }
       $offset=-1*($wineCount-$offset);

     // echo $wineCount;
       
      for ($x = 1; $x <= $wineCount; $x++) {

         if($IDarray[$x] == $variable){
		if($x == 1){
	
		$prevID=0;
		$currentID=$array[$x];
		$nextID=$IDarray[$x+1];
		}
		elseif($x == $winecount){
	
		$prevID=$IDarray[$x-1];
		$currentID=$array[$x];
		$nextID=0;
		}
		else{
		$prevID=$IDarray[$x-1];
		$currentID=$array[$x];
		$nextID=$IDarray[$x+1];
	
		}
	}

      }
    

      if($linkCount==3){
            // *disabled for now*  the first time this page is called we load all the urls for every wine in this profile, varietal or country
            // into the browser history then jump to the page for the selected wine
            // this allows the slider to scroll through all pages.
            // the first page in the history will be the listing page that called this so you can swipe back to that.

             for ($x = 1; $x <= $wineCount; $x++) {
               
                   $url="http://192.168.104.254/wordpress/single-item?WineID=".$IDarray[$x]."&listType=".$listType."&listName=".$listName."&linkCount=2";
                   echo "<script>push('".$url."'); </script>";
                  // echo "<script>loadURL($url); </script>";
            }

           
      }

 
// code here to assign $prevID and $nextID to the prev and next buttons
// href='http://192.168.104.254/wordpress/single-item?WineID=".$wines['ID']."&listType=byProfile&listName=".$variable."'

      // echo "<table width='100%' cellpadding='10'>";
  	
       // echo "<col width='33%'>";
       // echo "<col width='33%'>";
  //	echo "<col width='34%'>";
  //	echo"<tr><td align='left'>"; 
   
      if($prevID !=0){
      $prevWine = "[maxbutton id='1' url='http://192.168.104.254/wordpress/single-item?WineID=".$prevID."&listType=".$listType."&listName=".$listName."']";
    //  echo do_shortcode($prevWine);
     
        

      // echo "<a class='button' ".$prevWine.">Previous</a>";
      }
     // echo "</td><td align='center'>";
      
      
      $homeWine = "[maxbutton id='3' url='http://192.168.104.254/wordpress/".$listType."/?Listing=".$listName."']";
     // echo do_shortcode($homeWine);
     // echo "</td><td align='right'>";

      if($nextID !=0){
      $nextWine = "[maxbutton id='7' url='http://192.168.104.254/wordpress/single-item?WineID=".$nextID."&listType=".$listType."&listName=".$listName."']";
      
    
       //  echo do_shortcode($nextWine);
      
      }
     // echo"</td></tr></table>";
     
     
      
      


	$sql="SELECT * FROM Wines WHERE ID='".$variable."'"; 
	$records=mysql_query($sql);

while ($row = mysql_fetch_assoc($records)) {

  	echo "<table cellpadding='40'>";
  	echo "<col width='3%'>";
        echo "<col width='30%'>";
        echo "<col width='3%'>";
  	echo "<col width='61%'>";
        echo "<col width='3%'>";

  	// echo "<col height='100%'>";
   

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

  	echo "<td valign='top' align='center'><font size='+2'><b>$".$row['WinePrice']."</b></font><br><br><img src='".$row['Image1']."' alt='WineImage'/><br><br>";
        echo do_shortcode($homeWine);
        echo "</td>";
  	echo "<td></td>";
  	 if (strpos($row['WineName'], $row['WineVarietal']) !== false or strpos($row['WineName'], 'Blend') !== false){

              $varietalText="";
        }
        else{
               $varietalText=$row['WineVarietal'];
         }
     
         if($row['WineVintage'] != 0){
  	         echo "<td valign='top'><font size='+1'><b>".$row['WineName']."&emsp; ".$row['WineVintage']."</b></font><br><font size='+0'>".$varietalText."</font>";
        }
        else{
               echo "<td valign='top'><font size='+1'><b>".$row['WineName']."</b></font><br><font size='+0'>".$varietalText."</font>";
         }

        if($varietalText!=""){
               echo "<br>";
         }
   
        // if($row['WineVintage'] != 0){
        //      echo "<font size='+1'><b>".$row['WineVintage']."</b></font><br>";
         // }
         echo "<font size='+1'><b>".$row['WineCountry'].">".$row['WineRegion']."</b></font><br><br>";
    
    
    if (!empty($row['Eyes'])) {
       echo "<font size='+1'><b>Eyes: &nbsp;</b>".$row['Eyes']."</b><br><font>";
      }
  
  	if (!empty($row['Nose'])) {
       echo "<font size='+1'><b>Nose: &nbsp;</b>".$row['Nose']."</b><br><font>";
      }
  
  	if (!empty($row['Mouth'])) {
       echo "<font size='+1'><b>Mouth: &nbsp;</b>".$row['Mouth']."</b><br><font>";
      }
   
  	if (!empty($row['Desc3'])) {
       echo "<font size='+1'><b>Ratings: &nbsp;</b>".$row['Desc3']."</b><br><font>";
      }
    
  	if (!empty($row['Desc2'])) {
       echo "<font size='+1'><b>Why we like it: &nbsp;</b>".$row['Desc2']."</b><br><font>";
      }
    
  	if (!empty($row['PairingSuggestion'])) {
       echo "<br><font size='+1'><b>Pairs with: &nbsp;</b>".$row['PairingSuggestion']."</b><br><font>";
      }
    
  	if (!empty($row['Desc1'])) {
       echo "<br><font size='+1'><b>From the Winemaker: &nbsp;</b>".$row['Desc1']."</b><br><font>";
      }
    
    echo "</td>";
    echo "<td valign='top' align='left'>";
    echo do_shortcode($nextWine);
    echo "</td>";

  	echo "</tr>";
	echo "</table>";
    
}

if($linkCount==3){
 echo "<script>window.history.go($offset); </script>";
}


