// JavaScript Document

/********************************************************
- Script is a modified version of that found in 
  the O'Reilly _JavaScript and DHTML Cookbook_ by Danny Goodman.
- Script modifications by Josef Cook are (c) Assembly Studio, 2004.
- This script may be used in whole or in part so long as 
  this notice is left intact.
********************************************************/

/**********************************
Global Variables
***********************************/
// define variables to determine if page resides on testing server or live server
// then find out which folder level the referring file is on and adjust all links in file accordingly
var pagePath = location.pathname;
var re = /\\/g;
pagePath = pagePath.replace(re, "/");
var levels = pagePath.split("/");
var pageLevel = "";
var uriLoc = "";
var imgLoc = "";
var depth1, depth2, depth3;
	if (location.protocol == "http:") {
		depth1 = 1;
		depth2 = 3;
		depth3 = 4;
	} else {
		depth1 = 3;
		depth2 = 5;
		depth3 = 6;
	}
	
// get the folder level for the referring file to 
// adjust links to correct level in hierarchy
pageLevel = levels.length - 1;

if (pageLevel == depth1) {
		imgLoc = "";
	} else if (pageLevel == depth2) {
		imgLoc = "../../";
	} else if (pageLevel == depth3) {
		imgLoc = "../../../";
	}

// precache art files and sizes for widget styles and spacers
// (all images must have same height/width)	
var collapsedWidget = new Image(13, 13);
	collapsedWidget.src = (imgLoc + "Images/MenuImages/mnuPlus.gif");
var expandedWidget = new Image(13, 13);
	expandedWidget.src = (imgLoc + "Images/MenuImages/mnuMinus.gif");
var nodeWidget = new Image(13, 13);
	nodeWidget.src = (imgLoc + "Images/MenuImages/mnuNode.gif");
var emptySpace = new Image(13, 13);
	emptySpace.src = (imgLoc + "Images/MenuImages/mnuEmpty.gif");
	
// miscellaneous globals
var widgetWidth = "13";
var widgetHeight = "13";
var currState = "";
var displayTarget = "_self";

/**********************************
Data Collections
***********************************/
var expansionState = "";

// constructor for outline item objects
function outlineItem(text, uri, title) {
	
	// adjust links to correct depth in hierarchy 
	// depending upon file's location in folder structure
	if (pageLevel == depth1 || uri == "") {
		uriLoc = uri;
	} else if (pageLevel == depth2) {
		uriLoc = ("../../" + uri);
	} else if (pageLevel == depth3) {
		uriLoc = ("../../../" + uri);
	}
	this.text = text;
	this.uri = uriLoc;
	this.title = title;
}

var olData = {childNodes:
	[{item:new outlineItem("Home","index.htm", "Automated Digital Technologies Homepage")},
	 {item:new outlineItem("Cabling Services", "", "Cabling Services"),childNodes:
		[{item:new outlineItem("Cabling Home","Sections/Cabling/", "Cabling Home")},
		 {item:new outlineItem("Image Gallery","Sections/Cabling/Cbl-Gallery/", "Cabling Image Gallery")},
		 {item:new outlineItem("Cabling Contacts","Sections/Contact/default.asp?div=cabling", "Cabling Contacts")},
		 {item:new outlineItem("Vendors &amp; Partners","Sections/Cabling/vendors.htm", "Vendors &amp; Partners")}
		]
	 },
	 {item:new outlineItem("Business Solutions", "", "Business Solutions"),childNodes:
		[{item:new outlineItem("Business Home","Sections/Business/", "Business Home")},
		 {item:new outlineItem("Credit Union Services","Sections/Business/CreditUnions/", "Credit Union Services")},
		 {item:new outlineItem("Image Gallery","Sections/Business/Busn-Gallery/", "Business Solution Image Gallery")},
		 {item:new outlineItem("Business Contacts","Sections/Contact/default.asp?div=business", "Business Contacts")},
		 {item:new outlineItem("Vendors &amp; Partners","Sections/Business/vendors.htm", "Vendors &amp; Partners")}
		]
	 },
	 {item:new outlineItem("Government Services", "", "Government Services"),childNodes:
		[{item:new outlineItem("GSA Contracting","", "GSA Contracting"),childNodes:
	 		[{item:new outlineItem("GSA Home", "Sections/GSA/default.htm", "GSA Section Home")},
			 {item:new outlineItem("Detailed Info", "Sections/GSA/detailInfo.htm", "Detailed Information")},
			 {item:new outlineItem("References", "Sections/GSA/references.htm", "References")},
			 {item:new outlineItem("SBA Profile", "http://dsbs.sba.gov/dsbs/dsp_profile.cfm?User_Id=P0220937", "SBA Profile")}
			]
		 },
		 {item:new outlineItem("Government Contacts","Sections/Contact/default.asp?div=govmt", "Government Contacts")}
		]
	 },
	 {item:new outlineItem("About ADT","Sections/About/", "About ADT")},
	 {item:new outlineItem("Employment","Sections/About/Employ/", "Job Opportunities")},
	 {item:new outlineItem("Contact Us","Sections/Contact/", "Contact Us")},
	 {item:new outlineItem("Log In", "JTime/", "Log In To Your Timesheet Account")}
	]
};

/**********************************
Toggle Display and Icons
***********************************/
// invert item state (expanded to/from collapsed)
function swapState(currState, currVal, n) {
	var newState = currState.substring(0,n);
	newState += currVal ^ 1 // Bitwise XOR item n;
	newState += currState.substring(n+1,currState.length);
	return newState;
}

// retrieve matching version of 'minus' images
function getExpandedWidgetState(imgURL) {
	if (imgURL.indexOf("Start") != -1) {
		return expandedWidgetStart.src;
	}
	if (imgURL.indexOf("End") != -1) {
		return expandedWidgetEnd.src;
	}
	return expandedWidget.src;
}

// retrieve matching version of 'plus' images
function getCollapsedWidgetState(imgURL) {
	if (imgURL.indexOf("Start") != -1) {
		return collapsedWidgetStart.src;
	}
	if (imgURL.indexOf("End") != -1) {
		return collapsedWidgetEnd.src;
	}
	return collapsedWidget.src;
}

// toggle an outline mother entry, storing new state value;
// invoked by onclick event handlers of widget image elements
function toggle(img, blockNum) {
	var newString = "";
	var expanded, n;
	
	// modify state string based on parameters from IMG
	expanded = currState.charAt(blockNum);
	currState = swapState(currState, expanded, blockNum);
	// dynamically change display style
	if (expanded == "0") {
		document.getElementById("OLBlock" + blockNum).style.display = "block";
		img.src = getExpandedWidgetState(img.src);
	} else {
		document.getElementById("OLBlock" + blockNum).style.display = "none";
		img.src = getCollapsedWidgetState(img.src);
	}
}

function toggleFromText(whichWidget, blockNum) {
	var newString = "";
	var expanded, n;
	var imageSource = document.images["widget" + whichWidget].src;
	// modify state string based on parameters from IMG
	expanded = currState.charAt(blockNum);
	currState = swapState(currState, expanded, blockNum);
	// dynamically change display style
	if (expanded == "0") {
		document.getElementById("OLBlock" + blockNum).style.display = "block";
		document.getElementById("widget" + whichWidget).src = getExpandedWidgetState(imageSource);
	} else {
		document.getElementById("OLBlock" + blockNum).style.display = "none";
		document.getElementById("widget" + whichWidget).src = getCollapsedWidgetState(imageSource);
	}
}

function expandAll( ) {
	var newState = "";
	while (newState.length < currState.length) {
		newState += "1";
	}
	currState = newState;
	initExpand( );
}

function collapseAll( ) {
	var newState = "";
	while (newState.length < currState.length) {
		newState += "0";
	}
	currState = newState;
	initExpand( );
}

/*********************************
Outline HTML Generation
**********************************/
// apply default expansion state from outline's header
// info to the expanded state for one element to help
// initialize currState variable

function calcBlockState(n) {
	// get default expansionState data
	var expandedData = (expansionState.length > 0) ? expansionState.split(",") : null;
	if (expandedData) {
		for (var j = 0; j < expandedData.length; j++) {
			if (n == expandedData[j] - 1) {
				return "1";
			}
		}
	}
	return "0";
}

// counters for reflexive calls to drawOutline( )
var currID = 0;
var blockID = 0;

// generate HTML for outline
function drawOutline(ol, prefix) {
	var output = "";
	var nestCount, link, nestPrefix, lastInnerNode, itemTitle, itemImageSrc;
	prefix = (prefix) ? prefix : "";
	for (var i = 0; i < ol.childNodes.length ; i++) {
		nestCount =(ol.childNodes[i].childNodes) ? ol.childNodes[i].childNodes.length : 0;
		output += "<div class='MenuRow' id='line" + currID++ + "'>\n";
		if (nestCount > 0) {
			output += prefix;
			widgetNumber = currID-1;
			output += "<img class='WidgetPlacement' id='widget" + (currID-1) + "' src='" + ((i == ol.childNodes.length-1 && blockID != 0) ? collapsedWidget.src : (blockID == 0) ? collapsedWidget.src : collapsedWidget.src);
			output += "' height=" + widgetHeight + " width=" + widgetWidth;
			output += " alt='' title='Click to expand/collapse' onClick=" + "'toggle(this," + blockID + ")' />";
			link = (ol.childNodes[i].item.uri) ? ol.childNodes[i].item.uri : "";
			itemTitle = ol.childNodes[i].item.title;
			if (link) {
				output += "<a href='" + link + "' class='MenuItemTitle' onMouseOver='window.status=this.name; return true' onMouseOut='window.status=\"Automated Digital Technologies: Taming Technology for the Business World.\"; return true' name='" + itemTitle + "' title='" + itemTitle + "' target='" + displayTarget + "'>" ;
			} else {
				output += "<a href='#' class='MenuItemTitle' onMouseOver='window.status=this.name; return true' onMouseOut='window.status=\"Automated Digital Technologies: Taming Technology for the Business World.\"; return true' name='" + itemTitle + "' title='" + itemTitle + "' onClick = " + "'toggleFromText(" + widgetNumber + ", " + blockID + "); return false;'>";
			}
			output += "<span style='position:relative; top:0px; height:11px'>" + ol.childNodes[i].item.text + "</span></a>";
			currState += calcBlockState(currID-1);
			output += "<div class='MenuBlock' blocknum='" + blockID + "' id='OLBlock" + blockID++ + "'>";
			nestPrefix = prefix;
			nestPrefix += (i == ol.childNodes.length - 1) ?
				"<img src='" + emptySpace.src + "' height=" + widgetHeight +
				" width=" + widgetWidth + " alt='' />" :
				"<img src='" + emptySpace.src + "' height=" + widgetHeight +
				" width=" + widgetWidth + " alt='' />"
			output += drawOutline(ol.childNodes[i], nestPrefix);
			output += "</div></div>\n";
		} else {
			output += prefix;
			output += "<img style='margin-left: 2px;' id='widget" + (currID-1) + "' src='" + ((i == ol.childNodes.length - 1) ? nodeWidget.src : nodeWidget.src);
			output += "' height=" + widgetHeight + " width=" + widgetWidth + " alt='' />";
			link = (ol.childNodes[i].item.uri) ? ol.childNodes[i].item.uri : "";
			itemTitle = ol.childNodes[i].item.title;
			if (link) {
				output += "<a href='" + link + "' class='MenuItemTitle' onMouseOver='window.status=this.name; return true' onMouseOut='window.status=\"Automated Digital Technologies: Taming Technology for the Business World.\"; return true' name='" + itemTitle + "' title='" + itemTitle + "' target='" + displayTarget + "'>";
			} else {
				output += "<a class='MenuItemTitle' onMouseOver='window.status=this.name; return true' onMouseOut='window.status=\"Automated Digital Technologies: Taming Technology for the Business World.\"; return true' name='" + itemTitle + "' title='" + itemTitle + "'>";
			}
			output += "<span style='position:relative; top:0px; height:10px'>" +
			ol.childNodes[i].item.text + "</span></a>";
			output += "</div>\n";
		}
	}
return output;
}

/*********************************
Outline Initializations
**********************************/
// expand items set in expansionState var, if any
function initExpand() {
	for (var i = 0; i < currState.length; i++) {
		if (currState.charAt(i) == 1) {
			document.getElementById("OLBlock" + i).style.display = "block";
		} else {
			document.getElementById("OLBlock" + i).style.display = "none";
		}
	}
}

// initialize first time -- invoked onload
function initExpMenu(xFile) {
	// wrap whole outline HTML in a span
	var olHTML = "<span id='renderedOL'>" + drawOutline(olData) + "</span>";
	// throw HTML into 'content' div for display
	document.getElementById("content").innerHTML = olHTML;
	initExpand();
}