var DropDown = {
	myInt: false,
	myTime: false,
	hInc: 8,
	prev: false,

	init: function() {
		// find our drop down menus
		// attach events and what not...
		var drops = document.getElementsByTagName('ul');

		for(var i = 0; i < drops.length; i++) {
			ul = drops[i];

			if(Lib.matchClass(ul, 'dropDown')) {
				// find all expanding lis...
				expanding = ul.getElementsByTagName('li');

				for(var g = 0; g < expanding.length; g++) {
					li = expanding[g];

					Lib.addEvent(li, 'mouseover', DropDown.getLiOver(li));
					Lib.addEvent(li, 'mouseout', DropDown.getLiOut(li));
					Lib.addEvent(li, 'mouseover', DropDown.getKeepOpen(li));

					// assign our child UL...
					li.ul = li.getElementsByTagName('ul')[0];
					li.myInt = false;
					li.myCloseInt = false;
					li.myTime = false;
					li.opened = false;
					li.root = false;
				}

				// attach special property to root LI nodes...
				for(var k = 0; k < ul.childNodes.length; k++) {
					node = ul.childNodes[k];

					if(node.nodeName.toLowerCase() == 'li') {
						node.root = true;
					}
				}
			}
		}
	},

	getLiOver: function(node) {
		return function(e) { DropDown.liOver(e, node) }
	},

	getLiOut: function(node) {
		return function(e) { DropDown.liOut(e, node) }
	},

	getKeepOpen: function(node) {
		return function(e) { DropDown.keepOpen(e, node) }
	},

	keepOpen: function(e, li) {
		if(!(el = Lib.getTarget(e))) {
			return false;
		}

		if(li.myTime) {
			window.clearTimeout(li.myTime);
			li.myTime = false;
		}
	},

	liOver: function(e, li) {
		if(!(el = Lib.getTarget(e))) {
			return false;
		}

		// previous menu open?
		/*if(li.root && DropDown.prev && DropDown.prev != li) {
			DropDown.hideMenu(DropDown.prev);
			DropDown.prev = false;
		}*/

		// do some things for all li's...
		li.className = Lib.addClass(li, 'hover');

		// assign cursor...
		try {
			li.style.cursor = 'pointer';
		}

		catch(csserror) {
			li.style.cursor = 'hand';
		}

		// no!
		if(li.myInt) {
			return false;
			window.clearInterval(li.myInt);
			li.myInt = false;
		}

		// close one!
		if(li.myTime) {
			window.clearTimeout(li.myTime);
			li.myTime = false;
		}

		if(li.opened) {
			return false;
		}

		// make sure it's an expanding LI if we want to proceed
		if((typeof li.ul) == 'undefined') {
			return false;
		}

		// show our menu...
		DropDown.showMenu(li);
		li.opened = true;
	},

	liOut: function(e, li) {
		if(!(el = Lib.getTarget(e))) {
			return false;
		}

		// do some things for all li's...
		li.className = Lib.removeClass(li, 'hover');

		// make sure it's an expanding LI if we want to proceed
		if((typeof li.ul) == 'undefined') {
			return false;
		}

		// put back cursor...
		li.style.cursor = 'default';

		// give the user some time...
		li.myTime = window.setTimeout(function () { DropDown.hideMenu(li); }, 200);
	},

	hideMenu: function(li) {
		if(li.myInt) {
			window.clearInterval(li.myInt);
			li.myInt = false;
		}

		ul = li.ul;

		// start hiding
		if(!li.myCloseInt) {
			ul.rOffsetHeight = ul.offsetHeight;
			ul.rOffsetWidth = ul.offsetWidth;

			proportion = ul.rOffsetWidth / ul.rOffsetHeight;
			DropDown.wInc = DropDown.hInc * proportion;

			li.clipping = Array(0, ul.rOffsetWidth, ul.rOffsetHeight, 0);
			li.myCloseInt = window.setInterval(function() { DropDown.hideMenu(li); }, 10);
		}

		// just add to the height... possibly stop it...
		else {
			// calc new vals...
			li.clipping[1] -= DropDown.wInc;
			li.clipping[2] -= DropDown.hInc;
			both = true;

			if(li.clipping[1] <= 0) {
				li.clipping[1] = 0;
			}

			else {
				both = false;
			}

			if(li.clipping[2] <= 0) {
				li.clipping[2] = 0;
			}

			else {
				both = false;
			}

			// set the new clip...
			ul.style.clip = 'rect(' + li.clipping.join('px ') + 'px)';

			if(both) {
				ul.style.display = 'none';
				window.clearInterval(li.myCloseInt);
				li.myCloseInt = false;

				if(document.all && !window.opera) {
					ul.style.clip = 'rect(auto)';
				}

				else {
					ul.style.clip = 'auto';
				}
			}
		}

		li.myTime = false;
		li.opened = false;
	},

	showMenu: function(li) {
		ul = li.ul;

		if(li.myCloseInt) {
			window.clearInterval(li.myCloseInt);
			li.myCloseInt = false;
		}

		// start the display
		if(!li.myInt) {
			ul.style.display = 'block';
			ul.rOffsetHeight = ul.offsetHeight;
			ul.rOffsetWidth = ul.offsetWidth;
			ul.style.display = 'none';

			proportion = ul.rOffsetWidth / ul.rOffsetHeight;
			DropDown.wInc = DropDown.hInc * proportion;

			li.clipping = Array(0, 0, 0, 0);
			li.myInt = window.setInterval(function() { DropDown.showMenu(li); }, 10);
		}

		// just add to the height... possibly stop it...
		else {
			// calc new vals...
			li.clipping[1] += DropDown.wInc;
			li.clipping[2] += DropDown.hInc;
			both = true;

			if(li.clipping[1] >= ul.rOffsetWidth) {
				li.clipping[1] = ul.rOffsetWidth;
			}

			else {
				both = false;
			}

			if(li.clipping[2] >= ul.rOffsetHeight) {
				li.clipping[2] = ul.rOffsetHeight;
			}

			else {
				both = false;
			}

			// set the new clip...
			ul.style.clip = 'rect(' + li.clipping.join('px ') + 'px)';
			ul.style.display = 'block';

			if(both) {
				window.clearInterval(li.myInt);
				li.myInt = false;

				if(document.all && !window.opera) {
					ul.style.clip = 'rect(auto)';
				}

				else {
					ul.style.clip = 'auto';
				}
			}
		}
	}
}

Lib.addEvent(window, 'load', DropDown.init);