#!/usr/bin/python

print "Content-type: text/html\n\n"


from string import *
from re import *
from time import *
import os
import cgi

form = cgi.FieldStorage()

# Header
print """
<HTML>
<HEAD>
<META NAME="Author" CONTENT="Danny Adams">
<META NAME="Keywords" CONTENT="Photos">
<META NAME="Descriptions" CONTENT="Danny's Photo Album">
<AUTHOR>Danny Adams</AUTHOR>
<TITLE>Danny Adams' Photo Album</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
"""

# Defintions

this_dir = "/home/dda/public_html/album/"
this_url = "index.py"
album_file = "album.dat"


# Informational
cats = {}
picts = {}
viewing_cat = ""

# Show Image
def show_image( image ) :
  global viewing_cat

  for k in picts.keys() :
    l = picts[k]
    indx = -1
    for i in l :
      indx = indx + 1
      if i[0] == image :
        viewing_cat = k
        print "<H1>" + k +"</H1>"
        print "<CENTER><A HREF=\"" + this_url + "?cat=" + k + "\"></A>"
        print "<IMG SRC=\"" + image + "\" BORDER=0></A><BR>"
        print i[1] + "</CENTER><P>"

        print "<TABLE BORDER=0 WIDTH=\"100%\"><TR>"

        if indx > 0 :
          name = sub(".jpg", "", l[indx-1][0] )
          
          print "<TD ALIGN=\"LEFT\"><A HREF=\"" + this_url + "?image=" + l[indx-1][0] + "\">PREV</A><BR>"
          print "<A HREF=\"" + this_url + "?image=" + l[indx-1][0] + "\"><IMG SRC=\"small.py?" + name + "\" BORDER=0></A></TD>"


        if indx < len(l) -1 : 
          name = sub(".jpg", "", l[indx+1][0] )
          print "<TD ALIGN=\"RIGHT\"><A HREF=\"" + this_url + "?image=" + l[indx+1][0] + "\">NEXT</A><BR>"
          print "<A HREF=\"" + this_url + "?image=" + l[indx+1][0] + "\"><IMG SRC=\"small.py?" + name + "\" BORDER=0></A></TD>"


        print "</TR></TABLE>"


# List Images In Cat
def list_images( cat ) :
  
  print "<H1>" + cat + "</H1>"
 
  # We make lots of 3 column tables so render happens faster.
  count = 0
  for image in picts[cat] :
    count = count + 1
    if count % 3  == 1 : print "<TABLE WIDTH=\"100%\" BORDER=0><TR>",
   
    name = sub(".jpg", "", image[0])

    print "<TD ALIGN=\"CENTER\" WIDTH=\"33%\" VALIGN=\"TOP\"><A HREF=\"" + this_url + "?image=" + image[0] + "\">", 
    print "<IMG SRC=\"tiny.py?" + name + "\" BORDER=0>",
    print "</A><BR>", str(count), ". ",image[1] , "</TD>"

    if count % 3  == 0: print "</TR><TR><TD>&nbsp;</TD></TR></TABLE>"   

  # If there's cells left, draw it
  if count % 3 != 0 : 
    print "<TD WIDTH=\"33%\"></TD>" * ( 3 - ( count % 3 ) ), "</TR></TABLE>"




# Print all the categories
def print_categories() :

  print "<H1>Danny Adams' Photo Album</H1>"
  print "The following sections are in the photo album:<P>"
  print "<UL>"

  list = map( None, cats.keys() );
  list.sort()
  for name in list : 
    print "<LI><A HREF=\"" + this_url +"?cat=" + name + "\">" + name + "</A>&nbsp; (<EM>", cats[name],"</EM> pictures )"

  print "</UL>"


# How to process lines in the album
def process( line ) :

  line = line[ 0:len(line) - 1]  # Remove newlines
  x = split( "\|", line, 0)


  file = ""
  category = "Unfiled"
  description = "No Description"

  if len(x) > 0 and len( x[0] ) > 0 :  file         = x[0];  
  if len(x) > 1 and len( x[1] ) > 0 :  category     = x[1];  
  if len(x) > 2 and len( x[2] ) > 0 :  description  = x[2];  

  # Add the categories to the list
  if len(file) > 0 :
    if cats.has_key(category) :
      cats[category] = cats[category] + 1
      picts[category].append( (file, description) )
    else :
      cats[category] = 1
      picts[category] = [ (file, description) ]


# Process the album
album = open( album_file, "r" )

# Read the album into memory
while 1:
  line = album.readline()  # Get a line
  if not line : break      # EOF?  We're done

  process( line )

album.close()              # Close



# Determine what to do


if form.has_key( "cat" ) :
  list_images( form['cat'].value )
elif form.has_key( "image" ) :
  show_image( form['image'].value )
else :
  print_categories()





# Trailer
print """
<P><HR>
<TABLE BORDER=0 WIDTH="100%">
<TR>
"""

if form.has_key("cat") or form.has_key("image"):
  print "<TD ALIGN=\"LEFT\">View <A HREF=\"/~dda/album\">Album Sections</A></TD>"

if len(viewing_cat) > 0 :
  print "<TD ALIGN=\"CENTER\">View other pictures in <A HREF=\"" + this_url + "?cat=" + viewing_cat + "\">" + viewing_cat + "</A></TD>"

print "<TD ALIGN=\"RIGHT\">Return to <A HREF=\"/~dda/\">Danny Adams' Home Page</A></TD>"

print """
</TR>
</TABLE>

</BODY>
</HTML>
"""

# djpeg -scale 1/4 <$f | cjpeg >`basename $f .jpg`-small.jpg
