$(document).ready(function() {

	var root = $('#masthead > h1 a').attr('href');

	$('.inline-comment-form').hide().filter('.failed').show().each(function(){
		$('html, body').attr('scrollTop', $(this).offset().top-100);
	});

	$('a.inline-comment').live('click', function(e) {
		e.preventDefault();
		$(this).closest('section').find('.inline-comment-form').slideToggle("fast");
	});

	/* Contract long comment lists */
	jQuery.fn.commentToggler = function(cfg){
		this.each(function(){
			var $this = jQuery(this),
				$posts = $this.find(cfg.selector);

			if($posts.length <= cfg.max){
				return;
			}

			var $overflow = $posts.slice(cfg.max, $posts.length),
				$button = jQuery('<a class="more">View all '+$posts.length+' comments</a>').insertBefore($posts.eq(0));

			$overflow.wrapAll('<div />');
			var $wrapper = $overflow.parent().hide();

			$button.toggle(function(){
				$wrapper.slideDown();
			}, function(){
				$wrapper.slideUp();
			});
		});
	};
	$('section.comments').commentToggler({selector: 'article', max: 3});

	/* DIALOGS */
	var $dialog = $('<div />').delegate('form', 'submit', function(evt){
		// Intercept the submission of forms within the dialog
		evt.preventDefault();
		var $form = $(this);

		$.post($form.attr('action') + $form.find('input[name="id"]').val() + '/', $form.serialize(), function(data){
			$data = $(data).find('report-abuse');

			if($data.attr('status') == 'success') {
				$dialog.find('fieldset > *').remove();
				$dialog.find('fieldset').append("<p>Thanks for your report.</p><p>A moderator will follow up your report as soon as possible</p>");

				window.setTimeout(closeDialog, 2500);
			}
			else {
				var errors = $data.find('*[type=missing]'),
					ul = $('<ul />').addClass('errors');

				$dialog.find('ul.errors').remove();

				errors.each(function(i, e) {
					ul.append($('<li />').text($(e).attr('message')));
				});

				$dialog.find('fieldset').prepend(ul);
			}
		}, "xml");

		return false;
	});

	/* Intercept dialog links */
	$('#primary-content').delegate('a.dialog', 'click', function(evt){
		evt.preventDefault();

		// Nuke any open dialogs
		createDialog();

		$('#dialog-content input[name="id"]').val($(this).attr('href').split('report-abuse-')[1]);

		$dialog.hide().fadeIn().html($('#dialog-content').show(), function(){
			Cufon.refresh();
			// Give the first form element focus
			$dialog.find('input, textarea, select').eq(0).focus();
		}).dialog({width:400, resizable:false, position: [evt.clientX+50, evt.clientY-30]});

		return false;
	});

	// add Report form to HTML
	function createDialog() {
		closeDialog();
		$('body').append(
			'<form method="post" id="dialog-content" action="' + root + '/ajax/report-abuse/' + '">' +
				'<h1 class="replace">Report abuse</h1>' +
				'<fieldset>' +
					'<label>' +
						'<span>Name</span>' +
						'<input type="text" name="name" />' +
					'</label>' +
					'<label>' +
						'<span>Email</span>' +
						'<input type="text" name="email" />' +
					'</label>' +
					'<label>' +
						'<span>Reason</span>' +
						'<textarea name="reason"></textarea>' +
					'</label>' +
					'<input type="hidden" name="id" />' +
					'<button type="submit">Submit</button>' +
				'</fieldset>' +
			'</form>'
		);
	}

	function closeDialog() {
		$("#dialog-content").remove();
		$dialog.dialog('destroy');
	}
});