// auto-play? stop on mouseover?
// get images

var slide_frequency = 40;
var slide_duration = 650;
var image_offset = 500 * 1000 / (slide_frequency * slide_duration);
var caption_offset_top = 3 * 30 * 1000 / (slide_frequency * slide_duration);
var caption_offset_bottom = 3 * 90 * 1000 / (slide_frequency * slide_duration);

var images = new Array();
var tmp;
for (var i in slideshow) {
	tmp = new Image();
	tmp.src = '/' + slideshow[i].image;
	images[i] = tmp;
}

var image = document.getElementById('slideshow_image');
var captionTop = document.getElementById('slideshow_top');
var captionBottom = document.getElementById('slideshow_bottom');

var textLink = document.getElementById('slideshow_imgwrapper');
var textHeadline = document.getElementById('slideshow_headline');
var textDate = document.getElementById('slideshow_date');
var textTime = document.getElementById('slideshow_time');
var textVenue = document.getElementById('slideshow_venue');
var textTickets = document.getElementById('slideshow_tickets');

var baseUrl = textLink.href.substr(0, textLink.href.lastIndexOf('=') + 1);

var locked = false;
var index = 0;
var posImage = 0;
var posCaptionTop = 0;
var posCaptionBottom = 0;
var clkImage = null;
var clkCaption = null;

var initialposCaptionTop = -300;
var initialposCaptionBottom = -145;

image.style.position = 'relative';
image.parentNode.style.display = 'block';
image.parentNode.style.width = '500px';
image.parentNode.style.height = '300px';

var nextimage = document.createElement('img');
nextimage.src = images[images.length > 1 ? 1 : 0].src;
nextimage.style.width = '500px';
nextimage.style.height = '300px';
nextimage.style.position = 'relative';
nextimage.style.top = '-303px';
nextimage.style.left = '500px';

image.parentNode.appendChild(nextimage);

function slide(direction) {
	if (!locked) {
		locked = true;
		index += direction == 'left' ? -1 : 1;
		if (index < 0) {
			index = slideshow.length - 1;
		}
		if (index >= slideshow.length) {
			index = 0;
		}
		nextimage.src = images[index].src;
		clkImage = setInterval('animateImage(\'' + direction + '\');', 1000 / slide_frequency);
		clkCaption = setInterval('animateCaption(\'hide\');', 1000 / slide_frequency);
	}
}

function resetImages() {
//	image.onload = null;
	posImage = 0;
	image.style.left = '0px';
	nextimage.style.left = '500px';
	clkImage = null;
	if (!clkCaption) {
		locked = false;
	}
}

function animateImage(direction) {
	posImage += (direction == 'left' ? 1 : -1) * image_offset;
	if (Math.abs(posImage) > 500) {
		clearInterval(clkImage);
//		image.onload = resetImages;
		image.src = nextimage.src;
		setTimeout('resetImages();', 100);
	}
	else {
		image.style.left = Math.round(posImage) + 'px';
		nextimage.style.left = Math.round(posImage - (direction == 'left' ? 1 : -1) * 500) + 'px';
	}
}

function resumeAnimation() {
	clkCaption = setInterval('animateCaption(\'show\');', 1000 / slide_frequency);
}

function animateCaption(action) {
	posCaptionTop -= (action == 'hide' ? 1 : -1) * caption_offset_top;
	posCaptionBottom += (action == 'hide' ? 1 : -1) * caption_offset_bottom;
	if (Math.abs(posCaptionTop + 15) > 15) {
		clearInterval(clkCaption);
		if (action == 'hide') {
			posCaptionTop = -30;
			posCaptionBottom = 90;
			captionTop.style.top = (initialposCaptionTop - 30) + 'px';
			captionBottom.style.top = (initialposCaptionBottom + 90) + 'px';
			textLink.href = baseUrl + slideshow[index].id;
			textHeadline.firstChild.data = slideshow[index].title;
			textDate.firstChild.data = slideshow[index].date;
			textTime.firstChild.data = slideshow[index].time;
			textVenue.firstChild.data = slideshow[index].venue;
			textTickets.firstChild.data = slideshow[index].tickets;
			setTimeout('resumeAnimation();', slide_duration / 2);
		}
		else {
			posCaptionTop = 0;
			posCaptionBottom = 0;
			captionTop.style.top = initialposCaptionTop + 'px';
			captionBottom.style.top = initialposCaptionBottom + 'px';
			clkCaption = null;
			if (!clkImage) {
				locked = false;
			}
		}
	}
	else {
		captionTop.style.top = Math.round(initialposCaptionTop + posCaptionTop) + 'px';
		captionBottom.style.top = Math.round(initialposCaptionBottom + posCaptionBottom) + 'px';
	}
}

setInterval('slide(\'right\');', 5000);


