$(function()
{
	// resize content container according to the page/column height - depending on which is tallest
	if($("div.page").height()+220 > $("div.column").height())
	{
		$("div#content").css("height",$("div.page").height()+40+"px");
		
	}
	else 
	{
		$("div#content").css("height",$("div.column").height()-220+"px");
	}
	
	if($("ul#subnav").length > 0)
	{
		if(!$.browser.msie || $.browser.version != 5.5)
		{
			$("ul#subnav").css("display","none");
		}
	}
});

$(document).ready(function()
{

	/*
		Survey form control
	*/
	// show/hide "Other/More information" fields until option is selected
	$('input#q1_other').change(function() {
		if($('input#q1_other').is(':checked'))	
		{
			$('li.other_field.1').show();
		}
		else {
			$('li.other_field.1').hide();
		}
	});
	$('input#q2_other').change(function() {
		if($('input#q2_other').is(':checked'))	
		{
			$('li.other_field.2').show();
		}
		else {
			$('li.other_field.2').hide();
		}
	});
	$('input#q3_other').change(function() {
		if($('input#q3_other').is(':checked'))	
		{
			$('li.other_field.3').show();
		}
		else {
			$('li.other_field.3').hide();
		}
	});	
	$('input#q5_other').change(function() {
		if($('input#q5_other').is(':checked'))	
		{
			$('li.other_field.5').show();
		}
		else {
			$('li.other_field.5').hide();
		}
	});	
	$('input#q11_other').change(function() {
		if($('input#q11_other').is(':checked'))	
		{
			$('li.other_field.11').show();
		}
		else {
			$('li.other_field.11').hide();
		}
	});	
	
	$('input#q19_other').change(function() {
		if($('input#q19_other').is(':checked'))	
		{
			$('li.other_field.19').show();
		}
		else {
			$('li.other_field.19').hide();
		}
	});	
	$('input#q22_other').change(function() {
		if($('input#q22_other').is(':checked'))
		{
			$('li.other_field.22').show();
		}
		else {
			$('li.other_field.22').hide();
		}
	});	
	
	/*
		show/hide extra information, where required (fill in with N/A if unused to avoid validation errors
	*/
	$('input#q12_no').change(function() {
		$('li.extra_q.12').show();
	});
	$('input#q12_yes').change(function() {
		$('li.extra_q.12').hide();
	});	
	$('input#q13_no').change(function() {
		$('li.extra_q.13').hide();
	});
	$('input#q13_yes').change(function() {
		$('li.extra_q.13').show();
	});
	$('input#q15_no').change(function() {
		$('li.extra_q.15').hide();
	});
	$('input#q15_yes').change(function() {
		$('li.extra_q.15').show();
	});	
	$('input#q16_no').change(function() {
		$('li.extra_q.16').show();
	});
	$('input#q16_yes').change(function() {
		$('li.extra_q.16').hide();
	});
	$('input#q17_yes').change(function() {
		$('li.extra_q.17').show();
	});
	$('input#q17_no').change(function() {
		$('li.extra_q.17').hide();
	});		
	$('input#q18_no').change(function() {
		$('li.extra_q.18').hide();
	});
	$('input#q18_yes').change(function() {
		$('li.extra_q.18').show();
	});		
	$('input#q20_no').change(function() {
		$('li.extra_q.20').show();
	});
	$('input#q20_yes').change(function() {
		$('li.extra_q.20').hide();
	});	
	$('input#q21_no').change(function() {
		$('li.extra_q.21').hide();
		$('form#fa_survey_2 fieldset').slice(28,33).hide();	// if it's no, disable the rest of the section
		// check N/A boxes for disabled questions
		for(i=22;i<27;i++)
		{
			$('form#fa_survey_2 input#q'+i+'_na').attr('checked', true);
		}
	});
	$('input#q21_yes').change(function() {
		$('li.extra_q.21').show();
		$('form#fa_survey_2 fieldset').slice(28,33).show();	// if it's yes, enable the rest of the section
		// uncheck N/A boxes for enabled questions
		for(i=22;i<27;i++)
		{
			$('form#fa_survey_2 input#q'+i+'_na').attr('checked', false);
		}		
	});	
	$('input#q25_no').change(function() {
		$('li.extra_q.25').hide();
	});
	$('input#q25_yes').change(function() {
		$('li.extra_q.25').show();
	});	
	$('input#q26_no').change(function() {
		$('li.extra_q.26').hide();
	});
	$('input#q26_yes').change(function() {
		$('li.extra_q.26').show();
	});		
	
	// function to limit ticks
	$.fn.limit = function(n)
	{
		var self = this;
		this.click(function()
		{
			return (self.filter(':checked').length <= n);
		});
	}
	
	// limit number of tick boxes that can be ticked on various questions
	$('li.limit_Q1 dl.inputgroup input:checkbox').limit(3);
	$('li.limit_Q2 dl.inputgroup input:checkbox').limit(3);
	$('li.limit_Q3 dl.inputgroup input:checkbox').limit(3);
	$('li.limit_Q5 dl.inputgroup input:checkbox').limit(3);
	$('li.limit_Q19 dl.inputgroup input:checkbox').limit(2);
	$('li.limit_Q22 dl.inputgroup input:checkbox').limit(1);

	/*
		hide sections 2 and 3 until previous question is answered
		unless we've already submitted the form. In this case,
		only hide the area we're not using
	*/
	if($('p.error').length > 0)
	{
		switch($('select#Q10').val())
		{
			case 'Academy/Centre of Excellence player':
			case 'Club player':
			case 'Inter-college player':
			case 'Intra-college player':
			case 'Play with friends socially':
				$('form#fa_survey_2 fieldset').slice(24,33).hide();
				for(i=11;i<19;i++)
				{
					$('form#fa_survey_2 input#q'+i+'_na').parent().hide();
					$('form#fa_survey_2 input#q'+i+'_na').parent().next().hide();
				}
				break;
			case 'Used to play but have now stopped':
			case 'Never played and little interest in playing':
				$('form#fa_survey_2 fieldset').slice(15,24).hide();
				for(i=19;i<27;i++)
				{
					$('form#fa_survey_2 input#q'+i+'_na').parent().hide();
					$('form#fa_survey_2 input#q'+i+'_na').parent().next().hide();
				}
				if($('input#q21_no').is(':checked'))
				{
					$('form#fa_survey_2 fieldset').slice(28,33).hide();	// if it's no, disable the rest of the section
					// check N/A boxes for disabled questions
					for(i=22;i<27;i++)
					{
						$('form#fa_survey_2 input#q'+i+'_na').attr('checked', true);
					}
				}
				
				
				
				break;
			default:
				$('form#fa_survey_2 fieldset').slice(15,33).hide();
				break;
		}
	}
	else
	{
		// hide 'em all!
		$('form#fa_survey_2 fieldset').slice(15,33).hide();
	}
	// hide first two empty fieldsets
	$('form#fa_survey_2 fieldset').eq(1).hide();
	$('form#fa_survey_2 fieldset').eq(0).hide();
	
	$.fn.uncheck_and_hide = function() 
	{
		$(this).attr('checked', false);
		$(this).parent().hide();
		$(this).parent().next().hide();
	}
	
	$.fn.check_and_show = function() 
	{
		$(this).attr('checked', true);
		$(this).parent().show();
		$(this).parent().next().show();
	}	
	
	
	// select menu decides which question sections are displayed below
	$('select#Q10').unbind().bind('change',function()
	{
		switch($(this).val())
		{
			case 'Academy/Centre of Excellence player':		
			case 'Club player':
			case 'Inter-college player':
			case 'Intra-college player':
			case 'Play with friends socially': 
				$('form#fa_survey_2 fieldset').slice(15,24).show();
				$('form#fa_survey_2 fieldset').slice(24,33).hide();
				for(i=11;i<19;i++)
				{
					$('form#fa_survey_2 input#q'+i+'_na').uncheck_and_hide();
				}
				for(i=19;i<27;i++)
				{
					$('form#fa_survey_2 input#q'+i+'_na').check_and_show();
				}
				break;
				
			case 'Used to play but have now stopped':
			case 'Never played and little interest in playing':
				$('form#fa_survey_2 fieldset').slice(15,24).hide();
				$('form#fa_survey_2 fieldset').slice(24,33).show();	
				for(i=11;i<19;i++)
				{
					$('form#fa_survey_2 input#q'+i+'_na').check_and_show();
				}
				for(i=19;i<27;i++)
				{
					$('form#fa_survey_2 input#q'+i+'_na').uncheck_and_hide();
				}
				
				break;
				
			default:
				$('form#fa_survey_2 fieldset').slice(15,33).hide();
			break;
		}
	});

	
	
	
	
	var shadowbox_options = ''; //';options={animate:false}';
		
	if($('input#mailing_list_email_address').length > 0)
	{
		$('input#mailing_list_email_address').focus(function()
		{
			if($(this).val() == 'Email Address')
			{
				$(this).val('');
			}
		});
		
		$('input#mailing_list_email_address').blur(function()
		{
			if($(this).val() == '')
			{
				$(this).val('Email Address');
			}
		});
	}
	
	$('li.help div').hide();

	/*
	// resize content container according to the page/column height - depending on which is tallest
	if($("div.page").height()+220 > $("div.column").height())
	{
		$("div#content").css("height",$("div.page").height()+40+"px");
		
	}
	else 
	{
		$("div#content").css("height",$("div.column").height()-220+"px");
	}
	*/
	
	if($("ul#subnav").length > 0)
	{
		if(!$.browser.msie || $.browser.version != 5.5)
		{
			$("ul#subnav").css("display","none");
		}
	}
	
	$(function()
	{  
		if ($.browser.msie && parseInt($.browser.version)< 7)
		{
			$("ul#nav li.subnav").hover(function()
			{  
				$(this).addClass("active");  
            },
			function()
			{
				$(this).removeClass("active");  
            });
			
			$("ul#nav li.subnav ul li.subsubnav").hover(function()
			{  
				$(this).addClass("active");  
            },
			function()
			{
				$(this).removeClass("active");  
            });
		}
	});
	
	$('a[rel*="external"]').click(function()
	{
		open($(this).attr('href'));
		return false;
	});
	
	$('a[rel*="shadowbox"]').each(function()
	{
		if(!$(this).hasClass('page_media'))
		{
			var enlarge_img = $(this).attr('rel').split(' ');
			
			if(enlarge_img != 'undefined' && enlarge_img != '')
			{
				$(this).removeAttr('rel');
				
				if(enlarge_img[1] != undefined)
				{
					$(this).attr('rel', enlarge_img[1]+shadowbox_options);
				}
				else
				{
					$(this).attr('rel', enlarge_img[0]+shadowbox_options);
				}
			}
		}
		else
		{
			var media_rel = $(this).attr('rel');
			
			$(this).removeAttr('rel');
			
			$(this).attr('rel', media_rel+shadowbox_options);
		}
	});
	
	var nav_timeout = 'undefined';
	
	$("li.subnav").bind("mouseenter",function()
	{
		if(nav_timeout != 'undefined')
		{
			clearTimeout(nav_timeout);
		}
		
		$("li.subnav.active").removeClass("active");
		$(this).addClass("active");
	}).bind("mouseleave",function()
	{
		nav_timeout = setTimeout(function()
		{
			$("li.subnav.active").removeClass("active");
		},2000);
	});
	
	$("ul.thumbs li a img").each(function()
	{
		$.preloadImages.add($(this).attr("src").replace("thumb", "mid"));
	});
	
	$("ul.thumbs li a").mouseover(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "small"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
	});
	
	$("ul.thumbs li:first a").click(function()
	{
		$("a#enlarge").click();
	});
	
	$("ul.thumbs li a").click(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "small"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
		return false;
	});
	
	if($("li.help a").length > 0)
	{
		$("li.help a").click(function()
		{
			return false;
		});
		
		var help_timeout = 'undefined';
		
		$("li.help a").bind("mouseenter",function()
		{
			if(help_timeout != 'undefined')
			{
				clearTimeout(help_timeout);
			}
			$("li.help div").fadeOut();
			$(this).next().addClass('active').fadeIn();
		}).bind("mouseleave",function()
		{
			help_timeout = setTimeout(function()
			{
				$('li.help div.active').removeClass('active').fadeOut();
			},2000);
		});
		
		$('input').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('select').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('textarea').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
	}
	
	if($('a[rel*="shadowbox"]').length > 0)
	{
		if($('a.page_audio').length > 0 || $('a.page_video').length > 0)
		{
			Shadowbox.init({
								continuous		:	true,
								ext				: 	{
										                img		:	['png', 'jpg', 'jpeg', 'gif', 'bmp'],
										                swf		:	['swf'],
										                flv		:	['flv', 'm4v', 'mp4', 'mp3'],
										                qt		:	['dv', 'mov', 'moov', 'movie'],
										                wmp		:	['asf', 'wm', 'wmv'],
										                qtwmp	:	['avi', 'mpg', 'mpeg']
										            },
								players			: 	['img', 'flv', 'iframe'],
								useSizzle		: 	false
							});
		}
		else
		{
			Shadowbox.init({
								continuous		: 	true,
								players			: 	['img','iframe'],
								useSizzle		: 	false
							});
		}
	}	
	
	
});
