# ecp gallery module, © 2004 ecp, douglas savitsky
# protected by the GNU General Public License
# there is no warranty provided with this code

_image_db = {'1000' : {'name' : 'e-coli 1', 'size' : 10},
		'2000' : {'name' : 'e-coli 2', 'size' : 14},
		'3000' : {'name' : 'e-coli 3', 'size' : 5},
		'4000' : {'name' : 'greg johnson', 'size' : 11},
		'5000' : {'name' : 'cai xi silver', 'size' : 11},
		'6000' : {'name' : 'le xi', 'size' : 5},
		'7000' : {'name' : 'hua xi', 'size' : 8},
		'8000' : {'name' : 'aaron peterson', 'size' : 7}}

_path = '****'

def _get_copy(gl):
	if gl in ['4000', '5000', '6000', '7000', '8000']: return '/%s' % _image_db[gl]['name']
	else: return ''
		
def _get_link(i):
	s = str(i)
	if len(s)==1: return '00%s' % s
	else: return '0%s' % s
	
def _get_thumb(gl, i):
	l = list(gl)
	l[3] = '2'
	return '%s_%s.JPG' % (''.join(l), _get_link(i))
	
def index(req, img=None):
	l = []
	f = open(_path + '_head', 'r')
	s = f.read()
	f.close()
	l.append(s)
	l.append('<TD colspan=2 CLASS="BAR_TEXT" align="center" width=600><B>galleries</B> :: [')
	g = _image_db.keys()
	g.sort()
	l2 = []
	for i in g:
		l2.append('<A HREF="?img=%s_000" CLASS="BAR_TEXT">%s</A>' % (i, _image_db[i]['name']))
	l.append(', '.join(l2))
	l.append(']</TD><TR>')
	gl = '1000'
	if img:
		try:
			gl, im = img.split('_')
			if gl not in _image_db.keys():
				gl = '1000'
				raise RuntimeError, "Do'h!"
			if int(im) > _image_db[gl]['size'] + 1 or len(im) != 3:
				raise RuntimeError, "Do'h!"
			l.append('<TD ALIGN="center" VALIGN="TOP" width=500><IMG SRC="../../images/%s.JPG"></TD>' % img)
			l.append('<TD align="center" VALIGN="TOP" width=100>')
			for i in range(_image_db[gl]['size']):
				if i > 0:
					l.append('<A HREF="./?img=%s_%s">' % (gl, _get_link(i)))
					l.append('<IMG SRC="../../images/%s" BORDER=0><BR></A>' % _get_thumb(gl, i))
					l.append('<IMG SRC="../../images/spacer.gif"><BR>')
			l.append('</TD>')
		except:
			l.append('<TD COLSPAN=2 ALIGN="center"><IMG SRC="../../images/ecpsoftware_ico.jpg"></TD>')
	else:
		l.append('<TD COLSPAN=2 ALIGN="center"><IMG SRC="../../images/ecpsoftware_ico.jpg"></TD>')
	l.append('</TABLE>')
	l.append('<div align="center"><a href="../../__copy__" class="main_text"><b>© 2003 ecp%s</b></a><br>' % _get_copy(gl))
	l.append('<A HREF="./view_code" class="MAIN_TEXT">(view the gallery code)</a></div></html>')
	return '\n'.join(l)

def view_code():
	# a note ...
	# if you are viewing this code online, the replace functions will look like .replace('>', '>')
	# when in reality they look like .replace('>', '&gt;')
	# this is recursive, so the above line does not look like you are seeing either ;-)
	f = open(_path + 'gallery.py', 'r')
	l = f.readlines()
	f.close()
	return '<HTML><PRE>\n%s\n</PRE></HTML>' % ''.join([s.replace('<', '<').replace('>', '>').replace('****', '****') for s in l])