/**
 * @author LEC
 * colapse and expand location divs for FTI website '08
 */
var containerDiv = "alllocations";
var expandedClass = "locationExpanded";
var collapsedClass = "locationCollapsed";
var defaultClass = "Region";
var expandInt = 0;
var collapseInt = 0;
var interval = 25;
var increment = 30;
var collapsedHeight = 30;

function ToggleDiv(divToShow){

	try{
		var regionDivs = document.getElementById(containerDiv).childNodes;
				
		curdiv = document.getElementById(divToShow);
		
		if(curdiv.className == collapsedClass || curdiv.className == defaultClass){
			for(i=0; i<regionDivs.length; i++){
				if(regionDivs[i].className == expandedClass){
					collapsediv = regionDivs[i];
					collapseInt = setInterval( collapse, interval);
				}
			}
			expandInt = setInterval(expand, interval);
		}
		
	}catch(e){
		window.alert("caught exception: " + e.description);
	}
}

 function getTargetHeight(){
 	var targetElement = curdiv;
 	var output = 0;
	var childList = targetElement.childNodes;
	for(i=0; i<childList.length; i++){
		
		var gChildList = childList[i].childNodes;
		var outterHeight = childList[i].offsetHeight;
		var innerHeight = 0;
		for(q=0; q<gChildList.length; q++){
			if(gChildList[q].className == "address" || gChildList[q].nodeName.toLowerCase() == "h3"){
				//window.alert(gChildList[q].nodeName +": "+ gChildList[q].offsetHeight);
				innerHeight += gChildList[q].offsetHeight;
			}
		}
		output += Math.max(outterHeight, innerHeight) + 4;
		
		
	}
	return output;
 }


function expand(){
//clearInterval(expandInt);
	var elementToExpand = curdiv;
	var calcHeight = getTargetHeight(elementToExpand);
	var curHeight = elementToExpand.offsetHeight;
	elementToExpand.className = expandedClass;
	if(curHeight >= calcHeight){
		clearInterval(expandInt);
		elementToExpand.style.height = "auto";
	}else{
		elementToExpand.style.height = curHeight + increment +"px";
	}
}

function collapse(){
	var elementToCollapse = collapsediv;
	var calcHeight = collapsedHeight;
	var curHeight = elementToCollapse.offsetHeight;
	var distToGo = curHeight - calcHeight;
	var localIncrement = Math.min(increment, distToGo);
	elementToCollapse.className = collapsedClass;
	if(curHeight <= calcHeight){
		clearInterval(collapseInt);
	}else{
		elementToCollapse.style.height = curHeight - localIncrement +"px";
	}
}

