////////////////////////////////////////////////////////////////////////////////
// Dropdown menu for websites                                                 //
// Copyright © 2005 InleadMedia ApS                                           //
//                                                                            //
// This program is free software; you can redistribute it and/or              //
// modify it under the terms of the GNU General Public License                //
// as published by the Free Software Foundation; either version 2             //
// of the License, or (at your option) any later version.                     //
//                                                                            //
// This program is distributed in the hope that it will be useful,            //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with this program; if not, write to the Free Software                //
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.//
////////////////////////////////////////////////////////////////////////////////
/**
 * JavaScript for the dropdown menu.
 *
 * This file is include on the page through the menu/dropdown.tpl file.
 *
 * @author Karto <karto@karto.net>
 * @version $Id: dropdown.js 7411 2007-06-06 09:50:40Z larsson $
 * @copyright Copyright © 2005 InleadMedia ApS
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @package Menu
 * @subpackage Dropdown
 */
/**
 * Internet Explore workaround function
 * 
 * IE/Win only allows the :hover pseudo-class to be applied to a link — 
 * so the li:hover that makes the sub-menus appear means nothing to IE.
 * A tiny jot of JavaScript is required to kick IE back into action:
 * 
 * @package Common
 * @subpackage Base
 */

/* need this var to be global */
var activeMenuId = '';
var autoCloseTimeOutId = '';

function activateAutoClose(time)
{
	currentMenuId = activeMenuId;
	//clean up
	if (autoCloseTimeOutId) clearTimeout(autoCloseTimeOutId);
	// set the new timer
	autoCloseTimeOutId = setTimeout('autoCloseActiveMenu(currentMenuId)', time);
}

function autoCloseActiveMenu(id)
{
	if (!activeMenuId) return;
	if (activeMenuId != id) return;
	document.getElementById(id).className = document.getElementById(id).className.replace(" over", "");
	activeMenuId = '';
}


// This function is only for Mozilla / Safari
function keyboardControl() 
{
	var nodeList = document.getElementsByTagName('LI');
	for(var i = 0; i < nodeList.length; ++i)
	{
		if (-1 == nodeList[i].className.indexOf('dropdownNode')) continue;
		if (0 == nodeList[i].getElementsByTagName('UL').length) continue;
		if (0 == nodeList[i].getElementsByTagName('A').length) continue;
		with (nodeList[i].getElementsByTagName('A')[0])
		{
			// Action when mouse moves onto from the node
			nodeList[i].getElementsByTagName('A')[0].onfocus = function () 
			{					
				if (activeMenuId && activeMenuId == this.parentNode.id) return;
				
				if (activeMenuId && activeMenuId != this.parentNode.id)
				{
					document.getElementById(activeMenuId).className = document.getElementById(activeMenuId).className.replace(" over", "");
					activeMenuId = '';
				}
				activeMenuId = this.parentNode.id;									
				this.parentNode.className += " over";

				//How many level2 items ?
				numerOfLevel2Items = document.getElementById(activeMenuId).getElementsByTagName('LI').length;
				// What should the automatic timeout be 1000 = 1sec.
				activateAutoClose((numerOfLevel2Items * 3000));
				
			};
			// Action when mouse moves away from the node
			
		}
	}
}

// Only for Mozilla/Safari
if (window.addEventListener) 
{
  window.addEventListener('load',keyboardControl,false);
} 
else if (document.addEventListener)
{
  document.addEventListener('load',keyboardControl,false);
}
//else if (window.attachEvent && -1 != window.navigator.userAgent.indexOf('MSIE 7.0'))
//{
//	window.attachEvent('onload', keyboardControl);
//}
//else if (window.attachEvent && -1 != window.navigator.userAgent.indexOf('MSIE 6.0'))
//{
//	window.attachEvent('onload', keyboardControl);
//}


try
{
	//Special mode for IE6.0 and IE7.0
	if ((-1 != window.navigator.userAgent.indexOf('MSIE 6.0') && -1 != window.navigator.userAgent.indexOf('Windows'))
		|| (window.attachEvent && -1 != window.navigator.userAgent.indexOf('MSIE 7.0')))
	{
		window.attachEvent('onload', function ()
		{
			try
			{
				var nodeList = document.getElementsByTagName('LI');
				for(var i = 0; i < nodeList.length; ++i)
				{
					if (-1 == nodeList[i].className.indexOf('dropdownNode')) continue;
					if (0 == nodeList[i].getElementsByTagName('UL').length) continue;
					
					// Add events
					with (nodeList[i])
					{
						// Action when mouse moves onto from the node
						onmouseover = function () 
						{
							if (activeMenuId && this.id != activeMenuId)
							{
								document.getElementById(activeMenuId).className = document.getElementById(activeMenuId).className.replace(" over", "");
							}
							this.className += " over";
							activeMenuId = this.id;							
						};
						// Action when mouse moves away from the node
						onmouseout = function () 
						{
							this.className = this.className.replace(" over", ""); 
						};
					}
					
					if (0 == nodeList[i].getElementsByTagName('A').length) continue;
					with (nodeList[i].getElementsByTagName('A')[0])
					{
						// Action when mouse moves onto from the node
						onfocus = function () 
						{					
							if (activeMenuId && activeMenuId == this.parentNode.id) return;
							
							if (activeMenuId && activeMenuId != this.parentNode.id)
							{
								document.getElementById(activeMenuId).className = document.getElementById(activeMenuId).className.replace(" over", "");
								activeMenuId = '';
							}
							activeMenuId = this.parentNode.id;									
							this.parentNode.className += " over";
	
							//How many level2 items ?
							numerOfLevel2Items = document.getElementById(activeMenuId).getElementsByTagName('LI').length;
							// What should the automatic timeout be 1000 = 1sec.
							activateAutoClose((numerOfLevel2Items * 3000));
							
						};
						// Action when mouse moves away from the node
						
					}
				}
			}
			catch (e)
			{
				alert('Unable initialize dropdown menu.');
			}
		});
	}
}
catch (e)
{
	alert('Unable add dropdown menu initializer.');
}

// DEBUG
if (0)
{
	alert('navigator: '+window.navigator+'\n'+
			'appCodeName: '+window.navigator.appCodeName+'\n'+
			'appName: '+window.navigator.appName+'\n'+
			'appVersion: '+window.navigator.appVersion+'\n'+
			'cookieEnabled: '+window.navigator.cookieEnabled+'\n'+
			'language: '+window.navigator.language+'\n'+
			'mimeTypes: '+window.navigator.mimeTypes+'\n'+
			'oscpu: '+window.navigator.oscpu+'\n'+
			'platform: '+window.navigator.platform+'\n'+
			'plugins: '+window.navigator.plugins+'\n'+
			'product: '+window.navigator.product+'\n'+
			'productSub: '+window.navigator.productSub+'\n'+
			'userAgent: '+window.navigator.userAgent+'\n'+
			'vendor: '+window.navigator.vendor+'\n'+
			'vendorSub: '+window.navigator.vendorSub+'\n');
}