// JavaScript Document
//An image rollover script
//
//1.	Name your images -  
//			off state:	filename.jpg or filename.gif or filename.png
//			on state:	filename_on.jpg, etc
//
//2.	All rollover images need class="roll" ie. <a href="#"><img src="images/normal.jpg" class="roll" /></a>
//
////////////////////////////////////////////////////////////////

//find the images that need rollovers
function findimg() {
	var imgs,i;
	// create array to preload rollovers
	var imgspreload = new Array(); 
	// Loop through all images, check if they contain the class roll
	imgs=document.getElementsByTagName('img');
	for(i=0;i<imgs.length;i++) {
		if(/roll/.test(imgs[i].className))  {
			// add the function roll to the parent Element of the image
			imgs[i].parentNode.onmouseover=function(){roll(this);};
			imgs[i].parentNode.onmouseout=function(){roll(this);};
			imgs[i].parentNode.onfocus=function(){roll(this);};
			imgs[i].parentNode.onblur=function(){roll(this);};
			// add rollover image to preloader array
			var oldsrc,loadsrc,ftype;
			oldsrc = imgs[i].src;
			ftype = oldsrc.substring(oldsrc.lastIndexOf('.'), oldsrc.length);
			loadsrc = oldsrc.replace(ftype, '_on'+ftype);
			imgspreload.push(loadsrc);
		}
		// added by Bruce - add the slideshow onclick function to images with class 'slide'
		else if(/slide/.test(imgs[i].className)) {
			imgs[i].parentNode.onclick = function(){return showPic(this); };
			// swap the clicked image's class to 'selected', and any previously clicked ones back to normal
			imgs[i].onclick = function(){ swapclass(this); this.className = 'selected';};
		}
		// end Bruce's addition
	}
	// Preload rollovers
	document.imageArray = new Array(imgspreload.length);
	for(var i=0; i<imgspreload.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = imgspreload[i];
	}
}

function roll(o) {
	var i,isnode,src,ftype,newsrc,nownode;
	// loop through all childNodes
	for (i=0;i<o.childNodes.length;i++) {
		nownode=o.childNodes[i];
		// if the node is an element and an IMG set the variable and exit the loop
		if(nownode.nodeType==1 && /img/i.test(nownode.nodeName)) {
			isnode=i;
			break;
		}
	}
	// check src and do the rollover
	src = o.childNodes[isnode].src;
	ftype = src.substring(src.lastIndexOf('.'), src.length);
	if(/_on/.test(src)) {
		newsrc = src.replace('_on','');
	}else{
		newsrc = src.replace(ftype, '_on'+ftype);
	}
	o.childNodes[isnode].src=newsrc;
}

window.onload=function(){
 findimg();
}