﻿// javascript animation action start
// 2005. 6. 26. by guoo.net
// 2005. 7. 2. modified by guoo.net
var JAnimation_list = [];
function JAnimation(object, id, ease, spendTime, delayTime, initA, markA, initB, markB, method) {
	if(!explorer && !gecko) return;
	this.index = JAnimation_list.length;
	JAnimation_list[JAnimation_list.length] = this;

	this.isFinish = false;
	this.timeout = '';

	this.id = id;
	this.object = object;
	this.spendTime = spendTime;
	this.delayTime = delayTime;
	this.currentTime = 0;
	this.currentA = initA;
	this.currentB = initB;
	
	this.initA = initA;
	this.initB = initB;
	this.markA = markA;
	this.markB = markB;
	this.method = method;
	this.ease = ease;
	if(!this.ease) this.ease= '';

	this.fps = 30;
}
JAnimation.prototype.start = function() {
	this.isFinish = false;
	setTimeout('JAnimation_list['+this.index+'].active();', this.delayTime);
}
JAnimation.prototype.stop = function() {
	clearTimeout(this.timeout);
	this.isFinish = true;
	if(this.id == 'fade') {
		this.object.style.filter = 'alpha(opacity='+this.markA+')';
	}
	if(this.id == 'move') {
		this.object.style.left = this.markA;
		this.object.style.top = this.markB;
	}
	if(this.id == 'movex') {
		this.object.style.left = this.markA;
	}
	if(this.id == 'movey') {
		this.object.style.top = this.markA;
	}
	if(this.id == 'size') {
		this.object.style.left = this.markA;
		this.object.style.top = this.markB;
	}
	if(this.id == 'width') {
		if(this.markA < 0) {
			this.object.style.width = -this.markA;
			this.object.style.left = this.object.offsetLeft - this.markA;
		}else {
			this.object.style.width = this.markA;
		}
	}
	if(this.id == 'height') {
		if(this.markA < 0) {
			this.object.style.width = -this.markA;
			this.object.style.left = this.object.offsetLeft - this.markA;
		}else {
			this.object.style.width = this.markA;
		}
	}
}
JAnimation.prototype.finish = function() {
}

//ease start
/*   
Easing Equations v1.2
March 11, 2002

Tweening equations of every variety.

Robert Penner
www.robertpenner.com

Changes:
1.2
- inline optimizations (changing t and multiplying in one step)--thanks to Tatsuo Kato for the idea

t: current time, b: beginning value, c: change in value, d: duration

ease관련함수 출처.
*/
function JAnimation_normalTween(t, d, b, c) {
	return c*t/d + b;
}
//QUADRATIC EASING: x^2
function JAnimation_easeInQuad(t, d, b, c) {
	return c*(t/=d)*t + b;
}
function JAnimation_easeOutQuad(t, d, b, c) {
	return -c*(t/=d)*(t-2) + b;
}
function JAnimation_easeInOutQuad(t, d, b, c) {
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
}
// CUBIC EASING: x^3
function JAnimation_easeInCubic(t, d, b, c) {
	return c*(t/=d)*t*t + b;
}
function JAnimation_easeOutCubic(t, d, b, c) {
	return c*((t=t/d-1)*t*t + 1) + b;
}
function JAnimation_easeInOutCubic(t, d, b, c) {
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
}
// QUARTIC EASING: x^4
function JAnimation_easeInQuart(t, d, b, c) {
	return c*(t/=d)*t*t*t + b;
}
function JAnimation_easeOutQuart(t, d, b, c) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
}
function JAnimation_easeInOutQuart(t, d, b, c) {
	if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
	return -c/2 * ((t-=2)*t*t*t - 2) + b;
}
// QUINTIC EASING: x^5
function JAnimation_easeInQuint(t, d, b, c) {
	return c*(t/=d)*t*t*t*t + b;
}
function JAnimation_easeOutQuint(t, d, b, c) {
	return c*((t=t/d-1)*t*t*t*t + 1) + b;
}
function JAnimation_easeInOutQuint(t, d, b, c) {
	if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
	return c/2*((t-=2)*t*t*t*t + 2) + b;
}
// SINUSOIDAL EASING: sin(x)
function JAnimation_easeInSine(t, d, b, c) {
	return c * (1 - Math.cos(t/d * (Math.PI/2))) + b;
}
function JAnimation_easeOutSine(t, d, b, c) {
	return c * Math.sin(t/d * (Math.PI/2)) + b;
}
function JAnimation_easeInOutSine(t, d, b, c) {
    return c/2 * (1 - Math.cos(t/d * Math.PI)) + b;
}
// EXPONENTIAL EASING: e^x
function JAnimation_easeInExpo(t, d, b, c) {
	return c * Math.pow(2, 10 * (t/d - 1)) + b;
}
function JAnimation_easeOutExpo(t, d, b, c) {
	return c * (-Math.pow(2, -10 * t/d) + 1) + b;
}
function JAnimation_easeInOutExpo(t, d, b, c) {
	if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
	return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
}
// CIRCULAR EASING: sqrt(1-x^2)
function JAnimation_easeInCirc(t, d, b, c) {
	return c * (1 - Math.sqrt(1 - (t/=d)*t)) + b;
}
function JAnimation_easeOutCirc(t, d, b, c) {
	return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
}
function JAnimation_easeInOutCirc(t, d, b, c) {
	if ((t/=d/2) < 1) return c/2 * (1 - Math.sqrt(1 - t*t)) + b;
	return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
}
function JAnimation_easeSine(t, d, b, c) {
	var g=c*0.7*Math.sin(t*Math.PI/d);
	return Math.easeInQuad(t, d, b, c)+ g;
}
function JAnimation_elastic(t, d, b, c, eb, ec) {
	var g=c*Math.exp(-eb*t/d)*Math.cos((ec*2-1)/2*t*Math.PI/d);
	return c+b-g;
}
JAnimation.prototype.getCurrentValue = function(id, t, d, b, c) {
	if(0) {
	}else if(id=='easeInQuad') {
		return JAnimation_easeInQuad(t, d, b, c);
	}else if(id=='easeOutQuad') {
		return JAnimation_easeOutQuad(t, d, b, c);
	}else if(id=='easeInOutQuad') {
		return JAnimation_easeInOutQuad(t, d, b, c);
	}else if(id=='easeInCubic') {
		return JAnimation_easeInCubic(t, d, b, c);
	}else if(id=='easeOutCubic') {
		return JAnimation_easeOutCubic(t, d, b, c);
	}else if(id=='easeInOutCubic') {
		return JAnimation_easeInOutCubic(t, d, b, c);
	}else if(id=='easeInQuart') {
		return JAnimation_easeInQuart(t, d, b, c);
	}else if(id=='easeOutQuart') {
		return JAnimation_easeOutQuart(t, d, b, c);
	}else if(id=='easeInOutQuart') {
		return JAnimation_easeInQuart(t, d, b, c);
	}else if(id=='easeInQuint') {
		return JAnimation_easeInQuint(t, d, b, c);
	}else if(id=='easeOutQuint') {
		return JAnimation_easeOutQuint(t, d, b, c);
	}else if(id=='easeInOutQuint') {
		return JAnimation_easeInOutQuint(t, d, b, c);
	}else if(id=='easeInSine') {
		return JAnimation_easeInSine(t, d, b, c);
	}else if(id=='easeOutSine') {
		return JAnimation_easeOutSine(t, d, b, c);
	}else if(id=='easeInOutSine') {
		return JAnimation_easeInOutSine(t, d, b, c);
	}else if(id=='easeInExpo') {
		return JAnimation_easeInExpo(t, d, b, c);
	}else if(id=='easeOutExpo') {
		return JAnimation_easeOutExpo(t, d, b, c);
	}else if(id=='easeInOutExpo') {
		return JAnimation_easeInOutExpo(t, d, b, c);
	}else if(id=='easeInCirc') {
		return JAnimation_easeInCirc(t, d, b, c);
	}else if(id=='easeOutCirc') {
		return JAnimation_easeInCirc(t, d, b, c);
	}else if(id=='easeInOutCirc') {
		return JAnimation_easeInOutCirc(t, d, b, c);
	}else if(id=='easeSine') {
		return JAnimation_easeSine(t, d, b, c);
	}else if(id=='elastic') {
		return JAnimation_elastic(t, d, b, c, 5, 7);
	}else {
		return JAnimation_normalTween(t, d, b, c);
	}
}
//ease finish

JAnimation.prototype.active = function() {
	if(this.currentTime > this.spendTime) this.currentTime = this.spendTime;
	if(this.id == 'fade') {
		if(!this.isFinish) {
			if(this.currentTime==0) {
				if(this.method==1) {
					this.initA = this.object.offsetLeft;
				}else if(this.mehtod==2) {
					this.initA = this.object.offsetLeft;
					this.markA = this.initA + this.markA;
				}//if
			}
			this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (this.markA - this.initA));
			this.object.style.filter = 'alpha(opacity='+this.currentA+')';
			this.object.style.opacity = this.currentA/100;
		}
	}//fade
	if(this.id == 'move') {
		if(!this.isFinish) {
			if(this.currentTime==0) {
				if(this.method==1) {
					this.initA = this.object.offsetLeft;
					this.initB = this.object.offsetTop;
				}else if(this.mehtod==2) {
					this.initA = this.object.offsetLeft;
					this.initB = this.object.offsetTop;
					this.markA = this.initA + this.markA;
					this.markB = this.initB + this.markB;
				}//if
			}
			this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (this.markA - this.initA));
			this.currentB = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initB, (this.markB - this.initB));
			this.object.style.left = this.currentA;
			this.object.style.top = this.currentB;
		}
	}//move
	if(this.id == 'size') {
		if(!this.isFinish) {
			if(this.currentTime==0) {
				if(this.method==1) {
					this.initA = this.object.offsetLeft;
					this.initB = this.object.offsetTop;
				}else if(this.mehtod==2) {
					this.initA = this.object.offsetLeft;
					this.initB = this.object.offsetTop;
					this.markA = this.initA + this.markA;
					this.markB = this.initB + this.markB;
				}//if
			}
			this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (this.markA - this.initA));
			this.currentB = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initB, (this.markB - this.initB));
			this.object.style.width = this.currentA;
			this.object.style.height = this.currentB;
		}
	}//size
	if(this.id == 'movex') {
		if(!this.isFinish) {
			if(this.currentTime==0) {
				if(this.method==1) {
					this.initA = this.object.offsetLeft;
				}else if(this.mehtod==2) {
					this.initA = this.object.offsetLeft;
					this.markA = this.initA + this.markA;
				}//if
			}
			this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (this.markA - this.initA));
			this.object.style.left = this.currentA;
		}
	}//movex
	if(this.id == 'movey') {
		if(!this.isFinish) {
			if(this.currentTime==0) {
				if(this.method==1) {
					this.initA = this.object.offsetLeft;
				}else if(this.mehtod==2) {
					this.initA = this.object.offsetLeft;
					this.markA = this.initA + this.markA;
				}//if
			}
			this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (this.markA - this.initA));
			this.object.style.top = this.currentA;
		}
	}//movey
	if(this.id == 'height') {
		if(!this.isFinish) {
			if(this.currentTime==0) {
				if(this.method==1) {
					this.initA = this.object.offsetLeft;
				}else if(this.mehtod==2) {
					this.initA = this.object.offsetLeft;
					this.markA = this.initA + this.markA;
				}//if
			}
			if(this.markA < 0) {
				this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (-this.markA - this.initA));
				this.object.style.height = this.currentA;
				this.object.style.left = this.object.offsetLeft - this.currentA;
			}else {
				this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (this.markA - this.initA));
				this.object.style.height = this.currentA;
			}
		}
	}//height
	if(this.id == 'width') {
		if(!this.isFinish) {
			if(this.currentTime==0) {
				if(this.method==1) {
					this.initA = this.object.offsetLeft;
				}else if(this.mehtod==2) {
					this.initA = this.object.offsetLeft;
					this.markA = this.initA + this.markA;
				}//if
			}
			if(this.markA < 0) {
				this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (-this.markA - this.initA));
				this.object.style.width = this.currentA;
				this.object.style.left = this.object.offsetLeft - this.currentA;
			}else {
				this.currentA = this.getCurrentValue(this.ease, this.currentTime, this.spendTime, this.initA, (this.markA - this.initA));
				this.object.style.width = this.currentA;
			}
		}
	}//width
	if(this.id == 'set') {
		if(!this.isFinish) {
			eval('this.object.style.'+initA+' = \''+markA+'\';');
		}
	}//set

	if(!this.isFinish) this.timeout = setTimeout('JAnimation_list['+this.index+'].active();', this.fps);
	if(this.isFinish) this.finish();
	if(this.currentTime==this.spendTime) this.isFinish = true;
	this.currentTime = this.currentTime + this.fps;
}
// javascript animation action finish
