
function init(){
	indicateCurrentPage();
	
	//determine if catalog has course information
	if(document.getElementById('courseOfferings'))
		handleOfferingLink();
}

//add onload handler
if(self.addEventListener)
	self.addEventListener("load", init, true);
else if(window.attachEvent)
	window.attachEvent("onload", init);
else
	self.onload = init;

//find current page in the navigation menu
function indicateCurrentPage(){
	if(!document.getElementById)
		return false;
		
	document.getElementById('navigation-courseList').style.display = (self.location.toString().match(/\bcourses\b/i) ? 'block' : 'none');
	
	var thisLocation = self.location.protocol + "//" + self.location.host + self.location.pathname.toString().replace(/index.\w+$/, "");
	var links = document.getElementById('navigation-local-list').getElementsByTagName('a');
	for(var i = 0; i < links.length; i++){
		var link = links.item(i);
		var href = link.toString();
		if(href == thisLocation){
			link.className = 'currentPage';
		}
	}
	return true;
}

//show or hide a link to a course's catalog information
var time = new Date();
var year;
var startYear = time.getFullYear();
var thisMonth = time.getMonth()+1;
if(thisMonth < 4 || (thisMonth == 4 && time.getDate() < 10)) //start using new catalog in April
	startYear--;
var endYearOnesDigit = (parseInt(String(startYear).substr(-1))+1) % 10;
year = String(startYear) + String(endYearOnesDigit);
var catalogURL = self.location.protocol + "//" + self.location.host + "/depts/sas/catalog/" + year + "/";
var courseID = '';
function handleOfferingLink(){
    //code from: http://jibbering.com/2002/4/httprequest.html
    var xmlhttp = null;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try {
         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
         try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined')
        xmlhttp = new XMLHttpRequest();
    
    var courseMatches = self.location.toString().match(/\btheo(\d+)\b/i);
    if(courseMatches.length != 2)
		return false;
    courseID = 'THEO' + courseMatches[1];
    if(xmlhttp){
        xmlhttp.open("GET", catalogURL + courseID + ".html", true);
        xmlhttp.onreadystatechange = function() {
			var offeringLink = document.getElementById('courseOfferings');
			if(xmlhttp.readyState == 4){
				if (xmlhttp.status == 200){
					offeringLink.style.display = 'block';
					var a = offeringLink.getElementsByTagName('a')[0];
					
					a.setAttribute('href', catalogURL + courseID + ".html");
				}
				else 
					offeringLink.style.display = 'none';
			}
        }
        xmlhttp.send(null);
    }
}
