/*
 *	Class Animation, abstract
 */
function Animation() {
	this.StartingEvent = null;
	this.FinishedEvent = null;
	
	this.Interval = 20;
	this._hTimer = null;
	
	var pThis = this;
	this._animate = function() {
		pThis.Animate();
	}
}

Animation.prototype.initTimer = function() {
	if (this.Interval <= 0 || this._hTimer) return;

	this._hTimer = setInterval(this._animate, this.Interval);
};

Animation.prototype.stopTimer = function() {
	if (!this._hTimer) return;
	clearInterval(this._hTimer);

	this._hTimer = null;
};

Animation.prototype.Animate = function() {alert("");};

Animation.prototype.onStarting = function() {
	if (this.StartingEvent) this.StartingEvent(this);
};

Animation.prototype.onFinished = function() {
	if (this.FinishedEvent) this.FinishedEvent(this);
};

Animation.prototype.CheckIsPlaying = function() { return (this._hTimer != null); };

Animation.prototype.Init = function() { return true; };

Animation.prototype.Start = function() {
	if (this.Interval <= 0) return;
	
	if (!this.Init()) return;
	this.onStarting();
	this.initTimer();
};

Animation.prototype.Stop = function() {
	if (!this._hTimer) return;
	
	this.stopTimer();
	this.onFinished();
};

Animation.prototype.Reset = function() {
	if (!this._hTimer) return;

	this.Init();
};

function ImageSwapAnimation(pics, titles, alts, divParent, divFirst, divSecond, imgFirst, imgSecond, Width, Height, OffsetX, OffsetY) {
	Animation.call(this);
	
	this.CountToHold = 100;
	
	this.Pictures = pics;
	this.Titles = titles;
	this.Alts = alts;
	
	this.divParent = divParent;
	this.divFirst = divFirst;
	this.divSecond = divSecond;
	this.imgFirst = imgFirst;
	this.imgSecond = imgSecond;
	
	this.OffsetX = 0;
	this.OffsetY = 0;
	if (OffsetX) this.OffsetX = OffsetX;
	if (OffsetY) this.OffsetY = OffsetY;
	
	this.Width = Width;
	this.Height = Height;
	
	this.DirectionX = 0;
	this.DirectionY = 4;
	
	this.AccelX = 4;
	this.AccelY = 4;
	
	this.BounceEnable = true;
	
	this.BounceCount = 2;
	this.BounceG = 4;
	this.BounceStrengh = 0.3;

	this._holding = true;
	this._curPic = 0;
	this._curCountHold = 0;
	this._curX = 0;
	this._curY = 0;
	this._curVX = 0;
	this._curVY = 0;
	
	this.ChangingImageEvent = null;
	
	this._bouncing = false;
	this._curBounce = 0;
}

ImageSwapAnimation.prototype = new Animation();

ImageSwapAnimation.prototype._getX = function(X) {
	return this.OffsetX + X;
};

ImageSwapAnimation.prototype._getY = function(Y) {
	return this.OffsetY + Y;
};

ImageSwapAnimation.prototype._setNextPicture = function()
{
	this._curPic += 1;
	if (this._curPic >= this.Pictures.length) this._curPic = 0;
}

ImageSwapAnimation.prototype._setSecondPicture = function()
{
	var p = this._curPic + 1;
	if (p >= this.Pictures.length) p = 0;
	
	//this.imgSecond.src = this.Pictures[p];
	this._setImage(this.imgSecond, p);
}

ImageSwapAnimation.prototype._setImage = function(img, index)
{
	img.src = this.Pictures[index];
	img.alt = this.Alts[index];
	img.title = this.Titles[index];
}

ImageSwapAnimation.prototype._animateBounce = function() {
	var x = this._curX;
	var y = this._curY;

	var oldX = x;
	var oldY = y;

	if (this.DirectionX)
	{
		x += this._curVX;
		this._curX = x;
	}
	else if (this.DirectionY)
	{
		y += this._curVY;
		this._curY = y;
	}
	
//			alert("111111\n" + x + " " + y + " \nOld " + oldX + " " + oldY + " \nCur " + this._curVX + " " + this._curVY );
	
	var x2 = 0, y2 = 0;
	var bG = this.BounceG;

	var oldVX = this._curVX;
	var oldVY = this._curVY;
	
	if (this.DirectionX > 0)
	{
		x2 = x - this.Width;
		this._curVX += bG;
	}
	else if (this.DirectionX < 0)
	{
		x2 = x + this.Width;
		this._curVX -= bG;
	}
	else if (this.DirectionY > 0)
	{
		y2 = y - this.Height;
		this._curVY += bG;
	}
	else if (this.DirectionY < 0)
	{
		y2 = y + this.Height;
		this._curVY -= bG;
	}
	
//			alert("222222222\n" + x + " " + y + " " + x2 + " " + y2 + " \nOld " + oldVX + " " + oldVY + " \nCur " + this._curVX + " " + this._curVY + " \nBounce " + this.BounceStrengh + " " + bG);
	
	if ((oldVX < 0 && this._curVX > 0) || (oldVX > 0 && this._curVX < 0)) this._curVX = 0;
	if ((oldVY < 0 && this._curVY > 0) || (oldVY > 0 && this._curVY < 0)) this._curVY = 0;
	
	if (x2 <= 0 && this.DirectionX < 0)
	{
		x2 = 0;
		x = x2 - this.Width;
		this._curX = x;
	}
	else if (x2 >= 0 && this.DirectionX > 0)
	{
		x2 = 0;
		x = x2 + this.Width;
		this._curX = x;
	}
	else if (y2 <= 0 && this.DirectionY < 0)
	{
		y2 = 0;
		y = y2 - this.Height;
		this._curY = y;
	}
	else if (y2 >= 0 && this.DirectionY > 0)
	{
		y2 = 0;
		y = y2 + this.Height;
		this._curY = y;
	}
	
	this.divFirst.style.left = x + "px";
	this.divFirst.style.top = y + "px";
	
	this.divSecond.style.left = x2 + "px";
	this.divSecond.style.top = y2 + "px";
	
//			alert(x + " " + y + " " + x2 + " " + y2 + " \nOld " + oldVX + " " + oldVY + " \nCur " + this._curVX + " " + this._curVY + " \nBounce " + this.BounceStrengh + " " + bG);
	
	if (!x2 && !y2)
	{
		this._curBounce--;
		
		if (this._curBounce <= 0)
		{
			this._switchImage();
		}
		else
		{
//			alert("AAAA\n" + x + " " + y + " \nOld " + oldVX + " " + oldVY + " \nCur " + this._curVX + " " + this._curVY + " \nBounce " + this.BounceStrengh + " " + bG);
			if (this.DirectionX)
			{
				this._curVX = -(this._curVX * this.BounceStrengh);
			}
			else if (this.DirectionY)
			{
				this._curVY = -(this._curVY * this.BounceStrengh);
			}
//			alert("BBBB\n" + x + " " + y + " \nOld " + oldVX + " " + oldVY + " \nCur " + this._curVX + " " + this._curVY + " \nBounce " + this.BounceStrengh + " " + bG);
		}
	}
}

ImageSwapAnimation.prototype._animateMove = function() {
	var x = this._curX;
	var y = this._curY;
	
	if (this.DirectionX)
	{
		x += this._curVX;
		this._curX = x;
	}
	else if (this.DirectionY)
	{
		y += this._curVY;
		this._curY = y;
	}
	
	var x2 = 0, y2 = 0;

	if (this.DirectionX > 0)
	{
		x2 = x - this.Width;
		this._curVX += this.AccelX;
	}
	else if (this.DirectionX < 0)
	{
		x2 = x + this.Width;
		this._curVX -= this.AccelX;
	}
	else if (this.DirectionY > 0)
	{
		y2 = y - this.Height;
		this._curVY += this.AccelY;
	}
	else if (this.DirectionY < 0)
	{
		y2 = y + this.Height;
		this._curVY -= this.AccelY;
	}
	
	if (x2 <= 0 && this.DirectionX < 0)
	{
		x2 = 0;
		x = x2 - this.Width;
		this._curX = x;
	}
	else if (x2 >= 0 && this.DirectionX > 0)
	{
		x2 = 0;
		x = x2 + this.Width;
		this._curX = x;
	}
	else if (y2 <= 0 && this.DirectionY < 0)
	{
		y2 = 0;
		y = y2 - this.Height;
		this._curY = y;
	}
	else if (y2 >= 0 && this.DirectionY > 0)
	{
		y2 = 0;
		y = y2 + this.Height;
		this._curY = y;
	}
	
	this.divFirst.style.left = x + "px";
	this.divFirst.style.top = y + "px";
	
	this.divSecond.style.left = x2 + "px";
	this.divSecond.style.top = y2 + "px";
	
	if (!x2 && !y2)
	{
		if (this.BounceEnable)
		{
			this._bouncing = true;
			this._curBounce = this.BounceCount - 1;

			if (this.DirectionX)
			{
				this._curVX = -(this._curVX * this.BounceStrengh);
			}
			else if (this.DirectionY)
			{
				this._curVY = -(this._curVY * this.BounceStrengh);
			}
			
//			alert(x + " " + y + " - " + this._curVX + " " + this._curVY + " - " + this.DirectionX + " " + this.DirectionY);
		}
		else this._switchImage();
	}
}

ImageSwapAnimation.prototype._switchImage = function() {
	var c = this.divSecond;
	this.divSecond = this.divFirst;
	this.divFirst = c;

	var c = this.imgSecond;
	this.imgSecond = this.imgFirst;
	this.imgFirst = c;

	this._holding = true;
	this._bouncing = false;
	this._curCountHold = this.CountToHold;
	
	this._setNextPicture();
	this._setSecondPicture();
	
	if (this.ChangingImageEvent) this.ChangingImageEvent(this);
}

ImageSwapAnimation.prototype.Animate = function() {
	if (this._holding)
	{
		this._curCountHold--;
		if (this._curCountHold <=0)
		{
			var x = 0, y = 0;
			if (this.DirectionX > 0) x = -this.Width;
			else if (this.DirectionX < 0) x = this.Width;
			else if (this.DirectionY > 0) y = -this.Height;
			else if (this.DirectionY < 0) y = this.Height;

			this.divFirst.style.zIndex = 1;

			this.divSecond.style.left = this._getX(x) + "px";
			this.divSecond.style.top = this._getY(y) + "px";
			this.divSecond.style.zIndex = 2;

			this._holding = false;
			this._bouncing = false;
	
			this._curX = 0;
			this._curY = 0;
			
			this._curVX = this.DirectionX;
			if (!this.DirectionX) this._curVY = this.DirectionY;
			else this._curVY = 0;
		}
	}
	else
	{
		if (this._bouncing) this._animateBounce();
		else this._animateMove();
	}
};

ImageSwapAnimation.prototype.Init = function() {
	if (this.Pictures.length <=1) return false;

	this.divParent.style.position = "relative";
	this.divParent.style.overflow = "hidden";
	
	this.divFirst.style.position = "absolute";
	this.divFirst.style.left = this._getX(0) + "px";
	this.divFirst.style.top = this._getY(0) + "px";
	this.divFirst.style.zIndex = 2;
	
	this.divSecond.style.position = "absolute";
	this.divSecond.style.left = this._getX(0) + "px";
	this.divSecond.style.top = this._getY(this.Height) + "px";
	this.divSecond.style.zIndex = 1;

	this.imgFirst.width = this.Width;
	this.imgFirst.height = this.Height;

	this.imgSecond.width = this.Width;
	this.imgSecond.height = this.Height;
	
	this._curPic = 0;
	this._curCountHold = this.CountToHold;
	this._holding = true;
	
	this._setImage(this.imgFirst, 0);
	this._setImage(this.imgSecond, 1);
	
	//this.imgFirst.src = this.Pictures[0];	
	//this.imgSecond.src = this.Pictures[1];
		
	this._curVX = this.DirectionX;
	
	if (!this.DirectionX) this._curVY = this.DirectionY;
	else this._curVY = 0;
	
	this._bouncing = false;
	
	return true;
};
