// 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');
		
		// (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="/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();
});