var nav = ['about','news','econ','educ','comm','cons','bank'];

jQuery.noConflict();

jQuery(document).ready(function($){
	
	function hideAllSubs(){
		$.each(nav, function(i,val){
			$("#"+val+"_sub").hide();
		});
	}
	
	// display #aboutthefed feature on site homepage
	$('#aboutthefed').load('includes/aboutthefed.php');
	
	// open offsite links in new tab or window
	$('a[href^="http://"]:not([href*="frbsf.org"])').attr('target','_blank'); 

	// array of directories for highlights
	var dirs=['federalreserve', 'news', 'economics', 'education', 'community', 'consumer', 'banking'];
	var dir_found = "";
	var url = location.pathname;
	var array_position;
	// highlight based on directories
	for (var i = 0; i < dirs.length; i++){
		// if directory above is in current URL, set value of dir_found to that directory
		if (url.indexOf(dirs[i]) != -1){
			dir_found = dirs[i];
			array_position = i;
		}
	}
	
	// load toolbox, popular content, and footer
	if ($("#toolbox_inc").length) $("#toolbox_inc").load("/includes/toolbox.cfm");
	if ($("#popcontent_inc").length) $("#popcontent_inc").load("/includes/popcontent.cfm");
	if ($("#footer_inc").length) $("#footer_inc").load("/includes/revamp/footer.html");
	if ($("#frbsf_footer").length) $("#frbsf_footer").load("/includes/revamp/footer.html");
	
	/* exceptions to highlight */
	if (url.indexOf("cdinvestments") != -1 || url.indexOf("search_cd") != -1){
		dir_found = dirs[4];
		array_position = 4;
	}
	if (url.indexOf("drecon") != -1){
		dir_found = dirs[3];
		array_position = 3;
	}
	if (url.indexOf("rss") != -1){
		dir_found = dirs[0];
		array_position = 0;
	}
	
	if (dir_found != ""){
		$('#top-nav #' + nav[array_position]).css('background-color','#C3CFD8');
		$("#" + nav[array_position] + "_sub").load("/includes/nav/" + nav[array_position] + ".cfm");
		if ($("#popcontent_inc").length) $("#popcontent_inc").load("/includes/popcontent.cfm?" + nav[array_position]);
	}else{
		$.each(nav, function(i,val){
			$("#"+val+"_sub").load("/includes/revamp/"+val+".cfm");
		});
		hideAllSubs();
		$.each(nav, function(i,val){
			$("#"+val).mouseover(function(){
				hideAllSubs();
				$("#"+val+"_sub").show();
				$('#top-nav a').css('background-color','#D3CCCC');
				$('#top-nav #' + nav[i]).css('background-color','#C3CFD8');
			});
		});
	}
	
	// open offsite links in new window
	$('a[href^="http://"]:not([href*="frbsf.org"])').attr('target','_blank');
	
	// twitter feed
	/*$('.tweet').tweet({
		avatar_size: 32,
		count: 3,
		username: "sffedreserve",
		template: function(i){return i["text"]}
	});*/
	
	// auto-erase search field on focus/blur
	$('#search_textfield').focus(function() {
		if ($('#search_textfield').val() == 'Search frbsf.org') $('#search_textfield').val('');
	});
	$('#search_textfield').blur(function() {
		if ($('#search_textfield').val() == '') $('#search_textfield').val('Search frbsf.org');
	});
	
	// Dr. Econ fixes
	if ($("h2.pageSectionTitle:has('img')")) {
		$("h2.pageSectionTitle img").css("border","0");
		$("h2.pageSectionTitle img").wrap("<a href='/education/activities/drecon/askecon.cfm'></a>");
	}
	if ($("h2.paragraphHead:has('img')")) {
		$("h2.paragraphHead img").css("border","0");
		$("h2.paragraphHead img").wrap("<a href='/education/activities/drecon/askecon.cfm'></a>");
	}
	
	// includes for archive links
	$("#banking_reports_archive_links").load("/banking/applications/banking_reports_archive_links.html #banking_reports_archive_links_include");
	$("#cdinvestments_archive_links").load("/cdinvestments/cdinvestments_archive_links.html #cdinvestments_archive_links_include");
	$("#community_resources_archive_links").load("/community/resources/community_resources_archive_links.html #community_resources_archive_links_include");
	$("#district_circular_letters_archive_links").load("/banking/letters/district_circular_letters_archive_links.html #district_circular_letter_archive_links_include");
	$("#news_releases_archive_links").load("/news/releases/news_releases_archive_links.html #news_releases_archive_links_include");
});
