/**
 * @author Vlad Yakovlev (red.scorpix@gmail.com)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 * @link www.scorpix.ru
 * @version 0.1.3
 * @date 2010-03-05
 * @requires jQuery
 * @requires photoSizer
 */
$(function() {
	var
		border = 10,
		minWidth = 120,
		maxWidth = 240,
		scale;

	var
		rootBlocks = $('.simple_pictures'),
		pictures = rootBlocks.find('img'),
		imagesLoaded = 0;

	if (pictures.length) {
		scale = photoSizer.getScale();

		rootBlocks.filter('.simple_pictures_random').find('img').each(function() {
			$(this).css({
				left: -border + Math.round(2 * border * Math.random()),
				top: -border + Math.round(2 * border * Math.random())
			});
		});

		pictures.each(function() {
			var el = $(this);

			el.load(function() {
				el.data('width', $(this).width());
				scaleImg(el);
			});
		});

		photoSizer.onScale(onScale);
	}

	function onScale(newScale) {
		scale = newScale;

		pictures.each(function(index) {
			scaleImg($(this));
		});
	}

	function scaleImg(el) {
		if (el.data('width')) {
			el.css('width', Math.round(minWidth / maxWidth * el.data('width') + (1 - minWidth / maxWidth) * el.data('width') * scale));
		}
	}
});
