jQuery(document).ready(function($) {
	$('#header .top-nav li:last-child, #sidebar .widget_nav_menu li:last-child').addClass('last');
	$('#footer .shell .menu li:first-child').addClass('first').parent().first().addClass('first');
	
	// Cycle through the latest posts
	(function() {
		var links = $('.breadcrumb .latest-post');
		var pause = 4000;
		var duration = 200;
		
		links.first().show();
		
		setInterval(function() {
			var previous = links.filter(':visible').fadeOut(duration, function() {
				var next = previous.next();
				if (!next.length) next = links.first();
				
				next.fadeIn(duration);
			});
		}, pause);
	})();
	
	// Prepare Gravity Forms for blink
	(function() {
		var widget = $('#sidebar .gform_widget').each(function() {
			$(this).find('label').each(function() {
				var label = $(this).text();
				$(this).hide();
				var field = $(this).siblings('.ginput_container').find('input[type="text"], textarea').val(label).attr('title', label).addClass('blink');
			});
		});
	})();
	
	$('.blink')
		.focus(function(){
			if( $(this).val() == $(this).attr('title') ) {
				$(this).val('');
			}
		}).blur(function(){
			if( $(this).val() == '' ) {
				$(this).val( $(this).attr('title') );
			}
		});
	
	$('.gform_widget form').submit(function() {
		var valid = true;
		var errors = '';
		
		$(this).find('.gfield_required').parent().siblings('.ginput_container').find('input[type="text"], textarea').each(function() {
			if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				$(this).addClass('field-error');
				errors += 'The "' + $(this).attr('title') + '" field is required\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		if (!valid) {
			alert(errors + '\n ');
		}
		
		return valid;
	});
	
	$('#navigation ul li').hover(
		function(){
			$(this).find('ul').show();
			$(this).addClass('active');
		}, 
		function(){
			$(this).find('ul').hide();
			$(this).removeClass('active');
		}
	);
	
	$('.scroll-pane').jScrollPane({
		scrollbarWidth: 14,
		showArrows: true
	});
});
