$(document).ready(function(){
	
	/* === Comment Form === */
	
	// Hide the comment form
	$('#block-commentblock-comment_form').hide();
	
	// Add expand class and cursor to add link and add click functionality
	$('.comment-head .comment-link').addClass('comment-link-expand').css({'cursor': 'pointer'}).click(function(){

		// Toggle the comment form
		$('#block-commentblock-comment_form').toggle();
		
		// Toggle the expand class
		$(this).toggleClass('comment-link-expand');
		
		// Refresh all edge heights (if IE6)
		refreshEdges();
	});
	
	/* === Video Thumbnails === */
	
	// Get width, height and ratio thumbnails need to be
	var thumb_width = $($('.video-thumbnail .image')[0]).width();
	var thumb_height = $($('.video-thumbnail .image')[0]).height();
	var thumb_ratio = thumb_width / thumb_height;
	
	// Loop through each thumbnail, and resize it
	$('.video-thumbnail .image img').each(function(){
		var img = $(this);
		
		// Remove all width / height styles (so that we can get the actual size of the image)
		img.attr('style', 'width: auto !important; height: auto !important; min-height: none;');
		
		// Get the width, height and ratio of the image
		var width = img.width();
		var height = img.height();
		var ratio = width / height;
		
		// If the ratio of the image is smaller than the ratio of the thumbnail size, set the image width to the thumbnail width
		if(ratio < thumb_ratio) img.attr('style', 'width: ' + thumb_width + 'px !important; height: auto !important; min-height: none;');
		
		// Else set the image hieght to the thumbnail height
		else img.attr('style', 'width: auto !important; height: ' + thumb_height + 'px !important; min-height: none;');
		
		// Center the image horizontally and vertically
		img.css({
			'position': 'relative',
			'top': (img.height() - thumb_height) * -0.5,
			'left': (img.width() - thumb_width) * -0.5
		});
	});
	
	/* === Collapsible Result Years === */
	
	// Get all result sections, and add collapsed class
	var resultSections = $('.result-index .collapsible-year').addClass('collapsible-year-collapsed');
	
	// Give all collapsed content a height of 0
	resultSections.find('.content').wrap('<div class="content-wrapper" />').parent().css({
		height: 0,
		overflow: 'hidden'
	});
	
	// Add cursor, arrows and expand functionality to h3s
	resultSections.find('h3').prepend('<span class="arrow" />').css('cursor', 'pointer').click(function(){
		var wrapper = $(this).parent().find('.content-wrapper');
		var section = $(this).parent();
		if(!wrapper.is(':animated')){
			if(!ie6){
				if(wrapper.height() == 0){
					wrapper.animate({'height': wrapper.find('.content').outerHeight(true) }, 200);
					section.removeClass('collapsible-year-collapsed');
				}else{
					wrapper.animate({'height': 0 }, 200);
					section.addClass('collapsible-year-collapsed');
	
				}
			}else{
				if(wrapper.height() == 0){
					wrapper.css({'height': wrapper.find('.content').outerHeight(true) });
					section.removeClass('collapsible-year-collapsed');
				}else{
					wrapper.css({'height': 0 });
					section.addClass('collapsible-year-collapsed');
				}
				refreshEdges();
			}
		}
	});
	
});
