<?php
# PHP file written by Justin Goins
# Created for http://www.oakleafwoodworking.com
# Authored on Dec 17, 2007
# Script purpose:
# Display furniture photo album
# This script will either show all available pictures or (if the pid is set)
# will show a close up of the picture corresponding to the pid

//Create a var or two
$index = 0;
$page_title = "Online Furniture Gallery";
$photo_url = null;

//Standard template:
require_once ($_SERVER['DOCUMENT_ROOT'] . '/php/config.php');
require_once ($error_handler);
include ($header);
require_once ($db_connect);

if (isset($_GET['pid'])){
	$pid = (int) $_GET['pid']; //Cast the pid into an integer
	if ($pid <= 0){
		trigger_error("Invalid photo ID number provided!\n<br />");
		include ($footer);
		die();
	} else { 
		//We need to proceed with the webpage displaying a closeup of the photo requested...
		$query = "SELECT shortName, altText, description FROM furniturePhotos WHERE photoId=$pid";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$photo_url = FURNITURE_QUALITY_PATH . $row['shortName'] . '.jpg';
		echo '<table cellpadding="2" cellspacing="2" border="0" width="775" bgcolor="#FFFFFF">';
		echo '<tbody><tr>';
		echo "\n" . '<td valign="middle" align="center">';
		echo '<p>';
		echo '<font face="Helvetica, Arial, sans-serif" color="#006633"><big><b>';
		echo $row['description'];
		echo '</b></big></font></p>';
		echo "\n" . ' ' . "\n"; //New line
		//Determine the appropriate size for the picture
		list($width, $height) = getimagesize($root . $photo_url);
		if($height > $width) {
			$displaywidth = 450;
		} else {
			$displaywidth = 650;
		}
		echo '<p>' . '<img style="border: 2px solid; width: ' . $displaywidth . 'px;" ' . 'alt="' . $row['altText'] . '" ' . 'src="' . $photo_url . '"> </p> ';
		echo "\n" . ' ' . "\n"; //New line
		//Add link back to the original album
		echo '<a href="' . $_SERVER['PHP_SELF'] . '">';
		echo '<font face="Helvetica, Arial, sans-serif" color="#006633">';
		echo '<big>Clear here to return to the photo album</big>';
		echo '</font>' . '</a>';
		echo "\n" . ' ' . "\n"; //New line
		echo '</tr></tbody></table>';
	}
} else {

//If we're here, then the pid was not set
//We need to show all the pictures
//Our chore is to create a dynamic table

//Start by getting our query results
$query = "SELECT photoId, shortName, altText, description, hasPreview FROM furniturePhotos ORDER BY position ASC";
$result = mysql_query($query);
$num_of_photos = mysql_num_rows($result);
$mod_answer = $num_of_photos % 3;
if($mod_answer == 2){
	$howmanytimes = 1;
} elseif($mod_answer == 1){
	$howmanytimes = 2; 
} else{
    $howmanytimes = 0;
}

//Give instructions
echo '<table style="background-color: rgb(255, 255, 255); width: 775px;" border="0" cellpadding="1" cellspacing="1">';
echo '<tbody>';
echo '<tr>';
echo '<td height="121"><p align="left"><font color="#006633" face="Helvetica, Arial, sans-serif"><big><big> &nbsp; &nbsp; &nbsp; &nbsp; The furniture gallery contains pictures of hutches, tables, beds, nightstands and many other custom pieces of work. For a closer look, just click on any particular preview. If you would like to view the photo gallery containing kitchen and bathroom cabinets, just <a href="/pages/kitchenbath.php">click here</a>.</big></big></font></p></td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';

//Create the table
echo '<table style="background-color: rgb(255, 255, 255); width: 775px;" border="0" cellpadding="2" cellspacing="2">';
    echo '<tbody>';
    echo '<tr>';
    while($row = mysql_fetch_array($result)){
    	echo "\n" . ' ';
    	echo "\n";
    	echo '<td style="text-align: center;" valign="middle" width="33%">';
    	echo "\n";
    	
    	#Determine the URL to our photo
    	if ($row['hasPreview'] == 1){
    		//We will use the preview picture for the photo gallery
    		$photo_url = FURNITURE_PREVIEW_PATH . $row['shortName'] . '.jpg';
    	} else {
    		//Use the full quality picture
    		$photo_url = FURNITURE_QUALITY_PATH . $row['shortName'] . '.jpg';
    	}
    	
    	#Set our alternative (mouse over) text for the photo
    	//If the altText field is null, we use the description
    	if ($row['altText']){
    		$alt_text = $row['altText']; 
    	} else {
    		$alt_text = $row['description'];
    	}
    	
    	#Create the cell's contents...
    	echo '<a href="' . $_SERVER['PHP_SELF'] . "?pid={$row['photoId']}" . '">';
    	echo "\n";
    	echo '<img style="border: 2px solid; ';
    	echo 'width: 200' . 'px;" ';
    	echo 'alt="' . $alt_text . '" ';
    	echo 'src="'. $photo_url . '"></a> ';
    	echo "\n"; //new line
    	echo '<br>';
    	echo "\n";
    	echo '<b>' . $row['description'] . '</b>';
    	echo '</td>';
    	
    	if(($index+1)%3 == 0){ //Create new rows as needed
    		    echo "\n" . ' ' . "\n"; //Blank line
    		echo '</tr>';
    		echo '<tr>';
    	}
    	
    	//Increase our counter
        $index++;
    }

    for ($i=0; $i<$howmanytimes; $i++){
    	echo "\n" . ' ' . "\n"; //Blank line
        echo '<td style="text-align: center;" valign="middle" width="33%">' . "\n" . '<font color="#006633" face="Helvetica, Arial, sans-serif"></font>' . "\n" . '</td>';
    }

    //Close our table
    echo "\n" . ' ' . "\n"; //Blank line
    echo '</tr>';
    echo "\n";
    echo '</tbody>';
    echo "\n";
    echo '</table>';
}
    mysql_close($dbc);
    include ($footer);
    
    
?>