/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2007 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

/* Global Variables */

var thumbnails;			/* array of thumbnails */
var currentImageIndex;		/* index into thumbnails array */
var mainPhoto;			/* The central image */

var MAIN_PHOTO_WIDTH = 460;	/* Width each image is resized to when first loaded */


/* Initialisation */
addEvent(window, 'load', init, false);

function init() {
    if (!document.getElementById) {
			/* Required functionality for even the most basic DHTML. */
			return;
    }
    /* Set up thumbnails */
    var thumbholder = document.getElementById('gsThumbMatrix');
    if (!thumbholder) {
			setupBackToAlbumLink(); /* Not an album page */
			return;
    }
    thumbnails = thumbholder.getElementsByTagName('img');

		currentImageIndex = 0;

		var url = thumbnails[currentImageIndex].parentNode.href
	+ (thumbnails[currentImageIndex].parentNode.href.indexOf('?') >= 0 ? '&' : '?')
	+ 'thumbIndex=' + currentImageIndex;

		//window.location = url;
}

/* Called each time a new image is switched to */
function setupMainImage() {
    mainPhoto = document.getElementById('main-image');

    if (slideshowImageWidths[currentImageIndex] < 0 || MAIN_PHOTO_WIDTH <= slideshowImageWidths[currentImageIndex]) {
			mainPhoto.style.width = MAIN_PHOTO_WIDTH + 'px';
    }
		else {
			/*
			 * The image is below our ideal slideshow width, so we don't want to stretch it
			 * horizontally.  Instead we center the image that we have a little better.
			 */
			mainPhoto.style.width = slideshowImageWidths[currentImageIndex] + 'px';

			/*mainPhoto.style.left = parseInt(getElementStyle(mainPhoto.id, 'left')) +
					((MAIN_PHOTO_WIDTH - slideshowImageWidths[currentImageIndex])/2) + 'px';*/
    }
    mainPhoto.style.height = 'auto'; /* @REVISIT - Safari doesn't play nice with this */

    mainPhoto.origWidth = parseInt(getElementStyle(mainPhoto.id, 'width'));
    mainPhoto.origLeft = parseInt(getElementStyle(mainPhoto.id, 'left'));
    mainPhoto.galleryimg = 'no'; /* Disables IE's image toolbar, which was causing flicker */

		/* alert('url = ' + thumbnails[currentImageIndex].parentNode.href
	+ (thumbnails[currentImageIndex].parentNode.href.indexOf('?') >= 0 ? '&' : '?')
	+ 'thumbIndex=' + currentImageIndex); */

    //addEvent(mainPhoto, 'click', showPhotoDetails, false);
}

/* Photo page functions */
function setupBackToAlbumLink() {
    var pageLinks = document.getElementsByTagName('a');
    for (var i = 0; i < pageLinks.length; i++) {
			if (pageLinks[i].className.match(/\bbacktoalbum\b/)) {
					addEvent(pageLinks[i], 'click', saveCurrentImageIndex, false);
			}
    }
}


