<!-- 
// New Beginnings Nursery: Functionality for navigation
// NH 29.06.2007

// Our global vars
var block;
var slideinterval;
var divheights = new Array();
var divIndex;
var ie;

// The delay between the slide in/out, and a little inertia
var slideMenuVelocity = 3;
var slideintervalinc = 1;

function initToggle(node)
{
	var thisNav = document.getElementById(node);
	toggle(thisNav);
}

addOnLoadAction("intiNav()");

function intiNav()
{
	// Detect whether the user has ie or not, as how we detect the height is different 
	var useragent = navigator.userAgent.toLowerCase();
	ie = ((useragent.indexOf('msie') != -1) 
		 && (useragent.indexOf('opera') == -1)
		 && (useragent.indexOf('webtv') == -1));

	var divs = getElementsByClassName(document, "div", "slideblock");
	var aTagToHighlight=null;

	for(var i=0; i<divs.length; i++)
	{
		if (divs[i].className.indexOf("level3")<0)
		{
			// Get the original height
			var baseheight = (ie) ? divs[i].offsetHeight + "px" : document.defaultView.getComputedStyle(divs[i], null).getPropertyValue('height', null);
	
			// Explicitly display it (optional, you could use cookies to toggle whether to display it or not)
			divs[i].style.display = "block";
	
			// "Save" our div height, because once it's display is set to none we can't get the original height again
			var d = new div();
			d.el = divs[i];
			d.ht = baseheight.substring(0, baseheight.indexOf("p"));

			// Hide the div
			divs[i].style.display = "none";

			// Store our saved version
			divheights[i] = d;
		}
	}
}

// This is one of our divs, it just has a DOM reference to the element and the original height
function div(_el, _ht, _currentHeight, _velocityInc)
{
	this.el = _el;
	this.ht = _ht;
	this.currentHeight = _currentHeight;
	this.velocityInc = _velocityInc;
}

function toggle(t)
{
	// Reset our inertia base and interval
	clearInterval(slideinterval);

	// Initialise slide speeds
	for(var i=0; i<divheights.length; i++)
	{
		divheights[i].velocityInc=slideMenuVelocity;
		divheights[i].currentHeight=-1;
	}

	// Get our block
	block = t.parentNode.nextSibling;

	// For mozilla, it doesn't like whitespace between elements
	if(block.className == undefined)
	{
		block = t.parentNode.nextSibling.nextSibling;
	}
	
	divIndex = getDivIndex(block);

	if(block.style.display == "none")
	{
		// Slide out
		if (divheights[divIndex].ht!=0)
		{	
			selectLevel1Item(t.parentNode, true);

			block.style.display = "block";
			block.style.visibility = "visible";
			block.style.height = "1px";
			block.style.position = "relative";

			// Our goal and current height
			divheights[divIndex].currentHeight=1;
			
			// Our interval
			slideinterval = setInterval(slideout, slideintervalinc);
		}
	}
	else
	{
		// Slide in
		selectLevel1Item(t.parentNode, false);
		divheights[divIndex].currentHeight=divheights[divIndex].ht;

		// Our interval
		slideinterval = setInterval(slidein, slideintervalinc);
	}
}

// Unselect all the level one titles except for the supplied "level1TitleToSelect"
function selectLevel1Item(level1TitleToSelect, selectFlag)
{
	if (level1TitleToSelect!=null
		&& level1TitleToSelect.className!=null
		&& level1TitleToSelect.className.indexOf("level1Title")>-1)
	{
		var divs = getElementsByClassName(level1TitleToSelect.parentNode.parentNode, "div", "level1Title");
		for(var i=0; i<divs.length; i++)
		{
			if(divs[i]==level1TitleToSelect && selectFlag)
			{
				divs[i].className=divs[i].className+" level1TitleSelected";
			}
			else
			{
				divs[i].className=divs[i].className.replace(" level1TitleSelected","");
			}
		}
	}
}

// This is our slidein function the interval uses, it keeps subtracting from the height till it's 1px then hides it
function slidein()
{
	var targetheight = 1;
	var heightnow = divheights[divIndex].currentHeight;
	
	if(heightnow > targetheight)
	{
		// Reduce the height by intertiabase * inertiainc
		heightnow -= divheights[divIndex].velocityInc;

		// Increase the intertiabase by the amount to keep it changing
		divheights[divIndex].velocityInc += slideMenuVelocity;

		// (t's possible to exceed the height we want so we use a ternary - (condition) ? when true : when false;
		block.style.height = (heightnow > 1) ? heightnow + "px" : targetheight + "px";
		divheights[divIndex].currentHeight=heightnow;
	}
	else
	{
		// Finished, so hide the div properly and kill the interval
		clearInterval(slideinterval);
		block.style.height = targetheight + "px";
		block.style.display = "none";
	}
}

// This is the function our slideout interval uses, it keeps adding to the height till it's fully displayed
function slideout()
{
	var targetheight = divheights[divIndex].ht;
	var heightnow = divheights[divIndex].currentHeight;
	var slideOutDone=0;
	if(heightnow < targetheight)
	{
		// Increases the height by the inertia stuff
		heightnow += divheights[divIndex].velocityInc;

		// Increase the inertia stuff
		divheights[divIndex].velocityInc += slideMenuVelocity;
	}
	else
	{
		// Finished, so make sure the height is what it's meant to be (inertia can make it off a little)
		// then kill the interval
		block.style.height = targetheight + "px";
		slideOutDone=1;
	}
	
	var foundOpenMenu=0;
	var divs = getElementsByClassName(block.parentNode.parentNode, "div", "slideblock");
	for(var j=0; j<divs.length; j++)
	{
		var i=getDivIndex(divs[j]);
		
		if(i!=divIndex)
		{
			if (divheights[i].el.style.display!="none")
			{
				var targetheight2 = 1;
			
				if(divheights[divIndex].currentHeight=-1)
				{
					var baseheight = (ie) ? divheights[i].el.offsetHeight + "px" : document.defaultView.getComputedStyle(divheights[i].el, null).getPropertyValue('height', null);
					divheights[i].currentHeight = baseheight.substring(0, baseheight.indexOf("p"));
				}
				var heightnow2 = divheights[i].currentHeight;

				if(heightnow2 > targetheight2)
				{
					// Reduce the height by intertiabase * inertiainc
					heightnow2 -= divheights[i].velocityInc;

					// Increase the intertiabase by the amount to keep it changing
					divheights[i].velocityInc += slideMenuVelocity;
					
					if ((heightnow2)>targetheight2)
					{
						foundOpenMenu=1;
						divheights[i].el.style.height =  heightnow2 + "px";
						divheights[i].currentHeight=heightnow2;
					}
					else
					{
						divheights[i].el.style.height = "1px";
						divheights[i].el.style.display = "none";
						divheights[i].el.style.position = "absolute";
					}
				}
				break;
			}
		}
	}
	
	block.style.height = (heightnow < targetheight) ? heightnow + "px" : targetheight + "px";
	divheights[divIndex].currentHeight=heightnow;

	if(foundOpenMenu==0 && slideOutDone==1)
	{
		clearInterval(slideinterval);
	}
}

// Returns the height of the div from our array of such things
function divheight(d)
{
	for(var i=0; i<divheights.length; i++)
	{
		if(divheights[i].el == d)
		{
			return divheights[i].ht;
		}
	}
}

// Returns the index of the dev 
function getDivIndex(d)
{
	for(var i=0; i<divheights.length; i++)
	{
		if(divheights[i].el == d)
		{
			return i;
		}
	}
}

// Changes nav background image to "on" state - NH 25.06.2007
function roll(node,divClass)
{
	var thisNav = document.getElementById(node);
	
	switch (divClass)
	{
		case 1:
			thisNav.parentNode.className = thisNav.parentNode.className + " level1TitleHighlighted";
			break;
		case 2:
			thisNav.parentNode.className = thisNav.parentNode.className.replace(" level1TitleHighlighted","");
			break;
		case 3:
			thisNav.parentNode.className = "level2TitleOn";
			break;
		case 4:
			thisNav.parentNode.className = "level2Title";
			break;
	}
}

// -->
