
/* global javascript variables */

var uiucweb_directoryClass;  // name of directory within site, eg 'brownbags'
var uiucweb_bodyElement;  
var uiucweb_isHomepage = false; // is this the webmaster's home. boolean, defaults to false

/*
	general inititialize function for webmasters site
*/

function uiucweb_page_init() {
	uiucweb_navigationCSS_init();
	return true;
}

// add folder name to list of classes in body element
// add css rule to hide all subnavigation
// add css rule to show this particular folders subnavigation

function uiucweb_navigationCSS_init() {
	//alert(location);
	
	// find directory class from url
	uiucweb_directoryClass = location.toString().split("/")[3];  // the first level directory name parsed from url
	if(uiucweb_directoryClass == 'index.html' || uiucweb_directoryClass == '') {
	   uiucweb_directoryClass = 'rootdirectory'; 
	   uiucweb_isHomepage = true};
	
	if(!document.getElementById && !document.createTextNode){return;}
	// if DOM not supported, leave well enough alone and exit
	
	//alert(uiucweb_directoryClass);
	// mark subnavigation that matches directory name as class=currentNavSubSection
	var currentSubsection = document.getElementById('navsubsection_' + uiucweb_directoryClass);  // eg navsubsection_brownbags
	
//	alert(uiucweb_directoryClass + currentSubsection.name);
	if (currentSubsection) {
		uiucweb_addclass(currentSubsection,'currentNavSubSection');
		}
	//alert(uiucweb_directoryClass);
	// add directory class to body element
	var toplevelElements = document.documentElement.childNodes;
	for (var i=0; i<toplevelElements.length; i++) {
		if ( toplevelElements[i].nodeName == 'BODY') {
			uiucweb_bodyElement = toplevelElements[i];
		//	alert(uiucweb_directoryClass + ' directoryClassed');
			uiucweb_addclass(uiucweb_bodyElement,uiucweb_directoryClass+ ' directoryClassed');
			if(uiucweb_isHomepage) {uiucweb_addclass(uiucweb_bodyElement,'homepage') };
			}
		}
	
	// if div#maincontent is classed as a layout div, but has no divs classed as "content" directly within it, change its class from "layout" to "content".
	// this is the simple case of a page with no layout divs within the main content region.
	// "content" divs have padding, "layout" divs have none (in case two adjacent ones share a border or have different background colors)
	
	var maincontentDiv = document.getElementById('maincontent');  // get maincontent div
//	alert('testing for maincontent as layout div' + maincontentDiv);
//	alert(maincontentDiv.className);
	if (maincontentDiv && uiucweb_check(maincontentDiv,'layout') ){  // if it is classed as layout, test for layout divs directly beneath it
		var isContentDiv = true;	
		var possibleDivs = maincontentDiv.childNodes;
	//	alert('possible div count='+possibleDivs.length);
		for (var i=0;i<possibleDivs.length;i++) {  // cycle through all divs within maincontent
			// alert( possibleDivs[i].nodeType + possibleDivs[i].nodeName + uiucweb_check(possibleDivs[i],'layout'));
			if (possibleDivs[i].nodeType == 1 && possibleDivs[i].nodeName == 'DIV' && uiucweb_check(possibleDivs[i],'layout')) {isContentDiv = false};
			}
		if (isContentDiv == true) {maincontentDiv.className = maincontentDiv.className.replace('layout','content')};
		//alert(maincontentDiv.className );	
	}
		
}

function uiucweb_swapclass(o,c1,c2)
	{
		var cn=o.className
		o.className=!uiucweb_check(o,c1)?cn.replace(c2,c1):cn.replace(c1,c2);
	}
	
function uiucweb_addclass(o,c)
	{
		if(!uiucweb_check(o,c)){o.className+=o.className==''?c:' '+c;}
	}
	
function uiucweb_check(o,c)
	{
	 	return new RegExp('\\b'+c+'\\b').test(o.className);
	}
	
String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};



function setAttributeOfElementById(elementID,attributeName,attributeValue) {
   document.getElementById(elementID).setAttribute(attributeName,attributeValue);
}
