function ImageSwapper() {
	var FLASH_BORDER = 3;
	var fullImages = new Array();
	var tnImages = new Array();
	var rotate_idx = 0;
	var swap_idx = 0;
	var stopped = true;
	var flashCycle = new Array();
	var flashing = false;
	var pending = false;
	
	this.addFullImage = function(imgObj) {
		fullImages.push(imgObj);
		rotate_idx++;
	}

	this.addTNImage = function(imgObj) {
		tnImages.push(imgObj);
	}

	this.startRotate = function(timeout) {
		stopped = false;
		if(!pending) {
			this.rotate(timeout);
		}
	}

	this.stopRotate = function(timeout) {
		stopped = true;
	}	

	this.swapMain = function(idx) {
		stopped = true;
		swap(idx);
	}

	this.rotate = function(timeout) {
		pending = false;
		if(!stopped && fullImages.length) {
			if(!timeout) timeout = 4000;
			swap(rotate_idx);
			if(++rotate_idx == tnImages.length) {
				rotate_idx = 0;
			}
			while(checkVisible(rotate_idx) >= 0) {
				if(++rotate_idx == tnImages.length) {
					rotate_idx = 0;
				}
			}	
			pending = true;
			setTimeout('imageSwapper.rotate()', timeout);
		}
	}
	
	this.doFlash = function() {
		flash();
	}
	
	function swap(idx) {
		var visIdx = checkVisible(idx);
		
		if(visIdx >= 0) {
			startFlash(visIdx);
		}
		else {
			fullImages[swap_idx].src = tnImages[idx].src;
			flashCycle[swap_idx] = 0;
			fullImages[swap_idx].border = 0;
			fullImages[swap_idx].hspace = FLASH_BORDER;
			fullImages[swap_idx].vspace = FLASH_BORDER;
			
			if(++swap_idx == fullImages.length) {
				swap_idx=0;
			}
		}
	}
	
	function checkVisible(idx) {
		for(var i=0; i < fullImages.length; i++) {
			if(fullImages[i].src == tnImages[idx].src) {
				return i;
			}
		}
		
		return -1;
	}
	
	function startFlash(idx) {
		flashCycle[idx] = 2;
		if(!flashing) {
			flashing=true;
			flash();
		} 
	}
	
	function flash() {
		var noneFlashing = true;
		
		for(var i=0; i < fullImages.length; i++) {
			if(flashCycle[i] > 0) {
				if(fullImages[i].className == 'FullImage') {
					fullImages[i].className='FullImageHighlight';
				}
				else {
					fullImages[i].className='FullImage';
					flashCycle[i]--;
				}
				noneFlashing=false;
			}
		}
		
		if(noneFlashing) {
			flashing = false;
		}
		else {
			setTimeout('imageSwapper.doFlash()', 500);
		}
	}
}

function highlight(image) {
	var loc = image.src;

	image.src = loc.replace("_normal", "_high");
}

function lowlight(image) {
	var loc = image.src;

	image.src = loc.replace("_high", "_normal");
}

function hideThumbs() {
	hideLayer('tnDiv');
	hideLayer('hideTnDiv');
	showLayer('showTnDiv');
	imageSwapper.startRotate();
	photosHidden=true;
}

function showThumbs() {
	showLayer('tnDiv');
	hideLayer('showTnDiv');
	showLayer('hideTnDiv');
	photosHidden=false;
}

function toggleThumbs() {
	if(photosHidden) {
		showThumbs();
	}
	else {
		hideThumbs();
	}
}