/* JS image loader/fader
 * Copyright (C) 2008 Frank de Lange <xfade-f@unternet.org>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * see <http://www.gnu.org/licenses/>.
 *
 * based on http://slayeroffice.com/code/imageCrossFade/xfade2.html
 */

// user configurable parameters (times in ms, step in %/100)
var timeout=5000, steptime = 50, step = .2;

window.addEventListener ? window.addEventListener("load",nnx_init,false) : window.attachEvent("onload",nnx_init);

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false;

function nnx_init() {	
	if (d.getElementById && d.getElementById("xfade")) {
		imgs = d.getElementById("xfade").getElementsByTagName("img");
		if (imgs.length > 1) {
			for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
			imgs[current].style.visibility = "visible";
			imgs[current].xOpacity = .99;
	
			setTimeout(nnx_xfade,timeout);
		}
	}
}

function nnx_xfade() {
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;

	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity -= step; 
	nOpacity += step;
	
	if (cOpacity < 0) cOpacity = 0;
	
	imgs[nIndex].style.visibility = "visible";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if (cOpacity <= 0) {
		imgs[current].style.visibility = "hidden";
		current = nIndex;
		setTimeout(nnx_xfade,timeout);
	} else {
		setTimeout(nnx_xfade,steptime);
	}
	
	function setOpacity(obj) {
		if(obj.xOpacity >= .99 && obj.style.opacity >= .99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}	
}