

// image swap function
// swaps src on mouseover for images using the "imageSwap" CSS class
function initImageSwap(parentElId) {
	parentEl = (parentElId) ? document.getElementById(parentElId) : document;
	imgs = document.getElementsByTagName('IMG');
	inputs = document.getElementsByTagName('INPUT');
	for(i=0;i < (imgs.length + inputs.length);i++) {
		el = (i>=imgs.length) ? inputs[i-imgs.length] : imgs[i];
		if (el.className=='imageSwap') {
			el.ext = el.src.substr(el.src.length-3,3);
			addPreload(el.src.replace('1.'+el.ext,'2.'+el.ext));
			addPreload(el.src);
			if (targetId = el.getAttribute('swapTarget')) {
				// attach events to target element instead of this image
				if (!el.id) el.id = "swapTargetSrc"+Math.random();
				targetEl = document.getElementById(targetId);
				targetEl.setAttribute('swapTargetSrc',el.id);
				// is the target a form?
				targetIsForm = false;
				for(f=0;f<document.forms.length;f++) {
					if (document.forms[f]==targetEl) {
						targetIsForm = true;
						break;
					}
				}
				if (targetIsForm) {
					targetEl.onfocus = function() {
						srcImg = document.getElementById(this.getAttribute('swapTargetSrc'));
						// force animated GIFs to reload in IE
						r = (srcImg.getAttribute("swapAni") && document.all) ? "?r="+Math.random() : '';
						if (srcImg.src.indexOf('2.'+srcImg.ext)==-1) srcImg.src = srcImg.src.replace('1.'+srcImg.ext,'2.'+srcImg.ext);
						this.mactive = true;
					}
					targetEl.onblur = function() {
						// force animated GIFs to reload in IE
						r = (srcImg.getAttribute("swapAni") && document.all) ? "?r="+Math.random() : '';
						setTimeout("if (!document.getElementById('"+this.id+"').mactive) { srcImg = document.getElementById(document.getElementById('"+this.id+"').getAttribute('swapTargetSrc')); if (srcImg.src.indexOf('1.'+srcImg.ext)==-1) srcImg.src = srcImg.src.replace('2.'+srcImg.ext,'1.'+srcImg.ext)+r; }" ,1);
						this.mactive = false;
					}
				} else {
					targetEl.onmouseover = function() {
						// force animated GIFs to reload in IE
						srcImg = document.getElementById(this.getAttribute('swapTargetSrc'));
						r = (srcImg.getAttribute("swapAni") && document.all) ? "?r="+Math.random() : '';
						if (srcImg.src.indexOf('2.'+srcImg.ext)==-1) srcImg.src = srcImg.src.replace('1.'+srcImg.ext,'2.'+srcImg.ext) + r;
						this.setAttribute('mactive',true);
					}
					targetEl.onmouseout = function() {
						// force animated GIFs to reload in IE
						r = (srcImg.getAttribute("swapAni") && document.all) ? "?r="+Math.random() : '';
						setTimeout("if (!document.getElementById('"+this.getAttribute('swapTargetSrc')+"').getAttribute('mactive')) { srcImg = document.getElementById(document.getElementById('"+this.id+"').getAttribute('swapTargetSrc')); if (srcImg.src.indexOf('1.'+srcImg.ext)==-1) srcImg.src = srcImg.src.replace('2.'+srcImg.ext,'1.'+srcImg.ext)+r; }" ,1);
						this.setAttribute('mactive','');
					}
				}
			}
			el.onmouseover = el.onmouseup = function() {
				if (this.getAttribute('className')=='imageSwap' || this.getAttribute('class')=='imageSwap') {
					// force animated GIFs to reload in IE
					r = (this.getAttribute("swapAni") && document.all) ? "?r="+Math.random() : '';
					this.src = this.src.replace('1.'+this.ext,'2.'+this.ext)+r;
					this.setAttribute('mactive',true);
				}
			}
			el.onmouseout = el.onmousedown = function() {
				if (this.getAttribute('className')=='imageSwap' || this.getAttribute('class')=='imageSwap') {
					// force animated GIFs to reload in IE
					r = (this.getAttribute("swapAni") && document.all) ? "?r="+Math.random() : '';
					this.src = this.src.replace('2.'+this.ext,'1.'+this.ext)+r;
					this.setAttribute('mactive','');
				}
			}
		}
	}
}


//////////////// ONLOAD SEQUENCE //////////////


loadMeUp += "initImageSwap();preloadImages();";

