/*
	class: toc
	author: C.J. Saraiva
	date: May 18, 1999
        This class creates a document table of contents. Obtain a new instance by passing
        an array of <tocEntry> that you need to have previously built.
        Public interface: see the available method declarations below.
*/
function toc(aCollOfTocEntry) {
        
        // declare instance variables
        var widgetWidth = 11;                                   // widget width
        var widgetHeight = 11;                                  // widget height
        var tocEntries = aCollOfTocEntry;
        var indentPixels = 1;                                  // number of indented pixels between levels
        var fillerWidget = "images/filler.jpg";              // endpoint (defaults a blank filler)
        var fillerImg = new Image(1,1);
        var collapsedWidget = "images/plus.bmp";             // collapsed line widget
        var collapsedImage = new Image(widgetWidth,widgetHeight);
        var expandedWidget = "images/minus.bmp";             // expanded line widget
        var expandedImage = new Image(widgetWidth,widgetHeight);
        var displayTarget = "_top";                       // Target frame for documents loaded 
                                                                // when user clicks on a link
        var myCookie;
        var documentName = "Palmetto Ramble pages";

        var numberOfCookieRetrieves = 0;
        var numberOfCookieWrites = 0;        
        
        this.preLoadImages = preloadImages;
/* START COMMENT
        this.setCookie = setCookie;
        this.getCookie = getCookie;
        this.delCookie = delCookie;
        this.setCurrentState = setCurrentState;
        this.getCurrentState = getCurrentState;
   END COMMENT */
        this.toggle = toggle;
        this.isExpanded = isExpanded;
        this.getGIF = getGIF;
        this.writeDocument = writeDocument;



        // initialize 'current state' storage field
        if (getCurrentState() == "" || getCurrentState().length != (tocEntries.length)) {
        	initState = "";
        	for (i = 0; i < tocEntries.length; i++) {
	        	initState += "0";
	        };
	        setCurrentState(initState);
        };


        function preloadImages() {
                // pre-load all images into cache
                fillerImg.src = fillerWidget;
                collapsedImg.src = collapsedWidget;
                expandedImg.src = expandedWidget;
        }


        function getCookie(name){
        // Use this function to retrieve a cookie.
                numberOfCookieRetrieves++;
	        var cname = name + "=";
	        var dc = document.cookie;             
    	        if (dc.length > 0) {
        		begin = dc.indexOf(cname);       
                	if (begin != -1) {                   
		        	begin += cname.length;       
			        end = dc.indexOf(";", begin);
			        if (end == -1) end = dc.length;
			        return unescape(dc.substring(begin, end));
		        }
	        }	
	        return null;
        }


        function setCookie(name, value, expires) {
        	// Use this function to save a cookie.
                document.cookie = 
		        name + 
		        "=" + 
        		escape(value) + 
	        	"; path=/" + 
        		((expires == null) ? "" : "; expires=" + 
        		expires.toGMTString());
        }


        // Use this function to delete a cookie.
        function delCookie(name) {
	        document.cookie = 
                        name + 
        		"=; expires=Thu, 01-Jan-70 00:00:01 GMT" +  
		        "; path=/";
        }

        function setCurrentState(setting) {
                // set cookie data
                numberOfCookieWrites++;
                var exp = new Date();
                exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 365));
                setCookie("currentState", setting, exp);
                myCookie = null;
        }

        
        function getCurrentState() {
                // retrieve cookie data
                if (myCookie != null) {
                        return myCookie
                }
                var aString = getCookie("currentState");
                if (aString == null) {
                        return "";
                } else {
                        myCookie = aString;
                        return aString;
                }
        }

        function toggle(n) {
	        // **function that updates persistent storage of state**
                // toggles an outline mother entry, storing new value in the cookie
                var newString = "";
        	var currentState = getCurrentState(); // of whole outline
		var expanded = currentState.charAt(n) // of clicked item
		newString += currentState.substring(0,n)
		newString += expanded ^ 1 // Bitwise XOR clicked item
		newString += currentState.substring(n+1,currentState.length)
		setCurrentState(newString) // write new state back to cookie
        }
        
        function isExpanded(n) {
                return (getCurrentState().charAt(n) == 1)
        }

        function getGIF(n) {
                // **functions used in assembling updated outline**
                // returns the proper GIF file name for each entry's control
	        if (!tocEntries[n].isParent()) {
                        return fillerWidget
        	} else {
		        if (isExpanded(n)) {
        			return expandedWidget
		        }
	        }
        	return collapsedWidget
        }


        function getGIFStatus(n) {
        // returns the proper status line text based on the icon style
	        if (!tocEntries[n].isParent()) {
		        return "No further items"
	        } else {
		        if (isExpanded(n)) {
			        return "Click to collapse nested items"
		        }
	        }
	        return "Click to expand nested items"
        }

        function writeDocument() {
                // this function writes the toc document
                // build new outline based on the values of the cookie
                // and data points in the outline data array.
                // This fires each time the user clicks on a control,
                // because the HREF for each one reloads the current document.

                var newOutline = ""
                var prevIndentDisplayed = 0
                var showMyDaughter = 0
				var debugOnce = false;
		
                // cycle through each entry in the toc array
                for (var i = 0; i < tocEntries.length; i++) {
                        var theGIF = getGIF(i);
                        var theGIFStatus = getGIFStatus(i);
                        var currIndent = tocEntries[i].levelOfIndent;
                        var expanded = getCurrentState().charAt(i);
                        // display entry only if it meets one of three criteria
//                        if (
//                                currIndent == 0 || 
//                                currIndent <= prevIndentDisplayed 
//								|| 
//                               (showMyDaughter == 1 && (currIndent - prevIndentDisplayed == 1)	)
//                        ) {
								if (currIndent == 0) {
									newOutline += "<strong>";
								}
                                if (tocEntries[i].URL == "" || tocEntries[i].URL == null) {
									newOutline += "" + tocEntries[i].text + "<BR>";
                                } else {
									newOutline += "<A HREF=\"" + tocEntries[i].URL + "\" TARGET=\"" + displayTarget + "\" onMouseOver=\"window.status=\'" + tocEntries[i].statusMsg + "\';return true;\">" + tocEntries[i].text + "</A></NOBR><BR>";
                                }

								if (currIndent == 0) {
									newOutline += "</strong>";
								}

                                prevIndentDisplayed = currIndent;
                                showMyDaughter = expanded;
                                if (tocEntries.length > 25) {
                                        document.write(newOutline);
                                        newOutline = ""
                                }
 //                       }
                }
//				alert(newOutline);
				if (debugOnce) {
					alert(newOutline);
					debugOnce = false;
				};		
                document.write(newOutline);

        }
}
        
