//
// 2009-05-15
// Copyright (c) Art. Lebedev Studio | http://www.artlebedev.ru/
// Author - Svetlana Shustrova | soulntse@design.ru
//


function SimpleAnimation(eImage, iFrameHeight, iFrameCount, iFrameDelay, bLoop) {
	if (eImage) {
		this.eImage = eImage
		this.iFrameHeight = iFrameHeight;
		this.iFrameCount = iFrameCount;
		this.iFrameDelay = iFrameDelay;
		this.iCurrentFrame = 0;
		this.iLastFrame = iFrameCount - 1;
		this.bLoop = bLoop;
		this.TO = null;

		this.start = function(iFirstFrame,iLastFrame) {
			oThis = this;
			if (iFirstFrame) this.iCurrentFrame = iFirstFrame;
			if (iLastFrame) this.iLastFrame = iLastFrame;
			if (this.TO) {
				clearTimeout(this.TO);
				this.TO = null;
			}
			this.showCurrentFrame();
			if (this.iCurrentFrame < this.iLastFrame) {
				this.TO = setTimeout(function(){ oThis.showNextFrame() }, this.iFrameDelay);
			} else if (oThis.bLoop) {
				this.TO = setTimeout(function(){ oThis.showFirstFrame() }, this.iFrameDelay);
			} else this.TO = setTimeout(function(){ oThis.showPreviousFrame() }, this.iFrameDelay);
		};
		this.stop = function() {
			if (this && this.TO) clearTimeout(this.TO);
		};
		this.showFirstFrame = function() {
			if (this) {
				if (this.bLoop) {
					oThis = this;
					this.iCurrentFrame = 0;
					this.showCurrentFrame();
					this.TO = setTimeout(function(){ oThis.showNextFrame() }, this.iFrameDelay);
				} else this.onFinishEvent();
			}
		};
		this.showNextFrame = function() {
			if (this) {
				if (this.iCurrentFrame < this.iLastFrame || this.bLoop) {
					oThis = this;
					if (this.iCurrentFrame == this.iLastFrame && this.bLoop) this.iCurrentFrame = 0;
					else this.iCurrentFrame++;
					this.showCurrentFrame();
					this.TO = setTimeout(function(){ oThis.showNextFrame() }, this.iFrameDelay);
				} else this.onFinishEvent();
			}
		};
		this.showPreviousFrame = function() {
			if (this) {
				if (this.iCurrentFrame > this.iLastFrame) {
					oThis = this;
					this.iCurrentFrame--;
					this.showCurrentFrame();
					this.TO = setTimeout(function(){ oThis.showPreviousFrame() }, this.iFrameDelay);
				} else this.onFinishEvent();
			}
		};
		this.showCurrentFrame = function() {
			if (this) {
				if (this.iCurrentFrame) var iTop = '-' + (this.iFrameHeight * this.iCurrentFrame);
				else var iTop = 0;
				if (this.eImage) this.eImage.style.top = iTop + 'px';
			}
		};
		this.onFinishEvent = function() {
			if (this && this.onFinish) this.onFinish();
		};

		return this;
	} else return null;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * */

//var oBatAnim = null;
//var oThrowAnim = null;
window.onload = function() {
	if (document.getElementById('frames')) {
		oBatAnim = new SimpleAnimation(document.getElementById('frames'), 197, 8, 50, true);
		eBatStartBtn = document.getElementById('animation-on');
		eBatStopBtn = document.getElementById('animation-off');
	
		if (eBatStartBtn) eBatStartBtn.onclick = function() {
			if (eBatStartBtn.className != 'active') {
				eBatStartBtn.className = 'active';
				if (eBatStopBtn) eBatStopBtn.className = '';
				if (oBatAnim) oBatAnim.start();
			}
		}
	
		if (eBatStopBtn) eBatStopBtn.onclick = function() {
			if (eBatStopBtn.className != 'active') {
				eBatStopBtn.className = 'active';
				if (eBatStartBtn) eBatStartBtn.className = '';
				if (oBatAnim) oBatAnim.stop();
			}
		}
	}
	if (document.getElementById('bat-throw')) {
		oThrowAnim = new SimpleAnimation(document.getElementById('bat-throw'), 68, 20, 70, false);
		oThrowAnim.onFinish = function() {
			this.iCurrentFrame = 0;
			this.showCurrentFrame();
			if (eThrowBtn) eThrowBtn.className = '';
		}
		eThrowBtn = document.getElementById('throw-start');
		if (eThrowBtn) eThrowBtn.onclick = function() {
			if (oThrowAnim) {
				if (eBatStopBtn) eBatStopBtn.onclick();
				eThrowBtn.className = 'active';
				oThrowAnim.start(0,19);
			}
		}
	}
}



