/* Playing with Dropdown Menues... */
/* dropDown.js -- DropDownMenues in the header... 
 * 
 * Version 0.1 -- Getting serious
 *                Also changes the color of the title of the selected menu
 * 
 * Copyright 2009 - 2010 Johannes Markus
 * All Rights Reserved!
 *
 */


var dropDownMenu =
{
	// You can modify these:
	titleHLightColor: "#2222FF",
	titleNormalColor: "#FFF",


	// Don't change these:
	droppedMenu: false,
	busy: false,
	startHeight: false,
	nextMenu: false,


	// The functions start here:
	dropMenu: function()
	{
		if(!dropDownMenu.busy)
		{
			dropDownMenu.busy = true;
			dropDownMenu.droppedMenu.parentNode.style.maxHeight = "0.5em"
		}

		/* Find the proper height */
		var destHeight = dropDownMenu.droppedMenu.getElementsByTagName('li').length * 1.2;	
		var currentHeight = parseFloat(dropDownMenu.droppedMenu.parentNode.style.maxHeight);
		var d = currentHeight - 0.5;
		
		if(currentHeight < destHeight)
		{
			var newHeight = currentHeight + 
			( 0.5 * Math.cos((d-(destHeight-0.5)/2)*1.55/(destHeight-0.5)));
			dropDownMenu.droppedMenu.parentNode.style.maxHeight = newHeight + "em";
			setTimeout(dropDownMenu.dropMenu, 0.1);
		}
		else
		{
			dropDownMenu.droppedMenu.getElementsByTagName("a")[0].style.color = 
				dropDownMenu.titleHLightColor; 
			dropDownMenu.busy = false;
		}
	},
	collapseMenu: function()
	{
		if(!dropDownMenu.busy)
		{
			dropDownMenu.busy = true;
			dropDownMenu.startHeight =
				 parseFloat(dropDownMenu.droppedMenu.parentNode.style.maxHeight);	
		}

		/* Find the proper height */
		var destHeight = 0.5;
		var currentHeight = parseFloat(dropDownMenu.droppedMenu.parentNode.style.maxHeight);
		var d = currentHeight - 0.5;
		
		if(currentHeight > destHeight)
		{
			var startHeight = dropDownMenu.startHeight;
			var newHeight = currentHeight - 
			( 0.5 * Math.cos((d-(startHeight-0.5)/2)*1.55/(startHeight-0.5)));
			dropDownMenu.droppedMenu.parentNode.style.maxHeight = newHeight + "em";
			setTimeout(dropDownMenu.collapseMenu, 0.1);
		}
		else
		{
			dropDownMenu.droppedMenu.parentNode.style.maxHeight = "0.5em";
			dropDownMenu.droppedMenu.getElementsByTagName("a")[0].style.color = 
				dropDownMenu.titleNormalColor;
			dropDownMenu.droppedMenu = false;
			dropDownMenu.busy = false;
			if(dropDownMenu.nextMenu)
			{
				dropDownMenu.droppedMenu = dropDownMenu.nextMenu;
				dropDownMenu.nextMenu = false;
				dropDownMenu.dropMenu();
			}
		}
	},
	init: function()
	{
		var theMenuList = document.getElementById('mainMenu').getElementsByTagName('li');
		var currentMenu = false;
		var i = 0;	/* A Counter */

		for(i=0;i<theMenuList.length;i++)
		{
			if(theMenuList[i].firstChild.className == "subMenu")
			{
				currentMenu = theMenuList[i].firstChild; /* This is the ul */
				dropDownMenu.addEvent(currentMenu, 'click', dropDownMenu.menuSelect);
				/* Disable the link */
				currentMenu.getElementsByTagName("a")[0].setAttribute("href", "#");
			}
		}
	},

	menuSelect: function()
	{
		if(!dropDownMenu.busy)
		{
			if(!dropDownMenu.droppedMenu)
			{
				dropDownMenu.droppedMenu = this;
				dropDownMenu.dropMenu();
			}	
			else if(dropDownMenu.droppedMenu == this)
				dropDownMenu.collapseMenu();	
			else
			{
				dropDownMenu.nextMenu = this;
				dropDownMenu.collapseMenu();
			}
		}
	},


	addEvent: function(obj, type, fn)
	{
		if(obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if(obj.attachEvent)
		{
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );};
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};		


/* Initialize dropDownMenu when the document loads */
dropDownMenu.addEvent(window, 'load', dropDownMenu.init);

