// collapsable navigation menu
// currently configured for vertical, with only one level of children

TDCollapseNav = function() {
	var self = this;
	
	var TDNavParents = {};
	var TDNavChildren = {};
	var TDNavIcons = {};
	var ParentCount = 0;
	
	// tweening variables
	var timer;
	var index = new Array(); // expand index will be 0, collapse index will be 1
	var height = new Array(); // expand index will be 0, collapse index will be 1
	var step = new Array(); // expand index will be 0, collapse index will be 1
	
	function init() {
		var tags = document.getElementsByTagName('span');
		if (tags) {
			for (var x = 0; x < tags.length; x++) {
				if (tags[x].className.indexOf('TDNavParent') >= 0) {
					TDNavParents[ParentCount] = tags[x];
					ParentCount++;
				}
			}
		}

		if (TDNavParents) {
			for (var x = 0; x < ParentCount; x++) {
				AddEvent(TDNavParents[x], "mouseover", NavToggle);
				
				TDNavChildren[x] = document.getElementById(TDNavParents[x].id.replace('Parent', 'Child'));
				TDNavChildren[x].style.height = "0px";
				TDNavChildren[x].style.overflow = "hidden";
				
				TDNavIcons[x] = document.getElementById(TDNavParents[x].id.replace('Parent', 'Icon'));
				AddEvent(TDNavIcons[x], "mouseover", NavToggle);
			}
		}
	};
	
	function NavToggle(e) {
		e = e ? e : event;
		if (!e.target) e.target = e.srcElement;
		
		var collapsecount = 1;
		index = new Array();
		index[0] = -1;

		for (var x = 0; x < ParentCount; x++) {
			if (TDNavParents[x].id == e.target.id || TDNavIcons[x].id == e.target.id) {
				index[0] = x;
				var resetheight = TDNavChildren[x].offsetHeight;
				
				TDNavChildren[x].style.visibility = "hidden";
				TDNavChildren[x].style.height = "auto";
				height[0] = TDNavChildren[x].offsetHeight;
				if (height[0] == 0) index[0] = -1;
				TDNavChildren[x].style.height = resetheight + "px";
				TDNavChildren[x].style.visibility = "visible";
				step[0] = Math.floor((height[0] - resetheight) / 10);
				if (step[0] < 1) step[0] = 1;
				TDNavIcons[x].src = TDNavIcons[x].src.replace("arrow_right", "arrow_down");
			} else {
				if (TDNavChildren[x].offsetHeight > 0) {
					index[collapsecount] = x;
		
					height[collapsecount] = 0;
					step[collapsecount] = Math.floor(TDNavChildren[x].offsetHeight / 10) * -1;
					if (step[collapsecount] > -1) step[collapsecount] = -1;
					TDNavIcons[x].src = TDNavIcons[x].src.replace("arrow_down", "arrow_right");
					collapsecount++;
				}
			}
		}
		if (timer) window.clearInterval(timer);
		timer = window.setInterval(function() { Tween(); }, 20);
	};
	
	function Tween() {
		var stopcount = 0
		
		if (index.length > 1) {
			for (var x = 1; x < index.length; x++) {
				if (index[x] > -1) {
					if (TDNavChildren[index[x]].offsetHeight + step[x] <= 0) {
						TDNavChildren[index[x]].style.height = "0px";
						stopcount++;
					} else {
						TDNavChildren[index[x]].style.height = (TDNavChildren[index[x]].offsetHeight + step[x]) + "px";
					}
				} else {
					stopcount++;
				}
			}
		}
		
		if (index[0] > -1) {
			if (TDNavChildren[index[0]].offsetHeight + step[0] >= height[0]) {
				TDNavChildren[index[0]].style.height = height[0] + "px";
				stopcount++;
			} else {
				TDNavChildren[index[0]].style.height = (TDNavChildren[index[0]].offsetHeight + step[0]) + "px";
			}
		} else {
			stopcount++;
		}
		
		if (stopcount >= index.length) window.clearInterval(timer);
	};
	
	init();
};