
// club home slideshows

var SLIDESHOW_TIMEOUT = 5000; // msecs before refresh
var SLIDESHOW_LOOP = true; // loop when done
var SLIDESHOW_TIMER = null;

function slideshow_update(elslide, eltarget)
{
	var ajax = new Ajax('post', '/ajax/slideshow/');

	ajax.add_value();
	window.setTimeout(function() {
		slideshow_update(elslide, eltarget);
	}, SLIDESHOW_TIMEOUT);
}

function slideshow_play(el, from_timeout)
{
	if (SLIDESHOW_TIMER && !from_timeout) {
		return false;
	}
	if (el) {
		el.className = 'selected';
	}
	// currently selected item
	var curr = eltarget.getElementsByTagName('DIV')[1];
	var currid = curr.id.replace(/^slidetarget-/, '');
	var currsel = document.getElementById('slidethumb-' + currid);
	currsel.className = currsel.className.replace(/selected/, '');
	var ns = currsel.nextSibling ? currsel.nextSibling : null;
	while (!(ns.tagName && ns.tagName.toUpperCase() == 'LI')) {
		if (ns.nextSibling) {
			ns = ns.nextSibling;
		} else {
			if (SLIDESHOW_LOOP) {
				ns = elslide.getElementsByTagName('LI')[0];
			} else {
				slideshow_pause();
				return;
			}
		}
	}
	var d = document.createElement('DIV');
	d.className = 'ajax-loader';
	var ds = eltarget.getElementsByTagName('DIV');
	for (var i = 0; i < ds.length; i++) {
		if (ds[i].className && ds[i].className == 'slide') {
			var r = ds[i].getElementsByTagName('IMG')[0];
			break;
		}
	}
	var h = r.offsetHeight + 2;
	var i = document.createElement('IMG');
	i.src = '/img/ajax-loader.gif';
	i.style.marginTop = ((h - 66) / 2) + 'px';
	d.appendChild(i);
	d.style.height = h + 'px';
	d.style.width = r.offsetWidth + 'px';
	if (isIE) {
		d.style.marginLeft = -(r.offsetWidth + 4) + 'px';
	} else {
		// sane browsers
		d.style.marginTop = -h + 'px';
		d.style.marginLeft = (document.getElementById('rightcol').offsetWidth - 30) / 2 - r.offsetWidth / 2 + 'px';
	}
	r.parentNode.appendChild(d);
	var ajax = new Ajax('post', '/ajax_' + (window.location.href.split('/')[3]) + 'slideshowimg/');
	ajax.add_value('photo_id', ns.id.replace(/^.*?-/, ''));
	ajax.set_readystatehandler(function() {
		var ds = eltarget.getElementsByTagName('DIV');
		var realtarget = null;
		for (var i = 0; i < ds.length; i++) {
			if (ds[i].className && ds[i].className == 'slide') {
				break;
			}
		}
		eltarget.removeChild(ds[i]);
		eltarget.innerHTML += this.http.responseText;
		ns.className += ' selected';
		SLIDESHOW_TIMER = window.setTimeout(function() { slideshow_play(null, true); }, SLIDESHOW_TIMEOUT);
	});
	ajax.execute();
}

function slideshow_pause(el)
{
	var e = el.parentNode;
	while (!(e.tagName && e.tagName.toUpperCase() == 'UL')) {
		e = e.parentNode;
	}
	var lis = e.getElementsByTagName('LI');
	for (var i = 0; i < lis.length; i++) {
		if (lis[i].className && lis[i].className == 'play') {
			lis[i].getElementsByTagName('SPAN')[0].className = '';
			break;
		}
	}
	window.clearTimeout(SLIDESHOW_TIMER);
	SLIDESHOW_TIMER = null;
}

var elslide = eltarget = elnav = null;

bodyloadfunction(function() {
	var r = document.getElementById('rightcol');
	var rd = r.getElementsByTagName('DIV');
	for (var i = 0; i < rd.length; i++) {
		if (rd[i].className) {
			if (rd[i].className.match(/ scrollbar /)) {
				elslide = rd[i];
			} else if (rd[i].className == 'box') {
				eltarget = rd[i];
			} else if (rd[i].className.match(/^nav/)) {
				elnav = rd[i];
			}
		}
	}
	if (!(elslide && eltarget && elnav)) {
		return false;
	}
	var btns = new Array();
	var btnnames = {'play': ['Play', 'slideshow_play'], 'pause': ['Pause', 'slideshow_pause']};
	var u = elnav.getElementsByTagName('UL')[0];
	for (var i in btnnames) {
		var l = document.createElement('LI');
		l.className = i;
		if (i == 'play' && !elnav.className.match(/inside$/)) {
			var margin = elnav.offsetWidth / 2;
			margin -= 37; // width of play/pause buttons
			if (!isIE6 && u.getElementsByTagName('LI').length && u.getElementsByTagName('LI')[0].className == 'prev') {
				margin -= 82; // compensate voor previous button, except in ie6 which doesn't seem to respect anything
			} else if (isIE6) {
				// double-margin bug
				margin /= 2;
			}
			l.style.marginLeft = margin + 'px';
		}
		var s = '<span title="';
		s += i == 'play' ? 'Start slideshow' : 'Pause slideshow';
		s += '"';
		if (isIE6) {
			s += ' onmouseover="this.className = \'hover\'"';
			s += ' onmouseout="this.className = \'\'"';
		}
		s += ' onclick="' + btnnames[i][1] + '(this)"';
		s += '>' + btnnames[i][0] + '</span>';
		l.innerHTML = s;
		var lis = u.getElementsByTagName('LI');
		if (lis.length) {
			if (!lis[lis.length - 1].className.match(/^next/)) {
				u.appendChild(l);
			} else {
				u.insertBefore(l, lis[lis.length - 1]);
			}
		}
	}
	// needed since we're dealing with inline-blocks...
	var html = u.innerHTML;
	html = html.replace(/\/li>\s+<li/g, '/li><li');
	u.innerHTML = html.split('\n').join('');
});

