// photoscr.js
// Scripts for use by photog_album pages
// Created 6/12/2002 Peter Tribble.

// Last amended:
// 20/12/02	creation of menuinit.
// 12/08/03	Revisited to tidy up display of titles
//		and attempt to make page more navigable.
// 14/08/03	Replaced the photo page menu applet
//		with FORM drop down selections for
//		Album, Section and Page.
//		Also moved its re-creation point
//		to a call from the page setup functions
//		rather than directly from the HTML.


// Browser check
//isIE=document.all?1:0;  - for future use

// global variables
var g_imgRoot = '../images/';
var g_tmpImg = g_imgRoot + 'misc/tribblet_s.gif';
var g_stdpicwidth = '450';
var g_stdpicheight = '337';
var g_photos;
var g_album;
var g_thumbpics;
var g_rightleft;
var g_startmenu;
var g_currMenu = 1;
var g_currSection = 1;
var g_currAlbum = 1;
var g_phmenu;

if (parent.menu.firstmenu) { g_startmenu = parent.menu.firstmenu }


function surfToAlbum(form) {
        var myindex=form.selAlbum.selectedIndex;
	var phpage=form.selAlbum.options[myindex].value
//	if (myindex != 0) {
		parent.menu.firstmenu=1;
		document.location.href=phpage;
//	}
}

function surfToSection(form) {
        var myindex=form.selSection.selectedIndex;
	var phpage=form.selSection.options[myindex].value
	eval (phpage);
}

function surfToPage(form) {
        var myindex=form.selPage.selectedIndex;
	var phpage=form.selPage.options[myindex].value
	eval (phpage);
}

// this function added to be called directly from each photo page.
// it writes the html to load the top menu.
function phmenuinit() {
	var contentTxt;
	var subch;
	var i,j,k,m,mno;
	var heading;
	var showContentObjMenu = photomenu;

	var alm = new albumdef();
	var phm = new menudef();

	// Selected Ablum page top menu content
	contentTxt = '<TABLE><TR>';
	contentTxt = contentTxt + '<TD> Choose an Album -----------</TD>';
	contentTxt = contentTxt + '<TD> Choose a Section ----------</TD>';
	contentTxt = contentTxt + '<TD> Choose a Page</TD></TR>';

	// drop down menu for list of Albums
	contentTxt = contentTxt + '<TR><TD>';
	contentTxt = contentTxt + '<FORM><SELECT NAME="selAlbum"';
	contentTxt = contentTxt + ' SIZE=1 onchange="surfToAlbum(this.form)">';
	for (i = 0; i < alm.names.length; i++) {
	  contentTxt = contentTxt + '<OPTION';
	  if (i == g_currAlbum) {
	    contentTxt = contentTxt + ' SELECTED';
	  }
	  contentTxt = contentTxt + ' VALUE="';
	  contentTxt = contentTxt + alm.pages[i] +'">'+ alm.names[i];
	}
	contentTxt = contentTxt + '</SELECT>';
	contentTxt = contentTxt + '</FORM></TD><TD>';

	// drop down menu for the current Album Sections
	contentTxt = contentTxt + '<FORM><SELECT NAME="selSection"';
	contentTxt = contentTxt + ' SIZE=1 onchange="surfToSection(this.form)">';
	m = 0;
	for (i = 0; i < phm.subheadings.length; i++) {
	  subch = 'phm.subchoices' + (i+1);
	  mno = phm.menuseq[m];
	  m = m+ eval(subch).length;
	  contentTxt = contentTxt + '<OPTION';
	  if (i == g_currSection - 1) {
	    contentTxt = contentTxt + ' SELECTED';
	  }
	  contentTxt = contentTxt + ' VALUE="';
	  contentTxt = contentTxt + 'menu('+mno +')">'+ phm.subheadings[i];
	}
	contentTxt = contentTxt + '</SELECT>';
	contentTxt = contentTxt + '</FORM></TD><TD>';

	// drop down menu for the current Section Pages
	contentTxt = contentTxt + '<FORM><SELECT NAME="selPage"';
	contentTxt = contentTxt + ' SIZE=1 onchange="surfToPage(this.form)">';
	m = 0;
	for (i = 0; i < phm.subheadings.length; i++) {
	  k = i + 1;
	  subch = 'phm.subchoices' + k;
	  for (j = 0; j < eval(subch).length; j++) {
	    if (k == g_currSection) {
	      mno = phm.menuseq[m];
	      contentTxt = contentTxt + '<OPTION';
	      if (mno == g_currMenu) {
	        contentTxt = contentTxt + ' SELECTED';
	      }
	      contentTxt = contentTxt + ' VALUE="';
	      contentTxt = contentTxt + 'menu('+mno +')">'+ eval(subch)[j];
	    }
	    m++;
	  }
	}
	contentTxt = contentTxt + '</SELECT>';
	contentTxt = contentTxt + '</FORM></TD></TR></TABLE>';

	// alert( contentTxt );
	//document.write( contentTxt );
	showContentObjMenu.innerHTML = contentTxt;
}


//BASIC setup common to all thumb menus on this page
// Called from BODY ONLOAD on each photo album page
// This function fills in photomenu content from the data
// in the function menudef() of the calling page.
// This also initialises thumbs controls and main picture.
function menuinit(firstmenu) {
	var contentTxt;
	var subch;
	var i,j,m;
	var content;
	var showContentObjPic = picholder;
	var showContentObjTit = phmenutitle;
	var showContentObjFoot = phmenufoot;

	// assign GLOBAL object from current album script menudef
	g_phmenu = new menudef();

	// menu header text
	contentTxt = "<PRE><H2>" + g_phmenu.menutitle;
	contentTxt = contentTxt + " <DIV class='menuBar'><A class='menuButton' HREF='javascript:void(0);' ONCLICK='";
	contentTxt = contentTxt + "javascript:contentlist();'>";
	contentTxt = contentTxt + " Album Contents List</A></DIV></PRE></H2>";
	showContentObjTit.innerHTML = contentTxt;

	// menu footer text
	contentTxt = "<P>" + g_phmenu.menufoot + "</P>";
	showContentObjFoot.innerHTML = contentTxt;

	// initialize center main picture
	content = "<IMG SRC='" + g_tmpImg + "' ";
	content = content + "name=main0 ";
	content = content + "WIDTH=" + g_stdpicwidth;
	content = content + " HEIGHT=" + g_stdpicheight;
	content = content + " ALT='" + g_tmpImg + "' VSPACE=5 HSPACE=5 BORDER=1>";
	showContentObjPic.innerHTML = content;

	// the thumbs previous--next image control
	g_rightleft = '<DIV class="menuBar">' + "<A class='menuButton' HREF='javascript:void(0);' ";
	g_rightleft = g_rightleft + "ONCLICK='javascript:g_album.switchImages(-1);'>";
	g_rightleft = g_rightleft + "Previous</A>--<A class='menuButton' HREF='javascript:void(0);' ";
	g_rightleft = g_rightleft + "ONCLICK='javascript:g_album.switchImages(1);'>"; 
	g_rightleft = g_rightleft + "Next Image</A></DIV>";

	contentTxt = contentTxt + '{Contents,javascript:contentlist()}';

	//call first menu to display
	if (g_startmenu) {firstmenu = g_startmenu}
	menu(firstmenu);

}// end of menuinit

//umbrella function to call setup functions
function menu(menuno) {
	allSetup(menuno);
	phmenuinit();
	thumbsinit();
	albuminit();
}

// general menu choice function for photo pages
// there must be a corresponding 'menu<menuno>' function for
// each requested <menuno> number within the calling photo pages html
function allSetup(menuno) {
	var content;
	var i,j,k,m;
	var imgAlt;
	var subch;
	var pageHeading;
	var showContentObjTit = pictitle;
	var showContentObjHdr  = thumbshdr;
	var showContentObjTbl  = thumbstbl;
	var showContentObjFoot = thumbsfoot;

	//some basic error checking
	var menufn = 'menu' + menuno;
	if (eval("window." + menufn)==null){
		alert("There are no photos defined for menu item " + menuno);
		return;
	}

	// assign GLOBAL var to record current menu number
	g_currMenu = menuno;
	// assign GLOBAL object from specified menu function
	g_photos = eval('new ' + menufn + '()');

	//substitute image array zero sizes by the std definitions
	for ( i = 0; i < g_photos.ImageWidths.length; i++) {
		if (g_photos.ImageWidths[i] == 0) {
			g_photos.ImageWidths[i] = g_photos.StdImageWidth;
		}
		if (g_photos.ImageHeights[i] == 0) {
			g_photos.ImageHeights[i] = g_photos.StdImageHeight;
		}
	}
	//initialise photodisp header and initial image
	content = "<H2>" + g_photos.ImagePageTitle + "</H2>";
	showContentObjTit.innerHTML = content;

	//initialise photothumbs

	//work back to page heading from menudef menu number
	k=0;
	for ( i = 0; i < g_phmenu.subheadings.length; i++) {
	  subch = 'g_phmenu.subchoices' + (i+1);
	  for (j = 0; j < eval(subch).length; j++) {
	    m = g_phmenu.menuseq[k];
	    k++;
	    if (m==menuno) {
	    	pageHeading=eval(subch)[j];
		// assign GLOBAL var to record current Section number
		g_currSection = i+1;
	    }
	  }
	}

	// Thumbs Header title
	content = "<H2>" + pageHeading + "</H2>";
	content = content + g_photos.ThumbsPageTitle + "<BR>" + g_rightleft;
	showContentObjHdr.innerHTML = content;

	//Thumbs table
	// this part sets up named image place holders
	// for thumbsinit() to populate.
	content = "<HR><TABLE><TR>";
	j = 1;
	for (i = 0; i < g_photos.ImageFiles.length; i++) {
		if (j == 0) {
			content = content + "</TR><TR>";
			j = 1;
		}
		content = content + "<TD><A HREF='javascript:void(0);' ";
		content = content + "ONCLICK='g_album.showImage(" + i + ");'>";
		content = content + "<IMG SRC='" + g_tmpImg + "' ";
		content = content + "name=thumb" + i + "><BR>";
		content = content + (i+1) + "</A></TD>";
		j++;
		if (j == 5) j = 0;
	}
	content = content + "</TR></TABLE><HR>";
	showContentObjTbl.innerHTML = content;

	// Thumbs Footer segment
	content = g_rightleft + "<P>" + g_photos.ThumbsFootText + "</P>";
	showContentObjFoot.innerHTML = content;

}
// end of allSetup()

//initialize thumb pics
function thumbsinit() {

	// now for image objects
	// use the slideshow to populate the global thumbs collection
	var imgBase = g_imgRoot + g_photos.ImageDirectory + "/";
	g_thumbpics = new pSlideShow( g_photos,
					0,
					imgBase,
					'_t.jpg',
					g_photos.ImageFiles.length,
					'thumb',
					1);
	g_thumbpics.switchImages(0);
}
// end of thumbsinit()

//initialize album pics
function albuminit() {

	// use the slideshow to populate the global g_album collection
	var imgBase = g_imgRoot + g_photos.ImageDirectory + "/";
	g_album = new pSlideShow( g_photos,
				0,
				imgBase,
				'.jpg',
				1,
				'main',
				0);
	g_album.switchImages(0);
}
// end of albuminit()


// This function replaces the thumb box contents with a summary list of
// all the menu contents for the current photo page.
// It is essentially the menu and sub-menu titles with all their
// foot texts, and provides links to each menu.
// created 28/01/2003
function contentlist()
{
	var contentTxt;
	var subch;
	var heading;
	var i,j,m, mno;
	var content;
	var showContentObjHdr  = thumbshdr;
	var showContentObjTbl  = thumbstbl;
	var showContentObjFoot = thumbsfoot;
	var showContentObjCur  = thumbscurr;

	// collapse existing content on thumbs area
	showContentObjHdr.innerHTML = '';
	showContentObjTbl.innerHTML = '';
	showContentObjFoot.innerHTML = '';
	showContentObjCur.innerHTML = '';

	contentTxt = '<H2>Contents</H2>';
	showContentObjHdr.innerHTML = contentTxt;

	contentTxt = '';
	m = 0;
	for (i = 0; i < g_phmenu.subheadings.length; i++) {
	  contentTxt = contentTxt + '<DL><H3>'+g_phmenu.subheadings[i]+'</H3>';

	  contentTxt = contentTxt + "<P>" + g_phmenu.subtexts[i] + "</P>";

	  subch = 'g_phmenu.subchoices' + (i+1);
	  for (j = 0; j < eval(subch).length; j++) {
	    mno = g_phmenu.menuseq[m];
	    heading = eval(subch + "[" + j + "]");
	    contentTxt = contentTxt + "<DD><A ";
	    contentTxt = contentTxt + "HREF='javascript:menu(" + mno + ")'>";
	    contentTxt = contentTxt + heading +'</A></DD>';
	    m++;
	  }
	  contentTxt = contentTxt + "</DL><BR>"	  
	}

	showContentObjTbl.innerHTML = contentTxt;
}

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// PLUGIN DECLARATIONS:                                                       //
// NOTE: Plugins are called every time the images in a slide show are updated.//

// updatePlugins(): this function is called at the end of placing images into //
// image boxes on the page.                                                   //
function updatePlugins(pSSObject)
{
	if (pSSObject.pIsThumb == 0) {
//		pPreloadImage(pSSObject);
		// extra captions for main image
		pImageExtras(pSSObject);
	}
}

// pPreloadImage(): a pSlideShow plugin which will preload the images which   //
// will be displayed next, thus saving a bit of time when scrolling thru the  //
// slide show.
function pPreloadImage(pSSObject)
{
		// figure out which image numbers will be displayed next
		pOffBottomOfArray = mod(pSSObject.pCurrentImageNumber - 1,
					pSSObject.pImageDefs.ImageFiles.length);
		pOffTopOfArray = mod(pSSObject.pCurrentImageNumber +
					pSSObject.pNumberOfPages,
					pSSObject.pImageDefs.ImageFiles.length);
		
		// and preload the images
		if (!pSSObject.pImageObjectsArray[pOffBottomOfArray].src)
			pSSObject.pImageObjectsArray[pOffBottomOfArray].src =
					pSSObject.pImagePath +
					pSSObject.pImageDefs.ImageFiles[pOffBottomOfArray] +
					pSSObject.pImageExt;

		if (!pSSObject.pImageObjectsArray[pOffTopOfArray].src)
			pSSObject.pImageObjectsArray[pOffTopOfArray].src =
					pSSObject.pImagePath +
					pSSObject.pImageDefs.ImageFiles[pOffTopOfArray] +
					pSSObject.pImageExt;
}

// this part fills in  main image captions
function pImageExtras(pSSObject)
{
	var showContentObjTxt = txtholder;
	var showContentObjCur = thumbscurr;
	var imgNumber	= pSSObject.pCurrentImageNumber;
	var imgName	= pSSObject.pImageDefs.ImageFiles[imgNumber];
	var imgCaption	= pSSObject.pImageDefs.ImageCaptions[imgNumber];
	var captionTxt	= '';
	var currContent;

	if (imgCaption) {
	 captionTxt = "<H3>" + imgCaption + "</H3>";
	};
	captionTxt = captionTxt + "<H4>" + imgName + "</H4>";
	showContentObjTxt.innerHTML = captionTxt;

	currContent = "Current is: " + (imgNumber+1);
	showContentObjCur.innerHTML = currContent;
}



////////////////////////////////////////////////////////////////////////////////
// P.Tribble: these functions below have been cribbed and modified            //
// from someone elses work for my purposes.                                   //
// A note about implementation:                                               //
// This JavaScript source file contains all the code necessary for any kind of//
// pSlideShow.                                                                //
//                                                                            //
// If I have done my job right then an implementor should not need            //
// to venture into this code file in order to implement pSS; editing the      //
// config file ought to be enough.                                            //
//                                                                            //
// A separate file contains the variable declarations and other               //
// needfully-edited things.                                                   //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// FUNCTION DEFINITIONS:                                                      //

// pSlideShow function: constructor for a pSlideShow object
function pSlideShow(	pImageDefs,
			pCurrentImageNumber,
			pImagePath,
			pImageExt,
			pNumberOfPages,
			pImageSlotName,
			pIsThumb)
{
	// Variables and Arrays
	this.pImageDefs = pImageDefs;
	this.pCurrentImageNumber = pCurrentImageNumber;
	this.pImagePath = pImagePath;
	this.pImageExt = pImageExt;
	this.pNumberOfPages = pNumberOfPages;
	this.pImageSlotName = pImageSlotName;
	this.pIsThumb = pIsThumb;		//1=an array for thumbnails
	this.pImageLoaded = 0;			// is set to 1 by main image onload event
	this.pImageObjectsArray = new Array;
	
	// Methods
	this.switchImages = switchImages;
	this.showImage = showImage;
	
	// Initialization
	for(var pTemp = 0; pTemp < this.pImageDefs.ImageFiles.length; pTemp++)
		this.pImageObjectsArray[pTemp] = new Image();
}


// switchImages(): pSlideShow method: first set pCurrentImageNumber, then, for//
// each image slot in the slideshow, create a new image entry in pImageArray, //
// if needed, then install the new image. Lastly, update the plugins.         //
function switchImages(pScrollAmount)
{
	var imagefile;
	var imagename;
	var bTempNumber;

	// 1. wrap pCurrentImageNumber to a proper value.
	this.pCurrentImageNumber = mod( (this.pCurrentImageNumber + pScrollAmount),
					this.pImageDefs.ImageFiles.length);
	
	// 2. for each pPhotoSpot...   ("pin" == "p image number")
	for (var pin = 0; pin < this.pNumberOfPages; pin++) {
		
		// A. find the right number for the current installation
		bTempNumber = mod( (this.pCurrentImageNumber + pin),
						this.pImageDefs.ImageFiles.length);

		imagefile = this.pImagePath + this.pImageDefs.ImageFiles[bTempNumber] + this.pImageExt;
		imagename = this.pImageSlotName + pin;

		// B. if the image isn't loaded, load it
//		if (!this.pImageObjectsArray[bTempNumber].src) {
//			this.pImageObjectsArray[bTempNumber].src = imagefile;
//		}
		
		// C. install the image 
		if (this.pIsThumb == 1) {
			// displaying a thumb image here
			//keep orig sizes...document[this.pImageSlotName + pin].width = 80;
			//document[this.pImageSlotName + pin].height = 80;
			document[this.pImageSlotName + pin].alt = this.pImageDefs.ImageCaptions[bTempNumber];
		} else {
			document[this.pImageSlotName + pin].alt = this.pImageDefs.ImageFiles[bTempNumber];
			document[this.pImageSlotName + pin].width = this.pImageDefs.ImageWidths[bTempNumber];
			document[this.pImageSlotName + pin].height = this.pImageDefs.ImageHeights[bTempNumber];
		}
		// call a timeout function to update the image source
		//  - this gets around an IE6 bug where the image loading
		// got chopped when this function finished.
		window.setTimeout( "setImageSrc('" + imagename + "','" + imagefile + "')", 1);
	}
	// 3. call a function which executes implementation-specific code in the 
	// config file.
	updatePlugins(this);
}

function setImageSrc( imagename, imagefile ) {
	document[imagename].src = imagefile;
}

//pSlideShow object method
function showImage(ImageNumber)
{
	this.pCurrentImageNumber = ImageNumber;
	this.switchImages(0);
}

// mod(): javascript's mod function "does the wrong thing" by the definition I
// was taught. 
function mod(a, b)
{
	while (a < 0)
		a += b;
	while (a >= b)
		a -= b;
	return a;
}

//                                                                            //
////////////////////////////////////////////////////////////////////////////////


// end of script file.

