// For adding multiple functions to onload event
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {window.onload = func;}
	else {window.onload = function() {oldonload(); func();}}
}



var advSearchShowing = 0;
function showHideAdvSearch(){
	if(advSearchShowing == 0){
		document.getElementById('advSearch').style.display = 'block';
		document.getElementById('advSearchToggle').innerHTML = '<a href="#" onclick="javascript:showHideAdvSearch()" class="closeSearch">Hide Advanced Search options</a>';
		advSearchShowing=1;
	}
	else{
		document.getElementById('advSearch').style.display = 'none';
		document.getElementById('advSearchToggle').innerHTML = '<a href="#" onclick="javascript:showHideAdvSearch()" class="openSearch">Refine your Advanced Search</a>';
		advSearchShowing=0;
	}
}

function emailThisPage(which){
	window.open('emailThisPage.cfm?whichURL='+which,'_blank','width=570,height=575,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0');
}

function spreadTheWord(){
	window.open('spreadTheWord.cfm','_blank','width=710,height=650,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0');
}

function launchMultimediaQT(which){
	window.open('newsEvents_multimedia_QTPlayer.cfm?file='+which,'_blank','width=320,height=256,location=0,menubar=0,resizable=1,scrollbars=0,status=0,toolbar=0');
}

function launchMultimediaWMP(which){
	window.open('newsEvents_multimedia_WMPlayer.cfm?file='+which,'_blank','width=320,height=256,location=0,menubar=0,resizable=1,scrollbars=0,status=0,toolbar=0');
}

function submitFundingForm (ProgramID) {
	//submit the form for processing
	document.getElementById("ProgramID").value = ProgramID;
	document.funding.submit();
}

function launchFoxFlash(which){
	window.open('about_publications_foxFlashEmail.cfm?FileID='+which,'_blank','width=800,height=600,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0');
}





// Funding Opportunities Background Rollover
function programsRollover(){
	if(!document.getElementById) return;
	if(!document.getElementById('fundingOppsFrame')) return;
	var allDivs = document.getElementsByTagName('div');
	var divs = new Array();
	
	// If it's IE6, don't use background rollovers, it causes flickering
	var browser = navigator.userAgent
	if(document.all && browser.indexOf("MSIE 7") == -1) return;
	
	for(i=0; i<allDivs.length; i++){
		if(allDivs[i].className.indexOf("fundingOppsProgs") != -1){
			var divs = allDivs[i].getElementsByTagName('div');
			
			for(x=0; x<divs.length; x++){
				if(divs[x].className.indexOf("clear") < 0 && divs[x].className.indexOf("progBox") < 0 && divs[x].className.indexOf("empty") < 0 && divs[x].className.indexOf("fundingOppPopup") < 0 && divs[x].className.indexOf("row") < 0){
					divs[x].onmouseover = function(){
						if(curFrame != undefined && curFrame != this.parentNode.parentNode){
							clearTimeout(roTimer);
							resetBG(curFrame);
						} else {
							clearTimeout(roTimer);
						}
						w = this.className.substring(0,2);
						var frameDiv = this.parentNode.parentNode;
						switch(w){
							case "a3":
								frameDiv.style.backgroundImage = 'url(images/fundingOpps_bg_ABC.gif)';
								break;
							case "a2":
								frameDiv.style.backgroundImage = 'url(images/fundingOpps_bg_AB.gif)';
								break;
							case "a1":
								frameDiv.style.backgroundImage = 'url(images/fundingOpps_bg_A.gif)';
								break;
							case "b2":
								frameDiv.style.backgroundImage = 'url(images/fundingOpps_bg_BC.gif)';
								break;
							case "b1":
								frameDiv.style.backgroundImage = 'url(images/fundingOpps_bg_B.gif)';
								break;
							case "c1":
								frameDiv.style.backgroundImage = 'url(images/fundingOpps_bg_C.gif)';
								break;
							default:
								frameDiv.style.backgroundImage = 'url(images/fundingOpps_bg.gif)';
								break;
						}
					}
					divs[x].onmouseout = function(){
						var p = this.parentNode.parentNode.id;
						curFrame = p;
						roTimer = setTimeout(function() {resetBG(p)},350);
					}
				}
			}
		}
	}
			
}
var roTimer; var curFrame;
addLoadEvent(programsRollover);
function resetBG(p){
	 document.getElementById(p).style.backgroundImage = 'url(images/fundingOpps_bg.gif)';
}

// Funding Opportunities Popups
function fundingOppsPopup(){
	if(!document.getElementById) return;
	if(!document.getElementById('fundingOppsFrame')) return;
	
	// For Learn More Popups
	var allDivs = document.getElementsByTagName('div');
	
	for(x=0; x<allDivs.length; x++){
		if(allDivs[x].className == "progBox"){
			allDivs[x].onclick = function(){
				pop = this.parentNode.parentNode.id + "Popup";
				
				// Close all popups first
				var pops = document.getElementsByTagName('div');
				for(w=0; w<pops.length; w++){
					if(pops[w].className == "fundingOppPopup"){
						pops[w].style.display = "none";
					}
				}
				document.getElementById('popupCoverUp').style.display = "block";
				document.getElementById(pop).style.display = "block";
				document.getElementById(pop).style.width = "382px";
					var scroll = getScrollXY(); var size = getSize();
					var newY = (((size[1] - document.getElementById(pop).offsetHeight)/2)); if(newY<0) newY=0;
					var newX = ((size[0]-382)/2)+60;
				var browser = navigator.userAgent
				if(!document.all || browser.indexOf("MSIE 7") != -1) { // Don't use this for IE6
					document.getElementById(pop).style.top = newY + "px";
					document.getElementById(pop).style.left = newX + "px";
				}
				if(!document.all || browser.indexOf("MSIE 8") != -1) { // Don't use this for IE6
					document.getElementById(pop).style.top = newY + "px";
					document.getElementById(pop).style.left = newX + "px";
				}
			}
		}
	}
		
	// For Print Option popups
	var allAs = document.getElementsByTagName('a');
	
	for(x=0; x<allAs.length; x++){
		if(allAs[x].className == "printOptLink"){
			allAs[x].onclick = function(){
				var pop = document.getElementById(this.id.substring(0,this.id.indexOf("Link")) + "PrintOpt");
				
				document.getElementById('popupCoverUp').style.display = "block";
				pop.style.display = "block";
				pop.style.width = "382px";
					var scroll = getScrollXY(); var size = getSize();
					var newY = (((size[1] - pop.offsetHeight)/2)); if(newY<0) newY=0;
					var newX = ((size[0]-382)/2)+60;
				var browser = navigator.userAgent
				if(!document.all || browser.indexOf("MSIE 7") != -1) { // Don't use this for IE6
					pop.style.top = newY + "px";
					pop.style.left = newX + "px";
				}
			}
		}
	}
}
addLoadEvent(fundingOppsPopup);

function closePopup(w){
	document.getElementById(w.parentNode.parentNode.parentNode.parentNode.id).style.display = "none";
	document.getElementById('popupCoverUp').style.display = "none";
}

// Make all of the Program Boxes the same height, per row
function adjustDivHeights(){
	if(!document.getElementById) return;
	if(!document.getElementById('fundingOppsFrame')) return;
	
	// Get all DIVs on the page
	var allDivs = document.getElementsByTagName('div');
	for(var d=0; d<allDivs.length; d++){
		// If the DIV has a class="row"
		if(allDivs[d].className.indexOf("row") != -1){
			// All DIVs in the "row"
			var rowDivs = allDivs[d].getElementsByTagName('div');
			var maxH = 0;
			// Find Max Height
			for(var r=0; r<rowDivs.length; r++){
				if(rowDivs[r].className.indexOf("empty") == -1){
					if(rowDivs[r].className.indexOf("a") == 0 || rowDivs[r].className.indexOf("b") == 0 || rowDivs[r].className.indexOf("c") == 0){
						if(rowDivs[r].offsetHeight > maxH){maxH = rowDivs[r].offsetHeight;}
					}
				}
			}
			// Apply Max Height
			for(var s=0; s<rowDivs.length; s++){
				if(rowDivs[s].className.indexOf("empty") == -1){
					if(rowDivs[s].className.indexOf("a") == 0 || rowDivs[s].className.indexOf("b") == 0 || rowDivs[s].className.indexOf("c") == 0){
						rowDivs[s].style.height = (maxH-5)+"px";
						// Get all Divs that are progBox1 and progBox and set Heights
						var progDivs = rowDivs[s].getElementsByTagName("div");
						for(var c=0; c<progDivs.length; c++){
							if(progDivs[c].className.indexOf("progBox1") != -1){
								progDivs[c].style.height = (maxH - 17) + "px";
							}
						}
						for(var c=0; c<progDivs.length; c++){
							if(progDivs[c].className.indexOf("progBox") != -1 && progDivs[c].className.length == 7){
								progDivs[c].style.height = (maxH - 19) + "px";
							}
						}
					}
				}
			}
		}
	}
}
addLoadEvent(adjustDivHeights);


// Get Browser Width and Height
function getSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}
// Get Browser's Scroll Amounts
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}








// Add click event to entire Segment Intro Box
function segIntroClick(){
	if(!document.getElementById) return;
	if(!document.getElementById("segIntros")) return;
	
	var sI = document.getElementById("segIntros");
	var spans = sI.getElementsByTagName("span");
	for(var s=0; s<spans.length; s++){
		spans[s].onclick = function(){
			var hrefs = this.getElementsByTagName("a");
			for(var h=0; h<hrefs.length; h++){
				if(hrefs[h].className.indexOf("seg")!=-1){
					var href = hrefs[h].getAttribute("href");
				}
			}
			location.href = href;
		}
		// For IE6
		spans[s].onmouseover = function(){
			this.className = "mo";
		}
		spans[s].onmouseout = function(){
			this.className = "";
		}
		// For IE6 Ends
	}
}
addLoadEvent(segIntroClick);



// Funding Opps Jump To Links DropDown
function fundingOppsJumpToLinks(){
	if(!document.getElementById) return;
	if(!document.getElementById('jumpToLinks')) return;
	
	//Hide the Go button since they have JS enabled
	var b = document.getElementById('goButton');
	b.style.display = "none";
	
	var f = document.getElementById('jumpToLinks');// FORM
	var s = f.getElementsByTagName('select'); //SELECT Array; Should only be one SELECT in the form
	var e = s[0]; //ELEMENT
	e.onchange = function(){
		var val = this.options[this.selectedIndex].value;
		if(val!=""){
			location.href=val;
		}
	}
}
addLoadEvent(fundingOppsJumpToLinks);