// Lightbox 
// Implements a 'lightbox' feature -  a list of image ids stored
// in a session cookie.
//
// Copyright (c) Mark A. Brown 2007


function LightBox() {
	this.COOKIENAME = 'markb-photo-lightbox';
	this.enabled    = testPersistentCookie();
	this.imageid     = new Array();	// store images in this array
	if (this.enabled)
		this.initialise();
}


// initialise: read the lightbox state
LightBox.prototype.initialise=function()
{
	// Read the contents of the session cookie into
	// the imageid array. (Creates a new session cookie
	// if one did not already exist.)
	//
	this.readCookie();
}


// read the lightbox session cookie contents into the imageid array.
// Creates a new session cookie if there was not one already.
LightBox.prototype.readCookie=function() {
	var vals = getCookieValue(this.COOKIENAME);
	this.readSerialised(vals);
	if (!vals)
		writePersistentCookie(this.COOKIENAME, '', "days", 7);
}


// unserialise the lightbox values stored in the string s
// into the imageid array
LightBox.prototype.readSerialised=function(s) {
	if (s)
		this.imageid = s.split(':');
	else
		this.imageid.length = 0;
}


// write the contents of the image array into the lightbox 
// session cookie.
LightBox.prototype.writeCookie=function() {
	writePersistentCookie(this.COOKIENAME, this.serialise(), "days", 7);
}


// serialise the lightbox contents as a colon-separated string
LightBox.prototype.serialise=function() {
	var vals = new String('');
	var n = this.imageid.length;

	if (n) {
		vals += this.imageid[0];
		for (var i=1; i<this.imageid.length; i++)
			vals += ':' + this.imageid[i];
	}
	return vals;
}


// add an image to the lightbox
// Returns true if the image was added.
LightBox.prototype.addImage=function(imgid) {
	// First check for duplicate entries 
	for (var i in this.imageid) {
		if (imgid == this.imageid[i]) return false;
	}
	// Check not too many images on the lightbox
	if (this.numberOfImages() >= 40) {
		alert("Too many images on the lightbox!");
		return false;
	}
	this.imageid[this.imageid.length] = imgid;
	this.writeCookie();
	return true;
}


// remove an image from the lightbox
LightBox.prototype.delImage=function(imgid) {
	var n = this.imageid.length;
	for (var i=0; i<n; i++) {
		if (imgid == this.imageid[i]) {
			if (Array.prototype.splice) {
				this.imageid.splice(i, 1);
			}
			else {
				// Swap last entry into this location
				// if not already at last entry. Then,
				// discard last element.
				if (i != n-1)
					this.imageid[i] = this.imageid[n-1];
				this.imageid.length--;	
			}
			this.writeCookie();
			return;
		}
	}
	// no match
}


// returns true if the image is on the lightbox, otherwise returns 0.
LightBox.prototype.isOnLightBox=function(imgid) {
	for (var i in this.imageid) {
		if (imgid == this.imageid[i]) return 1;
	}
	return 0;
}

// returns number of images on lightbox
LightBox.prototype.numberOfImages=function() {
	return this.imageid.length;
}

// returns a copy of the imageid array
LightBox.prototype.getImages = function() {
	var n = new Array();
	n = this.imageid.slice();
	return n;
}


/* END */
