#!/usr/local/bin/perl
use CGI qw(:cgi-lib);
use XML::Simple;
use Data::Dumper;
use Image::Size;

&ReadParse;
$portfolio = $in{'portfolio'};											# Read in the requested portfolio

$portfolio_xml = XMLin("portfolio.xml", forcearray=>1);					# Parse the appropriate XML file

print &PrintHeader;														# Head up the HTML

print `cat header.html`;												# HTML Header

print	'<table width="750" border="0" cellspacing="0" cellpadding="0"><tr valign="middle">'
	.	'<td align="left" class="header" width="33%"><b>' . $portfolio_xml->{category}->{$portfolio}->{description}[0] . ' Portfolio</b></td>'
	.	'<td align="center" class="header_right" width="34%">';
print	$portfolio_xml->{category}->{$portfolio}->{note}[0] if $portfolio_xml->{category}->{$portfolio}->{note}[0];
print	'</td>'
	.	'<td align="right" class="header_right" width="33%">Click on images to enlarge</td>'
	.	'</tr></table><br>'
	. 	'<table width="100%" cellspacing="0" cellpadding="2" border="0" class="portfolio">'
	.	'<tr valign="top" align="center">';
	

for ($i = 0; $portfolio_xml->{category}->{$portfolio}->{item}[$i]->{path}; $i++) {
	($image_width,$image_height) = imgsize("../images/portfolio/$portfolio/$portfolio_xml->{category}->{$portfolio}->{item}[$i]->{path}_small.jpg");
	
	if ($image_height > 127) { $image_height = 127; $image_width = ''; } # IMAGE HEIGHT COMPENSATOR...PLEASE DON'T LEAVE ME IN FOREVER

	if ($image_height != 0) {	$vspace = int((141 - $image_height) / 2); }
	else { $vspace = ""; }


	print	'<td class="cell">'			# Cell
		.	'<a href="/cgi-local/enlarge.cgi?' . "portfolio=$portfolio&item=$i". '" class="portfolio">'
		.	'<img src="/images/portfolio/' . "$portfolio/" . $portfolio_xml->{category}->{$portfolio}->{item}[$i]->{path} . '_small.jpg" width="' . $image_width . '" height="' . $image_height . '" vspace="' . $vspace . '" border="0"><br>'
		.	'<b>' . $portfolio_xml->{category}->{$portfolio}->{item}[$i]->{project}[0] . '</b><br>' . "\n";
	
	print $portfolio_xml->{category}->{$portfolio}->{item}[$i]->{architect}[0] if $portfolio_xml->{category}->{$portfolio}->{item}[$i]->{architect}[0];
	print '</a></td>';							

	if ($i % 4 == 3 || !$portfolio_xml->{category}->{$portfolio}->{item}[$i + 1]->{path}) {												# End of Line or Last Image
		print '</tr>';
		if ($portfolio_xml->{category}->{$portfolio}->{item}[$i + 1]->{path}) {															# If just a line end (more images to come)
			print '<tr><td colspan="7"><img src="/images/blank.gif" height="15" width="744"></td></tr><tr valign="top" align="center">';	# Spacer
		}
	}
	else {	print '<td><img src="/images/blank.gif" width="15"></td>'; }
}

print '</table>';

print `cat footer.html`;												# HTML Footer