/*
	uniAnimation TextBox Effect Sliding

	Version:	1.0.0.
*/

uniAnimationTextBoxEffect=function(obj, param) {
	var self=this;
	this.obj		= obj;
	this.eventList		= {};
	this.typeList		= ['LEFT-TO-RIGHT', 'RIGHT-TO-LEFT', 'TOP-TO-BOTTOM', 'BOTTOM-TO-TOP'];
	this.type		= (param.type && this.typeList.inArray(param.type.toUpperCase()) ? param.type.toUpperCase() : 'RIGHT-TO-LEFT');
	this.status		= '';
	this.animation		= {
		position	: 0,
		length		: 20,
		destination	: 1
	}
	if (param.show) this.animation.position=this.animation.length;
	this.updatePosition();
}

uniAnimationTextBoxEffect.prototype.f=function(x) {
	return x/this.animation.length;
}

uniAnimationTextBoxEffect.prototype.updatePosition=function() {
	var x, y;
	if (this.type == 'LEFT-TO-RIGHT' || this.type == 'RIGHT-TO-LEFT') {	// Horizontal
		x=Math.round(-this.obj.clientWidth+this.obj.clientWidth*this.f(this.animation.position));
		y=0;
	} else {								// Vertical
		x=0;
		y=Math.round(-this.obj.clientHeight+this.obj.clientHeight*this.f(this.animation.position));
	}

	if (this.type == 'LEFT-TO-RIGHT') {
		this.obj.style.left	= x+'px';
		this.obj.style.right	= 'auto';
	} else if (this.type == 'RIGHT-TO-LEFT') {
		this.obj.style.left	= 'auto';
		this.obj.style.right	= x+'px';
	} else if (this.type == 'TOP-TO-BOTTOM') {
		this.obj.style.top	= y+'px';
		this.obj.style.bottom	= 'auto';
	} else if (this.type == 'BOTTOM-TO-TOP') {
		this.obj.style.top	= 'auto';
		this.obj.style.bottom	= y+'px';
	}
}

uniAnimationTextBoxEffect.prototype.sliding=function() {
	var self=this;

	this.updatePosition();
	if ((this.animation.position==0 && this.animation.destination<0) || (this.animation.position==this.animation.length && this.animation.destination>0)) {
		this.status='';
		this.dispatchEvent((this.animation.position==0 ? 'HideCompleted' : 'ShowCompleted'));
	} else {
		this.status='in process';
		this.animation.position+=this.animation.destination;
		this.durationTimeout=setTimeout(function() {self.sliding();}, 10);
	}
}

uniAnimationTextBoxEffect.prototype.show=function() {
	if (this.animation.position==this.animation.length) this.dispatchEvent('ShoweCompleted');
	else {
		this.animation.destination = 1;
		if (this.animation.position==0) {
			this.animation.position=0;
			this.sliding();
		}
	}
}

uniAnimationTextBoxEffect.prototype.hide=function() {
	if (this.animation.position==0) this.dispatchEvent('HideCompleted');
	else {
		this.animation.destination = -1;
		if (this.animation.position==this.animation.length) {
			this.animation.position=this.animation.length;
			this.sliding();
		}
	}
}

uniAnimationTextBoxEffect.prototype.addEventListener=function(type, listener) {
	if (typeof(listener)=='function') this.eventList[type]=listener;
}

uniAnimationTextBoxEffect.prototype.removeEventListener=function(type, listener) {
	delete this.eventList[type];
}

uniAnimationTextBoxEffect.prototype.dispatchEvent=function(type) {
	if (this.eventList[type]) {
		this.eventList[type].call();
	}
}
