// GLOBAL VARIABLES

var fps = 25;				// frames per second for animations
var dt = Math.round(1000/fps);

// FUNCTIONS

function getOpacity(el) {
	var compstyle, op;

	if (window.getComputedStyle) {								// standard
		compstyle = window.getComputedStyle(el, null);
	} else if (el.currentStyle) {								// IE
		compstyle = el.currentStyle;
	}
	
	if (compstyle.filters && compstyle.filters.alpha && compstyle.filters.alpha.opacity) {	// IE
		op = compstyle.filters.alpha.opacity*0.01;
	} else {					//everything else
		op = ( compstyle.opacity || compstyle.MozOpacity || compstyle.KhtmlOpacity);
	}
	return Number(op);
}


function setOpacity(el, op) {
	el.style.opacity = op;
	el.style.filter = "alpha(opacity=" + op*100 + ")";
	el.style.MozOpacity = op;
	el.style.KhtmlOpacity = op;
}

function fade(id, time, target) {
	var el = document.getElementById(id);

	function fadestep() {
		setOpacity(el, el.fadearray[el.step]);
		el.step++;
		if (el.step > el.steps ) {
			window.clearInterval(el.fadetimer);
		}
	}

	el.steps = Math.ceil(time/dt);
	el.fadearray = [];
	el.fadearray[0] = getOpacity(el);
	el.fadearray[el.steps] = target;
	for (var i = 1; i < el.steps; i++) {
		el.fadearray[i] = Math.round((el.fadearray[0] + i/el.steps*(target - el.fadearray[0]))*100)/100;
	}
	el.step = 0;	
	if (el.fadetimer) {
		window.clearInterval(el.fadetimer);
	}
	el.fadetimer = window.setInterval(fadestep, dt);
}
