// Globals
var dayNames   = new Array("Sunday",
						   "Monday",
						   "Tuesday",
						   "Wednesday",
						   "Thursday",
						   "Friday",
						   "Saturday");

// Initialise content frame pages
function initialise(html) {
	if (parent.adjustIFrameSize)
		parent.adjustIFrameSize(window);
	
	if (html)
		if (parent.setButtonExtra)
			parent.setButtonExtra(html);
}

// Resize iFrame to fit content
function adjustIFrameSize(iframeWindow) {
  if (iframeWindow.document.height) {
    var iframeElement = document.getElementById(iframeWindow.name);
    iframeElement.style.height = (iframeWindow.document.height + 20) + 'px';
//    iframeElement.style.width = iframeWindow.document.width + 'px';
  }
  else if (document.all) {
    var iframeElement = document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode &&
        iframeWindow.document.compatMode != 'BackCompat') 
    {
      iframeElement.style.height = 
	  	(iframeWindow.document.documentElement.scrollHeight + 10) + 'px';
//      iframeElement.style.width = 
//		iframeWindow.document.documentElement.scrollWidth + 0 + 'px';
    }
    else {
      iframeElement.style.height = 
	  	(iframeWindow.document.body.scrollHeight + 10) + 'px';
//      iframeElement.style.width = 
//		iframeWindow.document.body.scrollWidth + 5 + 'px';
    }
  }
}

// Radio Button functions
//
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

// Display current date on page
function showCurrentDate() {
	var monthNames = new Array("January",
							   "February",
							   "March",
							   "April",
							   "May",
							   "June",
							   "July",
							   "August",
							   "September",
							   "October",
							   "November",
							   "December");
	
	var dt = new Date();
	var y  = dt.getYear();
	
	// Y2K compliant
	if (y < 1000) y +=1900;
	
	var dayText =
		'<div class="page_header">' + 
		dayNames[dt.getDay()] + ", " +
		monthNames[dt.getMonth()] + " " +
		dt.getDate() + ", " +
		y +
		'</div>';
				  
	document.write(dayText);
}

// Add to button bar
function setButtonExtra(contentHTML) {
	document.getElementById("menu_extra").innerHTML = contentHTML;
}

// Display the requested contant pane
function setContent(contentPage) {
	if (window.frames["content"])
		window.frames["content"].location = contentPage;
	else
		parent.window.frames["content"].location = contentPage;
	setButtonExtra('');
}

// Schedule Functions

// Return the current day, adjusted if required
function getDayName() {
	var offset = 0;
	if (arguments.length == 1)
		offset = arguments[0];
	
	var now = new Date();
	var idx = (now.getDay() + offset) % 7;
	
	return dayNames[idx].substring(0,3).toLowerCase();
}

// Initialise the detailed schedule page
function initDetailSchedule() {
	var now = new Date();
	setCheckedValue(document.selectDay.day, now.getDay());
	showSelectedDay();
}

// Display correct day depending on radio button selection
function showSelectedDay() {
	var selected = getCheckedValue(document.selectDay.day);
	var dayName  = dayNames[selected];
	
	var banner = dayName + "'s Schedule";
	document.getElementById("detail_banner").innerHTML = banner;
	window.frames["detail_schedule"].location = 
		"schedule/detail_" + dayName.substring(0,3).toLowerCase() + ".html";
}
