var FadeAutoInterval = 8;
var FadeAutoInteraction = 10;
var FadeAutoTimer = FadeAutoInterval*20;

var FadeImgCount = 6;

var FadeCurFrom = 1;
var FadeCurTo = 1;
var FadeFinalTo = 1;
var FadeValue = 100;

function _(str) { return document.getElementById(str); }

function FaderInit() {
	var i = 1;
	var startImg = (Math.round(Math.random()*1000))%FadeImgCount+1;

	for (i=1; i<=FadeImgCount; i++) {
		if (i == startImg) {
			SetZ('fade_img_'+i, 3);
			SetAlpha('fade_img_'+i, 100);
			FadeFinalTo = FadeCurTo = FadeCurFrom = i;
		}
		else if (i == startImg +1 || (i == 1 && startImg == FadeImgCount)) {
			SetZ('fade_img_'+i, 1);
			SetAlpha('fade_img_'+i, 0);
			// FadeFinalTo = FadeCurTo = i;
		}
		else {
			SetZ('fade_img_'+i, 1);
			SetAlpha('fade_img_'+i, 0);
		}
	}

	window.setInterval(IntervalFader, 50);
}

function IntervalFader() {

	if (!(FadeCurTo == FadeFinalTo && FadeValue == 100)) {

		if (FadeValue < 100) {
			FadeValue += 4;
			SetAlpha('fade_img_'+FadeCurTo, FadeValue);
		}

		else if (FadeCurTo != FadeFinalTo) {
			SetAlpha('fade_img_'+FadeFinalTo, 0);
			SetAlpha('fade_img_'+FadeCurFrom, 0);
			SetAlpha('fade_img_'+FadeCurTo, 100);

			SetZ('fade_img_'+FadeCurFrom, 1);
			SetZ('fade_img_'+FadeCurTo, 2);
			SetZ('fade_img_'+FadeFinalTo, 3);

			FadeValue = 0;
			FadeCurFrom = FadeCurTo;
			FadeCurTo = FadeFinalTo;
		}
	}

	else {
		if (FadeAutoTimer)
			FadeAutoTimer--;
		else {
			FadeAutoTimer = FadeAutoInterval*20;
			FadeFinalTo++;

			if (FadeFinalTo > FadeImgCount) {
				FadeFinalTo = 1;
			}
		}
	}

}

function SetZ(Id, Val) {
	var Obj;

	if (typeof Id != 'object')
		Obj = document.getElementById(Id);
	else
		Obj = Id;

	Obj.style.zIndex = Val;
}

function SetAlpha(Id, Alpha) {

	var Obj;

	if (typeof Id != 'object')
		Obj = document.getElementById(Id);
	else
		Obj = Id;

	if (Alpha == 0) {
		Obj.style.display = 'none';
		Alpha = 100;
	}
	else
		Obj.style.display = 'inline';

	Obj.style.MozOpacity = Alpha==100?'1':'.'+(Alpha<10?'0':'')+Alpha;
	Obj.style.filter = 'alpha(opacity='+Alpha+')';
	Obj.style.opacity = Alpha==100?'1':'.'+(Alpha<10?'0':'')+Alpha;
}

