//------------------------------------------------------------------------------------------------------
//
//START Main Page javascript Functions
//
//------------------------------------------------------------------------------------------------------


	//------------------------------------------------------------------------------------------------------
	//
	//START Global Variables
	//
	//------------------------------------------------------------------------------------------------------
		var ShiftDown			=	false;	//false when shift is not being held down, true otherwise
		var CtrlDown			=	false;	//false when ctrl is not being held down, true otherwise
		var AltDown				=	false;	//false when alt is not being held down, true otherwise
		var MenuIndex			=	0;		//keeps track of position in menu
		var SelectedMenu		=	"";		//keeps track of selected menu id
		var MenuItemBG			=	"#ECF1EF";
		var MenuHeaderBG		=	"white";
		var	MenuHeaderSel		=	"orange";
		var MenuItemSel			=	"red";

	//-----------------------------------------------------------------
	//
	//End Global Variables
	//
	//-----------------------------------------------------------------





	//------------------------------------------------------------------------------------------------------
	//
	//START Global Code
	//
	//------------------------------------------------------------------------------------------------------
		if(!document.activeElement) 
		{
			try
			{
				document.addEventListener("focus",_dom_trackActiveElement,true);
				document.addEventListener("blur",_dom_trackActiveElementLost,true);	
			}
			catch (err)
			{
			}

		}
		
	//-----------------------------------------------------------------
	//
	//End Global Code
	//
	//-----------------------------------------------------------------




	//------------------------------------------------------------------------------------------------------
	//
	//START Onload Events
	//
	//------------------------------------------------------------------------------------------------------
		//------------------------------------------------------------------------------------------------------
		//Things that need to be done after page loads 
		//------------------------------------------------------------------------------------------------------
		function AfterPageLoad()
		{

			//to apply transparencies to IE 6 and below for PNG
			$("#MainLogo").supersleight();
			$("#PrintLogo").supersleight();
			$("#PrintLogo").css("width", "16px");
			$("#PrintLogo").css("height", "16px");
			$("#PrintLogo").css("border", "none");
		}

	//-----------------------------------------------------------------
	//
	//End onload Events
	//
	//-----------------------------------------------------------------





	//------------------------------------------------------------------------------------------------------
	//
	//START General events
	//
	//------------------------------------------------------------------------------------------------------

		//------------------------------------------------------------------------------------------------------
		//Creates a pop up window 
		//------------------------------------------------------------------------------------------------------
		function newPopup(url, Width, Height) {
			popupWindow = window.open(
				url,'popUpWindow','height=' + Height + ',width=' + Width + ',left=10,top=10,resizable=0,scrollbars=1,toolbar=0,menubar=1,location=0,directories=0,status=0')
		}

		//------------------------------------------------------------------------------------------------------
		//Enables 
		//------------------------------------------------------------------------------------------------------
		function _dom_trackActiveElement(evt)
		{
			if(evt && evt.target) 
			{ 
				try
				{
					document.activeElement = evt.target == document ? null : evt.target;
				}
				catch (err)
				{
				}
				
			}
		}

		function _dom_trackActiveElementLost(evt) 
		{ 
			try
			{
				document.activeElement = null;				
			}
			catch (err)
			{
			}

		}

		
		//------------------------------------------------------------------------------------------------------
		//Checks which key was let up and does what needs to be done for it
		//------------------------------------------------------------------------------------------------------
		function CheckKeyUp(e)
		{
			var keynum;
			keynum = e.keyCode;

			if(keynum == 16)
			{
				ShiftDown = false;
			}
			else if(keynum == 17)
			{
				CtrlDown = false;
			}
			else if(keynum == 18)
			{
				AltDown = false;
			}
		}

		//------------------------------------------------------------------------------------------------------
		//checks to see what key was pressed and if anything needs to be done about it
		//------------------------------------------------------------------------------------------------------
		function CheckKeyDown(e)
		{
			var keynum;
			keynum = e.keyCode;

			//if shift is pressed, then assumes shift is being held until released
			if(keynum == 16)
			{
				ShiftDown = true;
				return true;
			}
			//if ctrl is pressed, then assumes ctrl is being held until released
			else if(keynum == 17)
			{
				CtrlDown = true;
				return true;
			}
			//if ctrl is pressed, then assumes ctrl is being held until released
			else if(keynum == 18)
			{
					AltDown = true;
				return true;
			}
		}


		//------------------------------------------------------------------------------------------------------
		//returns all menu styles back to original styles
		//------------------------------------------------------------------------------------------------------
		function ClearMenuSelection()
		{
		
			ActiveElement = $(document.activeElement);

			MenuIndex = -1;
			SelectedMenu = "";
			$(".Item").css("visibility", "");
			$(".Item").css("display", "");
			$(".Item").css("background", MenuItemBG);
			$(".Header").css("background", "");
			
			if(ActiveElement.attr("type") != "textbox" && ActiveElement.attr("class") != "SelectBox")
			{
				ActiveElement.blur();
			}
		}


		//------------------------------------------------------------------------------------------------------
		//Processes key commands for any navigation that may be necesary
		//------------------------------------------------------------------------------------------------------
		function PageNavigation(e)
		{			
			var ActiveElement, keynum;
			ActiveElement = $(document.activeElement);
			
			keynum = e.keyCode;
	
			//hides all menu items if escape button is pressed
			if(keynum == 27)
			{
				$(".Item").css("visibility", "");
				$(".Item").css("display", "");
				$(".Item").css("background", MenuItemBG);
				$(".Header").css("background", "");
				ActiveElement.blur();
			}

			//navigation for when ctrl is being held down
			if(CtrlDown)
			{
				//if `(grave accent) is pressed then drops focus from everything
				if(keynum == 192)
				{
					ActiveElement.blur();
				}
			}
			//navigation for when shift is being held down
			else if(ShiftDown)
			{
				//processes tabbing through elements
				if(keynum == 9)
				{
					var ActiveID = ActiveElement.attr("id");
					$("ul .Item").css("visibility", "");
					$("ul .Item").css("display", "");
					$("ul .Header").css("background", "");
					$("ul .Item").css("background", MenuItemBG);
					MenuIndex = -1;
					SelectedMenu = "";

					if(ActiveID == "About")
					{
						$("#AboutList > .Item").css("visibility", "visible");
						$("#AboutList > .Item").css("display", "block");
						$("#AboutList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "AboutList";
					}
					else if(ActiveID == "EText")
					{
						$("#ETextList > .Item").css("visibility", "visible");
						$("#ETextList > .Item").css("display", "block");
						$("#ETextList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "ETextList";
					}
					else if(ActiveID == "Braille")
					{
						$("#BrailleList > .Item").css("visibility", "visible");
						$("#BrailleList > .Item").css("display", "block");
						$("#BrailleList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "BrailleList";
					}
					else if(ActiveID == "FAQ")
					{
						$("#FAQList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "FAQ";
					}
					else if(ActiveID == "Login")
					{
						$("#LoginList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "Login";
					}
				}
			}
			//navigation for when alt is being held down
			else if(AltDown)
			{

			}
			else
			{
				//processes tabbing through elements
				if(keynum == 9)
				{
					var ActiveID = ActiveElement.attr("id");
					$("ul .Item").css("visibility", "");
					$("ul .Item").css("display", "");
					$("ul .Header").css("background", "");
					$("ul .Item").css("background", MenuItemBG);
					MenuIndex = -1;
					SelectedMenu = "";

					if(ActiveID == "About")
					{
						$("#AboutList > .Item").css("visibility", "visible");
						$("#AboutList > .Item").css("display", "block");
						$("#AboutList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "AboutList";
					}
					else if(ActiveID == "EText")
					{
						$("#ETextList > .Item").css("visibility", "visible");
						$("#ETextList > .Item").css("display", "block");
						$("#ETextList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "ETextList";
					}
					else if(ActiveID == "Braille")
					{
						$("#BrailleList > .Item").css("visibility", "visible");
						$("#BrailleList > .Item").css("display", "block");
						$("#BrailleList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "BrailleList";
					}
					else if(ActiveID == "FAQ")
					{
						$("#FAQList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "FAQ";
					}
					else if(ActiveID == "Login")
					{
						$("#LoginList > .Header").css("background", MenuHeaderSel);
						SelectedMenu = "Login";
					}
				}
			}	
		}

		//------------------------------------------------------------------------------------------------------
		//Detects if user pressed the enter button for earlier versions of IE(since IE is stupid)
		// and apparently people are still using IE 6 for some god awfule reason
		//------------------------------------------------------------------------------------------------------
		function CheckForEnter(e)
		{
			
			if(typeof(e) == "undefined")
			{
				e = event;
			}


			var keynum = e.keyCode;

			if(SelectedMenu != "" && keynum == 13)
			{
				var ListSize = 0;
				ListSize = $("#" + SelectedMenu + " > .Item").length;
				
				//sets last menu item to default color
				$("#" + SelectedMenu).find(".Item").eq(MenuIndex).css("background", MenuItemBG);

					
				//if list has more than zero elements then it means user can click elements
				//inside of list, else it means list header is clickable
				if(ListSize > 0)
				{
					GoHere($("#" + SelectedMenu).find(".Item a").eq(MenuIndex).attr("href"));
					//alert($("#" + SelectedMenu).find(".Item a").eq(MenuIndex).attr("href"));
					//$("#" + SelectedMenu).find(".Item a").eq(MenuIndex).focus();
					//$("#" + SelectedMenu).find(".Item a").eq(MenuIndex).click();
				}
				else
				{
					$("#" + SelectedMenu).find("a").click();
				}
				
				//sets selected menu option to red
				$("#" + SelectedMenu).find(".Item").eq(MenuIndex).css("background", MenuItemSel);

			}

		}


		//------------------------------------------------------------------------------------------------------
		//Processes key commands for any navigation that may be necesary
		//------------------------------------------------------------------------------------------------------
		function MenuNavigation(e)
		{
						
			if(typeof(e) == "undefined")
			{
				e = event;
			}


			var  ActiveElement, keynum;
			ActiveElement = $(document.activeElement);
		
			keynum = e.keyCode;

			//navigation for when ctrl is being held down
			if(CtrlDown)
			{
				//if `(grave accent) is pressed then drops focus from everything
				if(keynum == 192)
				{
					ActiveElement.blur();
				}
			}
			//navigation for when shift is being held down
			else if(ShiftDown)
			{
				
			}
			//navigation for when alt is being held down
			else if(AltDown)
			{
				
			}
			else
			{
				if(SelectedMenu != "" && keynum != 9 && keynum != 27)
				{
					var ListSize = 0;
					ListSize = $("#" + SelectedMenu + " > .Item").length;	
					$("#" + SelectedMenu).find(".Item").eq(MenuIndex).css("background", MenuItemBG);
					/*
					code moved to keypress event since it fires for enter in all browsers
					(mainly IE 6 and below)  Commented out in case I am wrong
					//clicks current menu item they are on
					if(keynum == 13)
					{
						
						//if list has more than zero elements then it means user can click elements
						//inside of list, else it means list header is clickable
						if(ListSize > 0)
						{
							
							$("#" + SelectedMenu).find(".Item a").eq(MenuIndex).focus();
							$("#" + SelectedMenu).find(".Item a").eq(MenuIndex).click();
						}
						else
						{
							$("#" + SelectedMenu).find("a").click();
						}
						
					}
					*/
					//up arrow key was pressed
					if(keynum == 38)
					{
						if(MenuIndex <= 0)
						{
							MenuIndex = ListSize - 1;
						}
						else
						{
							MenuIndex--;
						}
					}
					//down arrow key was pressed
					else if(keynum == 40)
					{
						if(MenuIndex >= ListSize - 1)
						{
							MenuIndex = 0;
						}
						else
						{
							MenuIndex++;
						}
					}

					$("#" + SelectedMenu).find(".Item").eq(MenuIndex).css("background", MenuItemSel);

				}
			}	
		}
	//-----------------------------------------------------------------
	//
	//End General events
	//
	//-----------------------------------------------------------------



	//------------------------------------------------------------------------------------------------------
	//
	//START General functions
	//
	//------------------------------------------------------------------------------------------------------
		//------------------------------------------------------------------------------------------------------
		//Takes user to passed url
		//usually used in the 508 compliant pages
		//------------------------------------------------------------------------------------------------------
		function GoHere(Url)
		{
		  document.location.href = Url;
		}
	//-----------------------------------------------------------------
	//
	//End General functions
	//
	//-----------------------------------------------------------------


//------------------------------------------------------------------------------------------------------
//
//END Main Page javascript Functions
//
//------------------------------------------------------------------------------------------------------

