/**
 * @projectDescription
 * Модуль JavaScript анимации
 * @author Kotelnikov Dmitriy (dimonnot@design.ru)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 * @vesion 0.1 (16.11.2009)
 * @requires jQuery
 */

/*
 * Класс инициализации эффекта морфинка для элемента
 * @param {JQueryElement} element Элемент для каторого создается морфинг
 * @param {Number} imagesCount Количество кадров
 */
function Morphing( element, imagesCount ){
	var self = this;
	this.currentIndex = 0;
	this.element = element;
	this.width = element[0].clientWidth;
	this.imagesCount = imagesCount;

	element.hover(
		function() {
			self.morphIn();
		},
		function() {
			self.morphOut();
		}
	)
}

Morphing.prototype = {
	FRAME_INTERVAL: 20,
	
	morphIn: function (){
		if( this.timeout )
			clearTimeout(this.timeout);
			
		var self = this;
		
		if( this.currentIndex >= this.imagesCount ){
			return;
		}
			
		this.currentIndex++;
		
		this.element.css('background-position', '-' + this.currentIndex * this.width + 'px 0' );

		self.timeout = setTimeout(
			function(){
				self.morphIn()
			},
			this.FRAME_INTERVAL
		);
	},
	
	morphOut: function (){
		if( this.timeout )
			clearTimeout(this.timeout);

		var self = this;
		
		if( this.currentIndex == 0 )
			return;
		
		this.currentIndex--;
		
		this.element.css('background-position', '-' + this.currentIndex * this.width + 'px 0' );

		self.timeout = setTimeout(
			function(){
				self.morphOut()
			},
			this.FRAME_INTERVAL
		);
	}
}