function loadImage(div, url, width) {
	var img = new Image();
	$(img).load(function () {
		$(this).hide();
		div.removeClass('loading').append(this);
		img_width = $(this).width();
		img_height = $(this).height();
		var height = 198;
		var new_width;
		var new_height;
		if (img_width >= img_height) {
			height_diff = width / img_height;
			new_width = img_width * height_diff;
			new_height = width;
			margin = (new_width - width) / 2;
			$(this).css({width: new_width+'px', height: new_height+'px', marginLeft: '-' + margin + 'px'});
		} else {
		    new_height = (img_height * width) / img_width;
		    new_width = width;
			$(this).css({width: new_width+'px', height: new_height+'px'});
		}
		/*margin_top = (new_height - height) / 2;
	    $(this).css("margin-top", '-'+margin_top+"px");*/
		$(this).fadeIn();
	}).error(function () {
		div.removeClass('loading').css("margin-top", (width/2.5)+"px").html("yikes!<br />this image has<br />gone missing.");
	}).attr('src', url);
}

$(function(){
	$('a.show_categories').click(function () {
	  $(this).hide();
      $(this).next().show();
      return false;
    });
    
    $('a.show_more').click(function () {
	  $(this).parent().hide();
      $(this).parent().next().slideDown();
      return false;
    });
    
    $('a#show_comment_form').click(function () {
	  $(this).parent().hide();
      $(this).parent().next().show();
      return false;
    });
    
    $('a.media_audio').nyroModal({minWidth: '350', minHeight: '52', closeButton: ''});
    $('a.media_video').nyroModal({minWidth: '350', minHeight: '313', closeButton: ''});
    $('.audio_thumb').nyroModal({minWidth: '350', minHeight: '52', closeButton: ''});
    $('.video_thumb').nyroModal({minWidth: '350', minHeight: '313', closeButton: ''});
    $('.video_icon').nyroModal({minWidth: '350', minHeight: '313', closeButton: ''});
    $('.reel_thumb_audio').nyroModal({minWidth: '350', minHeight: '52', closeButton: ''});
    $('.reel_thumb_video').nyroModal({minWidth: '350', minHeight: '313', closeButton: ''});
	
	$('.load_inset').each(function(index){
		var div = $(this);
		var width = $(this).width();
		var url = $(this).attr('alt');
		loadImage(div, url, width);
	});
	
	$('.block_thumb').each(function(index){
		var div = $(this);
		var width = $(this).width();
		var url = $(this).attr('alt');
		loadImage(div, url, width);
	});
	
	$('a.mailing_list_signup').nyroModal({minWidth: '600'});
});
