$(document).ready(function(){
	
	/*******************
	 StyleSheet Switcher
	********************/
	$.stylesheetInit();
	$('.stylesheetToggle').click(function(e){
			$.addLargeFontStylesheet();
			// Change Icon Representing Current State
			var bgImg = $(this).css("background-image");
			bgImg = bgImg.indexOf("_sml")!=-1 ? bgImg.replace(/_sml/g,"_lrg") : bgImg.replace(/_lrg/g,"_sml");
			$(this).css("background-image",bgImg);
			return false;
		}
	);
	
	
	
	
	
	/***********************
	 Autocomplete for Search
	************************/
	$(".keywords").autocomplete("/Insite/GetACWords.aspx",{width:200, minChars:2});
	
	
	
	
	/**********
	 Print Link
	***********/
	$(".print").live("click",function(){
		window.print();
		return false;
	});
	
	
	
		

	/************************************
	 Remove LAST dashed line on UL & OL's
	*************************************/
	$(".dashedUL, .dashedOL").each(function(){
		$("li:last-child", this).css("border-bottom","none");
	});
	
	
	
	
	
	/*******************************
	 Add link to homepage hero boxes
	********************************/
	$(".hero_home_boxes .box").click(function(){
		location.href = $(this).find("a").attr("href");
	});
	
	
	
	
	
	
	/******************************
	 Search Page - Advanced Options
	*******************************/
	$(".searchAdvOpts").change(function(){
		searchAdvOpts($(this));
	});
	/* On Page Load... */
	if($(".searchAdvOpts").length>0){
		searchAdvOpts($(".searchAdvOpts"),1);
	}
	function searchAdvOpts(t,s){
		var w = $("option:selected, t").attr("class");
		if(w=="loc"){
			$(".advOpts_lib").hide();
			if(s){
				$(".advOpts_local").show()
			} else {
				$(".advOpts_local").removeClass("hidden");
				$(".advOpts_local").fadeIn("fast");
			}
		} else if(w=="lib"){
			$(".advOpts_local").hide();
			s ? $(".advOpts_lib").show() : $(".advOpts_lib").fadeIn("fast")
		} else {
			$(".advOpts_local, .advOpts_lib").hide();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	/************************
	 Highlight Glossary Terms
	*************************/
	jQuery.fn.highlightTerms = function(options) {  
		var glossaryCopy = glossary.slice();
		return this.each(function() {  
			var html = $(this).html();
			for(var g=0; g<glossaryCopy.length; g++){
				// Check to see if we've already used this term on the page
				if(usedTerm(glossary[g])) continue;
				
				var term = glossaryCopy[g];
				var termIndex = html.toLowerCase().indexOf(term.toLowerCase());
				
				if(termIndex==-1) continue;
				
				var first = html.substring(0,termIndex);
				var word  = html.substring(termIndex,termIndex+term.length);
				var last  = html.substring(termIndex+term.length);
				
				// Check to see if the word is in its plural form (neurons)
				var reg = /[a-zA-Z]/;
				if(last.search(reg)==0) continue;
				
				// Rule out terms found inside of HTML tags (like href="term")
				var tagOpenIndexInFirst  = first.lastIndexOf("<");
				var tagCloseIndexInFirst = first.lastIndexOf(">");
				if(tagOpenIndexInFirst>tagCloseIndexInFirst) continue;
				
				
				// Check to see if we have already used this word
					// Find surrounding SPAN's
					var spanOpenIndexInFirst = first.lastIndexOf("<span class='glossaryTerm'>");
					var spanCloseIndexInFirst = first.lastIndexOf("</span>");
					var spanCloseIndex = last.indexOf("</span>")+first.length+word.length;
					
					if(spanCloseIndexInFirst<spanOpenIndexInFirst && spanOpenIndexInFirst>-1 && termIndex>spanOpenIndexInFirst && termIndex<spanCloseIndex){
						if(last.toLowerCase().indexOf(term.toLowerCase())==-1) continue;
						
						// Split 2nd half (last) of copy
						var lastA = last.substring(0,last.toLowerCase().indexOf(term.toLowerCase()));
						var lastWord = last.substring(last.toLowerCase().indexOf(term.toLowerCase()),last.toLowerCase().indexOf(term.toLowerCase())+term.length)
						var lastB = last.substring(last.toLowerCase().indexOf(term.toLowerCase())+term.length);
						
						html = first + word + lastA + "<span class='glossaryTerm'>" + lastWord + "</span>" + lastB;
						usedTerms.push(term);
					} else {
						html = first + "<span class='glossaryTerm'>" + word + "</span>" + last;
						usedTerms.push(term);
					}
			}
			$(this).html(html)
		});
	};
	function usedTerm(g){
		for(var u=0; u<usedTerms.length; u++){
			if(g==usedTerms[u]){return true;}
			else {continue;}
		} return false;
	}
	var usedTerms = new Array();
	$(".copy p:not(div.searchResults p), .copy li:not(div.searchResults li)").highlightTerms();
	
	
	
	

	
	
	
	/******************************
	 qTip - Setup of tool tips
	*******************************/
	$(".helpIcon").each(function(){
		var className = $(this).attr("rel");
		var content = $(className).html();
		$(this).qtip({
			content: content,
			style: {
				border:0,
				padding:0
			}
	});
		$(this).click(function(){
			return false;
		});
	});
	$(".glossaryTerm").each(function(){
		var term = $(this).text();
		for(var x=0;x<glossary.length;x++){
			if(term.toLowerCase()==glossary[x].toLowerCase()){
				var def = "<h4>"+glossary[x]+"</h4><p>"+definitions[x]+"</p>";
			}
		}
		$(this).qtip({
			content: def,
			style: {
				border:0,
				padding:0
			},
			position: {
				corner: {
					target: 'bottomLeft'
				}
			}
		});
	});
	
	
	
	
	/***********************************
	 Glossary Anchor Links and Scrolling
	************************************/
	var scrollAmount;
	$(window).scroll(function(){
		scrollAmount = $(window).scrollTop();
		$(".scroller").each(function(){
			$.scroller(this);
		});
	});
	$.scroller = function(t){
		var pos = $(t).position();
		var parentPos = $(t).parent().position();
		if((parentPos.top) > scrollAmount){
			$(t).css("position","absolute").css("top",0).css("left","-2px");
		} else {
			$(t).css("position","fixed").css("top",0).css("left",parentPos.left-2+"px");
		}
	}
	if($(".scroller").length>0){
		scrollAmount = $(window).scrollTop();
		$(".scroller").each(function(){
			$.scroller(this);
		});
	}
	$(".glossary ul li a").bind("click",function(){
		$(".glossary ul li a.active").removeClass("active");
		var jumpTo = $(this).attr("href").substring($(this).attr("href").lastIndexOf("#")+1);
		if(jumpTo!="top"){
			var jumpPos = ($("a[name='"+jumpTo+"']").position().top)-50;
			$(this).addClass("active");
		} else {
			jumpPos = 0;
		}
		$.scrollTo({top:jumpPos+'px', left:0}, 400);
		
		return false;
	});
	
	
	
	
	
	/**************************************
	 Show Terms of Use for Share Your Story
	***************************************/
	$(".termsofuse").each(function(){
		$(this).click(function(){
			$("#termsofuse").show();
			return false;
		});
	});
	
	
	
	
	
	
	
	
	
	/*************************************************
	 Trap Enter to Submit Forms (except header search)
	**************************************************/	
	// Add trapEnter class to all text inputs, besides header search.
	// Function replaces header search submit button with dummy button, allowing other forms to submit. 
	$("*[class~='trapEnter']").bind('keypress', function(e) {
        var code = e.charCode || e.keyCode;
        if (code == 13) {
			$("<span class='headerSearchSubmit'></span>").insertAfter($("input.headerSearchSubmit"));
			$("input.headerSearchSubmit").remove();
			$("span.headerSearchSubmit").css("display","block");
        }
    });
	
	
	
	
	
	
	
	/*********************************
	 Show/Hide archived press releases
	**********************************/	
	if($("div.pressReleases").length>0){
		$("div.pressReleases a[class*=y2]").each(function(){
			var year = $(this).attr("class").substring(6);
			// Hide articles for previous years
			if($(this).attr("class").indexOf("current")==-1){
				$("div.pressReleases div.m"+year).css("display","none");
			}
			// Setup 'show' functionality
			$(this).toggle(function(){
				$("div.pressReleases div.m"+year).fadeIn("normal");
				return false;
			}, function(){
				$("div.pressReleases div.m"+year).hide();
				return false;
			});
		});
		
		
	}
	
	
	
	
	
	
});