// JavaScript Document
/*
$(document).ready(function() {

	// The following is used to insert the external link image icon immediately after all external links.  
	// The safeList var determines which links are internal/external.
		var safeList = new Array('airprojects.org', 'iwgyp.airprojects.org', 'dev2-iwgyp.airprojects.org', 'nationalservice.org', 'addthis.com', 'api.addthis.com', 'recaptcha.net');
		
		// (Only works with elements that have href):
		// Compares safeList against all href attributes within A tags
		$.extend($.expr[':'], {
			external: function(a, i, m) {
				if (!a.href) { return false; }
				if (a.hostname && a.hostname !== window.location.hostname) {
					var fixedHostname = a.hostname.replace(/^\s+|\s+$/g, '').replace('www.', '').toLowerCase();
					if (jQuery.inArray(fixedHostname, safeList) >= 0 || a.hostname.indexOf(".gov") >=0 || a.hostname.indexOf(".mil") >=0) {
						return false;
					}
					else return true;
				}
				else return false;
			}
		});
	
		// Inserts icon behind link and wrap the image in a link leading to the disclaimer page
		$('a:external').ready(function addExtLink() {
			if (document.createElement){
				var extLink='<a href="http://www.findyouthinfo.gov/ExternalLink.aspx"><img src="http://www.hhs.gov/web/images/exit_small.png" style="margin-left:2px" alt="External Web Site Policy" class="disclaimer" /></a>';
				$('#content a:external').after(extLink);
			}
		});

});
*/
$(document).ready(function() {

	// ADD THIS
	keyboardSubmit = function(selector) {
		$(selector).keypress(function(e){
			var code = e.keyCode || e.which;
			if (code == 13) {
				e.preventDefault();
				$(this).click()
			}
		});
	}	
	addThisList = function(){			
		$('.addthis_list').addClass('invisible');
		$('.addthis_button').attr('tabindex','0').click(function(){							
			var list = $(this).siblings('.addthis_list');
			
			if(list.hasClass('show')) {
				$(this).siblings('.addthis_list').addClass('invisible').removeClass('show');
			} else {					
				$(this).siblings('.addthis_list').removeClass('invisible').addClass('show');
			}
		});
		
		$('.addthis_list a').click(function(){
			
			$(this).parent('.addthis_list').removeClass('show').addClass('invisible');
			return true;
		});
		keyboardSubmit('.addthis_button');
	}

	addThisList();

	// dynamically updates AddThis link URL parameter with URL of current page
	var url = document.URL;	
	addThisURL = function(){
		$(".addThisLink").each(function(){
			this.href = this.href.replace(/url=/, "url="+url);
		});
	}
	
	addThisURL();

	
	// Other question input
	$(".other").hide();
	$(".labelOther").hide();
	// hides/shows other input option based on #referrer selection
	if($("#referrer").val() == 99){  // re-shows input if other value is still selected after page refresh
		$("#referrer_other_label").show();
		$("#referrer_other").show(); 
	}
	$("#referrer").change(function() { 
		if($("#referrer").val() == 99){	
			$("#referrer_other_label").show();
			$("#referrer_other").show(); 
		}else{
			$("#referrer_other_label").hide();											
			$("#referrer_other").hide();
			$("#referrer_other").attr("value","");
		}
	});
	// hides/shows other input option based on #involvement selection
	if($("#involvement").val() == 99){  // re-shows input if other value is still selected after page refresh
		$("#involvement_other_label").show();
		$("#involvement_other").show(); 
	}
	$("#involvement").change(function() { 
		if($("#involvement").val() == 99){	
			$("#involvement_other_label").show();
			$("#involvement_other").show(); 
		}else{
			$("#involvement_other_label").hide();											
			$("#involvement_other").hide();
			$("#involvement_other").attr("value","");											
		}
	});		


});
