// JavaScript Document

var lightbox; // the lightbox element
var lightboxElements = Array(); // array of elements in lightbox (obviously)
var lightboxStart = 0; // first element shown
var lightboxSize = 4; // number of elements shown at one time
var lightboxNextButton;
var lightboxPreviousButton;

function setupLightbox() {
	/*
		Function looks for a div (actually any element) with id='lightbox'. (This limits you to one lightbox per page, but that's probably good design anyway.)
		The div is assumed to have as its child nodes (ignoring text nodes) the elements that are to be slid along. They can be simply images, or DLs, etc.
		If the lightbox div has a numeric className, that is taken as the size, i.e. the number of childNodes to be displayed at once.
		The lightbox is also assumed to have a header -- immediately before the lightbox div -- into which the control buttons will be placed.
		
		These scripts play nice with lightbox.css.
	*/
	if( document.getElementById('lightbox') ) {
		lightbox = document.getElementById('lightbox');
		
		// Determine the size of the lightbox
		var classes = lightbox.className.split(' ');
		for( var i=0; i<classes.length; i++ ) {
			if( classes[i] == parseInt(classes[i]) ) {
				lightboxSize = parseInt(classes[i]);   
			}
		}
		
		// Find the elements of the lightbox
		for( var i=0; i<lightbox.childNodes.length; i++ ) {
			if( lightbox.childNodes[i].nodeType == 1 ) {
				lightboxElements[lightboxElements.length] = lightbox.childNodes[i];
			}
		}
		
		// Hide elements that don't fit
		lightboxStart = 0; // first element shown
		for( i=lightboxSize; i<lightboxElements.length; i++ ) {
			lightbox.removeChild(lightboxElements[i]);
		}
		
		// Find the lightbox header
		var lightboxHeader = lightbox.previousSibling;
		while( lightboxHeader.nodeName.substr(0,1).toLowerCase() != 'h' ) {
			lightboxHeader = lightboxHeader.previousSibling;
		}
		
		// Add next/previous buttons
		if( lightboxSize < lightboxElements.length ) {
			lightboxButtons = document.createElement('SPAN');
			lightboxButtons.id = 'lightboxButtons';
			
			var span = document.createElement('SPAN');
			var newText = document.createTextNode('More: ');
			span.appendChild(newText);
			lightboxButtons.appendChild(span);
			
			lightboxPreviousButton = document.createElement('A');
			lightboxPreviousButton.id = 'lightboxPreviousButton';
			lightboxPreviousButton.className = 'previous-off'; // previous button is off, since we're at the start
			addEvent(lightboxPreviousButton,'click',advanceLightbox);
			newText = document.createTextNode('previous');
			lightboxPreviousButton.appendChild(newText);
			lightboxButtons.appendChild(lightboxPreviousButton);
			
			newText = document.createTextNode(' ');
			lightboxButtons.appendChild(newText);
			
			lightboxNextButton = document.createElement('A');
			lightboxNextButton.id = 'lightboxNextButton';
			lightboxNextButton.className = 'next-on'; // next button is on 
			addEvent(lightboxNextButton,'click',advanceLightbox);
			newText = document.createTextNode('next');
			lightboxNextButton.appendChild(newText);
			lightboxButtons.appendChild(lightboxNextButton);
			
			lightboxHeader.appendChild(lightboxButtons);
		}
	}

	return;
}

function advanceLightbox(evt) {
	// Slides lightbox in either direction, depending on which button was clicked.
	var button = findEventOwner(evt);
	if( button.id == 'lightboxNextButton' ) {
		if( lightboxStart + lightboxSize < lightboxElements.length ) {
			lightbox.removeChild(lightboxElements[lightboxStart]);
			lightbox.appendChild(lightboxElements[lightboxStart + lightboxSize]);
			lightboxStart++;
		}
	}
	else if( button.id == 'lightboxPreviousButton' ) {
		if( lightboxStart > 0 ) {
			lightbox.removeChild(lightboxElements[lightboxStart+lightboxSize-1]);
			lightbox.insertBefore(lightboxElements[lightboxStart-1],lightbox.firstChild);
			lightboxStart--;
		}
	}
	setLightboxButtons();
}

function setLightboxButtons() {
	// Turn buttons on or off as needed
	if( lightboxStart > 0 ) {
		lightboxPreviousButton.className = 'previous-on';
	}
	else {
		lightboxPreviousButton.className = 'previous-off';
	}
	if( lightboxStart + lightboxSize < lightboxElements.length ) {
		lightboxNextButton.className = 'next-on';
	}
	else {
		lightboxNextButton.className = 'next-off';
	}
}





