
// ***************************************
// DropMenu Client Javascript Include File
// ***************************************

function DMInit(id) {

	// get this DropMenu (outer div)
	var DM = document.getElementById(id);
	
	// if IE, get this DropMenu's iframe
	// (masks out windowed elements such as dropdown lists)
	if (document.all)
		DM.IFrame = document.getElementById(id + ":IF");

	// call to show the DropMenu
	DM.Show = function(alignElement, offsetTop, offsetLeft) {

		// remember DropMenu is being shown
		// (to short circuit document.onclick from hiding it)
		DM.SuppressHide = true;
		
		// if id passed instead of object reference,
		// get the element to align the DropMenu with
		if (typeof alignElement == "string")
			alignElement = document.getElementById(alignElement);
		
		var top = GetDocumentOffsetTop(alignElement) + (offsetTop ? offsetTop : 0);
		var left = GetDocumentOffsetLeft(alignElement) + (offsetLeft ? offsetLeft : 0);
		this.style.display = "";
		var left_max = document.body.offsetWidth - this.offsetWidth - 20;
		if(left > left_max)
			left = left_max; 
		this.style.left = left + "px";
		this.style.top = top + "px"; 
		
		// if IE, show this DropMenu's iframe
		// (masks out windowed elements such as dropdown lists)
		if (document.all) {
			DM.IFrame.style.width = this.offsetWidth + "px";
			DM.IFrame.style.height = this.offsetHeight + "px";
			DM.IFrame.style.left = this.style.left;
			DM.IFrame.style.top = this.style.top;
			DM.IFrame.style.display = "";
		}
	};
	
	// call to hide the DropMenu
	DM.Hide = function() {

		// if DropMenu is being shown, short circuit
		// this hide triggered by document.onclick
		if (DM.SuppressHide) {
			DM.SuppressHide = false;
		} else {

			// hide the DropMenu
			DM.style.display = "none";

			// if IE, hide this DropMenu's iframe
			// (masks out windowed elements such as dropdown lists)
			if (document.all)
				DM.IFrame.style.display = "none";
		}
	};
	
	// iterate through this DropMenu's checkboxes setting the
	// onclick event to short circuit document.onclick from hiding it
	var IS = document.getElementsByName(id + ":IS");
	if (IS) {
		var length = IS.length;
		for (var i = 0; i < length; i++)
			IS[i].onclick = function(e) {e? e.stopPropagation(): event.cancelBubble = true};
	} 

	// add document.onclick handler
	if (document.attachEvent)
		document.attachEvent("onclick", DM.Hide);
	else if (document.addEventListener)
		document.addEventListener("click", DM.Hide, false);

	// add window.onresize handler
	if (window.attachEvent)
		window.attachEvent("onresize", DM.Hide);
	else if (window.addEventListener)
		window.addEventListener("resize", DM.Hide, false);
}

function GetDocumentOffsetTop(element) {
	var offsetTop = 0;
	while (element.offsetParent) {
		offsetTop += element.offsetTop;
		element = element.offsetParent;
	}
	return offsetTop;
}

function GetDocumentOffsetLeft(element) {
	var offsetLeft = 0;
	while (element.offsetParent) {
		offsetLeft += element.offsetLeft;
		element = element.offsetParent;
	}
	return offsetLeft;
}
