$(function() {
	
	// Place focus on each form's first input when a page containing a form is loaded...
	$('.forms input:first').focus();
	
	// Remove sign-up form conditional fields from view if not necessary
	// remove conditionally irrelevant form fields on registration form
	$('#ages').blur(function(){
		if ($(this).val() == '18+')
		{
			$('#parent-name')
			.find('option[value="18+"]').attr('selected', 'selected')
			.end()
			.find('input').attr('value', '')
			.end().slideUp('slow');
		}
		else
		{
			$('#parent-name')
			.find('option[value="PSO"]').attr('selected', 'selected')
			.end()
			.find('input').attr('value', '')
			.end().slideDown('slow');
		}
	});
	$('#paypal').blur(function(){
		if ($(this).is(':checked'))
		{
			$('#pp-email-wrap')
			.find('input').attr('value', '')
			.end().slideUp('slow');
		}
		else
		{
			$('#pp-email-wrap')
			.find('input').attr('value', '')
			.end().slideDown('slow');
		}
	}); 
 
	$('#pay_pref').blur(function(){
		if ($(this).val() == 'check')
		{
			$('#pp_info')
			.find('input[type=text]').attr('value', '')
			.end()
			.find('input[type=checkbox]').attr('checked', '')
			.end()
			.slideUp('slow');
		}
		else if ($(this).val() == "paypal")
		{
			if ($('#pp_info').is(':hidden'))
			{
			    	$('#pp_info').slideDown('slow'); 
			}
			else
			{
				return false;
			}
			
		}
	}); 
	
	
	$('#rule-container > div').removeClass('bbot');
	$(".top-anchor, a[name='top']").remove();
	
	$('#rule-container, #steps-content').children(':gt(0)').hide();
	
	// swap rules and guidelines page content
	$('#rules-nav li a')
		.filter(':first').addClass('selected')
		.end()
		.click(function(){
			$(this).addClass('selected').parent().siblings().children('a').removeClass('selected');
			$('#rule-container')
				.children()
				.css('margin-bottom', '15px')
				.hide(1)
				.filter(this.hash)
				.fadeIn(400);
		return false;
	});
	
	// swap home page overview content
	$('#steps li a')
		.filter(':first').css({'position' : 'relative', 'margin-left' : '15px', 'z-index' : '5', 'background' : 'url(./images/site/selectors_bg_sprite.png) top left no-repeat', 'color' : '#333'})
		.end()
		.hover(function(){
			$(this)
				.stop()
				.css({'position' : 'relative', 'z-index' : '5', 'background' : 'url(./images/site/selectors_bg_sprite.png) left top no-repeat'})
				.animate({marginLeft : '15px', color : '#333'}, 400)
				.parent()
				.siblings()
				.children('a')
				.css({'position' : 'static', 'background' : 'url(./images/site/selectors_bg_sprite.png) left -42px no-repeat'})
				.animate({marginLeft : '0px', color : '#fff'}, 400);
			$('#steps-content')
				.filter(':is(:hidden)')
				.children()
				.css('margin-bottom', '15px')
				.hide(0)
				.filter(this.hash)
				.fadeIn(400);
		return false;
	}, function(){
		$(this)
			.stop()
			.css({'background' : 'url(./images/site/selectors_bg_sprite.png) left top no-repeat'})
			.animate({marginLeft : '15px', color : '#333'}, 50);
	}).click(function(){
		return false;
	});
	
	// Colorbox image overlay...
	
	$('a.gallery, a.gallery-single').colorbox({
		'maxHeight' : 700
	});
	
	// Show/hide Repository Filter Results Form...
	$('#filter-form')
		.css('margin-top', '20px')
	    	.hide()
		.parent()
		.prev()
		.css('margin-bottom', '0');
	$('<p id="filter-tab">Open Filter Options</p>')
		.appendTo('#repository-search')
		.css({
			float : 'right',
			padding : '5px',
			width : '150px',
			textAlign : 'center',
			background : '#990000',
			color : '#fff',
			cursor : 'pointer',
			'-moz-border-radius' : '0 0 6px 6px',
			'-webkit-border-bottom-right-radius' : '6px',
			'-webkit-border-bottom-left-radius' : '6px'
		})
		.wrap('<div/>')
		.parent()
		.css({'border-top' : '1px solid #9b142e', 'marginTop' : '-1px'})
		.click(function(){
		    $(this).prev().slideToggle(400, function(){
				var $tab = $(this).parent().find('#filter-tab');
				if ($tab.text() == 'Open Filter Options')
				{
					$tab.text('Close Filter Options');
				}
				else
				{
					$tab.text('Open Filter Options');
				}
			});
		});
		
	// Force filtering form reset as values are populated by session variables for pagination (normal form reset doesn't work)
	$('#reset').click(function(){
		$('#filter-form').find('#term, select').val('');
		return false;
	});
		
});