var currentImage = ''	;
var ss_interval;
var ss_running = false;
var swapping = false;

function GalleryLink(photo,altPhoto,scriptPath,width) {
	if(swapping)
		return;
	if(!SetGalleryPhoto(photo,width))
		document.location.href = scriptPath + photo;
}

function MyGetElem(name) {
	var myElem;
	if(document.getElementById)
		myElem = document.getElementById(name);
	else if(document.all)
		myElem = document.all[name];
	else if(document.layers)
		myElem = document.layers[name];
	else
		return false;
	return myElem;
}

function SetGalleryPhoto(photo,width) {
	var phElem;
	if(photo == currentImage)
		return true;
	if(swapping)
		return true;
	new Insertion.After('galImage','<img id="galImage_swap" src="' + photo + '" style="display: none; width:' + width + 'px;" />');
	//center id
	var dim = $('galImage_swap').getDimensions();
	var wrapDim = $('galImageWrap').getDimensions();
	MyCrossFade('galImage','galImage_swap');
	SetCurrentPhoto(photo);
}

function MyCrossFade(from,to) {
	swapping = true;
	new Effect.Fade(from,{duration: 1.5});
	new Effect.Appear(to,{duration: 1.5});
	setTimeout('FadeDone("' + from + '","' + to + '")',1500);
}

function FadeDone(from,to) {
	$(from).remove();
	$(to).id = from;
	swapping = false;
}

function SetCurrentPhoto(photo) {
	currentImage = photo;
}

function FindPhoto(current) {
	for(i = 0; i < galImages.length; i++) {
		if(galImages[i] == current)
			return i;
	}
	return 0;
}

function NextImage() {
	var nextPhoto = 0;
	var index = FindPhoto(currentImage);
	if(index >= galImages.length - 1)
		nextPhoto = 0;
	else
		nextPhoto = index + 1;
	SetGalleryPhoto(galImages[nextPhoto],imgWidths[nextPhoto]);
}

function ToggleSlideShow(area) {
	var tLink = MyGetElem('toggle_link');
	if(!ss_running) {
		ss_interval = setInterval(NextImage,3000);
		ss_running = true;
		tLink.src = 'images/' + area + '_slideshow_on.gif';
	} else {
		clearInterval(ss_interval);
		ss_running = false;
		tLink.src = 'images/' + area + '_slideshow_off.gif';
	}
}

function PageVis(pageNum,vis) {
	document.getElementById('galPage_' + pageNum).style.visibility = vis;
}

function NextPage() {
	PageVis(currentPage,'hidden');
	if(currentPage < numPages)
		currentPage++;
	PageVis(currentPage,'visible');
	UpdatePageButtons();
}

function LastPage() {
	PageVis(currentPage,'hidden');
	if(currentPage > 1)
		currentPage--;
	PageVis(currentPage,'visible');
	UpdatePageButtons();
}

function UpdatePageButtons() {
	var backVis;
	var nextVis;
	if(currentPage > 1)
		backVis = 'visible';
	else
		backVis = 'hidden';
	if(currentPage < numPages)
		nextVis = 'visible';
	else
		nextVis = 'hidden';
	document.getElementById('gallery_back').style.visibility = backVis;
	document.getElementById('gallery_next').style.visibility = nextVis;
}