hs.graphicsDir = '/static/highslide/4.1.5/graphics/';

hs.transitions = ['expand', 'crossfade'];
hs.restoreCursor = null;
hs.lang.previousTitle = 'Précédent (Flèche gauche)',
hs.lang.nextTitle     = 'Suivant (Flèche droite)',
hs.lang.playTitle     = 'Défilement automatique',
hs.lang.pauseTitle    = 'Arrêt du défilement automatique',
hs.lang.number        = 'Photo %1 sur %2',
hs.lang.restoreTitle  = 'Cliquer pour voir la photo suivante';
hs.dynamicallyUpdateAnchors = false; // Optimisation.
hs.showCredits = false;
hs.minHeight = 0;
hs.minWidth  = 0;

// Add the slideshow providing the controlbar and the thumbstrip
hs.addSlideshow({
	interval: 5000,
	repeat: true,
	useControls: true,
	overlayOptions: {
		position: 'bottom right',
		offsetY: 50
	},
	thumbstrip: {
		position: 'above',
		mode: 'horizontal',
		relativeTo: 'expander'
	}
});

// Options for the in-page items
var inPageOptions = {
	outlineType: null,
	allowSizeReduction: true,
	wrapperClassName: 'in-page controls-in-heading',
	useBox: true,
	targetX: 'gallery-area 10px',
	targetY: 'gallery-area 1px', // Margin to preserve the gallery-area border
	captionEval: 'this.thumb.alt',
	numberPosition: 'caption'
}

function adjustGalleryAreaSize() {
	var gallery_area = document.getElementById('gallery-area'); // TODO Error handling.

	// Adjust height.

	hs.getPageSize();
	var adjusted_gallery_area_height = hs.page.height
	                                 - gallery_area.offsetTop // Keep everything above the gallery visible.
	                                 - 2                      // From inPageOptions.targetY (gallery-area border).
	                                 - 2;                     // Unknown???
	var Y_margin_between_gallery_area_and_expander = 50 // From hs.addSlideshow.overlayOptions.offsetY
                                                   + 47 // From .highslide-marker top property
                                                   +  7;// Unknown???
	var expander_height = adjusted_gallery_area_height - Y_margin_between_gallery_area_and_expander;
	if (expander_height < 100) { // Display is too small.
		expander_height = 100;
		adjusted_gallery_area_height = expander_height + Y_margin_between_gallery_area_and_expander;
	}
	inPageOptions.height = expander_height;
    gallery_area.style.height = adjusted_gallery_area_height + 'px';

    // Adjust width. (After adjusting height, to allow for the vertical scrollbar width)

    gallery_area.style.width = (gallery_area.offsetWidth - 2) + 'px';  // Do not allow horizontal resizing.
	inPageOptions.width = gallery_area.offsetWidth - 20;  // From inPageOptions.targetX
}

// Open the first thumb on page load
hs.addEventListener(window, 'load', function() {
	document.getElementById('photos-container').style.display = 'none';
	adjustGalleryAreaSize();
	document.getElementById('thumb1').onclick();
});

// Cancel the default action for image click and do next instead
hs.Expander.prototype.onImageClick = function() {
	if (/in-page/.test(this.wrapper.className))	return hs.next();
}

// Under no circumstances should the static popup be closed
hs.Expander.prototype.onBeforeClose = function() {
	if (/in-page/.test(this.wrapper.className))	return false;
}
// ... nor dragged
hs.Expander.prototype.onDrag = function() {
	if (/in-page/.test(this.wrapper.className))	return false;
}

// Keep the position after window resize
hs.addEventListener(window, 'resize', function() {
	var i, exp;
	hs.getPageSize();

	for (i = 0; i < hs.expanders.length; i++) {
		exp = hs.expanders[i];
		if (exp) {
			var x = exp.x,
				y = exp.y;

			// get new thumb positions
			exp.tpos = hs.getPosition(exp.el);
			x.calcThumb();
			y.calcThumb();

			// calculate new popup position
		 	x.pos = x.tpos - x.cb + x.tb;
			x.scroll = hs.page.scrollLeft;
			x.clientSize = hs.page.width;
			y.pos = y.tpos - y.cb + y.tb;
			y.scroll = hs.page.scrollTop;
			y.clientSize = hs.page.height;
			exp.justify(x, true);
			exp.justify(y, true);

			// set new left and top to wrapper and outline
			exp.moveTo(x.pos, y.pos);
		}
	}
});
