// JavaScript Document

function Params(elemOut, elemIn, timeout, steps, endFunc) {
	this.elemOut = elemOut;
	this.elemIn = elemIn;
	this.timeout = timeout;
	this.steps = steps;
	this.endFunc = endFunc;
	this.step = 0;
	this.opaOut = 100;
	this.opaIn = 0;
	this.opaD = Math.floor(100 / steps);
}
var params;

function crossFade(elemOut, elemIn, timeout, steps, endFunc) {
	setOpacity(elemOut, 100);
	setOpacity(elemIn, 0);
	elemIn.style.visibility = 'visible';
	x = Math.floor(100 / steps);
	params = new Params(elemOut, elemIn, timeout, steps, endFunc);
	setTimeout('doCrossFade()', timeout);
}

function doCrossFade() {
	params.opaIn += params.opaD;
	params.opaOut -= params.opaD;
	setOpacity(params.elemIn, params.opaIn);
	setOpacity(params.elemOut, params.opaOut);
	params.step++;
	if (params.step < params.steps) {
		setTimeout('doCrossFade()', params.timeout);
	} else {
		setOpacity(params.elemOut, 0);
		setOpacity(params.elemIn, 100);
		params.elemOut.style.visibility = 'hidden';
		if (params.endFunc != null) {
			params.endFunc();
		}
	}
}

function fadeIn(e, step, timeout, to, from, callback) {
	if (e.opa == undefined || e.opa == null) {
		e.opa = new Object();
		e.opa.from = from || 0;
		e.opa.opacity = e.opa.from;
	} else if (e.opa.timer != undefined && e.opa.timer != null) {
		clearTimeout(e.opa.timer);
		e.opa.timer = null;
	}
	e.opa.step = step;
	e.opa.timeout = timeout;
	e.opa.to = to || 100;
	e.opa.from = from || e.opa.opacity;
	e.opa.opacity = from || e.opa.opacity;
	e.opa.callback = callback;
	setOpacity(e, e.opa.opacity);
	e.style.visibility = 'visible';
	doFadeIn(e);
}

function doFadeIn(e) {
	setOpacity(e, e.opa.opacity);
	e.opa.opacity += e.opa.step;
	if (e.opa.opacity >= e.opa.to) {
		e.opa.opacity = e.opa.to;
		setOpacity(e, e.opa.opacity);
		clearTimeout(e.opa.timer);
		e.opa.timer = null;
		if (e.opa.callback) {
			e.opa.callback();
		}
	} else {
		e.opa.timer = setTimeout(function(){doFadeIn(e); e = null;}, e.opa.timeout);
	}
}

function fadeOut(e, step, timeout, to, from, callback) {
	if (e.opa == undefined || e.opa == null) {
		e.opa = new Object();
		e.opa.from = from || 100;
		e.opa.opacity = e.opa.from;
	} else if (e.opa.timer != undefined && e.opa.timer != null) {
		clearTimeout(e.opa.timer);
		e.opa.timer = null;
	}
	e.opa.step = step;
	e.opa.timeout = timeout;
	e.opa.to = to || 0;
	e.opa.from = from || e.opa.opacity;
	e.opa.opacity = from || e.opa.opacity;
	e.opa.callback = callback;
	setOpacity(e, e.opa.opacity);
	e.style.visibility = 'visible';
	doFadeOut(e);
}

function doFadeOut(e) {
	setOpacity(e, e.opa.opacity);
	e.opa.opacity -= e.opa.step;
	if (e.opa.opacity <= e.opa.to) {
		e.opa.opacity = e.opa.to;
		setOpacity(e, e.opa.opacity);
		clearTimeout(e.opa.timer);
		e.opa.timer = null;
		if (e.opa.to == 0) {
			e.style.visibility = 'hidden';
		}
		if (e.opa.callback) {
			e.opa.callback();
		}
	} else {
		e.opa.timer = setTimeout(function(){doFadeOut(e); e = null;}, e.opa.timeout);
	}
}

function stretchFadeIn(id, nSteps, fullWidth, fullHeight, timeout) {
	var e = document.getElementById(id);
	if (e.stretchFade) {
		clearTimeout(e.stretchFade.timer);
	} else {
		e.stretchFade = {width:0, height:0, opa:0};
	}
	e.stretchFade.mode = 1;
	e.stretchFade.fullWidth = fullWidth;
	e.stretchFade.fullHeight = fullHeight;
	e.stretchFade.nSteps = nSteps;
	e.stretchFade.timeout = timeout;
	e.stretchFade.stepW = Math.round(fullWidth / nSteps);
	e.stretchFade.stepH = Math.round(fullHeight / nSteps);
	e.stretchFade.stepO = Math.round(100 / nSteps);
	doStretchFadeIn(id);
}

function doStretchFadeIn(id) {
	var e = document.getElementById(id);
	e.style.visibility = 'visible';
	e.stretchFade.width += e.stretchFade.stepW;
	e.stretchFade.height += e.stretchFade.stepH;
	e.stretchFade.opa += e.stretchFade.stepO;
	e.style.width = Math.min(e.stretchFade.fullWidth, e.stretchFade.width) + 'px';
	e.style.height = Math.min(e.stretchFade.fullHeight, e.stretchFade.height) + 'px';
	setOpacity(e, Math.min(100, e.stretchFade.opa));
	if (e.stretchFade.width >= e.stretchFade.fullWidth ||
			e.stretchFade.height >= e.stretchFade.fullHeight ||
			e.stretchFade.opa >= 100) {
		e.style.width = e.stretchFade.fullWidth + 'px';
		e.style.height = e.stretchFade.fullHeight + 'px';
		setOpacity(e, 100);
		delete e.stretchFade;
	} else {
		e.stretchFade.timer = setTimeout(function(){doStretchFadeIn(id);}, e.stretchFade.timeout);
	}
}

function stretchFadeOut(id, nSteps, fullWidth, fullHeight, timeout) {
	var e = document.getElementById(id);
	if (e.stretchFade) {
		clearTimeout(e.stretchFade.timer);
	} else {
		e.stretchFade = {width:fullWidth, height:fullHeight, opa:100};
	}
	e.stretchFade.mode = 1;
	e.stretchFade.fullWidth = fullWidth;
	e.stretchFade.fullHeight = fullHeight;
	e.stretchFade.nSteps = nSteps;
	e.stretchFade.timeout = timeout;
	e.stretchFade.stepW = Math.round(fullWidth / nSteps);
	e.stretchFade.stepH = Math.round(fullHeight / nSteps);
	e.stretchFade.stepO = Math.round(100 / nSteps);
	doStretchFadeOut(id);
}

function doStretchFadeOut(id) {
	var e = document.getElementById(id);
	e.style.visibility = 'visible';
	e.stretchFade.width -= e.stretchFade.stepW;
	e.stretchFade.height -= e.stretchFade.stepH;
	e.stretchFade.opa -= e.stretchFade.stepO;
	e.style.width = Math.max(0, e.stretchFade.width) + 'px';
	e.style.height = Math.max(0, e.stretchFade.height) + 'px';
	setOpacity(e, Math.max(0, e.stretchFade.opa));
	if (e.stretchFade.width <= 0 ||
			e.stretchFade.height <= 0 ||
			e.stretchFade.opa <= 0) {
		e.style.width = '0px';
		e.style.height = '0px';
		e.style.visibility = 'hidden';
		setOpacity(e, 0);
		delete e.stretchFade;
	} else {
		e.stretchFade.timer = setTimeout(function(){doStretchFadeOut(id);}, e.stretchFade.timeout);
	}
}

