function makeselected(selectbox, value) {
	for (i=0;i<selectbox.length;i++) {
		selectbox.options[i].selected = false;
	}
	for (i=0;i<selectbox.length;i++) {
		if (selectbox.options[i].value == value) {
			selectbox.options[i].selected = true;
		}
	}
}
function init()
{
	var query = location.search.substring(1);
	if (query.indexOf('img=') > -1) {
		var img = query.substring( query.indexOf('img=') + 4 );
		if (img.indexOf('&') > -1) {
			img = img.substring( 0, img.indexOf('&') );
		}
		gotoImage(img);
	} else {
		
		gotoImage( getImageFilename() );
	}
}
function gotoImage(fileName)
{
	var list = document.getElementById('optionlist');
	document.mainimage.src = fileName;
	makeselected( list, fileName );
	writeCaption();
	writeDownloadLink();
	writeRefLink();
}
function scrapbook(reportTitle, reportDate, season, locn)
{
	var pageurl = window.location.pathname + window.location.search + window.location.hash;
	var imgurl = getImageFilename();
	var caption = getCaption();
	add_image_to_scrapbook(escape(pageurl),escape(imgurl),escape(reportTitle),escape(reportDate),escape(caption),'',escape(season),escape(locn));
}
function writeCaption()
{
	var caption = getCaption();
	caption = caption.replace(/\[(\/?\w+)\]/g, '<$1>');
	document.getElementById('caption_text').innerHTML = caption;
}
function writeDownloadLink()
{
	var a = document.getElementById('epslink');
	if (a) {
		a.style.display = 'none';
	}
	a = document.getElementById('psdlink');
	if (a) {
		a.style.display = 'none';
	}
	a = document.getElementById('jpglink');
	if (a) {
		a.style.display = 'none';
	}
	a = document.getElementById('ziplink');
	if (a) {
		a.style.display = 'none';
	}
	a = document.getElementById('urllink');
	if (a) {
		a.style.display = 'none';
	}
	var downloadoption = getOption('downloadlist');
	if (downloadoption) {
		if (downloadoption.value.indexOf(".eps") > -1) {
			a = document.getElementById('epslink');
		} else if (downloadoption.value.indexOf(".psd") > -1) {
			a = document.getElementById('psdlink');
		} else if (downloadoption.value.indexOf(".jpg") > -1) {
			a = document.getElementById('jpglink');
		} else if (downloadoption.value.indexOf(".zip") > -1) {
			a = document.getElementById('ziplink');
		} else {
			a = document.getElementById('urllink');
			if (a) {
				a.innerHTML = downloadoption.value;
			}
		}
		if (a && a.href) {
			if (downloadoption.text) {
				a.innerHTML = downloadoption.text;
			}
			a.href = downloadoption.value;
			a.style.display = 'inline';
		}
	}
}
function writeRefLink()
{
	a = document.getElementById('reflink');
	if (a) {
		a.style.display = 'none';
	}
	var downloadoption = getOption('reflist');
	if (downloadoption) {
		a = document.getElementById('reflink');
		if (a && a.href) {
			a.innerHTML = downloadoption.value;
			if (downloadoption.text) {
				a.innerHTML = downloadoption.text;
			}
			a.href = downloadoption.value;
			a.style.display = 'inline';
		}
	}
}
function changeImage()
{
	var newimg = getImageFilename();
	var newlocation = ""+window.location;
	if (newlocation.indexOf("img") > -1) {
		newlocation = newlocation.replace(/(img\=)[^&]+/g, '$1'+newimg);
	} else {
		newlocation = newlocation+"?img="+newimg;
	}
	window.location = newlocation;
}
function getCaption()
{
	var list = document.getElementById('optionlist');
	return list.options[list.selectedIndex].text;
}
function getImageFilename()
{
	var list = document.getElementById('optionlist');
	if (list.selectedIndex) {
		return list.options[list.selectedIndex].value;
	} else {
		return list.options[0].value;
	}
}
function getOption(list)
{
	var captions = document.getElementById('optionlist');
	var downloadlinks = document.getElementById(list);
	if (downloadlinks) {
		return downloadlinks.options[captions.selectedIndex];
	} else {
		return "";
	}
}
function prevImage()
{
	var list = document.getElementById('optionlist');
	if(list.selectedIndex == 0)
	{
		list.selectedIndex = list.options.length-1;
	}
	else
	{
		list.selectedIndex--;
	}
	changeImage();
}

function nextImage()
{
	var list = document.getElementById('optionlist');
	if(list.selectedIndex == list.options.length-1)
	{
		list.selectedIndex = 0;
	}
	else
	{
		list.selectedIndex++;
	}
	changeImage();
}
