function getImagePositionOffset ( arg0, arg1 ) {
	var overflow = arg0 - arg1;
	var offset = overflow / 2;
	return (-Math.floor(offset));
}

function centerImages ( images, maxWidth, maxHeight ) {
	images.each(function() {
		var image = $(this);
		var width = image.width();
		var height = image.height();
		image.css('position', 'relative');
		if ( width > height ) {
			image.addClass('landscape');
			var offset = getImagePositionOffset(image.width(), maxWidth);
			image.css('left', offset);
		} else {
			image.addClass('portrait');
			var offset = getImagePositionOffset(image.height(), maxHeight);
			image.css('top', offset);
		}
	});
}

function showMore ( collections, showMore ) {
    var c = $(collections);
    var collection = $(c.get(0));
    collection.removeClass('hidden');
    prepareImages(collection, 228);
    if ( c.size() < 2 ) {
    	$(showMore).addClass('hidden');
    }
}

function prepareImages ( collection, maxHeight ) {
	var images = collection.find('img.image');
	var topHeight = 0;
	images.each(function(){
		var image = $(this);
		var width = image.width();
		var height = image.height();
		if ( height > topHeight ) {
			topHeight = height;
		}
		if ( width > height ) {
			$(this).addClass('landscape');
		} else {
			$(this).addClass('portrait');
		}
	});
	if ( topHeight > 0 && topHeight < maxHeight ) {
		maxHeight = topHeight;
	}
	var imageWraps = collection.find('.image-wrap');
	imageWraps.each(function(){
		var imageWrap = $(this);
		imageWrap.css('height', maxHeight);
		var image = imageWrap.find('img.image');
		image.css('position', 'absolute');
		if ( image.height() < maxHeight ) {
			image.css('bottom', '0px');
		} else {
			image.css('top', '0px');
		}
	});
}

$(window).load(function() {
	//addProportionClassToImages($('img[class=image]'));
	centerImages($('.lesernes-avis img[class=image]'), 198, 198);
	$('.four-horizontal').each(function(){
		prepareImages($(this), 228);
	});
});

