/* ================================================================================
Author: 
	Gil Pili (Manager)
	Justin Whiting (Developer)
Project: 
	Accessibilibility GUI code
	
Description:
	PRogrammed an interaction model to have popup displays for 
	
================================================================================ */
	var FORWARD 		= "forward";
	var REVERSE 		= "reverse";
	var LOC_STRING 		= self.location.href;
	var SKIP_NAV_IDENTIFIER = "skip";
	var FRAGMENT_IDENTIFIER = LOC_STRING.indexOf("#");
	var QUERY_IDENTIFIER 	= LOC_STRING.indexOf("?");
	// for holding page state upon parameters given during execution...
	var itemCoords 		= new Array();
	var LAST_X 			= null;
	var LAST_Y 			= null;
	var LAST_ARGUMENTS  = null;
	var DEBUG_WINDOW	= null;
	var TARGET_HREF 	= null;
	var DEBUG_MODE 		= false;
	var LAST_TABBED 	= null; // reference to the last tabbed element - gets updated from the onkeypress event...
	var NEXT_TABBED 	= null;
	var ELEMS 			= new Array();
	var IS_SAFARI 		= (navigator.vendor != null && navigator.vendor.indexOf("Apple ") != -1);
	var isIE 			= document.all;
	
	
	// Function object for holding X and Y...
	function coord(X,Y){
		this.x = X;
		this.y = Y;
		return this;
		}
		
		
		
	/*
		General debugging method...
	*/	
	function DEBUG(stringMEssage){
		if(DEBUG_MODE){
			DEBUG_WINDOW.document.write("<p>" + stringMEssage + "</p>");
			}
		}
	
	
		
	/*
		Function for when loading the document.
	*/
	function setSkipNavElements(){
		/*
		if(DEBUG_MODE)DEBUG_WINDOW = window.open("","hello","");
		*/
		// Setting the global elements arry on onload...
		ELEMS[0] = self.document.anchors;
		ELEMS[1] = self.document.getElementsByTagName("input");
		ELEMS[2] = self.document.getElementsByTagName("select");
		ELEMS[3] = self.document.getElementsByTagName("radio");
		ELEMS[4] = self.document.getElementsByTagName("checkbox");
		
		/*
		// Adding event handlers - deprecated - leaving here for documentation purposes. 
		for(i = 0 ; i < ELEMS.length ; i++){
			for(j = 0 ; j < ELEMS[i].length ; j++){
				// Setting event capture.
				if(isIE){
					ELEMS[i][j].onKeyDown = updateView(ELEMS[i][j]);
					} else {
					ELEMS[i][j].addEventListener("keydown",updateView,false);
					}
				}
			}
		*/

		// inline call to bring focus to an element in the page based upon the fragment identifier...
		var linkName  	= getSelfFragmentIdentifier();
		if(linkName == null || typeof(linkName) == "undefined")return;
		var element 	= getHrefElementByName(linkName,self.name);
		if(element == null || typeof(element) == "undefined")return;
			// sort of a double call here - firefox bug...debugging...
		showMe(element);
		element.focus();
		}

	
	
	
	/*
		Stub function to set everything on the page to non-visible in the page view port.
	*/
	function updateView(evt){	
		// set erefernce to the actual objetc calling...
		var element = (isIE && !IS_SAFARI)? evt.srcElement : evt.target ;
		//if(IS_SAFARI)element = evt.currentTarget;
		var att = element.target;
		var key = parseInt(evt.keyCode);
		// up and dwn arrows
		if((IS_SAFARI && key == 0) || key == 13 ||
			key == 9 || key == 40 || key == 38 ||
			(evt.shiftKey && key == 25) 
			){
				//alert("FRAMED");
			hideMe(element);
			//element.blur();
			// enter...
			} 
		}
		/*
		DEBUG("updateView: Calling showMe from " + element.tagName );
		// FIREFOX WORK AROUND...calls blur on element...
		// if the user tabs to an element that is not a skip nav element hide all the skip nav elements...
		// LAST_TABBED.blur();
		// hideMe(LAST_TABBED);
		if(element.getAttribute("class") != SKIP_NAV_IDENTIFIER){
			element.blur();
			for(j = 0 ; j < ELEMS[0].length ; j++){
				if(ELEMS[0][j].getAttribute("class") == SKIP_NAV_IDENTIFIER && ELEMS[0][j] == LAST_TABBED){
					ELEMS[0][j].blur();
					hideMe(ELEMS[0][j]);
					return;
					alert(event.keyCode);
					}
				}
			}
		}
	
	
	
	/*
		Function to show an element - if the X and Y (optional params) are present the item will be shown using abs positioning.
		If not it will be presented inline (static positioning)
	*/
	function showMe(objRef,X,Y){
		//if(LAST_TABBED != null)hideMe(LAST_TABBED);
		if(objRef.nodeType == 3)objRef = objRef.parentNode;
		var isAbs = true;
		/* DEBUG("SHOWME: " + objRef.innerHTML + "\nX:" + X + "\nY:" + Y); */
		LAST_TABBED = objRef;
		// if(document.all && LAST_TABBED != null)objRef.focus();
		// this was for setting the X and Y attributes on the tag itself...
		/*if(itemCoords[objRef] != null){
			X = itemCoords[objRef].x;
			Y = itemCoords[objRef].y;
			} else {
			var undefined = false;
			*/
			// eventually set in an XML style...?
			if((X == null || typeof(X) == "undefined") && (Y == null || typeof(Y) == "undefined")){
				isAbs 		= false;
				undefined 	= true;
				/*DEBUG("BOTH TRUE: " + undefined);*/
				return;
				}
			if(!undefined){
				itemCoords[objRef] = new coord(X,Y);
				} else {
				// if no coords given - return...
				// developer debugging...
				/*DEBUG("No X,Y coordinates have been gven for the " + objRef.name + " element.");*/
				}
			//}
			
		LAST_ARGUMENTS 	= showMe.arguments;
		LAST_X 			= X;
		LAST_Y 			= Y;
		LAST_TABBED 	= objRef;
		//NEXT_TABBED 	= getHrefElementByName(getFragmentString(objRef.href));
		
		// if frames... this needs to be extracted - talk to Gil...
		var isNavFrame 	= (top.frames && top.frames.length > 0);
		
		// everything is now absolute - as per Gil.\
		if(self.parent && self.name == "nav"){
			if(isNavFrame)top.rows = "100,*";
			}
			
		isAbs = true;
		// setting dynamic attributes...
		objRef.style.visibility = "visible";
		objRef.style.width 		= "auto";
		objRef.style.height 	= "auto";
		objRef.style.position	= ( isAbs )? "absolute" : "relative" ;
		//if(isIE)objRef.style.position = "relative";
		objRef.style.display 	= "block";
		X = (objRef.parentNode.offsetLeft <= 0)? X : objRef.parentNode.offsetLeft + X;
		Y = (objRef.parentNode.offsetTop <= 0)? Y : objRef.parentNode.offsetTop + Y;
		objRef.style.top 	= (isAbs)? Y + "px" : "1px";
		objRef.style.left 	= (isAbs)? X + "px" : "1px";
		objRef.style.zIndex 	= 999999999;
		// if not the nav bar - scroll to...
		if(self.name != "nav"){
			window.scrollTo(objRef.style.left,objRef.style.top);
			}
		//objRef.focus();
		}
		
		
		
	/*
		Function to hide an element.
	*/
	function hideMe(objRef){
		// DEBUG("<p>Calling hideMe from " + objRef.name + "</p>");
		objRef.style.display 	= "block";
		objRef.style.position 	= "absolute";
		objRef.style.zIndex 	= 99999999999999;
		objRef.style.left 		= "-3000px";
		objRef.style.top 		= "-3000px";
		}
	
	
	
	
	/*
		Returns a reference to the HREF with the name given. If the frame name is given the reference will be to the 
		a tag in the given frame.
	*/
	function getHrefElementByName(name,frameName){
		var a_Elements = null;
		//alert("getHrefElementByName doing self thing");
		var anchorArray = (frameName == null || typeof(frameName) == "undefined")? self.document.anchors : getFrame(frameName).document.anchors ;
		var found = null;
		for(n = 0 ; n < anchorArray.length ; n++){
			if(anchorArray[n].name == name){
				found = anchorArray[n];
				break;
				}
			}
		return found;
		}
	
	
	
	/*
		Abstracted this in case eventually have a hierarchy of frames.
	*/
	function getFrame(name){
		if(name == self.name)return self;
		return top.frames[name];
		}
	
	
	
	/*
		Generic function to bring the frame indicated (optional parameter - if not given the default is "_self")
		to the fragment identifier .
	*/
	function showContentFragment(objRef){
		LAST_TABBED 	= objRef;
		objRef 			= (objRef.nodeType == 3)? objRef.parentNode : objRef ; 
		// setting variables from HTML configuration...
		var targetAtt 	= objRef.target;
		var hrefString  = getFragmentString(objRef.href);
		var target_href;
		var isFrameLink = (targetAtt != null && typeof(targetAtt) != "udefined" && getFrame(targetAtt) != null && self.parent && self.parent.frames && self.parent.frames.length > 1);
		var FRAME_REF 	= (isFrameLink)? getFrame(targetAtt) : self ;
		/*DEBUG("SELF: " + self.location.href + "\nHASH: " + hrefString);*/

		// if a frame link...
		if(isFrameLink){
			
				var frameInstance = getFrame(targetAtt);
				// and frame is not present..
				if(frameInstance == null){
					alert("frame is null - please update the frame name");
					return true;
					}
				objRef.blur();
				frameInstance.location.hash = hrefString;
				//if(!isIE)frameInstance.location.href = frameInstance.location.href;
				target_href = getHrefElementByName(hrefString,targetAtt);
				if(target_href != null && typeof(target_href) != "undefined"){
					target_href.focus();
					if(target_href.className.indexOf("skip") == -1){
						// return now...
						return false;
						}
					} else {
					alert("target link does not exist: " +  objRef.target +" : "+ objRef.name + " : " + target_href);
					}
				if(frameInstance.showMe)frameInstance.showMe(target_href);
				
			} else {
				
				// not a frame...
				//alert("NOT FRAMED");
				objRef.blur();
				self.location.hash = hrefString;
				//if(!isIE)self.location.href = self.location.href;
				target_href = getHrefElementByName(hrefString,null);
				if(target_href != null && typeof(target_href) != "undefined"){
					target_href.focus();
					showMe(target_href);
					} else {
					// TODO: maybe add this back in????
					// alert("target link does not exist: " +  objRef.target +" : "+ objRef.name + " : " + target_href);
					}
				hideMe(objRef);
				//objRef.window.blur();
				
			}
			
		// should page link continue to reload...
		return false;
		}
	
	
	
	/*
		Function to get the locations fragment id - some browsers facilitated the location's method incorrect.
	*/
	function getFragmentString(refSTring){
		if(refSTring == null || typeof(refSTring) == "undefined")return "";
		if(refSTring.indexOf("#") == -1)return "";
		var fragName = "";
		if(refSTring.indexOf(".") != -1 || refSTring.indexOf("/") != -1 ){
			// more tan just relative document path...
			var loc 	= refSTring.split("#");
			fragName 	= loc[1];
			} else {
			fragName 	= refSTring.replace("#","");
			}
		return fragName;
		}
	
	// recursive function to get left...
	function getOffSetLeft(node){
		var X = 0;
		var nodeInstance = node;
		while(true){
			if(nodeInstance.tagName == "html" || nodeInstance.tagName == "body" || nodeInstance.window)return X;
			X += (nodeInstance.offsetLeft != null && typeof(nodeInstance.offsetLeft) != "undefined")? nodeInstance.offsetLeft : 0 ;
			nodeInstance = nodeInstance.parentNode;
			}
		}
	
	/*
		Function to return the fragment id for self.
	*/
	function getSelfFragmentIdentifier(){
		var endIndex = (QUERY_IDENTIFIER == -1)? LOC_STRING.length : QUERY_IDENTIFIER ;
		return LOC_STRING.substring(FRAGMENT_IDENTIFIER,endIndex).replace("#","");
		}
	