var mastheadLeftImg = new Image();
var mastheadCenterImg = new Image();
var mastheadRightImg = new Image();
var strFactoid = new Array(6)
var strCurrFactoid;
var objTmrFactoid;
var objTmrWipe;
var bolTmrFactoidRunning;
var bolTmrWipeRunning;
var lngCurrFactoid = 0;
var lngFactoidCursor = 1;
var bgcolor = "#FFFFFF";
var fcolor = "#666666";
var steps = 10;
var show = 15000;
var sleep = 30;
var loop = true;
var childWin;

mastheadLeftImg.src = "images/left_masthead_image.jpg";
mastheadCenterImg.src = "images/center_masthead_image.jpg";
mastheadRightImg.src = "images/right_masthead_image.jpg";

strFactoid[0] = "FAS employs about 420 people covering food and agricultural production, consumer trends, and trade issues in about 130 countries."
strFactoid[1] = "U.S. agricultural exports support about 925,650 jobs."
strFactoid[2] = "About a fourth of total farm cash receipts come from exports."
strFactoid[3] = "The United States contributes over half of the world's food aid."
strFactoid[4] = "FAS routinely forecasts worldwide crop production."
strFactoid[5] = "Since 1984, using the Cochran Program, FAS has provided agricultural training to more than 8,400 participants from over 85 countries."
strFactoid[6] = "The United States exports more meat than steel, more corn than cosmetics and more fruits and vegetables than household appliances."


			strCurrFactoid = strFactoid[lngCurrFactoid];

			// Load the three masthead images.
			function loadMastheadImages()
			{
				document["leftMastheadImage"].src = mastheadLeftImg.src;
				document["centerMastheadImage"].src = mastheadCenterImg.src;
				document["rightMastheadImage"].src = mastheadRightImg.src;
			}

			// Stop the timer. Start it. Set its interval to 20 seconds.
			function startTmrFactoid()
			{
				// Make sure the timer is stopped.
				stopTmrFactoid();
				objTmrFactoid = setInterval("rotateFactoid()", 20000);
				bolTmrFactoidRunning = true;
			}

			// Stop the factoid timer.
			function stopTmrFactoid()
			{
				if (bolTmrFactoidRunning)
				{
					clearInterval(objTmrFactoid);
					bolTmrFactoidRunning = false;
				}
			}

			// Wipe in the factoid.
			function startTmrWipe()
			{
				stopTmrWipe();
				objTmrWipe = setInterval("wipeFactoid()", 20);
				bolTmrWipeRunning = true;
			}

			// Stop the wipe timer.
			function stopTmrWipe()
			{
				if (lngFactoidCursor == strCurrFactoid.length)
				{
					//JSFX_StartEffects();
				}
				if (bolTmrWipeRunning)
				{
					clearInterval(objTmrWipe);
					bolTmrWipeRunning = false;
				}
			}

			// Switch out the factiod text.
			function rotateFactoid()
			{
				strCurrFactoid = strFactoid[lngCurrFactoid];
				startTmrWipe();
			}

			// Wipe in the new factiod text.
			function wipeFactoid()
			{
				if (lngFactoidCursor <= strCurrFactoid.length)
				{
					document.getElementById("factoid").innerHTML = strCurrFactoid.substr(0, lngFactoidCursor);
					lngFactoidCursor = lngFactoidCursor + 1;
				}
				else
				{
					stopTmrWipe();
					lngFactoidCursor = 1
					if (lngCurrFactoid < 6)
					{
						lngCurrFactoid = lngCurrFactoid + 1
					}
					else
					{
						lngCurrFactoid = 0
					}
				}
			}

			// Open a new child window or close and reopen an existing child window.
			function childWindow(strURL)
			{
				if (childWin == null)
				{
					childWin = window.open(strURL,"", "width=734,height=400,scrollbars=yes,left=0,top=0");
				}
				else
				{
					if (childWin.closed == false)
					{
						childWin.close();
					}
					childWin = window.open(strURL,"", "width=734,height=400,scrollbars=yes,left=0,top=0");
				}
			}

			var colors = new Array(steps);
			getFadeColors(bgcolor,fcolor,colors);
			var color = 0;
			var text = 0;
			var step = 1;

			// fade: magic factoid function
			function fade()
			{

			// actually write message to document
			document.getElementById("factoid").innerHTML = strFactoid[text];
			document.getElementById("factoid").style.color = colors[color];

			// select next factoid color
			color += step; 

			// completely faded in?
			if (color >= colors.length-1)
			{
				step = -1; // traverse colors array backward to fade out
			}

			// stop at last message if loop=false
			if (!loop && text >= strFactoid.length-1)
			{
				return; // loop should be defined in user script, e.g.: var loop=true;
			}

			// completely faded out?
			if (color == 0)
			{
				step = 1; // traverse colors array forward to fade in again

				// select next message
				text += 1;

				if (text == strFactoid.length)
				{
					text = 0; // loop back to first message
				}
			}

			// subtle timing logic...
			setTimeout("fade()", (color == colors.length-2 && step == -1) ? show : ((color == 1 && step == 1) ? sleep : 50)); // sleep and show should be defined in user script, e.g.: var sleep=30; var show=500;
			}

			// getFadeColors: fills Colors (predefined Array)
			// with color hex strings fading from ColorA to ColorB
			// note: Colors.length equals the number of steps to fade
			function getFadeColors(ColorA, ColorB, Colors)
			{
				len = Colors.length; 

				// strip '#' signs if present 
				if (ColorA.charAt(0)=='#') ColorA = ColorA.substring(1);
				if (ColorB.charAt(0)=='#') ColorB = ColorB.substring(1);

				// substract rgb compents from hex string 
				var r = HexToInt(ColorA.substring(0,2));
				var g = HexToInt(ColorA.substring(2,4));
				var b = HexToInt(ColorA.substring(4,6));
				var r2 = HexToInt(ColorB.substring(0,2));
				var g2 = HexToInt(ColorB.substring(2,4));
				var b2 = HexToInt(ColorB.substring(4,6));

				// calculate size of step for each color component
				var rStep = Math.round((r2 - r) / len);
				var gStep = Math.round((g2 - g) / len);
				var bStep = Math.round((b2 - b) / len);

				// fill Colors array with factoid colors
				for (i = 0; i < len - 1; i ++)
				{
					Colors[i] = "#" + IntToHex(r) + IntToHex(g) + IntToHex(b);
					r += rStep;
					g += gStep;
					b += bStep;
				}
				Colors[len-1] = ColorB; // make sure we finish exactly at ColorB
			}

			// IntToHex: converts integers between 0-255 into a two digit hex string.
			function IntToHex(n)
			{
				var result = n.toString(16);
				
				if (result.length == 1)
					result = "0" + result;
				return result;
			}

			// HexToInt: converts two digit hex strings into integer.
			function HexToInt(hex)
			{
				return parseInt(hex, 16);
			}

			// Change the page's location to the passed HREF.
			function goToURL(strHREF)
			{
				window.location.href = strHREF
			}

			//function setFactoidSwitch()
			//{
			//	if (document.factoid_form.switch_state.value == "1")
			//	{
			//		document.factoid_form.switch_state.value = "0";
			//	}
			//	else
			//	{
			//		document.factoid_form.switch_state.value = "1";
			//	}
			//	document.factoid_form.submit();
			//}

			function browseToNavItem(strDestination, strURL)
			{
				document.getElementById("destination").value = strDestination;
				document.left_navigation.action = strURL;
				document.left_navigation.submit();
			}

			function setNavItemState(strNavItem, strState)
			{
				if (strState == "on")
				{
					document.getElementById("nav_item_" + strNavItem).style.backgroundColor = "#C5C5C5";
					document.getElementById("nav_item_" + strNavItem).style.cursor = "hand";
					document.getElementById("nav_item_caption_" + strNavItem).style.color = "#000000";
					document.getElementById("nav_item_caption_" + strNavItem).style.paddingLeft = "8px";
					document.getElementById("nav_item_caption_" + strNavItem).style.paddingRight = "8px";
					document.getElementById("nav_item_outer_rect_" + strNavItem).style.borderLeftWidth = "1px";
					document.getElementById("nav_item_outer_rect_" + strNavItem).style.borderRightWidth = "1px";
					document.getElementById("nav_item_middle_rect_" + strNavItem).style.borderLeftWidth = "1px";
					document.getElementById("nav_item_middle_rect_" + strNavItem).style.borderRightWidth = "1px";
					document.getElementById("nav_item_middle_rect_" + strNavItem).style.borderTopColor = "#909090";
					document.getElementById("nav_item_middle_rect_" + strNavItem).style.borderBottomColor = "#D9D9D9";
					document.getElementById("nav_item_inner_rect_" + strNavItem).style.borderLeftWidth = "1px";
					document.getElementById("nav_item_inner_rect_" + strNavItem).style.borderRightWidth = "1px";
					document.getElementById("nav_item_inner_rect_" + strNavItem).style.borderTopColor = "#B4B4B4";
					document.getElementById("nav_item_inner_rect_" + strNavItem).style.borderBottomColor = "#CDCDCD";
				}
				else
				{
					document.getElementById("nav_item_" + strNavItem).style.backgroundColor = "#838595";
					document.getElementById("nav_item_" + strNavItem).style.cursor = "default";
					document.getElementById("nav_item_caption_" + strNavItem).style.color = "#F0F0F0";
					document.getElementById("nav_item_caption_" + strNavItem).style.paddingLeft = "11px";
					document.getElementById("nav_item_caption_" + strNavItem).style.paddingRight = "11px";
					document.getElementById("nav_item_outer_rect_" + strNavItem).style.borderLeftWidth = "0px";
					document.getElementById("nav_item_outer_rect_" + strNavItem).style.borderRightWidth = "0px";
					document.getElementById("nav_item_middle_rect_" + strNavItem).style.borderLeftWidth = "0px";
					document.getElementById("nav_item_middle_rect_" + strNavItem).style.borderRightWidth = "0px";
					document.getElementById("nav_item_middle_rect_" + strNavItem).style.borderTopColor = "#AEAFBA";
					document.getElementById("nav_item_middle_rect_" + strNavItem).style.borderBottomColor = "#60626D";
					document.getElementById("nav_item_inner_rect_" + strNavItem).style.borderLeftWidth = "0px";
					document.getElementById("nav_item_inner_rect_" + strNavItem).style.borderRightWidth = "0px";
					document.getElementById("nav_item_inner_rect_" + strNavItem).style.borderTopColor = "#9495A3";
					document.getElementById("nav_item_inner_rect_" + strNavItem).style.borderBottomColor = "#787A88";
				}
			}

			function performSearch()
			{
				document.QueryForm.method = "GET";
				document.QueryForm.action = "http://www.fas.usda.gov/search.asp";
				if (document.getElementById("search_usda").checked)
				{
					document.getElementById("site").value = "default_collection";
				}
				return true;
			}