/********************************************************************



	menu.js - contains functions for generating dropdown menus 

	

	To make it work, initMenu() must be called sometime after the document has loaded

	

*********************************************************************/





// general purpose function to determine the position of an HTML element

function getElementPosition(element) {

	var offsetTop = 0, offsetLeft = 0;

	

	while (element) {

		offsetLeft += element.offsetLeft * 1;

		offsetTop += element.offsetTop * 1;

		element = element.offsetParent;

	}

	

	return {x : offsetLeft, y : offsetTop};

}



function recTree(el) {

	var l = 0;

	if (el.tagName == "A") {

		return el.scrollWidth;

	} else {

		var r;

		for (var i = 0; i < el.childNodes.length; i++) {

			r = recTree(el.childNodes[i]);

			if (r > l)

				l = r;

		}

		return l;

	}

}



function addDropDown(menuItem, dropDown) {

	menuItem.onmouseover = function() {

		var pos = getElementPosition(menuItem);

		

		dropDown.style.top = (pos.y + menuItem.offsetHeight - 1) + "px";

		dropDown.style.left = pos.x + "px";

		

		dropDown.style.display = "block";

		/*dropDown.style.width = "300px";*/

		

		// ugly hack for IE 6....

		if (navigator.userAgent.indexOf("MSIE") > -1) {

			if (navigator.userAgent.indexOf("MSIE 6") >= 0)

				dropDown.style.width = "10px";



			dropDown.style.width = (recTree(dropDown) + 1) + "px";

		}



		

		dropDown.onmouseover = function() {

			this.style.display = "block";

		}

		

		dropDown.onmouseout = function () {

			this.style.display = "none";

			this.style.width = "auto";

		}

	}

	

	menuItem.onmouseout = function() {

		dropDown.style.display = "none";

	}

}



function initMenu() {

	addDropDown(document.getElementById("servicesMenuItem"),  document.getElementById("servicesDropdown"));

	addDropDown(document.getElementById("aboutMenuItem"),  document.getElementById("aboutDropdown"));

	addDropDown(document.getElementById("contactMenuItem"),  document.getElementById("contactDropdown"));
	
	addDropDown(document.getElementById("directionsMenuItem"),  document.getElementById("directionsDropdown"));
	
}