/*
	Picture Show Object - Graphic Controls additions

	by: Joe Szedula (szedula-at-mindspring.com)

	This file extends the picture show object to include form controls
	within a table. This file only needs to be used if user control of
	the picture show is desired

	Tbe documentation for using this Javascript object library can
	be found at:

		http://www.mindspring.com/~szedula/share/PictureShowDoc.html
		http://www.mindspring.com/~szedula/share/PictureShowFormDoc.html
		http://www.mindspring.com/~szedula/share/PictureShowControlDoc.html

	This source is best viewed with tabs set every 3 columns.


/* object method to create HTML for graphic controls
		theButtons	-	button images, they should be ordered for:
							0: spacer
							1: rewind
							2: step back
							3: play
							4: pause
							5: step forward
							6: go to last image
		theWidths	-	width of images
		theHeight	-	height of all images
		theSite		-	name of site/directory containing pictures (optional)
*/
PictureShow.prototype.DisplayButtons = function(theButtons,theWidths,theHeight,theSite) {
	var n = theButtons.length;
	if (n != 7) {
		alert('DisplayButtons: incorrect number of buttons: '+n);
		return false;
	}
	if (theWidths.length != n) {
		alert('DisplayButtons: incorrect number of button widths: '+theWidths.length);
		return false;
	}
	var site=""
	if (arguments.length == 4) {
		if (theSite != "") {
			site = theSite;
			if (site.substr(site.length-1,1) != "/") site += "/";
		}
	}
	
	var ts = this.theShow;

	// button action definitions
	var info = new Array("","Rewind To First Image","Step Back One Image At A Time","Play",
									"Pause","Step Forward One Image At A Time","Go To Last Image");
	var cmds = new Array("",ts+'_Rewind()',ts+'_Prev()',ts+'_PlayShow()',
									ts+'_Pause()',ts+'_Next()',ts+'_Last()');

	// build Javascript routine definitions
	var s = '<script language="JavaScript1.1" type="text/javascript">';
   s += 'function '+cmds[1]+' { '+ts+'.NoAnimate();'+ts+'.GoToSlideNumber(0); }';
   s += 'function '+cmds[2]+' { '+ts+'.NoAnimate();'+ts+'.NextSlide(-1); }';
   s += 'function '+cmds[3]+' { if (theShow.timeout_id==null) '+this.theCode+';}';
   s += 'function '+cmds[4]+' { '+ts+'.NoAnimate();   }';
   s += 'function '+cmds[5]+' { '+ts+'.NoAnimate();'+ts+'.NextSlide(1); }';
   s += 'function '+cmds[6]+' { '+ts+'.NoAnimate();'+ts+'.GoToSlideNumber(999999); }';
	s += '<\/script>';
	document.writeln(s);

	// write HTML for contols
	this.PictureTable();
	s = '<tr><td><center>';
	for (var i=1; i<n; i++) {
		s += ButtonImage(site+theButtons[0], theWidths[0],theHeight);
		s += ButtonImage(site+theButtons[i], theWidths[i],theHeight,info[i],"javascript:"+cmds[i]);
	}
	s += ButtonImage(site+theButtons[0], theWidths[0],theHeight);
	s += '<\/center><\/td><\/tr><\/table>'
	document.writeln(s);
	return true;
}

/* Routines from this point on are not to be used directly by users.
	These	routines are to be used by PictureShow object internally.

	function to display information in status pane
		text	-	text to be displayed in status pane
*/
function PSinfo(text) { window.status = text; return true; }

/* object method to create HTML for graphic controls
		img		-	button image
		w			-	width of images
		h			-	height of images
		info		-	information to be displayed in status window and
						when pointer hovers over button image
		action	-	Javascript action to be executed when image is clicked
*/
function ButtonImage(img,w,h,info,action) {
	var s="";
	if (arguments.length == 5) {
		s += '<a href="'+action+'" title="'+info+'" onMouseover="return PSinfo(\''+
					info+'\');" onMouseout="return PSinfo(\'\');">';
	}
	s += '<img src="'+img+'" alt="" width='+w+' height='+h+
			  ' align=middle hspace=0 vspace=0 border=0>';
	if (arguments.length == 5) s += '<\/a>';
	return s;
}

/* object method to specify note style used by picture show
		theStyle		-	note style used by picture show
		theBorder	-	border width in note style
*/

PictureShow.prototype.SetNoteStyle = function(theStyle,theBorder) {
	this.NoteStyle = theStyle;
	if (arguments.length == 2) { this.NoteBorder = theBorder*2; }
}

/* object method to specify notes used by picture show
		theNotes	-	notes to be displayed with pictures
		X			-	"X" position of image notes (pixels)
		H			-	image note height (pixels)
		W			-	image note width (pixels)
*/
PictureShow.prototype.SetNotes = function(theNotes,X,H,W) {

	if (arguments.length < 4) { W = this.ImageW; }

	var isSafari = navigator.appVersion.indexOf("Safari") >= 0;
	if (isSafari) {
		this.FormNote = this.theShow+"_form";
		var s = '<form name="'+this.FormNote+'">';
		s += '<input type=text name="Notes" size='+W+' value="">';
		s += '</form>';
		document.writeln(s);
		this.FormNote = document[this.FormNote].Notes;
	} else {
		var s = '<style type="text/css"> .PSnote { ';
		if (this.NoteStyle) { 
			s += this.NoteStyle;
		} else {
			s += 'text-align: center; vertical-align: top;';
		}
		s += ' }</style>';

		var pad = 4;
		var ht = H + 2*pad + this.NoteBorder;
		s +='<div style="{height: '+ht+'; }">';
		document.writeln(s);
	}

	this.Notes = new Array(this.SlideCount);
	for (var i=0; i<this.SlideCount ; i++) {
		if (i >= theNotes.length) {
			this.Notes[i] = this.filename[i];
		} else {
			this.Notes[i] = theNotes[i];
		}
		if (isSafari) {
			if (i == 0) this.FormNote.value = this.Notes[i];
		} else {
			s = '<div class=PSnote id="'+this.theShow+'_note'+i+'" style="{ visibility: hidden; position: relative; ';
			if (i == 0) {
				s +='left: '+X+'px; top: 0';
			} else {
				s +='left: '+X+'px; top: -'+(i*ht);
			}
			s += 'px; padding: '+pad+'; width: '+W+'px; height: '+H+'px; }">'+this.Notes[i]+'</div>';
			document.writeln(s);
			this.SetVisibility('hidden',i);
		}
	}
	if (! isSafari) document.writeln('</div>');
}

function PS_findObj(n, d) {
	var p,i,x;

	if (!d) d=document;

	if ((p=n.indexOf("?"))>0 && parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
  		
	if (!(x=d[n]) && d.all) x = d.all[n]; 
	for (i=0; !x && i<d.forms.length; i++) x=d.forms[i][n];
	for (i=0; !x && d.layers && i<d.layers.length; i++) x = MM_findObj(n,d.layers[i].document);
	if (!x && d.getElementById) x=d.getElementById(n);
	return x;
}

PictureShow.prototype.SetVisibility = function(visibility,ID) {
	if (arguments.length == 1) ID = this.Slide;
	if (ID >= 0) {
		if (this.FormNote) {
			if (visibility == 'visible') this.FormNote.value = this.Notes[this.Slide];
		} else {
			if ( (obj=PS_findObj(this.theShow+'_note'+ID)) != null) {
				if (obj.style) { obj=obj.style; }
				obj.visibility = visibility;
			}
		}
	}
}
