/**
 * @author pfigueiredo
 */
KFade.prototype = new KAnimation();
KFade.prototype.constructor = KFade;
function KFade(){
	KAnimation.call(this);
	this.alpha = 0;
	this.fade = null;
	this.threshold = 0;
}

KAnimation.prototype.goIn = function() {
	this.fadeIn(100);
}

KAnimation.prototype.goOut = function() {
	this.fadeOut(0);
}

KFade.prototype.fadeIn = function(alpha){
	if (this.state != 'stopped') {
		if (this.actionStack.length == 0 && this.state != 'in') {
			this.actionStack.push({"action":"in","alpha":alpha});
		}
		return;
	}
	this.setAlpha(alpha);
	this.state = this.fade = 'in';
	this.onStart();
	this.animate();
}

KFade.prototype.fadeOut = function(alpha){
	if (this.state != 'stopped') {
		if (this.actionStack.length == 0 && this.state != 'out') {
			this.actionStack.push({"action":"out","alpha":alpha});
		}
		return;
	}
	this.setAlpha(alpha);
	this.state = this.fade = 'out';
	this.onStart();
	this.animate();
}

KFade.prototype.checkActionStack = function() {
	if (this.actionStack.length != 0) {
		var action = this.actionStack.pop();
		this.setAlpha(action.alpha);
		this.state = this.fade = action.action;
		this.onStart();
		this.animate();
	}
}

KFade.prototype.setThreshold = function(threshold){
	switch (KBrowserDetect.browser) {
		case 1:
			this.threshold = threshold / 100;
			break;
		case 2:
			this.threshold = threshold;
			break;
		case 3:
			this.threshold = threshold / 100;
			break;
		case 4:
			this.threshold = threshold / 100;
			break;
		case 5:
			this.threshold = threshold / 100;
			break;
	}
}

KFade.prototype.setAlpha = function(alpha){
	switch (KBrowserDetect.browser) {
		case 1:
			this.alpha = alpha / 100;
			break;
		case 2:
			this.alpha = alpha;
			break;
		case 3:
			this.alpha = alpha / 100;
			break;
		case 4:
			this.alpha = alpha / 100;
			break;
		case 5:
			this.alpha = alpha / 100;
			break;
	}
}

KFade.prototype.animate = function(){
	this.timeScrolled = 0;
	this.amount = this.destination - this.currentPosition;
	this.duration = 40;
	this.target.style.display = 'block';
	var __fader = this;
	this.timer = setInterval(function () {timedCount((__fader.fade == 'in' ? __fader.threshold  : -__fader.threshold), __fader);}, 1);
}

function timedCount(threshold, __fader){
	var opacity = 0;

	switch (KBrowserDetect.browser) {
		case 1:
			opacity = parseFloat(__fader.target.style.MozOpacity) + threshold;
			break;
		case 2:
			opacity = parseInt(__fader.target.filters.alpha.opacity) + threshold;
			break;
		case 3:
			opacity = parseInt(__fader.target.style.opacity) + threshold;
			break;
		case 4:
			opacity = parseFloat(__fader.target.style.MozOpacity) + threshold;
			break;
		case 5:
			opacity = parseFloat(__fader.target.style.MozOpacity) + threshold;
			break;
	}
	if (isNaN(opacity)) {
		opacity = threshold;
	}

	if (__fader.fade == 'out' && opacity < __fader.alpha) {
		clearInterval(__fader.timer);
		__fader.timer = null;
		if (__fader.alpha == 0) {
			__fader.target.style.display = 'none';
		}
		__fader.onStop();
		__fader.state = 'stopped';
		__fader.checkActionStack();
	}
	else if (__fader.fade == 'in' && opacity > __fader.alpha) {
		clearInterval(__fader.timer);
		__fader.timer = null;
		__fader.onStop();
		__fader.state = 'stopped';
		__fader.checkActionStack();
	}
	else {
		switch (KBrowserDetect.browser) {
			case 1:
				__fader.target.style.MozOpacity = opacity;
				break;
			case 2:
				__fader.target.filters.alpha.opacity = opacity;
				break;
			case 3:
				__fader.target.style.opacity = opacity;
				break;
			case 4:
				__fader.target.style.KhtmlOpacity = opacity;
				break;
			case 5:
				__fader.target.style.MozOpacity = opacity;
				break;
		}
	}
}
