function is(bodyClass)
{
	return $('body').hasClass(bodyClass);
}

function navEffects()
{
	if (is('cssnakedday'))
	{
		return;
	}
	
	// 'Hide' (make invisible) the static <span> tags meant for graceful degradation
	$('#nav ul.nav li a span').css({ background: 'none', borderColor: 'transparent' });
	
	// Apply the dynamic hover element
	$('#nav ul.nav li').ahover({
		className: 'nav-ahover', 
		moveSpeed: 150, 
		toggleEffect: { opacity: 0.0 }, 
		toggleSpeed: 100
	});
}

// Inspiration: http://jonraasch.com/blog/a-simple-jquery-slideshow
function featuredWork(direction)
{
	if (is('cssnakedday'))
	{
		return;
	}
	
	var speed = direction ? 500 : 2000;
	var $active = $('#featured .featured-item.active');
	
	if (direction == 'prev')
	{
		if ($active.length == 0)
		{
			$active = $('#featured .featured-item:first');
		}
		
		var $prev = $active.prev().length > 0 ? $active.prev() : $('#featured .featured-item:last');
		
		$active.addClass('last-active');
		
		$prev.css('opacity', 0.0).addClass('active').animate({ opacity: 1.0 }, speed, function()
		{
			$active.removeClass('active last-active').css('opacity', 0.0);
			
			if ($.browser.msie)
			{
				this.style.removeAttribute('filter');
			}
		});
	}
	else
	{
		if ($active.length == 0)
		{
			$active = $('#featured .featured-item:last');
		}
		
		var $next = $active.next().length > 0 ? $active.next() : $('#featured .featured-item:first');
		
		$active.addClass('last-active');
		
		$next.css('opacity', 0.0).addClass('active').animate({ opacity: 1.0 }, speed, function()
		{
			$active.removeClass('active last-active').css('opacity', 0.0);
			
			if ($.browser.msie)
			{
				this.style.removeAttribute('filter');
			}
		});
	}
}

$(function()
{
	navEffects();
	
	var $rf = $('.required');
	$rf.find('.field-name span').hide();
	$rf.siblings('input, textarea').defaultText('required');
	
	$('.search-form input').defaultText('search...');
	
	if (is('home'))
	{
		$.preloadImages('/images/sarathos/novalistOrange/ajax-spinner.gif');
		
		if (!is('cssnakedday'))
		{
			if ($('#featured .featured-item').length > 1)
			{
				$('#featured').prepend('<p class="featured-nav"><a href="#" class="prev" title="Previous featured item">&lt;</a><a href="#" class="next" title="Next featured item">&gt;</a></p>');
			}
			
			$('#featured ul').css('position', 'relative').find('li').css('position', 'absolute');
			
			var hoverSpeed = 100;
			$('#featured .featured-nav').hoverFade({ opcOut: 0.0, speed: hoverSpeed }).find('a').hoverFade({ speed: hoverSpeed });
			$('#featured .featured-item a .more').css('display', 'block').hoverFade({ opcOut: 0.0, speed: hoverSpeed });
			
			// Start the ball rolling... or the featured items fading
			$('#featured .featured-item:first').addClass('active');
			setInterval('featuredWork(null)', 10000);
			
			// Clicky click!
			$('#featured .featured-nav a').click(function()
			{
				featuredWork($(this).attr('class'));
				return false;
			});
		}
		
		// Download tweets on the fly
		$(window).load(function()
		{
			$('#list-tweets noscript').after('<div id="tweets"><img class="ajax-spinner" src="/images/sarathos/novalistOrange/ajax-spinner.gif" alt="" /></div>');
			$('#list-tweets #tweets').load('/ajax/tweets', function()
			{
				$(this)
					.find('.ajax-spinner')
					.fadeOut(500)
					.next()
					.idle(500)
					.animate({ height: 'show', opacity: 'show' }, 500);
			});
		});
	}
	
	if (is('contact'))
	{
		$.preloadImages('/images/sarathos/novalistOrange/ajax-spinner-contact.gif');
		
		$('#url').defaultText('http://', false);
		
		$('#contact-form').submit(function()
		{
			var postData = $(this).serialize() + '&submit=Send!';
			
			$('#form').animate({ height: 'hide', opacity: 'hide' }, 500, function()
			{
				$('label').removeClass('error');
				
				if ($(this).prev().attr('class') != 'ajax-spinner')
				{
					$(this).before('<img class="ajax-spinner" src="/images/sarathos/novalistOrange/ajax-spinner-contact.gif" alt="" />');
				}
				
				$('.ajax-spinner').hide().fadeIn(500);
				$('.form-success').remove();
				$('.form-errors').remove();
				
				$.post('/about/contact', postData, function(data)
				{
					$('.ajax-spinner').fadeOut(500, function()
					{
						var result;
						
						if (data.success)
						{
							result = '<div class="form-success"><p>Your message has been sent! I\'ll get back to you with a reply soon, usually within 2 days or less. Thank you!</p></div>';
							$('#contact-form').remove();
						}
						else if (data.errors)
						{
							result = '<div class="form-errors"><p>There were some errors; please correct them and send your message again:</p><ul>';
							
							for (i in data.errors)
							{
								result += '<li>' + data.errors[i] + '</li>';
								$('label[for="' + i + '"]').addClass('error');
							}
							
							result += '</ul></div>';
						}
						else
						{
							result = '<div class="form-errors"><p>There was an unknown error sending your message; please try again. If you still get this error after many tries, please send me a direct email instead, at <strong>boltclock&nbsp;[at]&nbsp;NOVALISTIC&nbsp;[dot]&nbsp;com</strong>. Sorry for any inconvenience caused.</p><div>';
						}
						
						$rf.siblings('input, textarea').defaultText('required');
						$('#url').defaultText('http://', false);
						$('#form').prepend(result).animate({ height: 'show', opacity: 'show' }, 500, function()
						{
							$('.error:first').siblings('input, textarea').focus();
						});
					});
				}, 'json');
			});
			
			return false;
		});
	}
	
	if (is('single'))
	{
		$('#comment-url').defaultText('http://', false);
	}
});