var imgFade, imgEl;
var imgCount = 2;
var imgTotal = 3;
var frontVisible = true;

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function imgLoader() {
	var newImg = new Image();
	newImg.onload = function() {
		if (frontVisible == true) {
			imgDiv.style.backgroundImage = "url(" + newImg.src + ")";
			imgFade.start(1,0);
			frontVisible = false;
		} else {
			imgEl.src = newImg.src;
			imgFade.start(0,1);		
			frontVisible = true;
		}
	}
	newImg.src = "/images/" + document.body.className + "_" + document.body.id + "_fade_" + imgCount + ".jpg";
}

function setupFader() {
	if (Fx && $("fader_image") && $("fader_image_div")) {
		imgEl = $("fader_image");
		imgDiv = $("fader_image_div");
		imgFade = imgEl.effect("opacity", {duration: 2000, transition: Fx.Transitions.linear, onComplete: function() {
			if (imgCount<imgTotal) {
				imgCount++;
			} else {
				imgCount = 1;
			}
			setTimeout(imgLoader, 4000);
		}});
		setTimeout(imgLoader, 4000);
	}
}

addLoadEvent(setupFader);

function setupMenu() {
	if (document.all && document.getElementById) {
		navRoot = document.getElementById("menubar").getElementsByTagName("UL")[0];
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function() {
					this.className += " over";
				}
				node.onmouseout = function() {
					this.className = this.className.replace(" over", "");
				}
			}
		}
	}
}

addLoadEvent(setupMenu);

function divAni(ani) {
	if (aniCount < aniDivs.length) {
		window[ani].start({
		    'height': [0,160],
		    'width': [0,280],
		    'opacity': [0,1]
		});
		aniCount++;
	}
}

function homeAni() {
	if (Fx) {
		window ["aniDivs"] = $$("#montage div");
		window["aniCount"] = 0;
		for (var x=0; x<aniDivs.length; x++) {
			aniDivs[x].style.height = 0;
			aniDivs[x].style.width = 0;
			window["ani" + x] = new Fx.Styles(aniDivs[x], {duration: 1100, transition: Fx.Transitions.sineOut, onComplete: function() {
				divAni("ani" + aniCount);
			}});
		}
		divAni("ani" + aniCount);
	}
}

addLoadEvent(homeAni);
