/*
	Global JavaScript file for www.metzlerbros.com
	Created 8.13.2009
	By Jay Pilgreen
	copyright River City Studio
*/


	/* ------------------ Image Rotation -------------------------- */
	
	
var imageArray = new Array();
var ua = window.navigator.userAgent;
var msie = ua.indexOf( "MSIE" );

	// Simple IE check

var ie = false;

	if(msie > 0) {
		ie = true;
	}


	// Original Starting Function .. only fires once

function startImageRotation(startingImageNumber) {

	rotImages = document.getElementsByTagName("img");
	
	for(i=0; i<rotImages.length; i++) {
		if(rotImages[i].className == "fullSizeRotation") {
			imageArray.push(rotImages[i]);
		}
	}
	
	startingImageNumber--;
	
	startWindowTimer(startingImageNumber, imageArray[startingImageNumber]);
}


	// Original Starting Function .. Only fires once

function startWindowTimer(currentImageNumber, currentImageObject) {
	window.setTimeout(function(){swapImage(currentImageNumber, currentImageObject)}, 9000);
}


	//	This is the loop .. called each time by a timer function 

function swapImage(currentImageNumber, currentImageObject) {

	nextImageNumber = currentImageNumber + 1;
	if(nextImageNumber > imageArray.length - 1) {
		nextImageNumber = 0;
	}
	
	nextImageObject = imageArray[nextImageNumber];
	
	if(ie) {
		currentImageObject.style.filter = "alpha(opacity=100)";
		nextImageObject.style.filter = "alpha(opacity=1)";
	} else {
		currentImageObject.style.opacity = 1;
		nextImageObject.style.opacity = .1;
	}
	
	fadeOutImage(currentImageObject);
	fadeInImage(nextImageNumber, nextImageObject);
	
}


function fadeOutImage(incomingImage) {
	if(ie) {
		if(incomingImage.filters[0].opacity > 10) {
			incomingImage.filters[0].opacity = incomingImage.filters[0].opacity * .8;
			window.setTimeout(function(){fadeOutImage(incomingImage)}, 70);
		} else {
			incomingImage.style.display = "none";
		}
	} else {
		if(incomingImage.style.opacity > .1) {
			incomingImage.style.opacity = incomingImage.style.opacity * .8;
			window.setTimeout(function(){fadeOutImage(incomingImage)}, 70);
		} else {
			incomingImage.style.display = "none";
		}
	}
}


function fadeInImage(thisImageNumber, incomingImage) {
	incomingImage.style.display = "block";
	
	if(ie) {
		if(incomingImage.filters[0].opacity < 90) {
			incomingImage.filters[0].opacity = incomingImage.filters[0].opacity * 2;
			window.setTimeout(function(){fadeInImage(thisImageNumber, incomingImage)}, 70);
		} else {
			incomingImage.filters[0].opacity = 100;
			window.setTimeout(function(){swapImage(thisImageNumber, incomingImage)}, 9000);
		}
	} else {
		if(incomingImage.style.opacity < .9) {
			incomingImage.style.opacity = incomingImage.style.opacity * 2;
			window.setTimeout(function(){fadeInImage(thisImageNumber, incomingImage)}, 70);
		} else {
			incomingImage.style.opacity = 1;
			window.setTimeout(function(){swapImage(thisImageNumber, incomingImage)}, 9000);
		}
	}
}