$(document).ready(function() {
// Start jQuery goodness

	// Tabs

	$('#tabs_content div.on').siblings().hide();
	$('#tabs_container ul').addClass('news_on');
	$('#tabs_container ul li a').click(function(){
		$(this).parent().parent('ul').removeClass();
		var newTab = $(this).attr('href');
		if(newTab=='#news'){
			$(this).parent().parent('ul').addClass('news_on');
			$('#news').show().siblings().hide();
		}
		if(newTab=='#events'){
			$(this).parent().parent('ul').addClass('events_on');
			$('#events').show().siblings().hide();
		}
		if(newTab=='#downloads'){
			$(this).parent().parent('ul').addClass('downloads_on');
			$('#downloads').show().siblings().hide();
		}
		return false;
	});

	// QUICK LINKS

	$('#quick_links ul li:has(ul)').addClass('has_sub');

	$('#quick_links ul li:has(ul) a:first-child').click(function(){
		$(this).next('ul').slideToggle('fast');
		$(this).parent('li').toggleClass('open');
		$(this).parent('li').siblings('li').removeClass('open').children('ul').slideUp();
		return false;
	});
	
	// QUICK REFERENCE
	
	$('a.collapse').click(function(){
		$('.reference_content').slideToggle();
		$(this).parent('h2').toggleClass('open');
		return false;
	});
	
	
	
	// SLIDY FORM STUFF
	
	$('a.reset_form').click(function(){
		$('.reset_message').slideDown('fast');
		$(this).addClass('reset_form_on');
		return false;
	});
	$('a.reset_yes').click(function(){
		$('.reset_message').slideUp('fast');
		$('a.reset_form').removeClass('reset_form_on');
		$('form#diagnostic')[ 0 ].reset();
		$('label.checked').removeClass('checked');
		$('dl.responses').children('dd').hide();
		$('dd.default_response').show();
		$('div.form_container').trigger('goto', [0])
		$('#form_progress span').width(progressSegment + '%');
		currentQuestion = 1;
		return false;
	});
	$('a.reset_no').click(function(){
		$('.reset_message').slideUp('fast');
		$('a.reset_form').removeClass('reset_form_on');
		return false;
	});
	
	$('div.radio ul li:nth-child(1) label').click(function(){
		$(this).parent('li').parent('ul').siblings('dl.responses').children('dd').hide().siblings('dd:eq(0)').show();
	});
	$('div.radio ul li:nth-child(2) label').click(function(){
		$(this).parent('li').parent('ul').siblings('dl.responses').children('dd').hide().siblings('dd:eq(1)').show();
	});
	$('div.radio ul li:nth-child(3) label').click(function(){
		$(this).parent('li').parent('ul').siblings('dl.responses').children('dd').hide().siblings('dd:eq(2)').show();
	});
	$('div.radio ul li:nth-child(4) label').click(function(){
		$(this).parent('li').parent('ul').siblings('dl.responses').children('dd').hide().siblings('dd:eq(3)').show();
	});

// SLIDING PANEL AND PROGRESS BAR

	var questionsTotal = $('#form_slide').children('#form_slide div').size();
	var progressSegment = (100 / questionsTotal);

	$('#form_progress span').width(progressSegment + '%');
	
	var currentQuestion = 1;
	
	$('a.next_form').click(function(){
		var currentWidth = $('#form_progress span').width();
		if (currentQuestion < questionsTotal){
			currentQuestion++;
			var totalWidth = (progressSegment * currentQuestion) + '%';
			$('#form_progress span').width(totalWidth);
		}
	});
	
	$('a.prev_form').click(function(){
		var currentWidth = $('#form_progress span').width();
		if (currentQuestion > 1){
			currentQuestion--;
			var totalWidth = (progressSegment * currentQuestion) + '%';
			$('#form_progress span').width(totalWidth);
		}
	});
	
	$('#sign_up input.text').click(function(){
		if($(this).attr('name') == 'cm-name'){
			if($(this).attr('value') == 'Your name'){
				$(this).attr('value', '');
			}
		}
		if($(this).attr('name') == 'cm-wedk-wedk'){
			if($(this).attr('value') == 'Email address'){
				$(this).attr('value', '');
			}
		}
	});
	$('#sign_up input.text').blur(function(){
		if($(this).attr('name') == 'cm-name'){
			if($(this).attr('value') == ''){
				$(this).attr('value', 'Your name');
			}
		}
		if($(this).attr('name') == 'cm-wedk-wedk'){
			if($(this).attr('value') == ''){
				$(this).attr('value', 'Email address');
			}
		}
	});

	jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};

// End jQuery goodness
});