var animationTimeout = null;
var cellHeight = 363;
var pauseTime = 3000;
var fadeTime = 1000;
var framesPerSec = 20;
var transparencyStep = 100 / ((fadeTime / 1000) * framesPerSec);
var fadeUp = false;
var animateForward = true;
var currTransparency = 100;

function setTransparency(percentage) {
	var imageFader = document.getElementById("imageFader");
	if(!imageFader){return false;}
	if (imageFader.filters && imageFader.filters[0]) {
		if (typeof imageFader.filters[0].opacity == "number") { //if IE6+
			imageFader.filters[0].opacity = percentage;
		} else { //else if IE5.5-
			imageFader.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+percentage+")";
		}
	} else if (imageFader.style.MozOpacity) {
		imageFader.style.MozOpacity = percentage / 101;
	} else if (imageFader.style.KhtmlOpacity) {
		imageFader.style.KhtmlOpacity = percentage / 100;
	}
}

function animateImageFader() {
	clearTimeout(animationTimeout);
	if (fadeUp == false) {
		currTransparency -= transparencyStep;
		if (currTransparency <= 0) {
			fadeUp = true;
			currTransparency = 0;
		}
	} else {
		currTransparency += transparencyStep
		if (currTransparency >= 100) {
		  fadeUp = false;
			currTransparency = 100;
		}
	}
	setTransparency(currTransparency);
	if (currTransparency == 0) {
		if (animateForward == true) {
			showNextImage();
		} else {
			showPrevImage();
		}
	}
	if (currTransparency == 100) {
		animationTimeout = setTimeout('animateImageFader()', pauseTime);
		return;
	} else {
		animationTimeout = setTimeout('animateImageFader()', 1000 / framesPerSec);
	}
}

function showPrevImage() {
	var imageFader = document.getElementById("imageFader");
	if(!imageFader){return false;}
	var topMargin = 0;
	if (imageFader.style.marginTop != '') topMargin = parseInt(imageFader.style.marginTop.replace('px', ''));
	if (topMargin == 0) {
		topMargin = 0 - imageFader.offsetHeight + cellHeight;
	} else {
		topMargin += cellHeight;
	}
	imageFader.style.marginTop = topMargin + 'px';
}

function jumpToPrevImage() {
	clearTimeout(animationTimeout);
	showPrevImage();
	setTransparency(100);
	animateForward = false;
	animationTimeout = setTimeout('animateImageFader()', pauseTime);
}

function showNextImage() {
	var imageFader = document.getElementById("imageFader");
	if(!imageFader){return false;}
	var topMargin = 0;
	if (imageFader.style.marginTop != '') {
		topMargin = parseInt(imageFader.style.marginTop.replace('px', ''));
	}
	
	
	//if (topMargin == 0 - imageFader.offsetHeight + cellHeight) {
	
	//fix by loch for image cycling to reloop
	
	var reset_height =  0 - imageFader.offsetHeight + cellHeight
	
	if (Math.abs(topMargin-reset_height) < 15) {	
		topMargin = 0;
	} else {
		topMargin -= cellHeight;
	}
	
	
	//if (topMargin == 0 - imageFader.offsetHeight + cellHeight) {
		//topMargin = 0;
	//} else {
	//	topMargin -= cellHeight;
	//}
	imageFader.style.marginTop = topMargin + 'px';
}

function jumpToNextImage() {
	clearTimeout(animationTimeout);
	showNextImage();
	setTransparency(100);
	animateForward = true;
	animationTimeout = setTimeout('animateImageFader()', pauseTime);
}

function onRollOver(img_obj){
	img_obj.src = img_obj.src.split("-off").join("-on");
}

function onRollOut(img_obj){
	img_obj.src = img_obj.src.split("-on").join("-off");	
}  

// onMouseOver="onRollOver(this);" onMouseOut="onRollOut(this)" 

function e(id){
	return document.getElementById(id);
}
