//**		Collapsible Menu Script
//**		Sept 16, 2002
//

//	Script that handles showing/hiding collapsible items in a list, by
//	showing/hiding the layers, and repositioning the other layers in the list.

//	Each menu item consists of two layers, one contains 
//	the title, and is always visible, the second is named 
//	the same as the title layer, but with a "_main" 
//	appended to the name.  The second layer contains the 
//	part of the menu item that will be hidden/shown.
//	An object named "cMenuObj" needs to be created
//	that contains properties named for each of the title 
//	layers.
//
//	Revision
//	1.0		Aug 23, 2002	J.Vose	Original
//	1.1		Sep 16, 2002	J.Vose	Updated checkIfOpen() function

//	Used to track the highest position on the page that the footer 
//	should be positioned to, set in init based on the length of the 
//	left and right columns
var minimumFooterBottom=0;

//	Function that initializes the menu, should be called by 'onload'
function initCollapseMenu(){
	if(!cMenuObj||(typeof cMenuObj!="object"))return;
	
	var titleLayer;var mainLayer;var mlayer;
		
	//	Check to see if START_TOP is set to the name of an object,
	//	if it is, try to get the top position of the object and 
	//	set START_TOP to that position
	if(typeof START_TOP=="string"){
		var startTopObj=findObj(document,START_TOP);
		if(startTopObj)START_TOP=getAbsTop(startTopObj)+getHeight(startTopObj);
		else START_TOP=100;
		}
	START_TOP+=TOP_OFFSET;
	
	//	Check to see if START_LEFT is set to the name of an object,
	//	if it is, try to get the left position of the object]
	if(typeof START_LEFT=="string"){
		var startLeftObj=findObj(document,START_LEFT);
		if(startLeftObj)START_LEFT=getAbsLeft(startLeftObj);
		else START_LEFT=100;
		}
	START_LEFT+=LEFT_OFFSET;

	//	Calculate the minimum top pos for the footer layer, based
	//	on spacer graphics at the end of the left and right columns
	
	//	Get left column bottom position
	var leftColBotImg=findObj(document,"LeftColBottomSpacer");
	if(leftColBotImg)leftColBot=getAbsTop(leftColBotImg);
	else leftColBot=0;
	
	//	Get right column bottom position
	var rightColBotImg=findObj(document,"RightColBottomSpacer");
	if(rightColBotImg)rightColBot=getAbsTop(rightColBotImg);
	else rightColBot=0;
	
	//	Set min footer bottom to bottom of column that is longest
	minimumFooterBottom=((leftColBot>rightColBot)?leftColBot:rightColBot);
	
	//	Get references to the menu title layers
	cMenuObj=findObj(document,cMenuObj);
	
	//	Cycle through title layers, get references to 
	//	main layers and get positions, save to object,
	//	check if menu item is supposed to be open
	for(layr in cMenuObj){
		if(cMenuObj[layr]){
			//	Get reference to main layer
			mlayer=layr+"_main";
			mlayer=findObj(document,mlayer);
			if(mlayer){
				//	Create Object with references to layers and top/height info for each
				titleLayer={layer:cMenuObj[layr],top:getAbsTop(cMenuObj[layr]),height:getHeight(cMenuObj[layr])};
				mainLayer={layer:mlayer,top:getAbsTop(mlayer),height:getHeight(mlayer)};
				cMenuObj[layr]={title:titleLayer,main:mainLayer,changed:true,visible:checkIfOpen(layr)};
				}
			}
		}

	//	Try to get reference to footer layer and add to cMenuObj
	cMenuObj.footerLayer=findObj(document,"footerLayer");

	//	Position and Show/Hide layers
	positionMenuLayers();
	
	//	Now that footer is positioned, show it
	if(cMenuObj.footerLayer&&(typeof cMenuObj.footerLayer=="object"))showLayer(cMenuObj.footerLayer);
	
	loadDiv = findObj(document,"loadingDiv");
	if(loadDiv)hideLayer(loadDiv);
	};

//	flag indicating menu is being initialized, used by positionMenuLayers()
var menuInitialized=false;
	
//	Function that show/hides and positoins all layers
function positionMenuLayers(){
	if(!cMenuObj||(typeof cMenuObj!="object"))return;
	
	var curPos=START_TOP;var isChange=false;
	
	//	Cycle through and position layers
	for(layr in cMenuObj){
		if(layr=="footerLayer")continue;//	Skip footer layer, position last
		lay=cMenuObj[layr];
		//	Only start position layers when
		//	we find a layer that has changed
		if(lay.changed||isChange){
			isChange=true;
			//	Set top of title layer
			setAbsTop(lay.title.layer,curPos);
			}
		curPos+=lay.title.height+TITLE_SPACE_AFTER;
		
		if(!menuInitialized){
			//	Set left position for menu items
			setAbsLeft(lay.title.layer,START_LEFT);
			setAbsLeft(lay.main.layer,START_LEFT);
			showLayer(lay.title.layer);
			}
		
		//	Show/Hide main layer according to visible flag
		if(lay.visible){
			if(isChange){
				//	Position main layer
				setAbsTop(lay.main.layer,curPos);
				showLayer(lay.main.layer);
				}
			curPos+=lay.main.height+MAIN_SPACE_AFTER;
			}
		else hideLayer(lay.main.layer);
		//	Add space before title layer for next title layer
		curPos+=TITLE_SPACE_BEFORE;
		}
	//	Position footer layer if present, set it no higher than the minimum bottom
	if(cMenuObj.footerLayer&&(typeof cMenuObj.footerLayer=="object")){
		if(curPos<minimumFooterBottom)curPos=minimumFooterBottom;
		setAbsTop(cMenuObj["footerLayer"],curPos);
		}
	
	menuInitialized=true;
	};

//	Function that toggles the display of one layer
function toggleLayer(name){
	if(!cMenuObj||(typeof cMenuObj!="object"))return;
	if(cMenuObj[name]){
		lay=cMenuObj[name];
		if(lay.visible)lay.visible=false;
		else lay.visible=true;
		lay.changed=true;
		positionMenuLayers();
		}
	};

function checkIfOpen(name){
	if(!cMenuObj||(typeof cMenuObj!="object"))return false;
	if(cMenuObj[name])if(cMenuObj[name].visible)return true;
	return false;
	};

function swapArrow(name){
	close=!checkIfOpen(name);
	swapImage(name+"Arrow",close);
	};

//	Empty function used for href's
function process(){
	;
	};