function RM_Image_Animated(obImg, animateType, maxSize)
{
	this._obImage	= obImg;
	this._maxSize	= maxSize;
	if(animateType == 'center')
		this.strategy = new RM_Image_Animated_Center(this);
	else if(animateType == 'corner')
		this.strategy = new RM_Image_Animated_Corner(this);
	else
		throw new Error('RM_Image_Animated: strategy is undefined');
	
	var _mainHeight;
	var obMain = this;
	$(this._obImage).bind('mouseover',function(){
			if(!obMain._width)
			{
				obMain.initSizes();
				obMain.reinitParams();
			}
			if (_mainHeight != $('body').height())
			{
				obMain.reinitParams();
				_mainHeight = $('body').height();
			}
			obMain.strategy.expand(this);
	});
	$(this._obImage).bind('mouseout', function(){
			obMain.strategy.collapse(this);
	});
	
	$(window).bind('resize', function()
		{
			obMain.reinitParams();
		}
	);
}

function RM_Image_Animated_Center(obMain)
{
	this._obMain = obMain;
}

function RM_Image_Animated_Corner(obMain)
{
	this._obMain = obMain;
}


RM_Image_Animated.prototype.reinitParams = function()
{
	this._left		= $(this._obImage).offset().left;
	this._top		= $(this._obImage).offset().top;
	this._offsetX	= this._left - ((this._maxWidth - this._width) / 2);
	this._offsetY	= this._top - ((this._maxHeight - this._height) / 2);
}

RM_Image_Animated.prototype.initSizes = function()
{
	this._width		= $(this._obImage).width();
	this._height	= $(this._obImage).height();
	var k 			= this._maxSize / this._width;
	this._maxWidth	= parseInt(this._width * k);
	this._maxHeight	= parseInt(this._height* k);
}

RM_Image_Animated_Center.prototype.expand = function(pic)
{
	var picAttr	= this._obMain;
	$(pic)
		.stop()
		.css({'z-index' : 3, 'position' : 'absolute', 'width' : picAttr._width, 'height' : picAttr._height, 'left' : picAttr._left, 'top' : picAttr._top})
		.animate({'width' : picAttr._maxWidth, 'height' : picAttr._maxHeight, 'left' : picAttr._offsetX, 'top' : picAttr._offsetY} ,300)
	;
}

RM_Image_Animated_Center.prototype.collapse = function(pic)
{
	var picAttr	= this._obMain;
	$(pic)
		.stop()
		.css('z-index', 2)
		.animate({'width' : picAttr._width, 'height' : picAttr._height, 'left' : picAttr._left, 'top' : picAttr._top}, 300,
			function(){
				$(pic)
					.css({"z-index" : 1, 'position' : 'static'})
				;
			}
		)
	;
}

RM_Image_Animated_Corner.prototype.expand = function(pic)
{
	var picAttr	= this._obMain;
	$(pic)
		.stop()
		.css({'z-index' : 3, 'position' : 'absolute', 'width' : picAttr._width, 'height' : picAttr._height, 'left' : picAttr._left, 'top' : picAttr._top})
		.animate({'height' : picAttr._maxHeight, 'width' : picAttr._maxWidth}, 300)
	;
}

RM_Image_Animated_Corner.prototype.collapse = function(pic)
{
	var picAttr	= this._obMain;
	$(pic)
		.stop()
		.animate({'height' : picAttr._height, 'width' : picAttr._width}, 300,
			function(){
				$(pic)
					.removeAttr('style')
					.css({"z-index" : 1, 'position' : 'static'})
				;
			}
		)
	;
}

jQuery.fn.animated = function (animateType, maxSize)
{
	$(this).each(function ()
		{
			new RM_Image_Animated(this, animateType, maxSize);
		}
	);
}