// JavaScript Document
// Depends on jQuery JavaScript Library v1.3.2

//
// equalizeColumns(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 equalizeColumns() with the
// ids you want to equalize as parameters.
//
// Example:
//     equalizeHeight("leftColumn", "rightColumn", "mainColumn") ;
//
// This function differs from the version in /shared/js/layout.js in that it
// accounts for the top-padding of the elements to be resized. It also does
// not resize the tallest column.
//
function equalizeColumns() {
	var elements = equalizeColumns.arguments ;
	var height = 0;
	var maxHeight = 0;
	var tallestColumn = 0;
	for(var i=0; i<elements.length; i++) {
		height = $(elements[i]).height();
		if (height > maxHeight) {
			maxHeight = height;
			tallestColumn = i;
		}
	}
	for(var i=0; i<elements.length; i++) {
		if (i != tallestColumn) {
			$(elements[i]).height(maxHeight - parseInt($(elements[i]).css('padding-top')));
		}
	}
}

//
// _getSectionLink_RootRelative_English(idToSearch, idToIgnore)
//
// Only works when using root-realtive links in $idToSearch.
//
// Returns the DOM anchor object in $idToSearch, as long as it is not
// $idToIgnore, most likely to be the current section for this page.  The
// algorithm looks for the anchor with an href that matches the first folder
// in the current path (ie. the greatest ancestor of the current page).
// Returns null if there are more than one possible matches.
//
// This function has been adapted from the /shared/js/layout.js version
// to work properly when there is a link to /index.html in the navigation.
// It also uses jQuery to ensure that the section link comes only
// from the first level of links.
//
function _getSectionLink_RootRelative_English(idToSearch, idToIgnore) {
	if(!document.getElementById) return null ;						// browser detect DOM support
	
	// get the container we should be searching
	var container = document.getElementById(idToSearch) ;			// get the container
	if(container==null) return null ;								// bail if we can't find it
	
	// init variables
	var sectionCandidate = null ;
	var sectionPath = null ;
	var linkFound = 0 ;
	
	// get the current path and generate the href
	// to look for from the last folder in the path
	// (eg. currentPath = "/foo/bar/index.html", sectionPath = "/foo")
	var currentPath = document.location.pathname ;
	var pathParts = currentPath.split("/") ;
	for(var i=0; i<pathParts.length; i++) {
		if(pathParts[i] != "" && pathParts[i].indexOf('.') == -1) {
			sectionPath = "/" + pathParts[i] ;
			break ;
		}
	}
	if (sectionPath == null) {
		// If sectionPath is still null, the current page must be in the site-root
		sectionPath = currentPath;
	}
	
	var anchors = $('#' + idToSearch + ' > li > a');				// get the first-level links
	for(var i=0; i<anchors.length; i++) {							// iterate anchors...
		var linkCandidate = anchors[i] ;								// this candidate
		
		var href = linkCandidate.getAttribute("href", 2) ;
		var slashCount = _substrCount(href, "/") ;
		
		// only consider cases where href is of the form "../folder" or "../folder/"
		// and ensure the $idToIgnore is undefined or not the current link
		if((href.indexOf(sectionPath) == 0 && slashCount < 3) && (idToIgnore == null || linkCandidate.id != idToIgnore)) {
			sectionCandidate = linkCandidate ;
			break;
		}
	}																//...iterate anchors
	
	return sectionCandidate ;
}

/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * 
 * Thanks to 
 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 * Tom Leonard for some improvements
 * 
 */
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/ 
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if (jQuery(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if (jQuery(this).attr("src")!="undefined") {
	  	
	  	var strHref = jQuery(this).attr("src")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if (jQuery(this).attr("href")!="undefined") {
	  	
	  	var strHref = jQuery(this).attr("href")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});

/********* fading functions **********/

//toggles visibility of element for which this function is invoked with a fade transition effect
//Only works with jQuery(...) (aka '$(...)') objects
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

}; 

/**
 *	Description: This function is used to fade out a current psuedo-page's content (a div or other container type) and fade in the 
 *	new psuedo-page's content (a container with previous display css attribute set to 'none').  
 *	idToLoad: id of Div to fade in
 *	thisLink (optional): handle for links to be formated as 'in focus' (i.e. for which links should underlining be removed when  
 *																		idToLoad is shown)
 *
 *	
 *	Details: Each psuedo-page should be contained in a div, initialized with the css attribute 'display: none' and a unique id which 
 * 		must be passed to idToLoad. 
 *	thisLink is used to determine which links to display as 'in focus', and must be a handle representing the links associated with 
 * 		the currently active section.  When called from an onclick event the 'this' keyword works.  When loadNewSection() is called 
 *		at page initialization a class name uniquely identifying the links which are desired to be designated 'in focus' works.
 *	When a section fades in it's assigned .currentSubSection, and when an element needs to be faded out before fading another element
 * 		in, the element to fade out is identified by the .currentSubSection class.
 */
function loadNewSection(idToLoad,thisLink) {
	$('.currentSubSection').fadeToggle();
	$('.currentSubSection').removeClass("currentSubSection");
	$('.focusedSection').removeClass('focusedSection');
	if(thisLink) {
		var pageClass = thisLink.className.match(/page.Link/);
		$('.'+pageClass).addClass('focusedSection');
	}
	
	setTimeout(function() { //The timeout is necessary to give the old active section time to fade out before the new one fads in.
		$("#"+idToLoad).fadeIn();  
		setTimeout(function() {
			reAdjustHeights(); 
		},350);
		$("#"+idToLoad).addClass("currentSubSection");
	},350);
}
