/**
 * resize any images larger than the maximum allowed width to the
 * max width
 */
document.observe("dom:loaded", function() {
	var maxwidth = 413;
	var images = $$('img');
	var captions = $$('.wp-caption');

	// resize captions and images
	for (var i=0;i<captions.length;i++) {
		width = (captions[i].width) ? captions[i].width : parseInt(captions[i].style.width,10);
		if (width > maxwidth) {
			if (captions[i].width) {
				captions[i].width = maxwidth;
			} else {
				captions[i].style.width = maxwidth+"px";
			}

			var image = $$(".wp-image-"+captions[i].id.split(/_/)[1])[0];
			image.height = (image.height / image.width) * (maxwidth - 10);
			image.width  = maxwidth - 10;
		}
	}

	// resize remaining images
	for (var i=0;i<images.length;i++) {
		if (images[i].width > maxwidth) {
			images[i].height = (images[i].height / images[i].width) * maxwidth;
			images[i].width = maxwidth;
		}
	}
});

