// JavaScript Document

//calls set up function to load after the page is done loading
window.onload = winOnLoad;

//set up global vars for the sliding arrow
var arrowEl;
var arrowWidth;
var arrowHeight;
var arrowTop;
var homeX;
var startX;
if (!defaultID) {
	var defaultID;
}
var timeOut;
var	currentIndex = 0;
var loadingText = new Array('', '.', '..', '...')
var section;

function winOnLoad( ) {
	//setting up arrow sliding
	topNavSetup();
	//set up help center rollover	
	helpCenterRollover();
	
	if ( window.xGetElementsByClassName ) {
		//set up links for top category open and close for online services and how do i's 
		setUpTopLinks();
		//set up links for top category open and close for online services and how do i's 
		var deleteIcons = xGetElementsByClassName('delete_icon')
		if (  deleteIcons != undefined ) {
			//get all elements with the class of delete_icon
			//loop over all top_cat elements and assign onclick listeners
			for( var i=0; i<deleteIcons.length; i++ ) {
				xAddEventListener(deleteIcons[i],'click', deleteCheck, false);
			}
		}
	}
}

function topNavSetup() {
		//get url for getSection() so you can find out what section you are on
		if ( section == undefined) {
			section = getSection(document.URL);
		} else if (section == 'employee') {
			section = 'nol';
		}
		//get arrow element
		arrowEl = xGetElementById('arrow');
		//get arrow attributes
		arrowWidth = xWidth(arrowEl);
		arrowHeight = xHeight(arrowEl);
		arrowTop = arrowHeight*1.5; 
		//get the offset from the left of the window to the location of where the "home" link is at 
		homeX =  xPageX('nol') - xWidth('nol')/2;
		//use the currect section, the home offset, and the arrow width to set the start position of the arrow
		startX = xPageX(section) + xWidth(section)/2 - homeX - arrowWidth/3;
		//move the arrow to startX location
		xMoveTo( arrowEl, startX, arrowTop );
		//set up event listeners for moving the arrow on mouseover to the destination position
		xAddEventListener('nol','mouseover', moveArrow, false);
		xAddEventListener('business','mouseover', moveArrow, false);
		xAddEventListener('citizen','mouseover', moveArrow, false);
		xAddEventListener('education','mouseover', moveArrow, false);
		xAddEventListener('local','mouseover', moveArrow, false);
		//set up event listeners for returning the arrow on mouseout to the start position
		xAddEventListener('nol','mouseout', returnArrow, false);
		xAddEventListener('business','mouseout', returnArrow, false);
		xAddEventListener('citizen','mouseout', returnArrow, false);
		xAddEventListener('education','mouseout', returnArrow, false);
		xAddEventListener('local','mouseout', returnArrow, false);
}

function helpCenterRollover() {
	var imgDir = '/images/';
	var imgOverSuffix = '_over';
	var imgFileExt = '.gif';
	var help_center = 'help_center_img';
	var help_center_sub = 'help_center_sub_img';
	xImgRollSetup(imgDir,imgOverSuffix,imgFileExt,help_center,help_center_sub);
}

function startLoading( bIsActive ) {

	if ( bIsActive ) {
		xDisplay('loading_container', 'block')
		if ( currentIndex == loadingText.length ) {
			currentIndex = 0;
		}
		for(var i=0; i<divsToHide.length; i++) {
			if (window.xGetElementById) {
				if (xGetElementById(divsToHide[i])) {
					xDisplay(divsToHide[i], 'none')
				}
			}
		}
		xGetElementById('loading_text').innerHTML = loadingText[currentIndex]
		currentIndex++;
		timeOut = setTimeout('startLoading(true)', 350);
	} else {
		stopLoading();
	}
}

function stopLoading() {
		window.clearTimeout(timeOut);
		for(var j=0; j<divsToHide.length; j++) {
			xDisplay(divsToHide[j], 'block')
		}
		xDisplay('loading_container', 'none')
}

function setUpTopLinks() {
	//get all elements with the class of top_cat
	var topCatLinks = xGetElementsByClassName('top_cat');
	//loop over all top_cat elements and assign onclick listeners
	for( var i=0; i<topCatLinks.length; i++ ) {
		var topId = topCatLinks[i].id;
		xAddEventListener(topId,'mousedown', openCloseTop, false);
	}
	//set default top category to show
	//if you want to set a default add the following global var into a script in the head:
	//var defaultID = 'link_someNumberHere';
	if ( defaultID ) {
		var firstLink = new String(defaultID);
	} else if (topCatLinks[0]) {
		var firstLink = topCatLinks[0].id
	} else {
		return false;
	}
	//find the first index of "_" and then get the id number which also includes the underscore in the output
	var pos = firstLink.indexOf("_");
	if ( pos != -1 ) {
		var numID = firstLink.substring(pos,firstLink.length);
	}	
	//display the default top category div container and heading (if there is one);
	xDisplay('top'+numID, 'block');
	xDisplay('heading'+numID, 'inline');
}


//getArrowStartx(), getArrowHomex(), and getArrowTop() will recalculate positions
//of the sliding arrow for the moving functions when the arrow moves after the page loads.
//It was meant to fix the problem if you resized the page, it would get the new 
//locations it needed to stay in correct relation to the top menu.
	function getArrowStartx() {
		var arrowEl = xGetElementById('arrow');
		var arrowWidth = xWidth(arrowEl);
		var homeX =  xPageX('nol') - xWidth('nol')/2;
		var startX = xPageX(section) + xWidth(section)/2 - homeX - arrowWidth/3;
		return startX;
	}
	function getArrowHomex() {
		var homeX =  xPageX('nol') - xWidth('nol')/2;
		return homeX;
	}
	function getArrowTop() {
		var arrowEl = xGetElementById('arrow');
		var arrowHeight = xHeight(arrowEl);
		var arrowTop = arrowHeight*1.5; 
		return arrowTop;
	}


function getSection( url ) {
//use the url to find the current section
	//test if url has the function inxexOf
	if ( url.indexOf ) {
		//get positions of "section=" and "&", these will be used for start positions and ending 
		//positions of the section name. 
		var pos = url.indexOf("section=");
		var pos2 = url.indexOf("&");
		//check if "section=" exists
		if ( pos != -1 ) {
			//check if "&" exists
			if ( pos2 != -1 ) {
				//if "&" exists go from the end of "section=" to the position of "&"
				var section = url.substring(pos+8,pos2);
			} else {
				//if "&" does not exist go from the end of "section=" to the end of url
				var section = url.substring(pos+8,url.length);				
			}
			
			//this was added to fix certain section issues with the main portal
			if ( section == 'employee' ) {
				return 'nol';
			} else {
				return section;
			}
		} else {
			//default
			return 'nol';
		}			
	}
}

function moveArrow(evt) {
	//get event information 
	if	(window.xEvent) {
		var e = new xEvent(evt);
	};
	//use event information to find new section to move arrow to
	if (e) {
		var elementLeft = e.pageX - e.offsetX;
		var targ = new String( e.target );
		if (window.getSection) {
			var element = getSection( targ );
		};
		var elementObj = xGetElementById(element);
		var elementWidth = xWidth( element );
		//set up positions for the move to location
		if (window.getArrowHomex) {
			homeX = getArrowHomex();
		}
		if (window.getArrowHomex) {
			var newLeft = elementLeft - homeX + elementWidth/2 - arrowWidth/3;
		}
		if (window.getArrowTop) {
			arrowTop = getArrowTop();
		}
		//slide arrow
		if (window.xSlideTo && window.getArrowTop && window.getArrowHomex && window.getSection) {
			xSlideTo( arrowEl, newLeft, arrowTop, 1000 );
		}
	}
}

function returnArrow( e ) {
	//set up location for the return to position, use the functions so you 
	//take care of arrow location incase they decided to resize window.
	if (window.getArrowTop) {
		arrowTop = getArrowTop();
	}
	if (window.getArrowStartx) {
		startX = getArrowStartx();
	}
	//slide arrow
	if (window.xSlideTo && window.arrowEl && window.startX && window.arrowTop) {
		xSlideTo(arrowEl, startX, arrowTop, 1400 );
	}
}

function openCloseTop( e ) {
//function to take care of the opening and closing of sections and headings for 
//online services and how do i categories.
	//get all elements with "top_cat" class
	var topCatLinks = xGetElementsByClassName('top_cat');
	//loop over "top_cat" elements and use the id of the elements to strip out
	//the category number which includes an underscore and then sets the corresponding
	//div element and heading to none
	for( var i=0; i<topCatLinks.length; i++ ) {
		var topID = topCatLinks[i].id
		var pos = topID.indexOf("_");
		if ( pos != -1 ) {
			var numID = topID.substring(pos,topID.length);
		}
		topCatLinks[i].className = 'top_cat contentLink'
		xDisplay('top'+numID, 'none');
		xDisplay('heading'+numID, 'none');
	}
	//gets the id of the link that you clicked
	var ele = new xEvent(e);
	ele.target.className = 'top_cat menuActive';
	var linkID = ele.target.id
	//use the link id to open the correct div element and heading
	var pos = linkID.indexOf("_");
	if ( pos != -1 ) {
		var numID = linkID.substring(pos,linkID.length);
	}			
	var topCatEle = xGetElementById('top'+numID);
	var headCatEle = xGetElementById('heading'+numID);
	xDisplay(topCatEle, 'block');
	xDisplay(headCatEle, 'inline');
}