// ==================== pre-load top nav ==================== //
if (document.images) {
	topnav_programareas_off = new Image();
	topnav_programareas_off.src = '/images/common/topnav_programareas_off.gif';
	topnav_programareas_on = new Image();
	topnav_programareas_on.src = '/images/common/topnav_programareas_on.gif';
	topnav_grantapplications_off = new Image();
	topnav_grantapplications_off.src = '/images/common/topnav_grantapplications_off.gif';
	topnav_grantapplications_on = new Image();
	topnav_grantapplications_on.src = '/images/common/topnav_grantapplications_on.gif';
	topnav_granteeresources_off = new Image();
	topnav_granteeresources_off.src = '/images/common/topnav_granteeresources_off.gif';
	topnav_granteeresources_on = new Image();
	topnav_granteeresources_on.src = '/images/common/topnav_granteeresources_on.gif';
	topnav_publicationsandresearch_off = new Image();
	topnav_publicationsandresearch_off.src = '/images/common/topnav_publicationsandresearch_off.gif';
	topnav_publicationsandresearch_on = new Image();
	topnav_publicationsandresearch_on.src = '/images/common/topnav_publicationsandresearch_on.gif';
	topnav_newsroom_off = new Image();
	topnav_newsroom_off.src = '/images/common/topnav_newsroom_off.gif';
	topnav_newsroom_on = new Image();
	topnav_newsroom_on.src = '/images/common/topnav_newsroom_on.gif';
	topnav_aboutus_off = new Image();
	topnav_aboutus_off.src = '/images/common/topnav_aboutus_off.gif';
	topnav_aboutus_on = new Image();
	topnav_aboutus_on.src = '/images/common/topnav_aboutus_on.gif';
}



// ==================== pre-load sidebar buttons ==================== //
if (document.images) {
	btn_search_off = new Image();
	btn_search_off.src = '/images/common/btn_search_off.gif';
	btn_search_on = new Image();
	btn_search_on.src = '/images/common/btn_search_on.gif';
	btn_textsmall_off = new Image();
	btn_textsmall_off.src = '/images/common/btn_textsmall_off.gif';
	btn_textsmall_on = new Image();
	btn_textsmall_on.src = '/images/common/btn_textsmall_on.gif';
	btn_textmedium_off = new Image();
	btn_textmedium_off.src = '/images/common/btn_textmedium_off.gif';
	btn_textmedium_on = new Image();
	btn_textmedium_on.src = '/images/common/btn_textmedium_on.gif';
	btn_textlarge_off = new Image();
	btn_textlarge_off.src = '/images/common/btn_textlarge_off.gif';
	btn_textlarge_on = new Image();
	btn_textlarge_on.src = '/images/common/btn_textlarge_on.gif';
	nav_arrow_off = new Image();
	nav_arrow_off.src = '/images/common/nav_arrow_off.gif';
	nav_arrow_on = new Image();
	nav_arrow_on.src = '/images/common/nav_arrow_on.gif';
	topnav_arrow_off = new Image();
	topnav_arrow_off.src = '/images/common/topnav_arrow_off.gif';
	topnav_arrow_on = new Image();
	topnav_arrow_on.src = '/images/common/topnav_arrow_on.gif';
}



// ==================== pre-load icon labels ==================== //
if (document.images) {
	iconlabel_downloadfile_ebe2c1_off = new Image();
	iconlabel_downloadfile_ebe2c1_off.src = '/images/common/iconlabel_downloadfile_ebe2c1_off.gif';
	iconlabel_downloadfile_ebe2c1_on = new Image();
	iconlabel_downloadfile_ebe2c1_on.src = '/images/common/iconlabel_downloadfile_ebe2c1_on.gif';
	iconlabel_email_ebe2c1_off = new Image();
	iconlabel_email_ebe2c1_off.src = '/images/common/iconlabel_email_ebe2c1_off.gif';
	iconlabel_email_ebe2c1_on = new Image();
	iconlabel_email_ebe2c1_on.src = '/images/common/iconlabel_email_ebe2c1_on.gif';
	iconlabel_externallink_ebe2c1_off = new Image();
	iconlabel_externallink_ebe2c1_off.src = '/images/common/iconlabel_externallink_ebe2c1_off.gif';
	iconlabel_externallink_ebe2c1_on = new Image();
	iconlabel_externallink_ebe2c1_on.src = '/images/common/iconlabel_externallink_ebe2c1_on.gif';
	iconlabel_print_ebe2c1_off = new Image();
	iconlabel_print_ebe2c1_off.src = '/images/common/iconlabel_print_ebe2c1_off.gif';
	iconlabel_print_ebe2c1_on = new Image();
	iconlabel_print_ebe2c1_on.src = '/images/common/iconlabel_print_ebe2c1_on.gif';
	iconlabel_shoppingcart_ebe2c1_off = new Image();
	iconlabel_shoppingcart_ebe2c1_off.src = '/images/common/iconlabel_shoppingcart_ebe2c1_off.gif';
	iconlabel_shoppingcart_ebe2c1_on = new Image();
	iconlabel_shoppingcart_ebe2c1_on.src = '/images/common/iconlabel_shoppingcart_ebe2c1_on.gif';
	iconlabel_webcast_ebe2c1_off = new Image();
	iconlabel_webcast_ebe2c1_off.src = '/images/common/iconlabel_webcast_ebe2c1_off.gif';
	iconlabel_webcast_ebe2c1_on = new Image();
	iconlabel_webcast_ebe2c1_on.src = '/images/common/iconlabel_webcast_ebe2c1_on.gif';
}



//========================= standard rollover =========================
function rollover(imgName, active) {
	if (document.images && document.images[imgName]) {
		active? document[imgName].src = self[imgName + '_on'].src : document[imgName].src = self[imgName + '_off'].src;
	}
}



//========================= nav rollover =========================
function rolloverNav(imgName, active) {
	if (document.images && document.images[imgName]) {
		active? document[imgName].src = nav_arrow_on.src : document[imgName].src = nav_arrow_off.src;
	}
}

// ==================== interactive story photo rotation script ====================
// using a cookie, loop through a different image associated with an interactive
// story with each visit to the home page

function rotateInteractiveStory(MAX_PHOTOS) {
    if (isNaN(num = getCookie('interactiveStory')) || num >= MAX_PHOTOS) num = 0;
    num++;
    setCookie('interactiveStory', (num % MAX_PHOTOS));
    return num-1;
}

function rotatecarousel(MAX_DIVs) {
    if (isNaN(num = getCookie('carouselIndex')) || num >= MAX_DIVs) num = 0;
    num++;
    setCookie('carouselIndex', (num % MAX_DIVs));
    return num-1;
}

function setCookie(name, value) {
	var date = new Date();
	var exp = date.getTime() + (90 * 60 * 60 * 1000);	// expires in 90 days
	date.setTime(exp);

	document.cookie = name + "=" + value + "; expires=" + date.toGMTString();
}


function getCookie(name) {
	name = name + '=';			// MacIE includes the equals sign
	var nameLen = name.length;
	var cookie = document.cookie;
    var cookieLen = cookie.length;

	var end;
	var start;
	var i = 0;
	while (i < cookieLen) {
		start = i + nameLen;
        if (cookie.substring(i, start) == name) {
			end = cookie.indexOf(";", start);
			if (end == -1) {
				end = cookie.length;
			}
			return unescape(cookie.substring(start, end));
		}
		i = cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return ""	// cookie not found
}


// ==================== set left margin ==================== //
// Content is centered in window using CSS, but if the window is smaller than the size of the content
// the CSS will push the content too far left, causing it to partially disappear from the browser window.
// This function resets the left margin if the browser window is too small to hold the content.
function setLeft() {
	if (document.body.clientWidth) { //MacIE, Safari, WinIE
		if (document.body.clientWidth < 778) {
			if (document.getElementById) {
				document.getElementById('body').style.left = '388px';
			} else if (document.all) {
				document.all('body').style.left = '388px';
			}
		} else {
			//alert(document.getElementById('body').style.left);
			if (document.getElementById) {
				document.getElementById('body').style.left = '50%';
			} else if (document.all) {
				document.all('body').style.left = '50%';
			}
		}
	}
}



// ==================== pop-up window script ====================
var popUpWin = null;

function popUp(theURL,winName,features) {
	popUpWin = window.open(theURL,winName,features);
	if(popUpWin.focus) popUpWin.focus();
}



// ==================== photo essay script ====================
//loops through the photo essay photos from 1 to var photos
// var photos and var photoName are initialized on the calling page
function swap(img) {
	for (var i = 1; i <= photos; ++i) {
		var imgName = 'photoessay_' + photoName + '_' + i;
		document.images[imgName].src = self[imgName + '_' + img].src;
	}
}

function openPrintFriendly(url) {
	tempUrl = url;
	winLeft = (screen.width) ? (screen.width-500)/2 : 0;

    winTop = (screen.height) ? (screen.height-500)/2 : 0;

    var params = "location=no,menubar=yes,resizable=yes,scrollbars=yes,status=no,toolbar=yes,directories=no,width=650,height=500,top=" + winTop + ",left=" + winLeft;
    if (url.indexOf(".jhtml") > -1) {
        url = "/common/getprintfriendly.jsp?resource="+url;
    } else {
    if (url.indexOf("#")>-1) url = url.substring(0,url.indexOf("#"));
    if (url.indexOf("?")>-1) url += "&print=true";
    else url += "?print=true";
    }
	url+="&referer="+escape(tempUrl);
	popUpWin = window.open(url,"PFpanel",params);
}

function openEmailFriend(url, title) {
    winLeft = (screen.width) ? (screen.width-500)/2 : 0;

    winTop = (screen.height) ? (screen.height-500)/2 : 0;

    var params = "location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no,directories=no,width=600,height=400,top=" + winTop + ",left=" + winLeft;
    if (url.indexOf("#")>-1) url = url.substring(0,url.indexOf("#"));
    var urlParams = "?emailLink="+escape(url)+"&title="+title;
	popUpWin = window.open("/global/emailAFriend.jsp"+urlParams,"EAFpanel",params);
}

String.prototype.endsWith = function(test) {
    return (this.indexOf(test) > -1 && (this.indexOf(test) + test.length) == this.length);
}
String.prototype.atLocation = function(test) {
    return (this.indexOf(test) > -1 && this.endsWith(test)) || (this + "/").indexOf(test) > 01;
}

// <![CDATA[
//==================== create rollover menus ====================//
var OFFSETLEFT = 0;
var OFFSETTOP = 0;
var LENGTHOFFSET = 3
var WIDTH = 172;
var MENU6OFFSET = -77;

//========== trap for NS4 ==========//
if (document.layers) {
    OFFSETTOP += 20;
    LENGTHOFFSET += 20;
    MENU6OFFSET = -77;
}

//========== trap for Win IE ==========//
if (document.all && !window.opera) {
    OFFSETTOP += 1;
}

//========== trap for Win IE5 ==========//
if (document.all && !document.fireEvent && document.mimeType && !window.opera) {
    LENGTHOFFSET += 31;
}

//========== trap for Win IE5x ==========//
if (document.all && !document.createComment) {
    MENU6OFFSET = -77;
}

//========== trap for Mac IE5 ==========//
if (navigator.userAgent.indexOf('Mac') != -1 && navigator.userAgent.indexOf('MSIE') != -1) {
    OFFSETTOP += -1;
    LENGTHOFFSET += 20;
}

//========== trap for Safari ==========//
if (navigator.userAgent.toLowerCase().indexOf('safari') != -1) {
    LENGTHOFFSET += 25;
}

function openiWindow(url, parameters, newWindow) {
    //LeftPosition = (screen.width) ? (screen.width-700)/2 : 0;
    //TopPosition = (screen.height) ? (screen.height-500)/2 : 0;
    //settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll;

    if(newWindow) windowname = newWindow;
    else windowname = "interactives";

    win = window.open(url, windowname, parameters);
    if (win.window.focus) {
        win.window.focus();
    }
}

// external_script.js
function embedFlash(movie, id, width, height, bgcolor, flashvars, base, quality, wmode, menu){

    if(!quality) quality="high";
    if(!wmode) wmode="opaque";
    if(!menu) menu="false";

    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '" height="' + height + '" id="' + id + '" align="middle">');
    document.write('<param name="allowScriptAccess" value="sameDomain" />');
    document.write('<param name="movie" value="' + movie + '" />');
    document.write('<param name="quality" value="' + quality + '" />');
    document.write('<param name="bgcolor" value="' + bgcolor + '" />');
    document.write('<param name="wmode" value="' + wmode + '">');
    document.write('<param name="menu" value="' + menu + '" />');
    if(flashvars) document.write('<param name="flashvars" value="' + flashvars + '" />');
    if(base) document.write('<param name="base" value="' + base + '">');
    document.write('<embed src="' + movie + '"' + ((flashvars)?' flashVars="' + flashvars + '"':'') + ' quality="' + quality + '" bgcolor="' + bgcolor + '" wmode="' + wmode + '" width="' + width + '" height="' + height + '" name="' + id + '"' + ((base)?' base="' + base + '"':"") + ' align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
    document.write('</object>');
}

// Removes website from being embedded on any frames based website
    if (window.self != window.top) {
		//top.location.replace(window.location.pathname);
	}

// ]]>
