/*
	Generic image rollover script
	Source:
	Standards Compliant Rollover Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
	Modified with info from http://www.wandforge.com/rollover/
	Other modifications per site.
*/
// generic image rollovers
function initRollovers() {
	if (!document.getElementById) return
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className.match("rollover")) {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '-over'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('-over'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

// Gallery menu; insert popup prompts into each thumbnail and preload the med-sized images
function gallerySeed() {
	if (!document.getElementById('gallery_menu')) { return; }
	thumblist = new Array(); medlist = new Array(); biglist = new Array(); b = 0;
	moreimages = '';
	for (a=0;a<document.getElementById('gallery_menu').getElementsByTagName('img').length;a++) {
		if (document.getElementById('gallery_menu').getElementsByTagName('img')[a].id.match(/thumb_/)) {
			thumblist[b] = document.getElementById('gallery_menu').getElementsByTagName('img')[a];
			thumblist[b].style.cursor = 'pointer';
			// process image filename here to preload the medium sized images
			imgkey = thumblist[b].id.match(/\d+/);
			medsrc = thumblist[b].src.replace('/thumb/','/med/');
			if (b!=0) {
				moreimages += '<img src="' + medsrc + '" alt="Click to enlarge" id="gallery_med_' + imgkey + '" class="gallery_photo" style="display:none;" />';
			}
			b++
		}
	}
	document.getElementById('photo_container').innerHTML = moreimages + document.getElementById('photo_container').innerHTML;
	for (a=0;a<thumblist.length;a++) {
		thumblist[a].onmouseup = function() {
			medImage(this.id);
			return;
		}
		imgkey = thumblist[a].id.match(/\d+/);
		medlist[a] = document.getElementById('gallery_med_'+imgkey);
		bigpagename = medlist[a].src.slice(medlist[a].src.lastIndexOf('/')+1,medlist[a].src.lastIndexOf('.'));
		bigpagename = 'gallery/' + bigpagename + '.html';
		
		medlist[a].setAttribute('bigsrc',bigpagename);
		medlist[a].onmouseup = function() {
			bigImage(this);
			return;
		}
	}
	medImage(thumblist[0].id);
	return;
}
// Gallery menu; swap image when thumb is clicked. Invoked by gallerySeed()
function medImage(thumbid) {
	// mumble mumble thumbid generate new image name
	imgkey = thumbid.match(/\d+/);
	for (a=0;a<thumblist.length;a++) {
		browsekey = thumblist[a].id.match(/\d+/);
		medimg = 'gallery_med_' + browsekey;
		browsekey = Number(browsekey); imgkey = Number(imgkey);
		if (browsekey==imgkey) {
			document.getElementById(medimg).style.display = 'block';
		} else {
			document.getElementById(medimg).style.display = 'none';
		}
	}
	return;
}
// generic window popup. Invoked by gallerySeed()
function bigImage(thismed) {
	imgkey = thismed.id.match(/\d+/);
	url = thismed.getAttribute('bigsrc');
	id = "gallerypage_" + imgkey;
// need: url,sizew,sizeh,id
	sizew = 600;
	sizeh = 600;
	thisbigimage = window.open(url,id,'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width='+sizew+',height='+sizeh);
	return;
}
// simulate a static background. Should only be executed in IE 6.
function ie6bg() {
	if (!document.getElementById('container')) { return; }
	document.getElementById('container').style.height = parseInt(document.documentElement.clientHeight);
	// Make sure we do this again
	setTimeout("ie6bg()", 70);
	document.getElementById('container').style.top = document.documentElement.scrollTop;
	return;
}

/**
 * @notused
 */
function galleryRnd() {
	alert('GalleryRnd() has been envoked, it should not!');
	return;
	if (!document.getElementById('gallery_photo_rnd') && !document.getElementById('gallery_photo_thumbs')) { return; }
	thumbset = document.getElementById("gallery_photo_thumbs").getElementsByTagName('img');
	gseed = Math.floor(Math.random() * thumbset.length);
	medsrc = thumbset[gseed].src.replace('/thumb/','/med/');
	document.getElementById('gallery_photo_rnd').src = medsrc;
	return;
}
//launch everything at once. ORDER MATTERS
function launchAll() {
	initRollovers();
	//gallerySeed();
	//galleryRnd();
	/*@cc_on
	@if (@_jscript_version < 5.7)
	// for MSIE 6 and earlier, nudge the corners into position.
	// MSIE 7 uses JScript engine 5.7
	ie6bg();
	@end
	@*/
}
window.onload = launchAll;
