/*****************************************************
 ** Dynamic hide-reveal for Tenant Representation page
 ** Sunil Karve 11/04/2007
 ** contracted to Popular Forces, Inc.
 *****************************************************/

var index; // Global variable for currently chosen term
var prevTargetID; // Last selected definition term's ID

function initTenantRepDL() {
/* Add show-hide functionality to each listing */

	if( !document.getElementById ) { return; }

	var defList = document.getElementById("tenantRepDL");

	var itemCounter = 0; // Index to group together terms and multiple definitions

	// Get all first-level child nodes (i.e. <dt>s and <dd>s )
	var defListContent = defList.childNodes;

	// Add event handlers, hide all <dd> and tag with classnames
	for( var i=0; i < defListContent.length; i++ ) {
		if( defListContent[i].nodeName == "DT" ) {
			itemCounter++;
			addEvent( defListContent[i].childNodes[0], "click", showTenantRepItem );
			defListContent[i].childNodes[0].setAttribute("id", "term" + itemCounter );
			defListContent[i].childNodes[0].className = "bulletExpand";
		}
		if( defListContent[i].nodeName == "DD" ) {
			defListContent[i].style.display = "none";
			defListContent[i].className = "definition" + itemCounter;			
		}
	}
}

function showTenantRepItem(e) {
	if( !document.getElementById ) { return; }
	var targ;
	var targetID;
//	var index;

	if (!e) { var e = window.event; }
	if (e.target) { targ = e.target; }
	else if (e.srcElement) { targ = e.srcElement; }
	if (targ.nodeType == 3) {// Defeat Safari bug
		targ = targ.parentNode;
	}	
	
	// Hide all DDs
	var defList = document.getElementById("tenantRepDL");
	var allDDs = defList.getElementsByTagName("DD");
	for( var j=0; j < allDDs.length; j++ ) {
		allDDs[j].style.display = "none";
	}
	// Reveal only the DDs tied to the clicked definition term
	targetID = targ.getAttribute("id");
//	if( targetID.charAt(4) == index ) { index = 0; return; } // If same definition clicked twice, hide all (keep all hidden)

	if( prevTargetID != null ) {
		document.getElementById(prevTargetID).className = "bulletExpand";
	}
	
	
	if( targetID == prevTargetID ) { 
		prevTargetID = null; 
			if (e.stopPropagation) { 
				e.stopPropagation();
				e.preventDefault();
			}
			return false;
	} // Reveal nothing if same item clicked twice
	targ.className = "bulletCollapse"; // Change to "collapse" icon
	index = targetID.charAt(4);
	
	for( var k=0; k < allDDs.length; k++ ) {
		if( allDDs[k].className == "definition" + index ) {
			allDDs[k].style.display = "block";		
		}
		prevTargetID = targetID;	
	}
	
	if (e.stopPropagation) { // Prevent leap to page top in Firefox
		e.stopPropagation();
		e.preventDefault();
	}
	
	return false; // Prevent leap to page top in IE
	
	
}