/* Home page - Operations executed when the page loads
 * 
 * University of Ottawa
 * Computing and Communications Services
 */
 
 /* require modern browser by checking for required features */
if (document.getElementById 
	&& document.getElementsByTagName 
	&& document.createElement
	&& document.createTextNode
	&& typeof window.addDOMLoadEvent == 'function') {
	
	/* initialize quickpicks/fac/dept drop-down lists */
    addDOMLoadEvent(initHomePageDropDownLists);
	
}

function initHomePageDropDownLists() {
	var objQuickPicksList = new dropDownList(document.getElementById('quickpicks'));
	var objFacultiesList = new dropDownList(document.getElementById('list-faculties'));
	var objDepartmentsList = new dropDownList(document.getElementById('list-departments'));
	initCollapseOnPageBodyClick();
	
	/* if javascript if enabled, display the dropdowns */
	window.cssjs('add',document.getElementsByTagName('body')[0],cssClasses.jsEnabled);
}

function dropDownList(el) {
	
	this.objDropDown = el;
	if (!this.objDropDown) return;
	
	/* Dropdown methods */
	
	this.objDropDown.expand = function() {
		/* collapse any already-expanded list */
		if (window.currentDropDown) { window.currentDropDown.collapse(); }
		
		window.cssjs('add',this,cssClasses.expand);
		window.currentDropDown = this;
	}
	
	this.objDropDown.collapse = function() {
		window.cssjs('remove',this,cssClasses.expand);
		window.currentDropDown = null;
	}
	
	
	/* toggle expand/collapse onclick */
	
	addEvent(this.objDropDown,'click',function() {
		if (window.cssjs('check',this,cssClasses.expand)) {
			this.collapse();
		} else {
			this.expand();
		}
	} );
	
	/* expand if tabbed into */
	
	/* except if the browser is Netscape 7.1 or lower */
	var patternNS = /Netscape\/([0-9])\.([0-9])/i;
	var matchesNS = patternNS.exec(navigator.userAgent);
	if (matchesNS 
		&& ((parseInt(matchesNS[1]) == 7 && parseInt(matchesNS[2]) < 2)
			|| parseInt(matchesNS[1]) < 7)
		) { return; }
	
	/* add expand and collapse functions to all elements */
	var arrAllElements = this.objDropDown.getElementsByTagName('*');
	for (var i=0; i < arrAllElements.length; i++) {
		arrAllElements[i].expand = function() { this.parentNode.expand(); }
		arrAllElements[i].collapse = function() { this.parentNode.collapse(); }
	}

	var arrQuickPicksLinks = this.objDropDown.getElementsByTagName('a');
	for (var i=0; i < arrQuickPicksLinks.length; i++) {
		addEvent(arrQuickPicksLinks[i],'focus',function() { 
			this.expand();
		} );
		addEvent(arrQuickPicksLinks[i],'blur',function() { 
			this.collapse();
		} );
	}
	
}

function initCollapseOnPageBodyClick() {
	
	var objPageBody = document.getElementsByTagName('body')[0];

	/* collapse currently expanded drop down */
	addEvent(objPageBody,'click',function(e) { 
	
		if (!window.currentDropDown) { return; }
	
		/* if you click on the dropdown tool itself, do nothing */
		if( (e.target && e.target == window.currentDropDown )
			|| (e.srcElement && e.srcElement == window.currentDropDown)) return;
	
		/* if you click anywhere else, collapse the tool */
		if (window.cssjs('check',window.currentDropDown,cssClasses.expand)) {
			window.currentDropDown.collapse();
		}
	} );

}
