var _g=[];
var _glen=0;

function _gNextFrame(id)
{
	_g[id].nextFrame();

	return;
}

function Changer(id, attr, startValue, endValue, off)
{
	if (typeof id == 'string')
	{
		this.elementId=id;
		this.element=$(id);
	}
	else
	{
		this.element=id;
		this.elementId=null;
	}
	
	if (!this.element)	return false;
	
	this.id=_g.length;
	this.attr=attr;

	this.startValue=startValue;
	this.endValue=endValue;
	
	this.value=this.startValue;
	
	_glen++;
	_g.push(this);

	if (startValue>endValue)	this.inc=false;
	else	this.inc=true;
	
	this.off=off;
	
	if (this.inc)
	{
		if (this.startValue==null)	this.value=0;
		if (this.endValue==null)	this.endValue=100;

		this.setValue();
		show(this.element);
	}
	else
	{
		if (this.startValue==null)	this.value=100;
		if (this.endValue==null)	this.endValue=0;

		this.setValue();
		show(this.element);

	}

	return true;
}

Changer.prototype.id=null;
Changer.prototype.element=null;
Changer.prototype.timerId=null;
Changer.prototype.timer=10;
Changer.prototype.value=0;
Changer.prototype.v=1;
Changer.prototype.a=0;
Changer.prototype.endValue=null;
Changer.prototype.startValue=null;
Changer.prototype.onEnd=null;
Changer.prototype.unit='px';
Changer.prototype.isPauseEnd=false;
Changer.prototype.pauseEnd=0;
Changer.prototype.listener=null;
Changer.prototype.autoKill=true;

Changer.prototype.start=function()
{	
	if (!this.element)	return false;
	
	if (this.startValue>this.endValue)	this.inc=false;
	else	this.inc=true;
	
	this.isPauseEnd=false;
	
	if (this.timerId)	clearTimeout(this.timerId);
	this.timeId=null;
	
	this.nextFrame();
	
	return true;
}

Changer.prototype.setValue=function()
{
	var style=this.element.style;
	var gvalue=''+this.value+this.unit;
	switch(this.attr)
	{
		case 'opacity':
		{
			style.filter='alpha(opacity='+this.value+')';
			style.opacity=(this.value/100);
		}
		break;
		case 'top':
			style.top=gvalue;
		break;
		case 'left':
			style.left=gvalue;
		break;
		case 'right':
			style.right=gvalue;
		break;
		case 'bottom':
			style.bottom=gvalue;
		break;
		case 'width':
			style.width=gvalue;
		break;
		case 'height':
			style.height=gvalue;
		break;
		case 'margin-left':
			style.marginLeft=gvalue;
		break;
		case 'margin-top':
			style.marginTop=gvalue;
		break;
		case 'margin-right':
			style.marginRight=gvalue;
		break;
		case 'margin-bottom':
			style.marginBottom=gvalue;
		break;
		case 'margin':
			style.margin=gvalue;
		break;
		case 'padding-left':
			style.paddingLeft=gvalue;
		break;
		case 'padding-top':
			style.paddingTop=gvalue;
		break;
		case 'padding-right':
			style.paddingRight=gvalue;
		break;
		case 'padding-bottom':
			style.paddingBottom=gvalue;
		break;
		case 'padding':
			style.padding=gvalue;
		break;
		case 'z-index':
			style.zIndex=gvalue;
		break;
		case 'font-size':
			style.fontSize=gvalue;
		break;		
		case 'border-width':
			style.borderWidth=gvalue;
		break;		
	}
}

Changer.prototype.nextFrame=function()
{
	if (!this.isPauseEnd)
	{
		this.v+=this.a;
		if (this.inc)	this.value+=this.v;
		else	this.value-=this.v;
		if (this.inc && this.value>this.endValue)	this.value=this.endValue;
		if (!this.inc && this.value<this.endValue)	this.value=this.endValue;
		this.setValue();
		if (this.listener!=null)	this.listener();
	}
	
	if ((this.inc && this.value==this.endValue) || (!this.inc && this.value==this.endValue))
	{
		if (!this.isPauseEnd)
		{
			if (this.pauseEnd)
			{
				this.isPauseEnd=true;
				this.timerId=setTimeout('_gNextFrame('+this.id+');', this.pauseEnd);
			
				return;
			}
		}
		
		this.isPauseEnd=true;
		
		if (this.isPauseEnd)
		{
			if (this.autoKill)
			{
				this.id=null;
				_glen--;
		
				if (_glen<=0)
				{
					_glen=0;
					_g=[];
				}
			}
			
			if (this.off)	hide(this.element);
			if (this.onEnd)	this.onEnd();			
		}
		
		return;
	}
	else	this.timerId=setTimeout('_gNextFrame('+this.id+');', this.timer);
}
