// create arrays: div ids, image names
menu = new Object();
menu.divs = new Array('pop_governor','pop_priorities','pop_services','pop_archive','pop_office');
menu.imgs = new Array('governor','priorities','services','archive','office');

function TG_Preloads() {

	if ( document.images && ( location.href != '' ) ) {
		menu.cached = new Array();
		
		for ( a=1;a<4;a++ ) { // loop through all mouseover states
			for ( b=0;b<5;b++ ) { // loop through all images
				PLarray = menu.cached[b] + '_' + a;
				
				PLarray = new Image(); PLarray.src = 'http://www.gov.state.ak.us/global/' + menu.imgs[b] + '_' + a + '.jpg';
			}
		}
	}
	
}

function TG_SwapState(imgNum,action) { // swap image function for quicklinks menu

	if ( document.images && ( location.href != '' ) ) {
	
		// return the image name that corresponds to imgNum
		imageName = menu.imgs[imgNum];
		
		// sources' file names are based on four states: base state (0), mouseover (1), active menu (2), active menu mouseover (3) :: [button name]_[state].jpg
		
		// parse out the current state number from the source path
		path = document.images[imageName].src;
		stateId = path.indexOf('.jpg') - 1;
		swapstate = path.charAt(stateId);
		
		// identify which state to switch to for mouseovers | o/on, f/off, #/absolute
		if ( action == 'o' && ( swapstate == 0 || swapstate == 2 ) ) { newstate = parseInt(swapstate) + 1; }
		else if ( action == 'f' && ( swapstate == 1 || swapstate == 3 ) ) { newstate = parseInt(swapstate) - 1; }
		else if ( action == 0,1,3 ) { newstate = action; }
	
		// the dirty deed
		document.images[imageName].src = 'http://www.gov.state.ak.us/global/' + imageName + '_' + newstate + '.jpg';

	}
	
}

function TG_MenuDrop(divNum) { // toggles appropriate image/div properties to display popout menu, forwards old browsers
		
	if ( document.getElementById || ( document.layers && ( location.href != '' ) ) ) {
	
		// return the div id that corresponds to divNum
		whichDiv = menu.divs[divNum];
		
		// choose which syntax for div style

		if ( document.getElementById ) { ss = document.getElementById(whichDiv).style; bs = document.getElementById('pop_bg').style; v = 'visible'; h = 'hidden';}
		else if ( document.layers ) { nl = document.layers['HeadDIV'].document.layers['ClipDIV']; ss = nl.document.layers[whichDiv]; bs = nl.document.layers['pop_bg']; v = 'show'; h = 'hide'; }
		
		// if the div is visible, hide it and swap the button image to state (1)
		if ( ss.visibility == v ) {
			ss.visibility = h; bs.visibility = h; TG_SwapState(divNum,1);
		}
			
		// if the div is hidden, hide all divs, swap all image states to (0), THEN show the div and swap the button image to state (3)
		else {
			for ( i=0;i<5;i++ ) {
				if ( document.getElementById ) { document.getElementById(menu.divs[i]).style.visibility = h; }
				else if ( document.layers ) { nl.document.layers[menu.divs[i]].visibility = h; }
				TG_SwapState(i,0);
			}
			ss.visibility = v; bs.visibility = v; TG_SwapState(divNum,3);	
		}
	
	}
	
	// forward old browsers
	else { location.href = "http://www.gov.state.ak.us/" + menu.imgs[divNum] + "/"; }
	
}

function TG_AbsFix(){ // don't show absolute elements until loaded
	if ( document.layers && ( location.href != '' ) ) { document.layers['HeadDIV'].visibility = 'show'; }
	else if ( document.getElementById ) { document.getElementById('HeadDIV').style.visibility = 'visible'; }
}

function doResize() { // netscape resize fix
	location.reload(); return false;
}

// Search Functions

function Load_S() { // make FieldSearch point to the search field
	if ( document.layers && ( location.href != '' ) ) { FieldSearch = document.layers['HeadDIV'].document.layers['ClipDIV'].document.layers['SearchBox'].document.forms['SearchForm'].elements[1]; }
	else if ( document.getElementById ) { FieldSearch = document.getElementById('search'); }
}

function LoadVarFieldQ() { // make FieldQ point to the hidden field which satisfies google's search syntax
	if ( document.layers && ( location.href != '' ) ) { FieldQ = document.layers['HeadDIV'].document.layers['ClipDIV'].document.layers['SearchBox'].document.forms['SearchForm'].elements['q']; }
	else if ( document.getElementById ) { FieldQ = document.getElementById('q'); }
}

function SetSearchValue() { // site restriction for google querystring
	LoadVarFieldQ(); Load_S();
	FieldQ.value = "site:www.gov.state.ak.us " + FieldSearch.value;
}

function DefaultSearch() { // restore search box value
	Load_S();
	FieldSearch.value = "site search";
}

function RestoreSearch() { // restore search value (for onblur)
	Load_S();
	if ( FieldSearch.value == "" ) { DefaultSearch(); }
}

function ClearSearch() { // clear search box value
	Load_S();
	if ( FieldSearch.value == "site search" ) { FieldSearch.value = ""; }
	else { FieldSearch.select(); }
}

function TG_ShowSearch(){ // display the search tool

	Load_S();
	
	if ( document.layers && ( location.href != '' ) ) {
		document.layers['HeadDIV'].document.layers['ClipDIV'].document.layers['SearchBox'].visibility = "visible";
		FieldSearch.focus(); FieldSearch.value = "";
	}

	else if ( document.getElementById ){
		document.getElementById('SearchBox').style.visibility = 'visible';
		FieldSearch.focus();
	}
	
	else { location.href = 'http://www.gov.state.ak.us/search/'; }

}