var width_small = 80;
var width_big = 230;

$(document).ready(function(){
	$('div.sorting-month span').click(function() {
		deselectAllIn();
		togglePhotoSize($(this).parent().parent());
		
		//hack for IE6 - resizing same element for rebilding all code model
		if($.browser.msie){
			$('#footer').css('position','relative');
			$('#footer').css('position','absolute');
		}
		
// NEW
		if ($("font", this).text() == "Увеличить фотки") {
			$("font", this).text("Уменьшить фотки");
		} else {
			$("font", this).text("Увеличить фотки");
		}
	});

	resizeAllPhotos();
	resizePhotoBlocks();

	if($.browser.msie){
		$('#footer').css('position','relative');
		$('#footer').css('position','absolute');
	}
});

function deselectAllIn()
{
	$('ul#sorting-collection li').find('modify').each(function(index) {
		$(this).removeClass('modify');
	});
}

function resizeAllPhotos()
{
	$('ul#sorting-collection li:not(.big)').each(function(index) {
		$(this).find('img').each(function(index) {
			makeImageSmall($(this));
		});
	});
}

function resizePhotoBlocks()
{
	$('div.uniting').each(function(index) {
		resizePhotoBlock($(this));
	});
}

function resizePhotoBlock( block )
{
	var maxHeight = 0;
	
	$(block).find('div.photo-item').each(function(index) {
		$(this).height('auto');
	});
	$(block).find('div.photo-item').each(function(index) {
		if($(this).height() > maxHeight)
			maxHeight = $(this).height();
	});

	$(block).find('div.photo-item').each(function(index) {
		$(this).height(maxHeight);
	});
}

function togglePhotoSize( li )
{
	var oldState = 'small';
	if($(li).hasClass('big')){
		oldState = 'big';
	}


	$(li).toggleClass('big');

	if(oldState == 'small'){
		//	Увеличиваем фотки
		$(li).find('img').each(function(index) {
			$(this).width('auto');
			$(this).height('auto');
		});
	}else{
		//	Уменьшаем фотки
		$(li).find('img').each(function(index) {
			makeImageSmall($(this));
		});
	}

	$(li).find('div.uniting').each(function(index) {
		resizePhotoBlock($(this));
	});
}

function makeImageSmall(image)
{
	var width = $(image).width();
	var height = $(image).height();
	
	// $(this).attr('def_width', width);
	// $(this).attr('def_height', height);
	
	var coef = width / width_small;
	$(image).width(width_small);
	$(image).height(height / coef);
}
