#!/usr/bin/python
#A simple image display program
#Copyright (C) 2006  Nicolas Kassis <nic.kassis@gmail.com>
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    

import os,cgi

image_root = "/images/albums/ctf/"

class Pic :
    def __init__(self, filename, name, width, height, desc, date):
        self.filename=filename
        self.name=name
        self.width=int(width)
        self.height=int(height)
        self.desc=desc
        self.date=date
    def getDescription(self):
        return name+" "+desc+" "+width+'x'+height
    def getSize(self):
        return width,height

def albumParserTXT(album_desc) :
    pics=[]
    for i in album_desc[1:]:
        
        if(i=='\n' or i[0]=='#'):
            pass
        else :
            i=i.split(':')
            new_pic=Pic(i[0],i[1],i[2],i[3],i[4],i[5])
            pics.append(new_pic)
    return pics       

album_desc=open("/var/www/html/images/albums/ctf/album.txt",'r').readlines()
title="Nic's little album"

print "Content-type: text/html\n"
#print important html stuff
print """
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head> 
<title>%s</title>
<link rel=stylesheet type=\"text/css\"
    href=\"/css/album.css\"
    title=\"image gallery\">

</head>
<body>
    <h1>Nic's little photo album</h1>
<div class=\'gallery\'>
"""%(title)

#Get list of pictures
pics = albumParserTXT(album_desc) 
pic_num = 0
for i in pics :
    height=i.height
    width=i.width
    if (i.width >= i.height) :
        if(width<=150) :
            pass
        else :
            conv_factor=150.0/width
            width=150
            height=int(conv_factor*height)
    else :
        if(height<=150) :
            pass
        else :
            conv_factor=150.0/height
            height=150
            width=int(conv_factor*width)

    pic_num+=1
    print """
    <div class=\'image\'> 
    <a href=%s><img width=\"%d\" height=\"%d\" SRC=\"%s\" alt=\"%s\"></a>
    <div class=\'imagedesc\'>%s</div>
    </div>""" %(image_root+i.filename,width,height,image_root+i.filename,i.desc, i.desc)
    
    
print "</div>"
print "</body>"
