/* Initialize the page
 * Should be called from <body onload>
 */
function init() {
	// If a hash was given, highlight the corresponding area
	var name = location.hash;
	if(name) {
		name = name.substr(1);
		var box = document.anchors[name].parentNode;
		if(box && box.tagName == "DIV") {
			// If the box does not have an id, generate one
			var now = new Date();
			if(!box.id) box.id = "div_" + (new Date()).getTime();
			
			// Flash the box
			flashBox(box.id, 3, 200);
		}
	}
}

/* Flash a box to draw attention to it
 * @param id string - id of the DOM object to flash
 * @param count integer - number of times to flash
 * @param delay integer - delay (in milliseconds) between flashes
 */
function flashBox(id, count, delay) {
	if(count <= 0) return;
	var box = document.getElementById(id);
	if(!box) return;
	var flash = "#7df9f8";
	var bg = box.style.background;
	setTimeout('document.getElementById("' + id + '").style.background = "' + flash + '"', delay / 2);
	setTimeout('document.getElementById("' + id + '").style.background = "' + bg + '"', delay);
	setTimeout('flashBox("' + id + '", ' + (count - 1) + ', ' + delay + ')', delay);
}

/* Variables utilized by the popup functions below
 * step and delay can change the animation speed of popups
 * Others are for internal use only
 */
var popupOpenStep = 15, popupOpenDelay = 20;
var popupCloseStep = 20, popupCloseDelay = 5;
var popupID = null;
var popupContent = null;
var popupTimer = null;

/* Animate a popup opening
 * Should be called from <a onclick>
 * @param a object - DOM <a> object of link that was clicked
 * @param id string - id of popup to animate
 * @param width integer - final width for popup (optional)
 * @param height integer - final height for popup (optional)
 * @return boolean - always false
 */
function popupOpen(a, id, width, height) {
	// Close any open popup
	var div;
	clearInterval(popupTimer);
	if(popupID) {
		div = document.getElementById(popupID);
		if(div) div.style.display = "none";
	}
	popupID = id;

	// Get the final width/height to shoot for
	div = document.getElementById(id);
	if(!div) return false;
	div.style.display = "block";
	if(!width) width = div.offsetWidth;
	if(!height) height = div.offsetHeight;
	
	// Hide the contents
	var children = div.getElementsByTagName("DIV");
	for(var i=0; i<children.length; i++)
		if(children[i].className == "popup_content")
			children[i].style.display = "none";
	
	// Create the popup in the center of the parent element
	div.style.position = "absolute";
	div.style.width = "0px";
	div.style.height = "0px";
	var pos = findPos(a);
	div.style.left = pos[0] + (a.offsetWidth / 2) + "px";
	div.style.top = pos[1] + (a.offsetHeight / 2) + "px";
	
	// Start displaying the popup
	popupTimer = setInterval("popupGrow('" + id + "', " + width + ", " + height + ")", popupOpenDelay);

	return false;
}

/* Animation function used by popupOpen()
 * @param id string - id of popup to animate
 * @param width integer - target width for popup
 * @param height integer - target height for popup
 * @return boolean - always false
 */
function popupGrow(id, width, height) {
	// Get the object
	var div = document.getElementById(id);
	if(!div) return false;
	
	// Grow the object
	var widthChange = 0, heightChange = 0;
	var curWidth = parseInt(div.style.width.substring(0, div.style.width.length - 2));
	var curHeight = parseInt(div.style.height.substring(0, div.style.height.length - 2));
	if(curWidth < width) {
		curWidth += popupOpenStep;
		if(curWidth >= width) {
			widthChange = curWidth - width;
			curWidth = width;
		}
		else widthChange = popupOpenStep;
	}
	if(curHeight < height) {
		curHeight += popupOpenStep;
		if(curHeight >= height) {
			heightChange = curHeight - height;
			curHeight = height;
		}
		else heightChange = popupOpenStep;
	}
	
	// Shift the object
	var curLeft = parseInt(div.style.left.substring(0, div.style.left.length - 2));
	var curTop = parseInt(div.style.top.substring(0, div.style.top.length - 2));
	curLeft -= parseInt(widthChange / 2);
	curTop -= parseInt(heightChange / 2);
	
	// Don't let it go offscreen
	var pad = 20;
	if(curLeft + curWidth + pad > document.body.offsetWidth)
		curLeft = document.body.offsetWidth - curWidth - pad;
	
	// Apply the new settings
	div.style.width = curWidth + "px";
	div.style.height = curHeight + "px";
	div.style.left = curLeft + "px";
	div.style.top = curTop + "px";
	
	// If the popup has reached its final size, clean up
	if(curWidth == width && curHeight == height) {
		clearInterval(popupTimer);
	
    	// Show the contents
    	var children = div.getElementsByTagName("DIV");
    	for(var i=0; i<children.length; i++)
    		if(children[i].className == "popup_content")
    			children[i].style.display = "block";
	}
	return false;
}

/* Animate a popup closing
 * @param id string - id of popup to close
 * @return boolean - always false
 */
function popupClose(id) {
	// Make sure to set this as the open popup
	var div;
	clearInterval(popupTimer);
	if(popupID && popupID != id) {
		div = document.getElementById(popupID);
		if(div) div.style.display = "none";
	}
	popupID = id;
	
	// Start the animation
	popupTimer = setInterval("popupShrink('" + id + "', " + width + ", " + height + ")", popupCloseDelay);
	return false;
}

/* Animation function used by popupClose()
 * @param id string - id of popup to animate
 * @return boolean - always false
 */
function popupShrink(id, width, height) {
	// Get the object
	var div = document.getElementById(id);
	if(!div) return false;
	
	// Shrink the object
	var curHeight = parseInt(div.style.height.substring(0, div.style.height.length - 2));
	curHeight -= popupCloseStep;
	if(curHeight < 0) curHeight = 0;
	div.style.height = curHeight + "px";
	
	// Finished!
	if(curHeight == 0) {
		clearInterval(popupTimer);
		div.style.display = "none";
	}
	return false;
}

/* Get the position of an object on the page
 * @param obj object - DOM object
 * @return array - (x, y) absolute coordinates of object on page
 */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while(obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft, curtop];
}