function PicturePopup( picturesPath ) {
	if ( !picturesPath )
		alert( "PicturePopup(): picturesPath is empty!" );
	this.picturesPath = picturesPath;
	
	this.containerobj = null;
	this.pictures = null;
	this.postId = null;
	this.pictureId = null;
}

PicturePopup.prototype.create = function( container ) {
	var picturePopup = this;
	
	this.close();
	
	this.containerObj = create( "DIV", container, "pictureContainer" );
	this.containerObj.onclick = function() {
		picturePopup.close();
	}
	
	var pageTopOffset;
	if ( document.documentElement.scrollTop != undefined )
		pageTopOffset = document.documentElement.scrollTop
	else if ( document.body.scrollTop != undefined )
		pageTopOffset = document.body.scrollTop
	else if ( window.pageYOffset != undefined )
		pageTopOffset = window.pageYOffset
	
	// Position the container relative to the page offset
	this.containerObj.style.top = pageTopOffset + "px";
	
	var div;
	
	// Translucent div
	div = create( "DIV", this.containerObj, "translucent" );
	
	// Arrows
	var arrow;
	
	arrow = create( "IMG", this.containerObj, "arrowLeft" );
	arrow.src = "images/pictureArrowLeft.png";
	arrow.onclick = function(e) {
		picturePopup.prevPicture(e);
	}
	//arrow.ondblclick = arrow.onclick;
	
	arrow = create( "IMG", this.containerObj, "arrowRight" );
	arrow.src = "images/pictureArrowRight.png";
	arrow.onclick = function(e) {
		picturePopup.nextPicture(e);
	}
	//arrow.ondblclick = arrow.onclick;
	
	// Filmstrip
	div = create( "DIV", this.containerObj, "filmstrip" );
	div.onclick = function(e) {
		cancelBubble(e);
	}
	this.filmstripObj =  create( "DIV", div );
	
	// Main Image
	this.imgObj = create( "IMG", this.containerObj, "mainImage" );
	
	// Comment
	this.commentObj = create( "DIV", this.containerObj, "comment" );
	
}

PicturePopup.prototype.getPicture = function( pictureId ) {
	return this.pictures[this.getPictureIndex(pictureId)];
}

PicturePopup.prototype.getPictureIndex = function( pictureId ) {
	for ( var i=0; i<this.pictures.length; i++ ) {
		if ( this.pictures[i].id == pictureId )
			return i;
	}
	alert( "getPictureIndex(): can't find pictureId (" + pictureId + ")" );
	return null;
}

PicturePopup.prototype.prevPicture = function(e) {
	var index = this.getPictureIndex( this.pictureId );
	if ( index > 0 )
		this.showPicture( this.pictures[index-1].id );
	cancelBubble(e);
}

PicturePopup.prototype.nextPicture = function(e) {
	cancelBubble(e);
	var index = this.getPictureIndex( this.pictureId );
	if ( index < this.pictures.length-1 )
		this.showPicture( this.pictures[index+1].id );
}

PicturePopup.prototype.showPicture = function( pictureId, postId ) {
	
	if ( !this.pictures ) {
		// Load pictures
		this.pictureId = pictureId;
		this.postId = postId;
		this.request = new Request( "ajax/ajaxPictures.php?action=getPictures&postId=" + postId, null, new Callback( this, "onLoadPictures" ) );
	}
	else {
		//alert(pictureId);
		//alert(pic);
		//alert(typeof this.pictures);
		//alert(this.pictures.length);
		
		//var index = this.getPictureIndex(pictureId);
		//var pic = this.pictures[index];
		var pic = this.getPicture(pictureId);
		
		this.imgObj.style.width = pic.width + "px";
		this.imgObj.style.height = pic.height + "px";
		
		this.imgObj.src = pic.smallFilename;
		this.imgObj.src = pic.filename;
		this.pictureId = pictureId;
		setText( pic.comment, this.commentObj );
		
		this.updateThumbnails();
	}
}

PicturePopup.prototype.onLoadPictures = function( params ) {
	var picturePopup = this;
	
	// Build the pictures array:
	var root = params[0];
	this.pictures = [];
	var requestPictures = getNodes( root, "picture" );
	
	/*
	log( "root: " + root );
	for ( var i in root ) {
		log( "root." + i + ": " + root[i] );
	}
	*/
	
	//alert("requestPictures.length: " + requestPictures.length );
	
	for ( var i=0; i<requestPictures.length; i++ ) {
		var requestPicture = requestPictures[i];
		
		this.pictures.push( {
			id: getNodeValue(requestPicture, "id")
			, filename: getNodeValue(requestPicture, "filename")
			, smallFilename: getNodeValue(requestPicture, "smallFilename")
			, comment: getNodeValue(requestPicture, "comment")
			, width: getNodeValue(requestPicture, "width")
			, height: getNodeValue(requestPicture, "height")
		} );
	}
	
	// Show the main image
	this.showPicture( this.pictureId );
	
	// Show the thumbnail images
	for ( var i in this.pictures ) {
		this.pictures[i].imgObj = create( "IMG", this.filmstripObj );
		this.pictures[i].imgObj.onload = function() {
			picturePopup.updateThumbnails();
		}
		this.pictures[i].imgObj.src = this.pictures[i].smallFilename;
		this.pictures[i].imgObj.pictureId = this.pictures[i].id;
		this.pictures[i].imgObj.onclick = function(e) {
			picturePopup.showPicture( this.pictureId );
			cancelBubble(e);
		}
		//this.pictures[i].imgObj.ondblclick = this.pictures[i].imgObj.onclick;
	}
	
	// Load preload images
	for ( var i in this.pictures ) {
		
		var pic = create( "img", this.containerObj, "preload" );
		pic.src = this.pictures[i].filename;
	}
	
}

PicturePopup.prototype.close = function() {
	if ( this.containerObj )
		removeNode( this.containerObj );
	
	this.containerObj = null;
	this.pictures = null;
	this.postId = null;
	this.pictureId = null;
}

function setOpacity( obj, opacity ) {
	if ( opacity < 0 ) {
		alert("setOpacity(): opacity is < 0 (" + opacity + ")");
		return;
	} else if ( opacity > 1 ) {
		alert("setOpacity(): opacity is > 1 (" + opacity + ")");
		return;
	} else if ( isNaN(opacity) ) {
		alert("setOpacity(): opacity is NaN (" + opacity + ")");
		return;
	}

	obj.style.filter = "alpha(opacity=" + (opacity*100) + ")";
	obj.style.opacity = opacity;
}


PicturePopup.prototype.updateThumbnails = function() {
	var i = this.getPictureIndex( this.pictureId );
	
	var x;
	var opacity;
	var fadeRadius = 6;
	
	x = this.filmstripObj.offsetWidth/2;
	
	if ( this.pictures[i].imgObj ) {
		x -= parseInt( this.pictures[i].imgObj.offsetWidth/2 );
		this.pictures[i].imgObj.style.left = x + "px";
		this.pictures[i].imgObj.className = "active";
		setOpacity( this.pictures[i].imgObj, 1 );
	}
	
	for ( var j=i-1; j>=0; j-- ) {
		if ( this.pictures[j].imgObj ) {
			x += -this.pictures[j].imgObj.offsetWidth - 5;
			this.pictures[j].imgObj.style.left = x + "px";
			this.pictures[j].imgObj.className = "";
			opacity = Math.max( ( fadeRadius - 1 + j - i ) / fadeRadius, 0 );
			setOpacity( this.pictures[j].imgObj, opacity );
		}
	}
	
	x = this.filmstripObj.offsetWidth/2 +5;
	if ( this.pictures[i].imgObj ) {
		x += parseInt( this.pictures[i].imgObj.offsetWidth/2 );
	}
	
	for ( var j=i+1; j<this.pictures.length; j++ ) {
		if ( this.pictures[j].imgObj ) {
			this.pictures[j].imgObj.style.left = x + "px";
			this.pictures[j].imgObj.className = "";
			x += this.pictures[j].imgObj.offsetWidth + 5;
			
			opacity = Math.max( ( fadeRadius + i - j ) / fadeRadius, 0 );
			setOpacity( this.pictures[j].imgObj, opacity );
		}
	}
}

/*
PicturePopup.prototype.setDisplayModePicture = function() {
var thisObj = this;

if ( this.displayMode != "picture" ) {
// Show single picture display mode

this.prevPicture = {};
this.prevPicture.linkObj = create( "A", this.container, "prevPicture" );
this.prevPicture.linkObj.href = "#prevPicture";
this.prevPicture.linkObj.onclick = function() {
	thisObj.showPrevPicture();
	return false;
}
this.prevPicture.obj = create( "IMG", this.prevPicture.linkObj );

this.picture = {};
this.picture.linkObj = create( "A", this.container );
this.picture.linkObj.href = "#close";
this.picture.linkObj.onclick = function() {
	thisObj.close();
}
this.picture.obj = create( "IMG", this.picture.linkObj, "mainPicture" );

this.nextPicture = {};
this.nextPicture.linkObj = create( "A", this.container, "nextPicture" );
this.nextPicture.linkObj.href = "#nextPicture";
this.nextPicture.linkObj.onclick = function() {
	thisObj.showNextPicture();
	return false;
}
this.nextPicture.obj = create( "IMG", this.nextPicture.linkObj );

this.pictures = null;
this.displayMode = "picture";
}
}

PicturePopup.prototype.unsetDisplayModePicture = function() {
if ( this.displayMode == "picture" ) {
this.prevPicture.link.onclick = undefined;
this.nextPicture.link.onclick = undefined;
removeNode( this.prevPicture.linkObj );
removeNode( this.picture.obj );
removeNode( this.nextPicture.linkObj );
this.picture.obj = null;
this.picture = null;
this.pictures = null;
this.displayMode = "";
}
}

PicturePopup.prototype.showPicture = function( postId, pictureId ) {
this.setDisplayModePicture();
//alert( "showPicture" );
if ( !this.pictures ) {
// Need to load all pictures for this post first
this.loadPicturesSetPictureId = pictureId;
//alert( "postId="+postId+"\npictureId="+pictureId+"\nthis.loadPicturesSetPictureId="+this.loadPicturesSetPictureId );

this.loadPictures( postId );
}
else {
//alert( "showing picture" );
// All pictures for this post are loaded, show the desired picture

var i = arraySearch( this.pictures, "id", pictureId )

if ( i>0 )
	this.prevPicture.obj.src = this.pictures[i-1].smallFilename;
else
	this.prevPicture.obj.src = "images/blankpicture.png";

if ( i<this.pictures.length-1 )
	this.nextPicture.obj.src = this.pictures[i+1].smallFilename;
else
	this.nextPicture.obj.src = "images/blankpicture.png";

this.picture.active = i;
this.picture.obj.src = this.pictures[this.picture.active].filename;

//alert( this.container.innerHTML );
}
}

PicturePopup.prototype.showPrevPicture = function() {
if ( this.picture.active > 0 ) {
//alert( "this.pictures[this.picture.active-1].id="+this.pictures[this.picture.active-1].id );
this.showPicture( null, this.pictures[this.picture.active-1].id );
}
}

PicturePopup.prototype.showNextPicture = function() {
if ( this.picture.active < this.pictures.length-1 ) {
this.showPicture( null, this.pictures[this.picture.active+1].id );
}
}


PicturePopup.prototype.loadPictures = function( postId ) {
this.request = new Request( "ajax/ajaxPictures.php?action=getPictures&postId=" + postId, null, new Callback( this, "onLoadPictures" ) );
}

PicturePopup.prototype.onLoadPictures = function( params ) {
var root = params[0];
this.pictures = [];

var requestPictures = getNodes( root, "picture" );
for ( var i=0; i<requestPictures.length; i++ ) {
var requestPicture = requestPictures[i];

this.pictures.push( {
	id: getNodeValue(requestPicture, "id")
	, filename: getNodeValue(requestPicture, "filename")
	, smallFilename: getNodeValue(requestPicture, "smallFilename")
} );
}

//alert( "loaded " + this.pictures.length + " pictures" );
//alert( "this.loadPicturesSetPictureId="+this.loadPicturesSetPictureId );

if ( this.loadPicturesSetPictureId ) {
//alert( "setting desired picture " + this.loadPicturesSetPictureId );
// Show the desired picture now that it's loaded
var pictureId = this.loadPicturesSetPictureId;
this.loadPicturesSetPictureId = null;
this.showPicture( null, pictureId );

}
}

PicturePopup.prototype.close = function() {
//this.setDisplayMode();

// Remove circular dependencies
this.closeLink.onclick = undefined;
this.prevPicture.linkObj.onclick = undefined;
this.picture.linkObj.onclick = undefined;
this.nextPicture.linkObj.onclick = undefined;

removeNode( this.container );
this.container = null;
}
*/