#!/usr/bin/perl
$PHOTO_DIR="photo";
$MAX_HEIGHT=560;
$MAX_WIDTH=420;

#$MAX_HEIGHT=480;
#$MAX_WIDTH=360;
#$MAX_HEIGHT=640;
#$MAX_WIDTH=480;

@miniheight=("100","50","20","10");

sub getsize{
	require "./imgSz.pl";
	
	my $filename=$_[0];
	my $size = -s $filename;

	open(IN, "$filename");
	binmode(IN);
	read(IN, $buf, $size);
	close(IN);

	return &imgSz::szJpeg($buf);

}
sub setminiimage{
		my $ret="";
		my $dirname=$_[0];
		my $jpg=$_[1];
		my $tagind=$_[2];
		my $saishosaigo="";
		my $td_width="0";

		my $filename="$dirname/$jpg";
		(my $width,my $height)=&getsize("$filename");

		my $htmlname="./$jpg.html";
		$htmlname=~s/\.jpg//;

		$td_width=$miniheight[$tagind]*3/4;

		$width=int ($miniheight[$tagind] * ($width/$height) );
		$height=$miniheight[$tagind];
		if($width > $td_width ){
			$height=int ($td_width * ($height/$width) );
			$width=$td_width;
		}

		$ret=<<EOL;
		<td align="center" width="$td_width">
		<a href="$htmlname"><img height="$height" width="$width" 
		border="0" src="./$jpg"></a><br><font size="$size">$saishosaigo</font>
		</td>
EOL

		return $ret;
}
sub make_html{

	my $thisjpg=$_[0];
	my $thisnumber=$_[1];
	my $dirname=$PHOTO_DIR;
	my $jpgnum=$JPG_NUM;
	my @lefttags; my @righttags;
	my $colspan=0;
	my $left = "";
	my $right = "";
	my $ind = 0;


	my $filename="$dirname/$thisjpg";

	(my $width,my $height)=&getsize("$filename");

	if($height >$MAX_HEIGHT){
		$width=int ($MAX_HEIGHT * ($width/$height) );
		$height=$MAX_HEIGHT;
	}
	if($width >$MAX_WIDTH){
		$height=int ($MAX_WIDTH * ($height/$width) );
		$width=$MAX_WIDTH;
	}

	if($jpgnum==1){
		$colspan=1;

	}else{

		for($tagind =@miniheight-1 ; $tagind >=0 ; $tagind--){
			$ind=$thisnumber-$tagind - 1;
			if($ind<0){$ind = $ind + $jpgnum;}
			$lefttags[$tagind]=&setminiimage("$dirname","$JPGS[$ind]","$tagind");
			$left = $left . $lefttags[$tagind];
		}
		for($tagind =0 ; $tagind <@miniheight ; $tagind++){
			$ind=($thisnumber + $tagind + 1) % $jpgnum;
			$righttags[$tagind]=&setminiimage("$dirname","$JPGS[$ind]","$tagind");
			$right = $right . $righttags[$tagind];
		}

		$colspan=(@miniheight)*2+1;
	}

	$htmlname="./$PHOTO_DIR/$thisjpg.html";
	$htmlname=~s/\.jpg//;

	print "./$PHOTO_DIR/$thisjpg -> $htmlname\n";
	open(HTML,">$htmlname");
	require "./commonhtml.pl";

	print HTML &commonhtml::print_head("..");
	#print HTML "   <div id=\"photowaku\">";

	print HTML<<"EOL";
	<table>
		<tr height = "$MAX_HEIGHT">
			$left
			<td width = "$MAX_WIDTH" align="center">
				<a href ="./$thisjpg"><img src="$thisjpg" height="$height" width="$width"></a>
			</td>
			$right
		</tr>
	</table>
EOL
	#print HTML "    </div>";
	print HTML &commonhtml::print_tail("..");
	close(HTML);

}


$JPG_NUM=0;
opendir(DIR, './photo');
while (defined($file = readdir(DIR))) {
	if($file =~/jpg$/){
		$JPGS[$JPG_NUM]=$file;
		$JPG_NUM++;
	}
}
closedir(DIR);
$count=0;
opendir(DIR, './photo');
while (defined($file = readdir(DIR))) {
	if($file =~/jpg$/){
		&make_html("$file","$count");
		$count++;
	}
}
closedir(DIR);


