$(function() {
	// menu slideouts
	$('#nav > li').hoverIntent({
		sensitivity: 7,
		interval: 30,
		timeout: 0,
		over: function() {
			$(this).find('a:first').addClass('hover').end().find('ul').slideDown(250, 'easeOutExpo');
		},
		out: function() {
			$(this).find('a:first').removeClass('hover').end().find('ul').slideUp(400, 'easeOutQuint');
		}
	});
	
	// home, we need you stuff
	if ($('.home-need').length > 0) {
		$('.home-need').css('opacity', 0);
		setTimeout(function() {
			$('.home-need').animate({left: -161, opacity: 1}, 800, 'easeOutExpo');
		}, 5000);
	}
	
	// home projects and events boxes
	$('.home_bottom .pbox:not(:first), .home_bottom .ebox:not(:first)').hide();
	var cpi = cei = 1;
	$('.p-next').click(function() {
		if (cpi != $('.pbox').length) {
			$('.pbox:visible').fadeOut(450, function() {
				$(this).next().fadeIn(450);
			});
			cpi++;
		} else {
			$('.pbox:visible').fadeOut(450, function() {
				$('.pbox:first').fadeIn(450);
			});
			cpi = 1;
		}
		return false;
	});
	$('.p-prev').click(function() {
		if (cpi != 1) {
			$('.pbox:visible').fadeOut(450, function() {
				$(this).prev().fadeIn(450);
			});
			cpi--;
		} else {
			$('.pbox:visible').fadeOut(450, function() {
				$('.pbox:last').fadeIn(450);
			});
			cpi = $('.pbox').length;
		}
		return false;
	});
	
	$('.e-next').click(function() {
		if (cei != $('.ebox').length) {
			$('.ebox:visible').fadeOut(450, function() {
				$(this).next().fadeIn(450);
			});
			cei++;
		} else {
			$('.ebox:visible').fadeOut(450, function() {
				$('.ebox:first').fadeIn(450);
			});
			cei = 1;
		}
		return false;
	});
	$('.e-prev').click(function() {
		if (cei != 1) {
			$('.ebox:visible').fadeOut(450, function() {
				$(this).prev().fadeIn(450);
			});
			cei--;
		} else {
			$('.ebox:visible').fadeOut(450, function() {
				$('.ebox:last').fadeIn(450);
			});
			cei = $('.ebox').length;
		}
		return false;
	});
	
	$('.p-next, .p-prev, .e-next, .e-prev').hover(function() {
		$(this).find('img').stop().animate({opacity: 0}, 250);
	}, function() {
		$(this).find('img').stop().animate({opacity: 1}, 250);
	});
	
	// link fades
	var $links = $('.home-welcome-text .more-link a, .home-need-link a, .project-get-involved-image a, .donateimg');
	$links.hover(function() {
		$(this).find('img').stop().animate({opacity: 0}, 250);
	}, function() {
		$(this).find('img').stop().animate({opacity: 1}, 250);
	});
	
	// project tags filter
	$('.project-landing-list-menu.plist a').click(function() {
		$(this).parents('.project-landing-list-menu').find('a').removeClass('current');
		$(this).addClass('current');
		
		var filter = $(this).text().toLowerCase();
		if (filter == 'all projects') {
			$('.project-listing').show();
		} else {
			$('.project-listing').each(function(i) {
				var show = false;
				$(this).find('.project-listing-tags a').each(function() {
					if ($(this).text().toLowerCase() == filter) show = true;
				});
				if (show) {
					$(this).show();
				} else {
					$(this).hide();
				}
			});
		}
		return false;
	});
	
	$('.project-landing-list-menu.elist a').click(function() {
		$(this).parents('.project-landing-list-menu').find('a').removeClass('current');
		$(this).addClass('current');
		
		var filter = $(this).text().toLowerCase();
		if (filter == 'all events') {
			$('.event-listing').show();
		} else {
			$('.event-listing').each(function(i) {
				var show = false;
				$(this).find('.event-listing-tags a').each(function() {
					if ($(this).text().toLowerCase() == filter) show = true;
				});
				if (show) {
					$(this).show();
				} else {
					$(this).hide();
				}
			});
		}
		return false;
	});
	
	// tabs
	$('#tabs').tabs();
	
	// forms
	$('input[type="text"]').focus(function() {
		if (this.defaultValue == $(this).val()) $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val(this.defaultValue);
	});
	
	$('.sign_maillistcheckbox :checkbox').change(function() {
		if ($(this).is(':checked')) {
			$('.sign_maillistemail input').show();
		} else {
			$('.sign_maillistemail input').hide();
		}
	}).change();
	
	$('#freeform').submit(function() {
		if ($('.sign_firstname').val() == $('.sign_firstname')[0].defaultValue) {
			$('.sign_firstname').focus();
			return false;
		}
		if ($('.sign_lastname').val() == $('.sign_lastname')[0].defaultValue) {
			$('.sign_lastname').focus();
			return false;
		}
		if ($('.sign_maillistemail input').val() == 'Email') {
			$('.sign_maillistemail input').val('');
		}
	});
});