function AnimationItem(sID){
	return this.Init(sID);
}

AnimationItem.prototype.oPtr=null;
AnimationItem.prototype.bIsRelative=false;
AnimationItem.prototype.bInPrc=false; //движение в пикселях будет пересчитываться на проценты

AnimationItem.prototype.Init=function(sID){
	var oElem=document.getElementById(sID)
	if(oElem){
		this.ID=sID;
		this.oPtr=oElem;
		this.SetDefaults();
		this.onLoad();
		return true;
	}
	
	return false;
}

AnimationItem.prototype.onLoad=function(){

}

AnimationItem.prototype.SetDefaults=function(){
	this.x=this.oPtr.offsetLeft;
	this.y=this.oPtr.offsetTop;
}

//стандартный метод, который запускается после завершения анимации
AnimationItem.prototype.onAnimationOver=function(){
	delete this.onAnimate;
}

AnimationItem.prototype.getProp=function(sProp){
	return (sProp) ? Number(sProp.match(/(\-?\d+)/)[1]) : 0;
}

AnimationItem.prototype.MoveTo=function(x, y){
	
	var _x=x-( (this.bIsRelative) ? this.x : 0 );
	var _y=y-( (this.bIsRelative) ? this.y : 0 );

	if(this.bInPrc)
		this.CalcAsPrc(_x);
	else
		this.oPtr.style.left=_x+'px';
	
	this.oPtr.style.top=_y+'px';
}

AnimationItem.prototype.CalcAsPrc=function(x){
	var iW=this.oPtr.offsetWidth;
	var iParW=this.oPtr.parentNode.offsetWidth;
	if( x + iW > iParW / 2 ){
		this.oPtr.style.left = "auto";
		this.oPtr.style.right = ( iParW - x - iW ) * 100 / iParW + "%"; 
	}
	else{
		this.oPtr.style.left = (x*100 / iParW) + "%";
		this.oPtr.style.right = "auto"; 
	}
}


AnimationItem.prototype.Get1Prc=function(){
	var oParent=this.oPtr.parentNode;
	var i1PrcX, i1PrcY;
	if(oParent.tagName == 'BODY'){
		if(document.body.clientWidth){
			i1PrcX=document.body.clientWidth/100;
			i1PrcY=document.body.clientHeight/100;
		}
		else{
			i1PrcX=window.innerWidth/100;
			i1PrcY=window.innerHeight/100;
		}
	}
	else{
		i1PrcX=oParent.offsetWidth/100;
		i1PrcY=oParent.offsetHeight/100;
	}
	//alert(i1PrcX + ' -- ' + i1PrcY);
	return [i1PrcX, i1PrcY];
}

AnimationItem.prototype.MoveToPrc=function(xPrc, yPrc){
	var aPrc=this.Get1Prc();
	this.MoveTo(aPrc[0]*xPrc, aPrc[1]*yPrc);
}

AnimationItem.prototype.InitEaseIn=function(oPnt, f, bEasePrc){
	this.tX=oPnt.x;
	this.tY=oPnt.y;
	this.f=f;
	this.bEasePrc=(bEasePrc) ? 1:0;
	/* this.cX=this.getProp(this.oPtr.style.left);
	this.cY=this.getProp(this.oPtr.style.top); */
	this.cX=this.oPtr.offsetLeft;
	this.cY=this.oPtr.offsetTop;
	if(bEasePrc){
		var aPrc=this.Get1Prc();
		this.cX=this.cX/aPrc[0];
		this.cY=this.cY/aPrc[1];
	}
	this.onAnimate=this.EaseIn;
}

AnimationItem.prototype.EaseIn=function(){
	/*
	cX, cY - текущие координаты
	tX, tY - целевые координаты
	f - ускорение (замедление)
	*/
	var sF=(this.bEasePrc) ? 'MoveToPrc' : 'MoveTo';
	this.cX=(this.tX-this.cX)*this.f + this.cX;
	this.cY=(this.tY-this.cY)*this.f + this.cY;
	
	if(Math.abs(this.tY-this.cY) < 2 && Math.abs(this.tX-this.cX) < 2){
		this.cX=this.tX;
		this.cY=this.tY;
		this.onAnimationOver();
	}
	
	this[sF](this.cX, this.cY);
}

AnimationItem.prototype.InitFade=function(iDelta, bIsOut){
	if(bIsOut){
		this.cA=100; //current alpha
		this.tA=0;   //target alpha
	}
	else{
		this.cA=0;
		this.tA=100;
	}
	this.iDelta=iDelta;
	this.onAnimate=(bIsOut) ? this.FadeOut : this.FadeIn;
}

AnimationItem.prototype.FadeOut=function(){
	this.cA-=this.iDelta;
	if(this.cA <= 0){
		this.cA=0;
		this.onAnimationOver();
	}
	this.SetOpacity(this.cA);
}

AnimationItem.prototype.FadeIn=function(){
	this.cA+=this.iDelta;
	if(this.cA >= 100){
		this.cA=99;
		this.onAnimationOver();
	}
	this.SetOpacity(this.cA);
}

AnimationItem.prototype.SetOpacity=function(iValue){
	this.oPtr.style.MozOpacity=iValue/100;
	this.oPtr.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+iValue+")";
}

AnimationItem.prototype.InitResize=function(oSPnt, oTPnt, iDecay, oSnapPoint){
	this.sW=oSPnt.x;
	this.sH=oSPnt.y;
	this.tW=oTPnt.x;
	this.tH=oTPnt.y;
	this.d=iDecay;
	
	this.iBodyWidth=document.body.clientWidth;
	
	this.oSnapInit = this.oSnapPoint = null;
	
	if(oSnapPoint){
		this.oSnapInit=oSPnt;
		if(typeof(oSnapPoint) == 'string'){ //укзываем наиболее частоиспользуемые параметры
			switch(oSnapPoint){
				case 'lt': //left top
					this.oSnapPoint=new pnt(0, 0);
					break;
				case 'rt': //right top
					this.oSnapPoint=new pnt(this.sW, 0);
					break;
				case 'lb': //left bottom
					this.oSnapPoint=new pnt(0, this.sH);
					break;
				case 'rb': //right bottom
					this.oSnapPoint=new pnt(this.sW, this.sH);
					break;
				case 'center':
					this.oSnapPoint=new pnt(this.sW/2, this.sH/2);
					break;
			}
		}
		else if(oSnapPoint.constructor == pnt){ //передали произвольную точку
			this.oSnapPoint = oSnapPoint;
		}
	}
	
	this.onAnimate=this.Resize;
}

AnimationItem.prototype.Resize=function(){
	this.sW+=(this.tW-this.sW)*this.d;
	this.sH+=(this.tH-this.sH)*this.d;
	
	if(Math.abs(this.tH-this.sH) < 2 && Math.abs(this.tW-this.sW) < 2){
		this.sW=this.tW;
		this.sH=this.tH;
		this.onAnimationOver();
	}
	
	this.SizeTo(this.sW, this.sH);
	if(this.oSnapPoint)
		this.Snap();
}

AnimationItem.prototype.Snap=function(){
	var dx=this.oSnapPoint.x*(1 - this.sW/this.oSnapInit.x);
	var dy=this.oSnapPoint.y*(1 - this.sH/this.oSnapInit.y);
	
	this.MoveTo(this.x + dx, this.y + dy);
}

AnimationItem.prototype.SizeTo=function(w, h){
	this.oPtr.style.width=w+'px';
	this.oPtr.style.height=h+'px';
}
