/*                                                                          gallery.js
-------------------------------------------------------------------------------------
Copyright Christopher Fulham
Email: christopher@christopherfulham.com

Based on web programming by Tim Brook

Purpose
~~~~~~~
Provide gallery behaviour for Christopher.com.au 
- uses data from christopherData.js or similar file


** Notes **
~~~~~~~~~~~
Calling HTML page must have:
    <body> or <div> element with id=ground
    <div> element with id=bigScreen
    <img> element with id=screen
    <cite> element with id=workTitle
(It may also have a <div> with id=navDisplay if it includes navigation buttons.)
Calling HTML page must load  windows.js  and  pix.js  before this file
                        and  <xx>data.js  after

-----------------------------------------------------------------------------------*/
// Constants
// ~~~~~~~~~
PIX = 1000 ; // Max No. of pix in <xx>data.js (inc. No.0) without recoding functions
             // variable declared in pix.js
             // not formally constant because may be reset in <xx>data.js

var W = screen.availWidth - 20 ;   // Effective available pixels in big window
var H = screen.availHeight - 100 ; // (again not technically constants because may be reset on HTML page)

// Global variables
// ~~~~~~~~~~~~~~~~
var marginB = 30 + 0.1 * H ; // Pixel margin under pic to allow for navigation buttons
                             // value may be overridden in <xx>data.js

var pic  = new Array(PIX) ; // The array of slides
var picNo = 0 ; // The No. of the slide (pic) currently showing -
                // updated by showSlide() in this file and docDraw() in drawing.js
                // it may be (re)initialised on the HTML page;

// ------------------------------------------
// Object types
// ~~~~~~~~~~~~
// An image in a slide show ----------------
function Slide() {
    this.title  = 'untitled' ;
    this.year	= '' ;
    this.file   = 'loading.jpg' ;  // Filename for pic
    this.width  = 640 ;            // Pixel width of pic in the file
    this.height = 360 ;            // Pixel height of pic in file
    this.matt   = '#000' ;         // Suitable background colour to surround the pic
    this.time   = 3 ;              // No. of seconds to display the pic on screen
    this.next   = NaN ;            // Successor (next pic)
    this.blurb  = document.createElement('SPAN') ; // Short text to accompany pic
    this.blurb.appendChild(document.createTextNode('')) ;
} // ----------------------------------------

// ------------------------------------------
// Initialisation
// ~~~~~~~~~~~~~~
for (var i = 0; i < PIX; i++) {
    pic[i] = new Slide ; // pic[i] fields redefined in the <xx>data.js file for the page
    // successors often redefined on the HTML page to make a specific show
    if (i+1 < PIX) pic[i].next = i+1 ;
}

pic[0].file = 'loading.png' ;
pic[0].title = 'Loading...' ;
pic[0].time = 1 ; // Only short delay while pic No.0 ('Loading...') displays

//--------------------------------------------------------------------------------
// Main functions
// ~~~~~~~~~~~~~~

// Return the successor of an integer -
// coded as a function for consistency with pred() and to allow for out-of -range arguments
function succ(n) { return n < 0  ?  1  :  n < PIX ? Math.max(1, pic[n].next) : NaN }

// Return the predecessor of an integer  n  (the No. of the pic preceding pic[n])
// [If there is more than 1 predecessor, return the last
//  unless pic[n] is it's own successor, in which case return  n  itself;
//  If there is no predecessor, return NaN. ]
function pred(n) {
    var p = NaN ; // the predecessor

    for (var i = 0 ; i < PIX ; i++) {
        if (pic[i].next == n)  p = i
    }

    return (pic[n].next == n) ? n : p
} // ---------------------------------------



// Show pic number n with title. blurb etc, and update picNo variable
function showPic(n) {
	// remember which pic is showing
	picNo = n ;
	
	with (document) {
	    // Show pic centred
		getElementById('screen').src = "../jpeg/" + pic[n].file ;
		/* getElementById('bigScreen').style.marginLeft = (-pic[n].width / 2) + 'px' ;	 */
		
		// Show title
		title = pic[n].title;
		getElementById('screen').alt = title ;
		getElementById('screen').title = pic[n].title ;
		getElementById('title').innerHTML = pic[n].title ;	
		getElementById('caption').innerHTML = pic[n].year + pic[n].blurb ;		
	}
}
 