/* ===============================================================
	Yet another Javascript thumbnailed picture gallery API - 1.2
	Nicolas Desprez - october 2004
	
	Feel free to use and modify this code to your heart's content!
   =============================================================== */

/* ----------------------------------------
   | Picture                              |
   ---------------------------------------- */

// Constructor
// --------------------------------------------------
function Picture(Title, FileName, FileExt, FileSize, Width, Height, BgColor){

	this.Parent = null
	this.Title = Title								// link text and pop-up window title
	this.Author = ""
	this.FileName = FileName						// file name only, path is excluded
	this.FileExt = FileExt							// file extension without the dot
	this.FileSize = FileSize						// file size in kilobytes, integer
	this.Width = Width
	this.Height = Height
	this.BgColor = BgColor
	this.WriteHREF = Picture_WriteHREF				// writes the first part of the <a> tag
	this.WriteLink = Picture_WriteLink				// writes a link to the picture
	this.WriteThumbnail = Picture_WriteThumbnail	// writes a linked thumbnail to the picture
	this.View = Picture_View						// opens a centered pop-up window displaying the picture
	this.Index = -1									// defined in Pictures.Add
}

// WriteHREF
// --------------------------------------------------
function Picture_WriteHREF(){

	window.document.writeln(
		"<a href=\"javascript:void ", this.Parent.Name, ".Items[", this.Index, "].View()\">"
	)
}

// WriteLink
// --------------------------------------------------
function Picture_WriteLink(){

	this.WriteHREF()
	window.document.writeln(this.Title + "</a>")
}

// WriteThumbnail
// --------------------------------------------------
function Picture_WriteThumbnail(){
	var thumb_height
	
	if(this.Height > this.Width * 1.7)
		thumb_height = Math.round(this.Parent.ThumbnailWidth * 1.7)
	else
		thumb_height = Math.round((this.Parent.ThumbnailWidth / this.Width) * this.Height)

	this.WriteHREF()
	window.document.writeln(
		"<img src='" + this.Parent.Folder + this.FileName + 
					   this.Parent.ThumbnailSuffix + "." + this.Parent.ThumbnailExt + "'",
		" width=" + String(this.Parent.ThumbnailWidth), 
		" height=" + String(thumb_height), 
		" border=1 alt='" + this.FileName + "." + this.FileExt + " - ",
		String(this.Width) + "x" + String(this.Height) + " - " + String(this.FileSize) + "KB'></a>"
	)
}

// View
// --------------------------------------------------
function Picture_View(){
	var pic_window, doc, link_color
	
	if((parseInt(this.BgColor.substr(0, 2), 16) +
		parseInt(this.BgColor.substr(2, 2), 16) +
		parseInt(this.BgColor.substr(4, 2), 16)) / 3 > 0x7F)
		link_color = "000000"
	else
		link_color = "FFFFFF"
	
	pic_window = OpenWin("", this.FileName.replace("/", ""), this.Width + 20, this.Height + 20)
	doc = pic_window.document

	doc.write("<html><head><title>" + this.Title + "</title>", 
			  "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'></head>",
			  "<body bgcolor='#" + this.BgColor + "' leftmargin='10' rightmargin='10' topmargin='10' bottommargin='10'>",
			  "<div align='center'>",
			  "<img src='" + this.Parent.Folder + this.FileName + "." + this.FileExt,
			  "' width=" + this.Width + " height=" + this.Height + "><br>",
			  "<a href='javascript:void window.close()' style='color:#" + link_color + "; font-size: x-small'>close</a>",
			  "</div></body></html>")

	var ow, oh, iw, ih
	with(pic_window){
		if(Browser() == brwNetscape){
			ow = outerWidth
			oh = outerHeight
			iw = innerWidth
			ih = innerHeight
		}
		else {
			ow = doc.body.offsetWidth
			oh = doc.body.offsetHeight
			iw = doc.body.clientWidth
			ih = doc.body.clientHeight
		}

		moveBy(-(ow - iw) / 2, -(oh - ih) / 2)
		resizeBy(ow - iw, oh - ih)
	}
}

/* ----------------------------------------
   | Pictures                             |
   ---------------------------------------- */

// Constructor
// --------------------------------------------------
function Pictures(Name, Folder, DefaultExt, ThumbnailExt, ThumbnailWidth){

	this.Name = Name
	this.Author = ""
	this.AuthorLink = ""
	this.Items = Array()
	this.Folder = Folder
	this.DefaultExt = DefaultExt			// default original picture extension
	this.ThumbnailExt = ThumbnailExt
	this.ThumbnailWidth = ThumbnailWidth	// thumbnail height is based on original picture height
	this.ThumbnailSuffix = "_small"
	this.Add = Pictures_Add
}

// Add
// --------------------------------------------------
function Pictures_Add(Picture){

	Picture.Parent = this
	Picture.Index = this.Items.push(Picture) - 1
	if(Picture.FileExt == "")
		Picture.FileExt = this.DefaultExt
}