// intended for use on posts with images that are set to the old 640px width

// this width looks awkward with the new layout when it's almost the full
// column width, but there's still enough room for a narrow column of text
// to its side - this little script detects images that are greate than 60%
// of the width of the content column, and makes them the full width

// also unwraps an enlarged image, since clicking the link to the full sized
// version would just return a smaller image

function fix_image_widths() {

	var content_width = $('#content').width();
	
	if ($('#content').height() < window.innerHeight - 200) {
		// this text (shorter than 500, narrower than 1000), will be in 2 columns
		content_width = content_width / 2;
		// if the width is greater than 1000, it will be formatted in 3 columns
		/*
		if (content_width > 1000) {
			content_width = content_width / 3;
		} 
		*/
	}
	
	$('#content img').each(function() {
		var image_width = $(this).width();
		var image_width_percentage = image_width / content_width * 100;
		
		if (image_width_percentage > 60) {
			$(this).width(content_width);
			// $(this).unwrap(); columns sometimes means that images get smaller - this is currently removing links on those too
		}
	});
	
	// this part resizes the featured images on the front page
	
	$('#featured_content .attachment-featured-thumb').width('700px');
}

// on page load
$(function() {

	fix_image_widths();
	
	// re-check image sizes on page resize
    $(window).resize(function() {
  		// only evaluate everything every 5 pixels
    	if (!(eval(parseInt(window.innerWidth) % 5))) {

			fix_image_widths();
		}
	
	});
	
	// this part does the same thing with youtube videos - making them fullwidth
	// no matter what their original width
	
	/* this is messing up youtube players and making them unusable or just weird looking
	$('#content object').each(function() {
	   $(this).children('embed').attr('width', content_width);
	});
	*/
	
	// this part does the same thing with old grooveshark players
	
	$('#old_player object').attr({'width': '500'});
	$('#old_player object embed').attr({'width': '500'});
	
	// this part does it with issuu embeds
	
	if ($('object').width() == 620) {
		$('object').width('1300px');
		$('object embed').width('1300px');
	}
});
