/* 	
	@author: Finbarr Taylor
	@author website: rhubarbweb.com
	@for: British Council
	@website: scotlandscholarshipsapplications.org
*/

var expandLink = '<a class="expand" href="#">(expand)</a>';

//when the document is ready
$(document).ready(function() {
	
	//alert a message if they have previously completed a full 4+ years ug degree in Scotland
	$('#partA-previously_completed_ug_degree-Yes').click(function() {
		alert('Thank you for your interest in the Scotland Saltire Scholarship scheme.\n\nUnfortunately you are not eligible for a Saltire Scholarship as you have already studied for a full undergraduate degree in Scotland (duration 4+ years).\n\nIf you have any questions about Saltire Scholarships please email scotlandsaltirescholarships@britishcouncil.org.');
	});		   
	
	//choiceb and choicec fieldset legend styling
	$('#fieldset-partB fieldset:not([id=fieldset-choiceA], [id=fieldset-agentsubform]) legend').css('cursor', 'pointer').css('color', 'blue');
	
	//fade out the flash message when hide is clicked
	$('<a class="hide-message" href="#">(hide)</a>').appendTo('div.flash-message').css('color', '#0066CC').click(function() {
		$(this).parent().fadeOut('fast');
		return false;
	});
	
	//add the paragraph about clicking to expand after the explanation paragraph
	$('<p>To enter a second or third choice of study click on Choice B and C below and enter your choices.</p>').appendTo('div.partb-explanation');
	
	//add required spans to select lists
	$('#uniA-element label').append('<span>*</span>');
	$('#dob_day-element label').append('<span>*</span>');
	
	//add required spans to relevant fields in agent subform
	$('#fieldset-agentsubform dt:not([id=agent_company_name-label], [id=agent_company_email-label], [id=agent_company_address-label], [id=agent_company_address-label], [id="agent_telephone-label"]) label.optional').append('<span class="required">*</span>');
	
	//hide the 'please specify' field if the 'other' referrer not checked
	if(!$('#partB-exposure-8').is(':checked')) {
		$('#exposure_specify-label').hide();
		$('#exposure_specify-element').hide();
	}
	
	//add a paragraph after choiceA
	$('<p>Optionally, specify up to 2 more institutions and course choices.</p>').insertAfter('#fieldset-choiceA').css('marginBottom', '10px');
	
	//hide choice b and c if no values present
	if($('#partB-choiceB-uniB').val()=='Please select' && $('#partB-choiceB-courseB').val().length==0 && $('#placeB-element input:checked').size()==0) {
		$('#fieldset-choiceB dl').hide();	
		$('#fieldset-choiceB').prepend(expandLink);
		//$('#fieldset-choiceB').css('padding', '0 21px').css('borderWidth', '1px 0 0 0');
	}
	if($('#partB-choiceC-uniC').val()=='Please select' && $('#partB-choiceC-courseC').val().length==0 && $('#placeC-element input:checked').size()==0) {
		$('#fieldset-choiceC dl').hide();
		$('#fieldset-choiceC').prepend(expandLink);
		//$('#fieldset-choiceC').css('padding', '0 21px').css('borderWidth', '1px 0 0 0');
	}

	//if education agent not set to yes, hide agent fields
	if($('#agent-element input:checked').val()!='Yes') {
		$('#agent-element').siblings().not('[id=agent-label]').hide();
	}
	
	//show agentsubform elements when yes is clicked
	$('#partB-agentsubform-agent-Yes').click(function() {
		$('#agent-element').siblings().not('[id=agent-label]').show('fast');
	});
	
	//hide agentsubform elements when no is clicked
	$('#partB-agentsubform-agent-No').click(function() {
		$('#agent-element').siblings().not('[id=agent-label]').hide('fast');
	});
	
	//show and hide please specify field based on checkbox state after click
	$('#partB-exposure-8').click(function() {
		if($('#partB-exposure-8').is(':checked')) {
			$('#exposure_specify-label').show();
			$('#exposure_specify-element').show();
		} else {
			$('#exposure_specify-label').hide();
			$('#exposure_specify-element').hide();
		}
	});
	
	//expandable choice fieldsets
	$('#fieldset-partB fieldset:not([id=fieldset-choiceA], [id=fieldset-agentsubform]) legend').click(function() {	
		if($(this).parent().find('dl').is(':visible')) {
			$(this).parent().find('dl').hide('fast');/*, function() {
				//$(this).parent().css('borderWidth', '1px 0 0 0');
				/*for($i = 20; $i >=0; $i--) {
					$(this).parent().css('padding', $i+'px 21px');
				}*/
				$(this).parent().find('a').show();
			//});
		
		} else {
			//$(this).parent().css('borderWidth', '1px 1px 1px 1px');
			//$(this).parent().css('padding', '20px');
			$(this).parent().find('dl').show('fast');
			$(this).parent().find('a').hide();
		}
	});
	
	//when the expand link is clicked
	$('a.expand').click(function() {
		//$(this).parent().css('borderWidth', '1px 1px 1px 1px');
		//$(this).parent().css('padding', '20px');
		$(this).parent().find('dl').show('fast');
		$(this).hide();
		return false;
	});
});
