/**
* This file demonstrates how a Highslide gallery can be extended to work across pages
*/


hs.graphicsDir = 'highslide/graphics/';
hs.align = 'center';
hs.transitions = ['expand', 'crossfade'];
hs.outlineType = 'rounded-white';
hs.fadeInOut = true;
//hs.dimmingOpacity = 0.75;

// Add the controlbar
hs.addSlideshow({
	slideshowGroup: 'galerie',
	crosspage: true,
	interval: 5000,
	repeat: false,
	useControls: true,
	fixedControls: 'fit',
	overlayOptions: {
		opacity: .75,
		position: 'bottom center',
		hideOnMouseOut: true
	}
});

// Define what pages to loop through


// Modify the previousOrNext method to jump to the next page when the last thumb is reached
hs.basePreviousOrNext = hs.previousOrNext;
hs.previousOrNext = function(el, op) {
	var exp = hs.getExpander(el);
    var autoplay = '';
	if (exp) {
		if (exp.slideshow && exp.slideshow.autoplay) autoplay = '&autoplay=true' ;
		if (exp.a.isLast && hs.next_offset>-1 && op == 1) {
            window.location= hs.crossbase+'?galerie='+hs.gallery+'&offset='+hs.next_offset+'&autoload=erstes'+autoplay;
        }
        else if (exp.a.isFirst && hs.prev_offset>-1 && op == -1) {
            window.location= hs.crossbase+'?galerie='+hs.gallery+'&offset='+hs.prev_offset+'&autoload=letztes';
		}
		else return hs.basePreviousOrNext(el, op);
	}
	return false;
}
hs.Slideshow.prototype.checkFirstAndLast = function() {
	/// disable first and last previous and next
	if (this.repeat || !this.controls) return;
	var exp = hs.expanders[this.expKey],
		cur = exp.getAnchorIndex(),
		re = /disabled$/,
		isFirst = (cur == 0),
		isLast = (cur + 1 == hs.anchors.groups[exp.slideshowGroup || 'none'].length);

	// crosspage addition
	if (this.crosspage) {
		// keep previous button on subsequent pages
		if (isFirst) {
			isFirst = false; // do not disable
			exp.a.isFirst = true;
		}
		// keep next button on all pages but the last
		if (isLast) {
			isLast = false; // do not disable
			exp.a.isLast = true;
		}
	}
	// end crosspage addition

	if (isFirst)
		this.disable('previous');
	else if (re.test(this.btn.previous.getElementsByTagName('a')[0].className))
		this.enable('previous');
	if (isLast) {
		this.disable('next');
		this.disable('play');
	} else if (re.test(this.btn.next.getElementsByTagName('a')[0].className)) {
		this.enable('next');
		this.enable('play');
	}
}
// Open a specific thumbnail based on querystring input.
hs.addEventListener(window, "load", function() {
	// get the value of the autoload parameter
	var autoload = /[?&]autoload=([^&#]*)/.exec(window.location.href);
	// virtually click the anchor
	if (autoload) document.getElementById(autoload[1]).onclick();
});

// Pick up and apply the autoplay parameter
hs.Expander.prototype.onInit = function() {
	var autoplay = /[?&]autoplay=([^&#]*)/.exec(window.location.href);
	this.autoplay = (autoplay && autoplay[1] == 'true');
}
