
function SetVisibility(l, v) {
	if (l != null) {
		if (v == null) {
			v = (l.style.visibility == "hidden") ? "visible" : "hidden";
		}
		l.style.visibility = v;
	}
}

function SetDisplay(l, d) {
	if (l != null) {
		if (d == null) {
			d = (l.style.display == "block") ? "none" : "block";
		}
		l.style.display = d;
	}
}

function OverCover(bookId) {
	var cover = document.getElementById("shot" + bookId);
	SetDisplay(cover, "block");
	cover.style.zIndex = 1000;
}

function OffCover(bookId) {
	var cover = document.getElementById("shot" + bookId);
	SetDisplay(cover, "none");
	cover.style.zIndex = 100;
}

function ImageSwitch(prefix, suffix) {

}

//=============================================
// Image swapping functions

// <img> tag should have 'name="prefixI"', e.g. "menu_1I"
// original image file should have filename "prefix.gif", e.g. "menu_1.gif"
// swapped-in image file should have filename 'prefixsuffix.gif', e.g. "menu_1over.gif"
// Assuming all images are gifs

var imageDir = "directory/menu/";
var swapped = new Object();

// Find a named image
function FindImage(imgName) {
	return document[imgName];
}

// Swap an image
function SwapImage(prefix, suffix) {
	// If we can find an image with the given name...
	var x = FindImage(prefix + "I");
	if (x != null) {
		// Store the original .src, but only if it wasn't stored already
		// This means that if we swap again, origSrc still holds the original
		if (!x.origSrc) {
			x.origSrc = x.src;
		}
		
		// Set the new .src
		x.src = imageDir + prefix + suffix + ".gif";
		
		// Note that this image is swapped
		swapped[prefix] = true;
	}
}

function KeepImageOpen(prefix) {
	if (swapped[prefix] == null) {
		return;
	}
	swapped[prefix] = "keepOpen";
}

// Restore an image to its original state
function UnswapImage(prefix, force) {
	// If the image wasn't swapped, return
	if (swapped[prefix] == null) {
		return;
	}
	
	// If something else has told us to keep this open, don't close unless
	// "force" is true
	if (swapped[prefix] == "keepOpen" && force != true) {
		return;
	}
	
	// Get the image object and swap it back to the original version
	var x = FindImage(prefix + "I");
	if (x != null && x.origSrc != null) {
		x.src = x.origSrc;
	}
	
	// Delete the image from swapped
	swapped[prefix] = null;
}

// Unswap all images
function UnswapAllImages() {
	for (s in swapped) {
		UnswapImage(s);
	}
}

var preloadedImages = new Array();
function PreloadImages() {
	for (var i = 0; i < arguments.length; ++i) {
		preloadedImages[i] = new Image();
		preloadedImages[i].src = arguments[i];
	}
}

PreloadImages(
	  "directory/menu/menu_1_over.gif"
	, "directory/menu/menu_2_over.gif"
	, "directory/menu/menu_3_over.gif"
	, "directory/menu/menu_4_over.gif"
);
//===============================================================================

function findPosX(obj) {
	var curleft = 0;
	while (obj.offsetParent) {
		curleft += obj.offsetLeft
		obj = obj.offsetParent;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	while (obj.offsetParent) {
		curtop += obj.offsetTop
		obj = obj.offsetParent;
	}
	return curtop;
}

// ******************************************************
// "Pop-In" window for instructor feedback

// Convenience variables to keep track of the DOM elements we need to manipulate
var popInWindow = null;
var popInTitle;

// Global variables for moving the window around
var startX, startY;		// position of mouse the last time we executed MovePopInWindow
var mouseX, mouseY;		// current position of mouse
var lastScrollY = 0;	// scroll position the last time we moved
var popInWindowX, popInWindowY;	// current position of the popInWindow div

// Initialize a popInWindow
function ShowPopInWindow(index, correctForScroll) {
	// If another popIn is open, close it
	if (popInWindow != null) {
		ClosePopInWindow();
	}
	
	popInWindow = document.getElementById("popInWindow" + index);
	popInTitle = document.getElementById("popInTitle" + index);
	
	// If we didn't find the popInWindow div, throw an error
	if (popInWindow == null) {
		alert("Error: Couldn't find popInWindow " + index);
	}
	
	// Show the popInWindow
	SetDisplay(popInWindow, "block");

	// Get the initial window positions
	popInWindowX = findPosX(popInWindow);
	popInWindowY = findPosY(popInWindow);

	// Set up event handlers to allow the window to be dragged
	popInTitle.onmousedown = EngagePopInWindow;
	document.onmouseup = Disengage;
	
	if (correctForScroll == true) {
		// Position the popInWindow in the same position as it was last left,
		// relative to the current scroll position
	
		// Find out what the current scroll position is
		var currentScrollY;
		if (document.documentElement && document.documentElement.scrollTop) {
			currentScrollY = document.documentElement.scrollTop;
		} else {
			currentScrollY = document.body.scrollTop;
		}
		
		// If the user has scrolled since the last time they viewed or moved the window,
		// shift the window so that it's near the top of the screen
		if (currentScrollY != lastScrollY) {
			ShiftPopInWindow(-1, currentScrollY + 10, true);
		}
	
		// Alternatively, we COULD:
		// Shift the window to compensate for any scrolling that happened since
		// the last time the window moved.
		// If, for example, the window was scrolled to 0, 0 before
		// and now is scrolled to 100, 0, we want to move down 100 pixels
		// ShiftPopInWindow(0, currentScrollY - lastScrollY);
		
		// Record the current scroll position
		lastScrollY = currentScrollY;
	}
}


// Shift the popInWindow.  If absolute = true, go to x, y (but don't shift to a -1 location). 
// If absolute = false or undefined, move by x, y
function ShiftPopInWindow(x, y, absolute) {
	if (absolute) {
		if (x != -1) {
			popInWindowX = x;
		}
		if (y != -1) {
			popInWindowY = y;
		}
	} else {
		popInWindowX += x;
		popInWindowY += y;
	}
	
	// Technically, the style should be "10px", not just "10".
	popInWindow.style.left = popInWindowX + "px";
	popInWindow.style.top = popInWindowY + "px";
}

// Get the current mouse position, taking account off any scrolling
function SetMousePositions(e) {
	// Netscape-compatible browsers send the event in as a parameter to the function,
	// but Explorer-compatible browsers require us to get the event.
	// Note that we need the window event, which will localize the mouse relative
	// to the main frame
	if (!e) var e = window.event;
	
	// See the bottom of http://www.quirksmode.org/index.html?/js/events_compinfo.html
	// for a version of this code that will work with all browsers; we only care about IE, NS, and Safari

	mouseX = e.clientX + document.body.scrollLeft;
	mouseY = e.clientY + document.body.scrollTop;
}

// This function is set as an event handler that's called
// when the user first clicks on the definition window
function EngagePopInWindow(e) {
	// Register an onmousemove event handler at the document level to move the window
	document.onmousemove = MovePopInWindow;

	// Get the mouse event (see note in SetMousePositions)
	if (!e) var e = window.event;
	
	// set the initial values of startX/Y
	SetMousePositions(e);
	startX = mouseX;
	startY = mouseY;
	
	// Also set the values for lastScrollY
	// Note that we don't bother with X scrolling
	if (document.documentElement && document.documentElement.scrollTop) {
		lastScrollY = document.documentElement.scrollTop;
	} else {
		lastScrollY = document.body.scrollTop;
	}
	
	return StopEvent(e);
}

// MovePopInWindow: called when the mouse is moved after engaging the window
function MovePopInWindow(e) {
	// Get the mouse event	
	if (!e) var e = window.event;

	// Get the new mouse position
	SetMousePositions(e);
	
	// Move the window
	ShiftPopInWindow(mouseX - startX, mouseY - startY, false);

	// Set startX/Y so that the next time this function is called we'll move appropriately
	startX = mouseX;
	startY = mouseY;

	return StopEvent(e);
}


// Disengage: called when the mouse button is released
function Disengage() {
	// Clear the onmousemove event handler
	document.onmousemove = null;
}

// Stop the event e from bubbling up to higher layers of the DOM
// see http://www.quirksmode.org/index.html?/js/events_compinfo.html
function StopEvent(e) {
	e.cancelBubble = true;		// IE
	if (e.stopPropagation) 		// everyone else
		e.stopPropagation();
	
	// In NS, we (also?) have to return false to cancel the default action from occurring.
	// StopEvent returns false so that the caller can say "return StopEvent(e);"
	// to cover all eventualities.
	return false;
}

// Hide the popin window
function ClosePopInWindow() {
	SetDisplay(popInWindow, "none");
	popInWindow = null;
}

//===============================================================================

// id, dir, left, top, width, height
var menuObjects = [
	"",
//	"",	// so first index is 2
	new ypSlideOutMenu("faqMenu", "down", 34, 126, 575, 500),		// for under menu only: 34, 126, 225, 500
	new ypSlideOutMenu("studMenu", "down", 260, 126, 349, 470),
	new ypSlideOutMenu("instMenu", "down", 435, 126, 349, 430),		// left/top should be 435,126 to line up with menu item
	new ypSlideOutMenu("contactMenu", "down", 610, 126, 240, 315)
];

for (var i = 1; i < menuObjects.length; i++) {
	menuObjects[i].onactivate = new Function("KeepImageOpen('menu_" + i + "')");
	menuObjects[i].ondeactivate = new Function("UnswapImage('menu_" + i + "',true)");
}



ypSlideOutMenu.writeCSS();


var suppwin;
function OpenSupp(url) {
	suppwin = window.open(url, "directorySuppWindow", "top=20,left=20,width=800,height=620,menubar,resizable,scrollbars,status", true);
}



var mhShowing = null
function ShowMH(mh) {
	// If the user clicks on the same mh again, it's like clicking back
	if (mh == mhShowing) {
		FAQBack();
		FAQBack();
		FAQBack();
		return;
	}
	
	// Show the specified mh
	SetDisplay(document.getElementById(mh), "block");
	
	// Make sure all heads in the mh are showing
	ToggleAllHeads(mh, 'block');
	headShowing = null;
	
	// Hide the other mh link
	if (mh == 'stu') {
		SetDisplay(document.getElementById('instM'), "none");
	} else {
		SetDisplay(document.getElementById('stuM'), "none");
	}
	
	// Show the back link
	SetDisplay(document.getElementById('faqBack'), 'block');
	
	mhShowing = mh;
}

function ToggleAllHeads(mh, display) {
	var i = 0;
	var d;
	// Go through all heads in the mh
	while ((d = document.getElementById(mh + "_" + i + "M")) != null) {
		// Set the head description to the given display
		SetDisplay(d, display);
		
		// Hide the content of the head
		SetDisplay(document.getElementById(mh + "_" + i), 'none');
		
		++i;
	}
}

var headShowing = null
function ShowHead(h) {
	HideCurrentFAQ();
	
	// If the user clicks on the same head again, it's like clicking back
	if (h == headShowing) {
		FAQBack();
		return;
	}
	
	// First hide all the heads
	ToggleAllHeads(mhShowing, 'none');
	
	// Show the given head and its contents
	SetDisplay(document.getElementById(h + "M"), "block");
	SetDisplay(document.getElementById(h), "block");

	// Show all the head's questions
	ToggleAllFAQs(h, "block");

	headShowing = h;
}


var faqShowing = null;
function ShowFAQ(n) {
	
	// If the user clicks on the same question again
	if (n == faqShowing) {
		// Close the answer text
		SetDisplay(document.getElementById(n), "none");
		
		// Show all questions
		ToggleAllFAQs(headShowing, "block");
		
		faqShowing = null;
	
	// Otherwise it's a new faq
	} else {
		// Hide all the faqs
		ToggleAllFAQs(headShowing, 'none');

		// Show this faq
		SetDisplay(document.getElementById(n + "M"), "block");
		
		// Show this faq's answer
		SetDisplay(document.getElementById(n), "block");
		faqShowing = n;
	}
}


function ToggleAllFAQs(head, display) {
	var i = 0;
	var d;
	// Go through all faqs in the head
	while ((d = document.getElementById(head + "_" + i + "M")) != null) {
		// Set the faq description to the given display
		SetDisplay(d, display);
		
		++i;
	}
}



function HideCurrentFAQ() {
	if (faqShowing != null) {
		SetDisplay(document.getElementById(faqShowing), "none");
		faqShowing = null;
	}		
}

function FAQBack() {
	// If a question is showing, back means go back to the category
	if (faqShowing != null) {
		HideCurrentFAQ();
		ToggleAllFAQs(headShowing, 'block');
		
	// If a head is showing, back means go back to all categories for the mh
	} else if (headShowing != null) {
		HideCurrentFAQ();
		ToggleAllHeads(mhShowing, 'block');
		headShowing = null;

	// Otherwise back means to back to the student/instructor menu
	} else {
		HideCurrentFAQ();
		SetDisplay(document.getElementById('stu'), 'none');
		SetDisplay(document.getElementById('inst'), 'none');
		SetDisplay(document.getElementById('faqBack'), 'none');
		SetDisplay(document.getElementById('stuM'), 'block');
		SetDisplay(document.getElementById('instM'), 'block');
		mhShowing = null;
	}
}

