#!/usr/bin/perl
$eol = "\n";
$cat = $ENV{'QUERY_STRING'};

if($cat =~ /^ld_/) {
	print "Location: http://www.seanbeach.com/portfolio_ld.shtml\n\n";
} else {
	print "Location: http://www.seanbeach.com/portfolio_photo.shtml\n\n";
}

sub getData {
	my @r;
	open(DATA,'img_portfolio/'.$cat.'/'.$_[0].'.txt');
	@r = <DATA>; close(DATA); chomp(@r);
	return join("\n",@r);
}

if(-e 'img_portfolio/'.$cat) {
	$data{'heading'} = getData('heading');
	$data{'title'} = getData('title');
	$data{'subtitle'} = getData('subtitle');
	
	print "Content-type: text/html\n\n";
	print '<?xml version="1.0" encoding="UTF-8"?>'.$eol;
	print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.$eol;
	print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'.$eol;
	print '<head>'.$eol;
	print '<script type="text/javascript" src="js/mootools.js"></script>'.$eol;
	print '<script type="text/javascript" src="js/slimbox.js"></script>'.$eol;
	print '<link rel="stylesheet" href="slimbox.css" type="text/css" media="screen" />'.$eol;
	print '<link rel="stylesheet" href="style.css" media="all" />'.$eol;
	print '<title>'.$data{'title'}.' - SeanBeach.com</title>'.$eol;
	print '</head>'.$eol;
	print '<body>'.$eol;
	print '<center>'.$eol;
	print '<div id="head"><img src="images/logo.jpg" width="790" height="64" alt="www.SeanBeach.com" /><div id="navigation">'.$eol;
	print '<a href="index.shtml">What\'s New?</a>'.$eol;
	print '<a href="bio.shtml">Biography</a>'.$eol;
	print '<a href="resume.shtml">Resume</a>'.$eol;
	print '<a href="design.shtml" style="background-image:url(images/arrow.jpg); color:white;">Design Portfolio</a>'.$eol;
	print '<a href="contact.shtml">Contact</a>'.$eol;
	print '</div>'.$eol;
	print '</div>'.$eol;
	print '<div id="content">'.$eol;
	
	
	
	print '<div class="heading">'.$data{'heading'}.'</div>'.$eol;
	print '<div id="portfolio">'.$eol;
	print '<div class="title">'.$data{'title'}.'</div>'.$eol;
	print '<div class="subtitle">'.$data{'subtitle'}.'</div>'.$eol;
	
	#load images
	opendir(FILES,'img_portfolio/'.$cat);
	@files = readdir(FILES); closedir(FILES);
	foreach $file (@files) { if($file =~ /.jpg$/i || $file =~ /.png$/i) { push(@images,$file); } }
	
	#figure out how many columns
	$cols = 0;
	if(scalar(@images)%5 == 0)							{ $cols = 5; }
	elsif(scalar(@images)%4 == 0)						{ $cols = 4; }
	elsif((scalar(@images)%5) >= (scalar(@images)%4))	{ $cols = 5; }
	else												{ $cols = 4; }
	if(scalar(@images) >= 20)							{ $cols = 5; }
	
	#print images
	$i = 1;
	foreach $img (@images) {
		if($i == 1)		{ print '<div class="row">'.$eol; $needsCloseTag = 1; }
		print '<span class="item">';
		print '<a href="img_portfolio/'.$cat.'/'.$img.'" rel="lightbox-'.$cat.'" title="'.$data{'title'}.'">';
		print '<img src="img_portfolio/'.$cat.'/thumbs/'.$img.'"></a></span> &nbsp;'.$eol;
		if($i == $cols)	{ print '</div>'; $i=0; $needsCloseTag=0; }
		$i++;
	}
	if($needsCloseTag) { print '</div>'.$eol; }
	print '</div>'.$eol;
	
#	print '<br/>'.$eol;
#	print '<div id="ads">'.$eol;
#	print '<script type="text/javascript"><!--'.$eol;
#	print 'google_ad_client = "pub-4098611735642060";'.$eol;
#	print '/* SeanBeach Portfolio 728x15, created 4/3/09 */'.$eol;
#	print 'google_ad_slot = "9745368748";'.$eol;
#	print 'google_ad_width = 728;'.$eol;
#	print 'google_ad_height = 15;'.$eol;
#	print '//-->'.$eol;
#	print '</script>'.$eol;
#	print '<script type="text/javascript"'.$eol;
#	print 'src="http://pagead2.googlesyndication.com/pagead/show_ads.js">'.$eol;
#	print '</script>'.$eol;
#	print '</div>'.$eol;
	
	
	print '</div>'.$eol;
	print '<div id="footer"> &copy; Copyright 2007-2009 Sean Beach</div>'.$eol;
	print '</center>'.$eol;

	print '<script type="text/javascript">'.$eol;
	print 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");'.$eol;
	print 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\''.
		' type=\'text/javascript\'%3E%3C/script%3E"));'.$eol;
	print '</script>'.$eol;
	print '<script type="text/javascript">'.$eol;
	print 'try {'.$eol;
	print 'var pageTracker = _gat._getTracker("UA-8214179-1");'.$eol;
	print 'pageTracker._trackPageview();'.$eol;
	print '} catch(err) {}</script>'.$eol;
	
	print '</body>'.$eol;
	print '</html>'.$eol;
} else { print "Location: http://www.seanbeach.com/design.shtml\n\n"; }

