$(function(){

	$("a[rel='fullsize']").colorbox();

	$('div#slideshow pre code').each(function() {
		eval($(this).text());
	});

	$("div#slideshow div").hide();
	$("div#slideshow div:first").show();

	$('a.click:first').css({color: '#951414', borderRight: '5px solid #f00'});

	$('ul#click li a').click(function(){

		var id = $(this).attr('href');

		$("div#slideshow div").hide();
		$("div#slideshow div#fade_" + id).show();

		$('a.click').css({color: '#0064A0', borderRight: 'none'});
		$('a#click_' + id).css({color: '#951414', borderRight: '5px solid #f00'});

		return false;
	});

	(function(maxht, maxwt, minht, minwt){
		var imgs = $("div.blockquote img.bbc_img");
		// Image resizing function
		var resize_image = function(img, newht, newwt){
			img.height = newht;
			img.width  = newwt;
			//$(img).wrap('<table><tr><td class="plainbox"><div class="resized_image"><a href="' + img.src + '" target="_blank"></a></td></tr></table>');
			//$(img).wrap('<div class="resized_image"><a href="' + img.src + '"></a></div>');
			$(img).wrap('<div class="resized_image"><a></a></div>');
			$(img).parent().before('<p class="note">NOTE: This image was resized. To view it full-size, click on the image.</p>');
			//$(img).parent().after('<p style="text-align:right;background:none;margin:0;padding-right:3px"><a href="http://vikhyat.net/code/resize_large_images_js/">Image Resizing Script</a>.</p>');
		};
		
		for (var i = 0; i < imgs.length; i++){
			// Set a variable for the current image to make the code make more sense.
			var img = imgs[i];
			if (img.height > maxht || img.width > maxwt){
				// Use Ratios to constraint proportions.
				var old_ratio = img.height / img.width;
				var min_ratio = minht / minwt;
				// If it can scale perfectly.
				if (old_ratio === min_ratio){
					resize_image(img, minht, minwt);
				} 
				// We need to do some magic now.
				else {
					var newdim = [img.height, img.width];
					// Sort out the height first.
					newdim[0] = minht;
					// The logic behind this is that if ratio = ht / wt, then wt = ht / ratio.
					newdim[1] = newdim[0] / old_ratio;
					// Do we still have to sort out the width?
					if (newdim[1] > maxwt){
						// Just do what we did with the height
						newdim[1] = minwt;
						newdim[0] = newdim[1] * old_ratio;
					}
					// So yeah, resize the image
					resize_image(img, newdim[0], newdim[1]);
				}
			}
		}
	})
	(650, 650, 650, 650);

});
