jQuery.fn.icPhotoGallery = function(options){
	var options = jQuery.extend({
		animate: true, //Анимировать ли изменение размеров
		animateDuration: 500, //Время анимации изменения размера в миллисекундах
		border: null, //CSS-код рамки элемента галереи
		debug: false, //просто свойство, не обращайте внимания
		imageBgColor: null, //цвет фона картинок
		imageHeight: null, //высота каждой картинки
		imageWidth: null, //ширина каждой картинки
		itemBgColor: null, //цвет фона каждого элемента DOM, содержащего картинку
		itemHeight: null, //высота каждого элемента DOM, содержащего картинку
		itemWidth: null, //ширина каждого элемента DOM, содержащего картинку
		marginTop: null, // CSS-свойство элемента галереи
		marginRight: null, // CSS-свойство элемента галереи
		marginBottom: null, // CSS-свойство элемента галереи
		marginLeft: null, // CSS-свойство элемента галереи
		galleryHeight: null, //принудительная высота галереи
		padding: null //размер внутреннего поля каждого элемента фотогалереи
	}, options);
	
	return this.each(function(){
		var animating = false;
		var opCount = 0;
		var contextObject = this;
		jQuery(contextObject).before('<div id = "her" class = "ic-photo-gallery-container"><div class = "ic-photo-gallery"></div></div>');
		jQuery(contextObject).after('<div style = "clear: both; position: relative;"></div>');
		var photoGallery = jQuery('div.ic-photo-gallery:last');
		var photoGalleryContainer = jQuery('div.ic-photo-gallery-container:last');
		var galleryWidth = jQuery(photoGallery).innerWidth();
		var photoWidth = 0;
		var photoCount = 0;
		jQuery(contextObject).find('img').each(function(){
			if (options.imageBgColor)
			{
				jQuery(this).css('background-color', options.imageBgColor);
			}
			if (options.imageHeight)
			{
				jQuery(this).css('height', options.imageHeight);
			}
			if (options.imageWidth)
			{
				jQuery(this).css('width', options.imageWidth);
			}
			var imgParent = jQuery(this).parents('a:first');
			var cuttingObject = (typeof imgParent[0] != 'undefined') ? imgParent : this;
			jQuery(cuttingObject).remove();
			var insertingObject = jQuery('<div class = "item"></div>');
			if (options.itemBgColor)
			{
				jQuery(insertingObject).css('background-color', options.itemBgColor);
			}
			if (options.itemHeight)
			{
				jQuery(insertingObject).css('height', options.itemHeight);
			}
			if (options.itemWidth)
			{
				jQuery(insertingObject).css('width', options.itemWidth);
			}
			jQuery(cuttingObject).appendTo(insertingObject);
			jQuery(insertingObject).appendTo(photoGallery);
			photoWidth += parseInt(jQuery(insertingObject).outerWidth()) + parseInt(jQuery(insertingObject).css('margin-left')) + parseInt(jQuery(insertingObject).css('margin-right'));
			photoCount ++;
		});
		jQuery(photoGallery).attr('rel', photoWidth);
		jQuery(photoGallery).attr('length', photoCount);
		jQuery('<div style = "clear: both"></div>').appendTo(photoGallery);
		function CalcMinHeight()
		{
			var firstGalleryItem = jQuery(photoGallery).find('.item:first');
			return options.galleryHeight ? parseInt(options.galleryHeight) : (parseInt(jQuery(firstGalleryItem).outerHeight()) + parseInt(jQuery(firstGalleryItem).css('margin-top')) + parseInt(jQuery(firstGalleryItem).css('margin-bottom')));
		}
		
		function CalcMaxHeight()
		{
			animating = true;
			opCount++;
			jQuery('#opCount').html(opCount);
			var firstGalleryItem = jQuery(photoGallery).find('.item:first');
			if (typeof firstGalleryItem[0] != 'undefined')
			{
				var itemWidth = parseInt(firstGalleryItem.outerWidth()) + parseInt(firstGalleryItem.css('margin-left')) + parseInt(firstGalleryItem.css('margin-right'));
				var minHeight = CalcMinHeight();
				var currentHeight = jQuery(photoGallery).css('height');
				var galleryWidth = jQuery(photoGallery).innerWidth();
				var photoWidth = jQuery(photoGallery).attr('rel');
				var photoCount = parseInt(jQuery(photoGallery).attr('length'));
				if ((photoWidth <= galleryWidth) || (photoCount == 0))
				{
					return minHeight;
				}
				var gwMODiw = galleryWidth % itemWidth;
				var colCount = parseInt((galleryWidth - gwMODiw) / itemWidth);
				var pcMODcc = photoCount % colCount;
				var rowCount = parseInt((photoCount - pcMODcc) / colCount + ((pcMODcc == 0) ? 0 : 1));
				if (options.debug)
				{
					jQuery(maximizeButton).html(opCount + ', ' + photoCount + ', ' + rowCount);
				}
				return minHeight * rowCount;
			}
		}
		function CheckButtonPos()
		{
			if (jQuery.browser.msie)
			{
				var buttonTop = parseInt(jQuery(maximizeButton).css('top'));
				var buttonBottom = parseInt(jQuery(maximizeButton).css('bottom'));
				var buttonLeft = parseInt(jQuery(maximizeButton).css('left'));
				var buttonRight = parseInt(jQuery(maximizeButton).css('right'));
				var buttonWidth = parseInt(jQuery(maximizeButton).outerWidth());
				var buttonHeight = parseInt(jQuery(maximizeButton).outerHeight());
				var galleryWidth = parseInt(jQuery(photoGallery).innerWidth());
				var galleryHeight = parseInt(jQuery(photoGallery).innerHeight());
				if (buttonBottom > 0)
				{
					jQuery(maximizeButton).css('top', parseInt(galleryHeight - buttonBottom + parseInt(jQuery(maximizeButton).css('margin-top')) - parseInt(jQuery(maximizeButton).css('margin-bottom'))));
				}
				if (buttonRight > 0)
				{
					jQuery(maximizeButton).css('left', parseInt(galleryWidth - buttonRight + parseInt(jQuery(maximizeButton).css('margin-left')) - parseInt(jQuery(maximizeButton).css('margin-right'))));
				}
			}
		}
		function Minimize()
		{
			var minHeight = CalcMinHeight();
			if (minHeight > 0)
			{
				jQuery(photoGallery).animate({height: minHeight}, (jQuery.browser.msie ? 0 : options.animateDuration), function(){
					jQuery(photoGallery).attr('maximized', 'false');
					animating = false;
					CheckButtonPos();
				});
			}
		}
		function Maximize()
		{
			var maxHeight = CalcMaxHeight();
			jQuery(photoGallery).animate({height: maxHeight}, (jQuery.browser.msie ? 0 : options.animateDuration), function(){
				jQuery(photoGallery).attr('maximized', 'true');
				CheckButtonPos();
			});
		}
		function CheckWidth()
		{
			var maximizeButton = jQuery(photoGalleryContainer).find('.maximize-button');
			if (typeof maximizeButton[0] != 'undefined')
			{
				var galleryWidth = parseInt(jQuery(photoGallery).innerWidth());
				if (galleryWidth < parseInt(jQuery(photoGallery).attr('rel')))
				{
					jQuery(maximizeButton).show();
				}
				else
				{
					jQuery(maximizeButton).hide();
				}
				if (jQuery(photoGallery).attr('maximized') == 'true')
				{
					Maximize();
				}
			}
		}
		jQuery(photoGallery).css('height', CalcMinHeight());
		var maximizeButton = jQuery('<div class = "maximize-button"></div>').appendTo(contextObject);
		if ((typeof maximizeButton[0] != 'undefined') && (typeof photoGalleryContainer[0] != 'undefined'))
		{
			jQuery(maximizeButton).remove();
			jQuery(maximizeButton).appendTo(photoGalleryContainer);
			jQuery(maximizeButton).hide();
			jQuery(maximizeButton).click(function(){
				if (jQuery(this).hasClass('maximize'))
				{
					Maximize();
					jQuery(this).removeClass('maximize');
					jQuery(this).addClass('minimize');
				}
				else if (jQuery(this).hasClass('minimize'))
				{
					Minimize();
					jQuery(this).removeClass('minimize');
					jQuery(this).addClass('maximize');
				}
			});
			jQuery(maximizeButton).addClass('maximize');
		}
		else
		{
			alert('maximize button is empty!');
		}
		var galleryID = jQuery(contextObject).attr('id');
		jQuery(contextObject).remove();
		jQuery(photoGallery).attr('id', galleryID);
		jQuery(photoGallery).attr('align', 'center');
		jQuery(photoGallery).css('margin', '0 auto');
		jQuery(window).resize(function(){ 
			jQuery(photoGallery).stopTime();
			jQuery(photoGallery).oneTime('300ms', function(){CheckWidth();});
		});
		CheckWidth();
	});
};