/* index of displayed Photograph object */
var disp_index;

/*
 * Include a javascript file despite any caching. The ?... argument
 * is always different and thus the browser always loads a fresh copy.
 * (The ?... is completely ignored by the include files.)
 */
function
force_include(path)
{
	var d = new Date(); /* unique timestamp */

	/* read in the file with a random argument appended to the end */
	document.write("<script type=\"text/javascript\" " +
	    "src=\"" + path + "?" + d.getTime() + "\"></script>");
}

/*
 * Constructor method to encapsulate per-photo information.
 *
 */
function
Photograph(file, comment)
{
	this.file = file;
	this.comment=comment;
}

function
Album(maxindex, dir, pattern)
{
	var i;
	this.max  = maxindex;
	this.dir = dir;
	this.pattern = pattern;
	this.photos = new Array();

	for (i = 1; i <= maxindex; i++) {
		this.photos[i] = new Photograph(dir + "/" + pattern + i + ".jpg",
			pattern + i + ".jpg");
	}
}

/*
 * Hide the current photo and display the new one.
 *
 * Using CSS, if the photo is first or last, certain buttons are
 * made to appear disabled (using class "nolink").
 */
function
update(index)
{
	var photo;
	if (index < 1 || index > CurrAlbum.max) {
		return;
	}

	photo = CurrAlbum.photos[index];
	
	document.thephoto.src = photo.file;
	if (photo.comment) {
		document.getElementById("comment").innerHTML = photo.comment;
	} else {
		document.getElementById("comment").innerHTML = "";
	}
}

/*
 * Four navigation functions called by form elements.
 */
function next() { 
	
	if (disp_index < CurrAlbum.max)
		disp_index++; 
	else
		disp_index = 1;
	update(disp_index);
}

function prev() { 
	if (disp_index > 1) 
		disp_index--;
	else
		disp_index = CurrAlbum.max;
	update(disp_index);
} 

function new_album(a) {
	if (Albums[a]) {
		CurrAlbum = Albums[a]; 
		disp_index =  1;
		update(disp_index); 
	}
}

/*
 * Initialize the data structures and display the first image.
 * Note that daily_init() must not be set to an onload event handler,
 * because Microsoft browsers don't consistently support self.onload().
 */
function
initialize_albums()
{
	var re, res;

	/* by default, display the most recent image */
	disp_index = 1;
	CurrAlbum = GreatWall; 

	/*
	 * This regexp matches arguments passed via URL, of
	 * the form http://site.domain.com/dir/dir/page?Album/##
	 *                                              ^^^^^^^
	 */
	re = /\?([a-zA-Z]+)\/([0-9]+)/
	res = document.URL.match(re);
	if (res) {
		if (Albums[res[1]]) {
			A = Albums[res[1]];	
			if (res[2] >=1 && res[2] <= A.max) {
				CurrAlbum = A;
				disp_index = res[2];
			}
		}
	}

	update(disp_index);
}
