$(document).ready(function(){

	// Resizes any iframe (video) proportionately in the recent news.
	$('section#kickingAndScreaming article').has('iframe').each(function() {
		$(this).children('iframe').attr('width', '289').attr('height', getApectRatio($(this).children('iframe').attr('width'), $(this).children('iframe').attr('height')));
	});

	// removes the margin off the last p tag (even if there are divs after it)
	$('section#kickingAndScreaming article .content').each(function() {
		$(this).children('p').each(function() {
			if($(this).text() == '') {
				$(this).remove();
			}
		});
		$(this).children('div.full, div.left, div.right').remove();
		$(this).children('p:last-child').css('margin-bottom', '0');
	});

	// Link all rel=external links to external pages and all pages starting with http://
	$("a[href^='http://']").attr("target","_blank");
	$("a[href^='https://']").attr("target","_blank");
	$("a[rel='external']").attr("target","_blank");

	// Serve higher quality template assets to higher density displays
	if (window.devicePixelRatio >= 1.5) {
		// add any other images that should be scaled, here.
		$('body').addClass('hq');
		$('#logo img, img.hqAvailable').each(function(){
			var t=$(this);
			src1 = t.attr('src');
			var src1Name = src1.substring(0, src1.lastIndexOf('.'));
			var hqImg = src1Name +  '@2x.' + /[^.]+$/.exec(src1);
			t.attr('src', hqImg).addClass('hq');
		});
	}

});

function getApectRatio(width, height) {
	return newHeight = Math.ceil(289 * (width/height)) + 5;
}
