var previewinterval = "";

function photoChange(inc) {
	// Loop though all images in main list box, and turn off the currently active one.
	photos = document.getElementById('AC-PhotoJS').getElementsByTagName('div');
	for(i=0;i<photos.length;i++) {
		if(/PhotoOn/.test(photos[i].className)) {
			photos[i].className = 'AC-PhotoOff'
			cursor = i;
		}
	}
	
	// Check cursor position...which image WAS on?  Constant refers to number of extra divs in box (controls for this usage).
	if ((cursor + inc) > (photos.length - 5)) {
		cursor = 1;
	} else if (cursor + inc < 1) {
		cursor = (photos.length - 5);
	} else {
		cursor = cursor + inc;
	}
	
	// Update counter text and turn on next graphic.
	var photoCountNewText = document.createTextNode('' + (cursor) + ' of ' + (photos.length - 5));
	photos[0].replaceChild(photoCountNewText, photos[0].childNodes[0]);
	photos[cursor].className = 'AC-PhotoOn'
}

function photoExpand(imgSrc) {
	if(previewinterval == "") {
		// build loading bar and show while main image is loading (using SetInterval to recursively check load status)
		var photoLoadingCt = document.createElement('div');
		photoLoadingCt.id	= "AC-PhotoLoading";
		var photoLoadingText = document.createTextNode('Loading...');
		photoLoadingCt.appendChild(photoLoadingText);
		document.getElementById('AC-PhotoViewer').appendChild(photoLoadingCt);
		document.getElementById('AC-PhotoViewerImg').onclick = function() {photoRemove()};
		document.getElementById('AC-PhotoViewer').style.display = "block";
		previewinterval = setInterval('photoExpandLoaded(\''+imgSrc+'\')', 600);
	}
}

function photoExpandLoaded(imgSrc) {
	if(imgSrc.length > 0) {
		// Test to see if image is loaded.  If so, kill loading bar and show expanded graphic.
		document.getElementById('AC-PhotoViewer').removeChild(document.getElementById('AC-PhotoLoading'));
		document.getElementById('AC-PhotoViewerImg').src=imgSrc;
    }
	if(previewinterval != "") {
      	// Clear recursive function call.
		clearInterval(previewinterval);
      	previewinterval = "";
	} 
}

function photoRemove() {
	// Reset image to transparent spacer, hide viewer box
	document.getElementById('AC-PhotoViewerImg').src = "/Media/Website%20Resources/images/sitewide/spacer.gif";
	document.getElementById('AC-PhotoViewer').style.display = "none";
}

function setup() {
	var i, j, photoCounter;
	photoCounter = 0;
	
	// Grab photo listing from end of page, and clone.  When done, remove old photo list in preparation for insertion at float point.
	var clonedImages = document.getElementById('AC-PhotoList').cloneNode(true);
	document.getElementById('AC-PhotoList').parentNode.removeChild(document.getElementById('AC-PhotoList'));
	
	// Step through photos, turning them off one by one.  Then turn on the first one.
	photoDivs = clonedImages.getElementsByTagName('div');
	for(j=0;j<photoDivs.length;j++)	{
		photoDivs[j].className = 'AC-PhotoOff';
		photoDivs[j].getElementsByTagName('a')[0].onclick = function(){photoExpand(this);return false;};
	}
	photoDivs[0].className = 'AC-PhotoOn';
	
	// CReate counter, Photo X of Y.
	var photoCountTextCt = document.createElement('div')
	photoCountTextCt.id = "AC-PhotoCount";
	var photoCountText = document.createTextNode('1 of ' + (photoDivs.length));
	photoCountTextCt.appendChild(photoCountText);
	
	// Create controls for stepping through photos.  This is a simple left/right, with a incrementer function on click.
	var photoControlsCt = document.createElement('div');
	photoControlsCt.id	= "AC-PhotoControls";
	var photoControlsPrev = document.createElement('div');
	var photoControlsPrevA = document.createElement('a');
	var photoControlsPrevText = document.createTextNode('PREV');
	photoControlsPrev.className='previous';
	photoControlsPrevA.onclick=function(){photoChange(-1);};
	var photoControlsNext = document.createElement('div');
	var photoControlsNextA = document.createElement('a');
	var photoControlsNextText = document.createTextNode('NEXT');
	photoControlsNext.className='next';
	photoControlsNext.onclick=function(){photoChange(1);};
	var photoControlsCtEnd = document.createElement('div');
	photoControlsCtEnd.className='spacer';
	photoControlsPrevA.appendChild(photoControlsPrevText);
	photoControlsPrev.appendChild(photoControlsPrevA);
	photoControlsNextA.appendChild(photoControlsNextText);
	photoControlsNext.appendChild(photoControlsNextA);
	photoControlsCt.appendChild(photoControlsPrev);
	photoControlsCt.appendChild(photoControlsNext);
	photoControlsCt.appendChild(photoControlsCtEnd);
	
	// Attach controls to main list box
	clonedImages.id = "AC-PhotoJS";
	clonedImages.insertBefore(photoCountTextCt,photoDivs[0]);
	clonedImages.appendChild(photoControlsCt);
	
	// Attach main list box to float point for display on page.
	document.getElementById('AC-PhotoCt').appendChild(clonedImages)
}

if (window.addEventListener) {
	window.addEventListener('load',setup,false);
} else  {
	window.attachEvent('onload',setup);
}