/*	Desc: Index JS goodies
	Author: Ben Babcock
	Date: May 5, 2008
=== === === === === === === === === === */

$(function(){
	$('ul.date-list').accordion({
		active: '.current-year',
		autoheight: false,
		header: 'span'
	}).addClass('accordion-list').find('span').wrapInner('<a></a>').css('cursor', 'pointer');
	
	// Emotes onto commentform
	loadEmotes('#vsns-content');
	loadToolBar('#vsns-content');
	
	// Hide the formatting explanation until it is needed
	$('#format-explain').hide();
	$('ul.toolbar').append($(document.createElement('li')).append(
		$(document.createElement('a')).click(function() { $('#format-explain').slideToggle();}).text('Formatting Help')
		));
	
	/*
	 * FORM VALIDATION
	 */
	$('#commentform').before($(document.createElement('div')).addClass('errorlist').css('display', 'none').append(
			$(document.createElement('p')).text('The following errors were found:')
		).append(
			$(document.createElement('ul')
		)
		)).before(
			$(document.createElement('div')).attr('id', 'preview-container')
		).validate({
		errorClass: 'error',
		errorContainer: 'div.errorlist',
		errorLabelContainer: 'div.errorlist ul',
		onkeyup: false,
		onfocusout: false,
		wrapper: 'li',
		rules: {
			'name': 'required',
			'commentemail': {
				'required': true,
				'email': true
			},
			'website': 'url',
			'comment': {
				'required': true,
				'minLength': 5
			}
		},
		messages: {
			'name': 'You must enter your name.',
			'commentemail': {
				'required': 'You must enter an email address.',
				'email': 'Your email address is not valid.  Please ensure you have entered a valid email address.'
			},
			'website': 'You must enter a valid URL.',
			'comment': {
				'required': 'You must enter a comment.',
				'minLength': 'Your comment must be 5 characters or more in length.'
			}
		}
	});
	
	
	// Preview comment
	$('#preview-comment').click( function () {
		if($('#commentform').valid()) {
			$.post(path + 'ajax.php', { go: 'comment-preview', name: $('#name').val(), commentemail: $('#email').val(), website: $('#website').val(), comment: $('#vsns-content').val() }, function (data) {
				if ($('#comment-preview').length > 0)
				{
					$('#comment-preview').fadeOut('fast');
				}
				$('#preview-container').empty().html(data);
				$('#comment-preview').hide().prepend(
				$(document.createElement('p')).addClass('response').text('This is a preview of your comment.  It has not been saved!')
			).fadeIn();
	 		});
		}
		return false;
	});
});
