$(document).ready(function() {


// Control the text size
	$('ul#options-nav li.text-size a').click(function() {
		$('#text-size').fadeIn('fast');
	});
	
	/* Set or change cookie for each text size */
	$('#text-size ul li.small a').click(function() {
		$('body').css('font-size','75%');
		$.cookie('text-size', 'small', { path: '/', expires: 10 });
		$('#main-nav').removeClass('large');
	});
	
	$('#text-size ul li.medium a').click(function() {
		$('body').css('font-size','100%');
		$.cookie('text-size', 'medium', { path: '/', expires: 10 });
		$('#main-nav').removeClass('large');
	});
	
	$('#text-size ul li.large a').click(function() {
		$('body').css('font-size','125%');
		$.cookie('text-size', 'large', { path: '/', expires: 10 });
		$('#main-nav').addClass('large');
	});
	
	$('#text-size ul li a').click(function() {
		$('#text-size').fadeOut();
	});
	
	if ($.cookie('text-size') == 'small') { $('body').css('font-size','75%'); $('#main-nav').removeClass('large'); }
	if ($.cookie('text-size') == 'medium') { $('body').css('font-size','100%'); $('#main-nav').removeClass('large'); }
	if ($.cookie('text-size') == 'large') { $('body').css('font-size','125%'); $('#main-nav').addClass('large'); }
	
	
// Assign an accesskey to each main navigation item
	$('#main-nav li').each(function() {
		var navIndex = $(this).index() +1;
		$(this).children('a').attr('accesskey', navIndex);
	});


// Social network links open in new window
	$('#social li a').click(function() {
		$(this).attr('target','_blank');
	});


// Assign unique class for each program
	$('#home #programs li').each(function() {
		var progNum = $(this).index() +1;
		$(this).attr('class', 'program-'+progNum+'');
	});
	
	
// Swap form field values when focused
	$('input[type=text]').each(function() {
	    var default_value = this.value;
	    $(this).focus(function() {
	        if(this.value == default_value) {this.value = '';}
	    });
	    $(this).blur(function() {
	        if(this.value == '') {this.value = default_value;}
	    });
	});
	
	
// Configuration for the cycle plugin
	$('#header #left ul').cycle({
		delay: 0,
		random: true,
		timeout: 8000
	});
	
	$('#header #middle ul').cycle({
		delay: 1000,
		random: true,
		timeout: 8000
	});
	
	$('#header #right ul').cycle({
		delay: 2000,
		random: true,
		timeout: 8000
	});
	
	$('#news #stories').cycle({
		timeout: 0,
		speed: 500,
		prev: '#news ul.controls li.prev',
		next: '#news ul.controls li.next',
		nowrap: true,
		after: function onAfter(curr, next, opts) {
					var index = opts.currSlide;
					$('#news ul.controls li.prev')[index == 0 ? 'addClass' : 'removeClass']('disabled');
					$('#news ul.controls li.next')[index == opts.slideCount - 1 ? 'addClass' : 'removeClass']('disabled');
				}
	});
	
	/* Hide controls if theres one or no stories */
	if ($('#news #stories li').size() <= 1) {
		$('#news ul.controls').hide();
	}


// Application selection form tool	
	$('#app-form li.q1 input').change(function() {
		var checkedYes = $(this).val() == 'yes' && $(this).attr('checked') == true;
		var checkedNo = $(this).val() == 'no' && $(this).attr('checked') == true;		

		if (checkedYes) {
			$('ul li.q2').show().children('input').removeAttr('disabled');
			$('ul li.q3, ul li.q4, ul li.q5, ul li.q6').hide().children('input').attr({'disabled':'disabled','checked': false});
			$('#application-results p').hide();
		} else if (checkedNo) {
			$('ul li.q2, ul li.q3, ul li.q4, ul li.q5').hide().children('input').attr({'disabled':'disabled','checked': false});
			$('ul li.q6').show().children('input').removeAttr('disabled');
			$('#application-results p').hide();
		}
	});
	
	$('#app-form li.q2 input').change(function() {
		var checkedYes = $(this).val() == 'yes' && $(this).attr('checked') == true;
		var checkedNo = $(this).val() == 'no' && $(this).attr('checked') == true;
		
		if (checkedYes) {
			$('ul li.q3, ul li.q4, ul li.q5, ul li.q6').hide().children('input').attr({'disabled':'disabled','checked': false});
			$('#application-results p').hide().parent().find('.q2-y').show();
		} else if (checkedNo) {
			$('ul li.q3').show().children('input').removeAttr('disabled');
			$('#application-results p').hide();
		}
	});		


	$('#app-form li.q3 input').change(function() {
		var checkedYes = $(this).val() == 'yes' && $(this).attr('checked') == true;
		var checkedNo = $(this).val() == 'no' && $(this).attr('checked') == true;		

		if (checkedYes) {
			$('ul li.q4').show().children('input').removeAttr('disabled');
			$('#application-results p').hide();
		} else if (checkedNo) {
			$('ul li.q4, ul li.q5, ul li.q6').hide().children('input').attr({'disabled':'disabled','checked': false});
			$('#application-results p').hide().parent().find('.q3-n').show();
		}
	});		
	
		
	$('#app-form li.q4 input').change(function() {
		var checkedYes = $(this).val() == 'yes' && $(this).attr('checked') == true;
		var checkedNo = $(this).val() == 'no' && $(this).attr('checked') == true;
		
		if (checkedYes) {
			$('ul li.q5, ul li.q6').hide().children('input').attr({'disabled':'disabled','checked': false});
			$('#application-results p').hide().parent().find('.q4-y').show();
		} else if (checkedNo) {
			$('ul li.q5').show().children('input').removeAttr('disabled');
			$('#application-results p').hide();
		}
	});
	
	
	$('#app-form li.q5 input').change(function() {
		var checkedYes = $(this).val() == 'yes' && $(this).attr('checked') == true;
		var checkedNo = $(this).val() == 'no' && $(this).attr('checked') == true;
		
		if (checkedYes) {
			$('#application-results p').hide().parent().find('.q5-y').show();
		} else if (checkedNo) {
			$('#application-results p').hide().parent().find('.q5-n').show();
		}
	});
	
	
	$('#app-form li.q6 input').change(function() {
		var checkedYes = $(this).val() == 'yes' && $(this).attr('checked') == true;
		var checkedNo = $(this).val() == 'no' && $(this).attr('checked') == true;
				
		if (checkedYes) {
			$('#application-results p').hide().parent().find('.q6-y').show();
		} else if (checkedNo) {
			$('#application-results p').hide().parent().find('.q6-n').show();
		}
	});


});
