$(document).ready(function(){	
	// Contact form //
	$('#contact-form').submit(function(event) {
		event.preventDefault();
		jQuery.post($(this).attr('action'), $(this).serialize(), function(response) {
			if (response.success) {
				$('#contact-form').append('<div class="ok"><p>Palaute lähetetty. Kiitos!</p></div>');
			} else {
				$('#contact-form ul.errors').remove();
				var errors = '<ul class="errors">';
				for (var i=0; i<response.errors.length; i++) {
					errors += '<li>'+response.errors[i]+'</li>';
				}
				errors += '</ul></p>';
				$('#contact-form').prepend(errors);
			}
		});
		
		return false;
	});
	
	var authorImg = $('#ajatuspanimo .author-avatar img');

	function positionAuthorAvatar() {
		var width = authorImg.width();
		var height = authorImg.height();
		
		if (width != 0 && height != 0) {
			var cropWidth = authorImg.parent().width();
			var cropHeight = authorImg.parent().height();
		
			var scale = Math.max(cropWidth/width, cropHeight/height);
			
			var newWidth = Math.floor(width*scale);
			var newHeight = Math.floor(height*scale);
		
			authorImg.css({
				width: newWidth +'px',
				height: newHeight +'px',
				left: Math.floor((cropWidth-newWidth)/2)+'px',
				top: Math.floor((cropHeight-newHeight)/2)+'px'
			});
		}
	}
	
	authorImg.load(positionAuthorAvatar);
	positionAuthorAvatar();
});
