//
// Layout Functions - layout.js
// Adam Mark Finlayson, WTS
// amf%northwestern!edu
//


//====== Documentation ======//

//
// equalizeHeight(id0, id1, ..., idN)
// Set heights of elements (by id) to the largest height of the group.
//
// preload(img0, img1, ..., imgN)
// Pre-loads images (or whatever) to be used later.
//
// setHeight(id, height)
// Sets the style.height of $id to $height.
//
// setHeightToDifference(idToChange, idToCompare1, idToCompare2)
// Sets $idToChange to the difference between $idToCompare1 and $idToCompare2.
//
// stripIndexLinks(id)
// Strips unnecessary index.htm/l component from links in $id.
//



//====== Notes ======//

//
// Changing width/height
//
// Functions use element.offsetHeight, which adds top/bottom border and padding
// thickness, to determine element size.  Uses element.style.height to set
// height, which does not include border/padding.  In practice this means even
// the tallest element will expand by its top/bottom border/padding thickness.
//



//====== Public Functions ======//

//
// equalizeHeight(id0, id1, ..., idN)
//
// Used to set heights of elements (by id) to the largest height of the group.
// Simulates <td> rows of table-based layouts.  Call equalizeHeight() with the
// ids you want to equalize as parameters.
//
// Example:
//     equalizeHeight("leftColumn", "rightColumn", "mainColumn") ;
//
function equalizeHeight() {
	if(!document.getElementById) return ;
	
	var elements = equalizeHeight.arguments ;
	var height = 0 ;
	for(var i=0; i<elements.length; i++) {
		var element = document.getElementById(elements[i]) ;
		if(element != null) {
			height = Math.max(height, element.offsetHeight) ;
		}
	}
	for(var i=0; i<elements.length; i++) {
		if(element != null) {
			setHeight(elements[i], height) ;
		}
	}
}


//
// preload(img0, img1, ..., imgN)
//
// Pre-loads images (or whatever) to be used later by JavaScript or CSS effects.
// More or less a clone of MM_preloadImages v3.0 with variable names changed and
// comment added for readability.
//
function preload() {
	// if there is an images collection in this document
	if(document.images) {
		// Create an array to preload images in
		if(!document.preloader) document.preloader = new Array() ;
		
		// Keep track of where the preloader array index is
		var preloaderIndex = document.preloader.length ;
		
		// The batch of things to preload is the arguments to this function
		var preloadBatch = preload.arguments ;
		
		// iterate through the preloadBatch, adding each to the preloader
		for(var i=0; i<preloadBatch.length; i++) {
			//if (preloadBatch[i].indexOf("#") != 0) {	// Not sure why MM_preloadImages v3.0 has this
			document.preloader[preloaderIndex] = new Image ;
			document.preloader[preloaderIndex].src = preloadBatch[i] ;
			preloaderIndex++ ;	// increment preloader array index
			//}
		}
	}
}


//
// setHeight(id, height)
//
// Sets the style.height of $id to $height.  SLSIA
//
function setHeight(id, height) {
	element = document.getElementById(id) ;
	element.style.height = height + "px" ;
}


//
// setHeightToDifference(idToChange, idToCompare1, idToCompare2)
//
// Sets $idToChange to the difference between $idToCompare1 and $idToCompare2.
// Used to make a filler block expand as large as needed.
//
// Example:
//     setHeightToDifference("buffer-above-content", "content", "main-column") ;
//
// Makes the most sense to use this after something has been equalized.
//
function setHeightToDifference(idToChange, idToCompare1, idToCompare2) {
	if(!document.getElementById) return ;
	
	var element1 = document.getElementById(idToCompare1) ;
	var height1 = element1.offsetHeight ;
	
	var element2 = document.getElementById(idToCompare2) ;
	var height2 = element2.offsetHeight ;
	
	var difference = Math.abs(height1 - height2) ;
	setHeight(idToChange, difference) ;
}


//
// stripIndexLinks(id)
//
// Looks through $id for links ending in index.htm/l and strips the index part 
// off the link.  Used to make prettier links so people don't copy-and-paste 
// (and print/publish/e-mail) links with the unnecessary index component.
//
function stripIndexLinks(id) {
	if(!document.getElementById) return ;
	
	var element = document.getElementById(id) ;
	var links = element.getElementsByTagName("a") ;
	for(i=0; i<links.length; i++) {
		var link = links.item(i) ;
		_stripLinkFile(link, "index.html") ;
		_stripLinkFile(link, "index.htm") ;
	}	
}



//====== Private Functions ======//


// Helper for stripIndexLinks().  Strips $filename off the $link <a> object 
function _stripLinkFile(link, filename) {
	var href = link.getAttribute("href") ;				// Get the href of the link
	
	filenameIndex = href.indexOf(filename) ;			// Get the index of this filename in href
	if(filenameIndex == -1) return ;					// Bail if filename isn't present
	
	if(href==filename) {								// Linking to this filename is a special case
		link.href = "." ;									// Just link to current folder
		return ;											// and return
	}
	
	if(_stringEndsWith(href, filename)) {				// if we should strip this
		link.href = href.substring(0, filenameIndex) ;		// strip it
	}
}

// SLSIA.  Returns true if $string ends in $substring, false otherwise
function _stringEndsWith(string, substring) {
	var index = string.indexOf(substring) ;
	if(index == -1) return false ;
	
	// if substring is the last part of string, this is true
	// examples:
	//     string = "friend", substring = "end"
	//     "fri" (3) + "end" (3) = "friend" (6)
	//
	//     string = "the beginning", substring = "begin"
	//     "the " (4) + "begin" (5) != "the beginning" (13)
	//
	if((index+substring.length) == string.length) {
		return true ;
	}
	return false ;
}


