// libguides custom javascript
// @yaconiellofj @erwhite 2010-09

// resize iframes from youtube to fit the width of their containers via width:height ratio

$(document).ready(function () {
	setTimeout('resize_youtube_divs()', 1000);
});

function resize_youtube_divs()
{
	// handle iframes
	$("div.innerbox:has(iframe[src*=yout])").each(function() {
		
		// find the width:height ratio
		var new_width 	= parseInt($(this).parent().parent().width() - 14);
		
		// don't let the video stretch beyond 720px wide
		if(new_width > 720)
		{
			new_width 	= 720;
		}

		var new_height 	= parseInt($('iframe[src*=yout]', this).height() * (new_width / $('iframe[src*=yout]', this).width()));
		
		// adjust the width
		$('iframe[src*=yout]', this).width(new_width);
		
		// adjust the height
		$('iframe[src*=yout]', this).height(new_height);
	});
	
	// handle objects
	$("div.innerbox:has(object)").each(function() {
		
		// find the width:height ratio
		var new_width 	= parseInt($(this).parent().parent().width() - 24);

		// don't let the video stretch beyond 720px wide
		if(new_width > 720)
		{
			new_width 	= 720;
		}
		
		new_height 	= parseInt($('object', this).parent().height() * (new_width / $('object', this).width()));
		
		// adjust the width
		$('object', this).attr('width', new_width);
		$('embed', this).attr('width', new_width);
		
		// adjust the height
		$('object', this).attr('height', new_height);
		$('embed', this).attr('height', new_height);
	});
}
