/* PhotoGallery.js
 * 
 * Script to support the Photo Gallery function on the Stellafane.org Home Page
 *
 * written by Ken Slater on 2006-Mar-13
 * Updated by Ken Slater on 2007-Sep-12 to added sequence array and string indexing
 */
		
/* The Phgal object holds all the Photo Gallery data */
var Phgal   = new Object();// The Photo Gallery Object
Phgal.out   = new Array(); // Mouse Out images
Phgal.ovr   = new Array(); // Mouse Over images
Phgal.url   = new Array(); // Hyperlinks
Phgal.plout = new Array(); // Preload images
Phgal.plovr = new Array(); // Preload images
Phgal.seq   = new Array(); // Sequence Map of normal images
Phgal.imgTimer = 8; 	   // Image change interval in seconds
Phgal.imgIndex = 1;        // Current image index 
Phgal.galTimerID = null;   // Timer ID to trigger 'slide changes', null if timer is off
Phgal.mode = true;         // True for Play, false for Pause

// The Sequence Map of rotating photos - this is what will actually get shown
//Phgal.seq[01] = 'SP';  // Star Party
//Phgal.seq[02] = 'C1';  // Convention 1
Phgal.seq[01] = 'P3';  // Pavilion 3
//Phgal.seq[04] = 'C2';  // Convention 2
Phgal.seq[02] = 'MC';  // Mirror Classs
Phgal.seq[03] = 'NL';  // National Historic Landmark

Phgal.imgCount = Phgal.seq.length-1; // Number of regular images in list, 1 thru N (Zero not used)
	
// The normal gallery photo list
// -- IYA: International Year of Astronomy 2009
Phgal.out['IYA'] = "homepage-gallery/GalleryPhoto-IYA.jpg";
Phgal.ovr['IYA'] = "homepage-gallery/GalleryPhoto-IYA-HL.jpg";
Phgal.url['IYA'] = "news/index.html#IYA2009";
// -- REG: New Convnetion Registration
Phgal.out['REG'] = "homepage-gallery/GalleryPhoto-Reg.jpg";
Phgal.ovr['REG'] = "homepage-gallery/GalleryPhoto-Reg-HL.jpg";
Phgal.url['REG'] = "convention/2008/index.html";
// -- SP: Star Party
Phgal.out['SP'] = "homepage-gallery/GalleryPhoto-Starparty.jpg";
Phgal.ovr['SP'] = "homepage-gallery/GalleryPhoto-Starparty-HL.jpg";
Phgal.url['SP'] = "news/index.html#StarParty";
// -- ORG: Change to .ORG (temp)
Phgal.out['ORG'] = "homepage-gallery/GalleryPhoto-Org.jpg";
Phgal.ovr['ORG'] = "homepage-gallery/GalleryPhoto-Org-HL.jpg";
Phgal.url['ORG'] = "news/news2007.html#ORG";
// -- BB: BBSO Wildfire (Temp)
Phgal.out['BB'] = "homepage-gallery/GalleryPhoto-BBSO.jpg";
Phgal.ovr['BB'] = "homepage-gallery/GalleryPhoto-BBSO-HL.jpg";
Phgal.url['BB'] = "news/news2007.html#Wildfire";
// -- MC: Mirror Class
Phgal.out['MC'] = "homepage-gallery/GalleryPhoto-MCW.jpg";
Phgal.ovr['MC'] = "homepage-gallery/GalleryPhoto-MCW-HL.jpg";
Phgal.url['MC'] = "tm/mc/index.html";
// -- P1: Pavilion 1
Phgal.out['P1'] = "homepage-gallery/GalleryPhoto-FlandersClosed.jpg";
Phgal.ovr['P1'] = "homepage-gallery/GalleryPhoto-FlandersClosed-HL.jpg";
Phgal.url['P1'] = "help/pavilion.html";
// -- P2: Pavilion 2
Phgal.out['P2'] = "homepage-gallery/GalleryPhoto-FlandersOpen.jpg";
Phgal.ovr['P2'] = "homepage-gallery/GalleryPhoto-FlandersOpen-HL.jpg";
Phgal.url['P2'] = "help/pavilion.html";
// -- P2: Pavilion 2
Phgal.out['P3'] = "homepage-gallery/GalleryPhoto-FlandersConv06.jpg";
Phgal.ovr['P3'] = "homepage-gallery/GalleryPhoto-FlandersConv06-HL.jpg";
Phgal.url['P3'] = "help/pavilion.html";
// -- NL: National Historic Landmark
Phgal.out['NL'] = "homepage-gallery/GalleryPhoto-Landmark.jpg";
Phgal.ovr['NL'] = "homepage-gallery/GalleryPhoto-Landmark-HL.jpg";
Phgal.url['NL'] = "history/index.html";
// -- OB: Observing
Phgal.out['OB'] = "homepage-gallery/GalleryPhoto-Dome.jpg";
Phgal.ovr['OB'] = "homepage-gallery/GalleryPhoto-Dome-HL.jpg";
Phgal.url['OB'] = "observing/index.html";
// -- C1: Convention
Phgal.out['C1'] = "homepage-gallery/GalleryPhoto-Conv.jpg";
Phgal.ovr['C1'] = "homepage-gallery/GalleryPhoto-Conv-HL.jpg";
Phgal.url['C1'] = "convention/2009/index.html";
// -- C2: Convention Kids
Phgal.out['C2'] = "homepage-gallery/GalleryPhoto-ConvKids.jpg";
Phgal.ovr['C2'] = "homepage-gallery/GalleryPhoto-ConvKids-HL.jpg";
Phgal.url['C2'] = "convention/2009/index.html";

// The special gallery photo list for permanent Home Page Button Support
// -- LM: Little Man
Phgal.out['LM']  = "homepage-gallery/GalleryPhoto-LittlemanPopup.jpg";
Phgal.ovr['LM']  = Phgal.out['LM'];
Phgal.url['LM']  = "history/early/index.html#early";
// -- ABT: About Button
Phgal.out['ABT'] = "homepage-gallery/GalleryPhoto-AboutPopup.jpg";
Phgal.ovr['ABT'] = Phgal.out['ABT'];
Phgal.url['ABT'] = "about/index.html";

// Public Methods
Phgal.startup = function () { // Init Photo Gallery
	this.preload_popover();	  // Preload popovers images
	this.updateDisplay( this.seq[this.imgIndex], false ); // Initialize Dispaly
	this.start(); 	//Start the Gallery Timer
	return( true );
}

Phgal.rollover =  function() { // Mouse has rolled over gallery
    this.stop();	// Stop timer
    this.updateDisplay( this.seq[this.imgIndex], true );	// Switch to highlighted image
    window.status = this.url[ this.seq[this.imgIndex] ];
    return( true );
}

Phgal.rollout = function() { // Mouse has rolled out of gallery
    this.updateDisplay( this.seq[this.imgIndex], false );	// Switch to normal image
    this.start();	// Start timer
    window.status = "";
    return( true );
}

Phgal.click = function() { // User clicked image
    location.href = this.url[ this.seq[this.imgIndex] ]; return true;
}

Phgal.popover = function(str) { // Mouse has rolled over popup button
    this.stop();// Stop timer
    this.updateDisplay( str, true );	// Switch to named popup image
    window.status = this.url[ str ];
    return( true );
}

Phgal.popout = function () { // Mouse has rolled out of popup button
	return( this.rollout() );	// Restore to normal image
}

Phgal.next = function() { // Called by 'VCR' forward button & timer
    this.reset();	// Reset timer
    // Advance image
	this.imgIndex++; if (this.imgIndex > this.imgCount) this.imgIndex = 1;
	this.updateDisplay( this.seq[this.imgIndex], false );	
    return( true );    
}

Phgal.prev = function() { // Called by 'VCR' reverse button
    this.reset(); // Reset timer
    // Backup image
	this.imgIndex--; if (this.imgIndex < 1) this.imgIndex = this.imgCount;
	this.updateDisplay( this.seq[this.imgIndex], false );
    return( true );
}

Phgal.playPause = function() { // Called by 'VCR' play/pause buttonf
    this.mode = !this.mode; // Toggle timer mode
	this.mode ? this.start() : this.stop();	// Set timer based on mode   
	changeImages( "vcrPP", this.moce ? "homepage-images/vcr-pause-HL.png" : "homepage-images/vcr-play-HL.png" );
	if ( this.mode ) { this.next() } // Be responsive, go to next image on play right away
    return( true );
}

Phgal.ppover = function() { // Play/pause rollover
	changeImages( 'vcrPP', (this.galTimerID==null)?'homepage-images/vcr-play-HL.png':'homepage-images/vcr-pause-HL.png'); 
	return true;
}

Phgal.ppout = function() { // Play/pause rollout
	changeImages( 'vcrPP', (this.galTimerID==null)?'homepage-images/vcr-play.png':'homepage-images/vcr-pause.png'); 
	return true;
}

// Private Methods
Phgal.preload_popover = function () { // Preload popover gallery images for better response
    for ( s in this.ovr ) {
        if ( !(s>=1 && s<=this.imgCount) ) { 
            this.preload_item( s ); 
        }
    }
}

Phgal.preload_item = function( n ) { // Preloads an individual item
    if ( isInteger( n ) && n > this.imgCount ) { return; }
    if ( this.plout[ n ]==undefined || !this.plout[ n ].complete ) {
        if ( isInteger( n ) ) { 
            this.plout[ n ] = newImage( this.out[ n ] ); 
        }
    }
    if ( !this.plovr[ n ] || !this.plovr[ n ].complete ) {
        this.plovr[ n ] = newImage( this.ovr[ n ] ); 
    }
}

Phgal.updateDisplay = function( ii, over ) { // Common display update routine
	changeImages( 'GalleryImg', over ? this.ovr[ ii ] : this.out[ ii ] ) // Main image
	var VCRstr = getIdx( ii ) + ' of ' + this.imgCount;
	document.getElementById( "galleryNumber" ).firstChild.nodeValue = VCRstr;
    if ( isInteger( ii ) ) { this.preload_item( ii ); this.preload_item( ii+1 ) }
}

Phgal.start = function() { // Start the timer
	if (this.galTimerID) clearInterval( this.galTimerID );
	if (this.mode) { this.galTimerID = setInterval( "Phgal.next();", this.imgTimer*1000 ) } 
}

Phgal.stop = function() { // Stop the timer
		if ( this.galTimerID ) clearInterval( this.galTimerID );
		this.galTimerID = null;
}

Phgal.reset = function() { this.start() } // Reset the timer
// Currently, Start does a reset, so they are the same function

/* Utility Function */
function isInteger( arg ) { 
   var ValidChars = "0123456789";
   for ( i=0; i<arg.length; i++ ) {
       if ( ValidChars.indexOf( arg.charAt( i ) ) == -1 ) { return( false ) }
       }
   return( true );
}

function getIdx( s ) {
    // Given am index string s, return its numerical index in the seq array, or '-' if it does not exisit
    for( var k=1; k<=Phgal.imgCount; k++ ) { 
    	if( Phgal.seq[k].toString() == s.toString() ) { return( k )} 
    }
    return( '-' );
}

/***** End of PhotoGallery.js *****/