/*
   Milonic DHTML Menu Keypress Module  keypress.js version 1.3 - February 2005
   Used for accessibility of the menu. This module will allow the user to navigate Milonic menus via the keyboard.
   
   This module is only compatible with the Milonic DHTML Menu version 5.16 or higher

   Copyright 2005 (c) Milonic Solutions Limited. All Rights Reserved.
   This is a commercial software product, please visit http://www.milonic.com/ for more information.
*/

// Object that wrapps up all behavioral changes and bug fixes.
// These are mostly corrections to bugs
//
var g_ObjBehaviorChanges = new ObjBehaviorChanges();

function keyAction(_inc)
{
	_itemRef+=_inc
	_popi(_itemRef)
}



function getNextKeyItem()
{
	_Kar=_KcM[0]
	_output=0;
	for(_a=0;_a<_Kar.length;_a++)
	{
		if(_Kar[_a]==_itemRef)_output=_Kar[_a+1]
		if(_output+" "=="undefined ")return _Kar[0]		
	}
	return _output	
	
}

function getPreKeyItem()
{
	_Kar=_KcM[0]
	_output=0;
	for(_a=0;_a<_Kar.length;_a++)
	{
		if(_Kar[_a]==_itemRef)_output=_Kar[_a-1]
		if(_output+" "=="undefined ")return _Kar[_Kar.length-1]		
	}
	return _output
}


function getKeyItem()
{	
	_KcM=_m[_mi[_itemRef][0]]; // this is the array of menu items for the current menu
	_KcI=_mi[_itemRef]
	
	if(jskey==27) // Escape
	{
		closeAllMenus();
		return false;
	}

	if(_KcM[9]) // Horizontal
	{	

        // Behavioral change:
        // Skip the first menu item, since it is hidden
        //
        //if(jskey==37)_popi(getPreKeyItem()) // left
        //if(jskey==39)_popi(getNextKeyItem()) //right
		if(jskey==37) g_ObjBehaviorChanges.moveToPrevMenu(); // left
		if(jskey==39) g_ObjBehaviorChanges.moveToNextMenu(); //right


        // Only include menu up/down navigation if a menu is open,
        // otherwise, the enter key should be used to open the menu.
        if (g_ObjBehaviorChanges.isMenuOpen() == true)
        {
            if(jskey==38)
            {
                _mn=getMenuByName(_mi[_itemRef][3])
                _popi(_m[_mn][0][_m[_mn][0].length-1])

            }
            if(jskey==40) // down
            {
                _mn=getMenuByName(_mi[_itemRef][3])
                _popi(_m[_mn][0][0])
            }
        }
	}
	else       // Vertical
	{
		if(jskey==38)_popi(getPreKeyItem()) // up
		if(jskey==40)_popi(getNextKeyItem()) //down

		if(jskey==37) // left
		{
			//alert(_mi[_itemRef][3])
			if(_mi[_itemRef][3])
			{
				_mn=getMenuByName(_mi[_itemRef][3])
				_popi(_m[_mn][0][_m[_mn][0].length-1])
			}
			else
			{
				_itemRef=getParentItemByItem(_itemRef);
				_KcM=_m[_mi[_itemRef][0]];
				_popi(getPreKeyItem())
				_popi(getNextKeyItem())
				//_popi(_itemRef)
			}
		}
		if(jskey==39) // right
		{
			if(_mi[_itemRef][3])
			{
				_mn=getMenuByName(_mi[_itemRef][3])
				_popi(_m[_mn][0][0])
			}
			else
			{
				_mni=_itemRef
				cnt=0;
				while(_mni!=-1)
				{
					if(_mni)_mni=getParentItemByItem(_mni); else _mni=-1
					if(_mni>-1)_itemRef=_mni					
				}
				_KcM=_m[_mi[_itemRef][0]];
				_popi(getNextKeyItem())
			}
		}
	}
}


KPgChildren="";
function KPcrawlChildren(_mn)
{
    //alert("here" + _m);
    var _ar=_m[_mn][0]

	for(var _am=0;_am<_ar.length;_am++)	
	{
		KPgChildren+=_ar[_am]+",";
		if(_mi[_ar[_am]][3])
		{
			KPcrawlChildren(getMenuByName(_mi[_ar[_am]][3]))
		}
	}
}

KPcrawlChildren(0)
KPgChildren=KPgChildren.split(",");

function getNextInRow()
{
	for(_k=0;_k<KPgChildren.length;_k++)
	{
		if(_itemRef==KPgChildren[_k])
		{
			if(KPgChildren[_k+1])return KPgChildren[_k+1]
		}
	}
}

function getPreInRow()
{
	
	
	for(_k=0;_k<KPgChildren.length;_k++)
	{
		if(_itemRef==KPgChildren[_k])
		{
			if(KPgChildren[_k-1])return KPgChildren[_k-1]
		}
	}
}


_Omo=_menuOpenDelay
_Cmo=_menuCloseDelay

_OiR=-1

KShift=0
KCtrl=0
function getMenuByKey(e)
{
    _ofMT=1
	_menuOpenDelay=0
	_menuCloseDelay=0
	
	if(ns6||ns4){jskey=e.which}else{jskey=event.keyCode}
	if(ns4){jskey=String.fromCharCode(jskey).toUpperCase();jskey=jskey.charCodeAt()}

	if(jskey==16)KShift=1
	if(jskey==17)KCtrl=1


    // behavioral change:
    // Set focus to the first menu, based on some keystroke
    //
    g_ObjBehaviorChanges.setMenuFocus(jskey, KCtrl, KShift);


	if(jskey==9)
	{
		if(_itemRef<-1)
		{
			return false;
		}
		else
		{
            if(KShift)
            {
                if(_itemRef>-1)
                {
                    // Behavioral change:
                    // Move to previous menu using the alt-tab key
                    //
                    //_itemRef=getPreInRow()
                    g_ObjBehaviorChanges.moveToPrevMenu();
                }
                if(_itemRef==0)closeAllMenus()
            }
            else
            {

                // Behavioral change:
                // Move to previous menu using the alt-tab key
                //
                //_itemRef=getNextInRow()
                g_ObjBehaviorChanges.moveToNextMenu();
            }

            if(_itemRef>-1)
            {
                _popi(_itemRef);
                _OiR=_itemRef
            }
            else
            {
                itemOff(_OiR)
                return true;
            }
		}
	}

	if(_itemRef==-1||_itemRef+" "==$u)
	{
		_rsMD()
		return
	}
	if(_mi[_itemRef][34]=="form")
	{
		_rsMD()
		return
	}


	_cm()
	getKeyItem()

	if(_itemRef==-1)
	{
		_rsMD()
		return
	}
	

	if(!ns4)
	{
		hrgm=gmobj("mmlink"+_mi[_itemRef][0])
		if(hrgm.style.visibility="visible")hrgm.focus()	
	}
	
	if(ie)
	{
		if(jskey==13)hrgm.click()
		_rsMD()		
		return false
	}
}


function gMBK(e)
{
	if(ns6||ns4){jskey=e.which}else{jskey=event.keyCode}
	if(ns4){jskey=String.fromCharCode(jskey).toUpperCase();jskey=jskey.charCodeAt()}
	if(jskey==13)return true	
	if(_itemRef>-1)if(_mi[_itemRef][34]!="form")return false
}


function getMenuByKeyU(e)
{	
	if(ns6||ns4){jskey=e.which}else{jskey=event.keyCode}
	if(ns4){jskey=String.fromCharCode(jskey).toUpperCase();jskey=jskey.charCodeAt()}
	if(jskey==16)KShift=0
	if(jskey==17)KCtrl=0
	_rsMD()
}

function _rsMD()
{
	_ofMT=0
	_menuOpenDelay=_Omo
	_menuCloseDelay=_Cmo
}


function _iF0C(_i)
{
	_popi(_i)
}

if(ns4)document.captureEvents(Event.KEYDOWN);
_d.onkeydown=getMenuByKey;
_d.onkeypress=gMBK;
_d.onkeyup=getMenuByKeyU;


//////////////////////////////////////////////////////////////
//
// ObjBehaviorChanges
//
// This object centralizes changes made to this file into
// one area so that it is easier to roll back those
// changes when Milonic fixes their bugs.
//
//////////////////////////////////////////////////////////////
function ObjBehaviorChanges()
{
    ObjBehaviorChanges.prototype.setMenuFocus   = setMenuFocus;
    ObjBehaviorChanges.prototype.moveToNextMenu = moveToNextMenu;
    ObjBehaviorChanges.prototype.moveToPrevMenu = moveToPrevMenu;
    ObjBehaviorChanges.prototype.isMenuOpen     = isMenuOpen;
    ObjBehaviorChanges.prototype.F2             = 113;

    /////////////////////////////////////////////////////////
    //
    // setMenuFocus
    //
    // Set focus to the menu based on a keystroke
    //
    /////////////////////////////////////////////////////////
    function setMenuFocus(nKey, nCtrl, nShift)
    {
        // Set focus to the  menu if the user uses
        // the F2 keystroke
        if (nKey == this.F2)
		{
			_popi(1);
		}
    }

    /////////////////////////////////////////////////////////
    //
    // moveToNextMenu
    //
    // Change focus to the next menu item
    //
    /////////////////////////////////////////////////////////
    function moveToNextMenu()
    {
        moveMenu("Next");
    }

    /////////////////////////////////////////////////////////
    //
    // moveToPrevMenu
    //
    // Change focus to the previous menu item
    //
    /////////////////////////////////////////////////////////
    function moveToPrevMenu()
    {
        moveMenu("Previous");
    }

    /////////////////////////////////////////////////////////
    //
    // moveMenu
    //
    // Change focus to either the next or previous top
    // level meny item
    //
    /////////////////////////////////////////////////////////
    function moveMenu(strDirection)
    {
        var objTargetMenu = null;

        if (_mi[_itemRef])
        {
            // Initialize values
            _KcM=_m[_mi[_itemRef][0]]; // this is the array of menu items for the current menu
            _KcI=_mi[_itemRef];

            if (strDirection == "Next")
            {
                // Move focus forward
                objTargetMenu = getNextKeyItem();

                // Skip the very first (hidden menu item)
                if (objTargetMenu == _Kar[0])
                {
                    objTargetMenu = _Kar[1];
                }
            }
            else
            {
                // Move focus backward
                objTargetMenu = getPreKeyItem();

                // Skip the very first (hidden menu item)
                if (objTargetMenu == _Kar[0])
                {
                    objTargetMenu = _Kar[_Kar.length - 1];
                }
            }

            // Set focus
            _popi(objTargetMenu);
        }
    }

    /////////////////////////////////////////////////////////
    //
    // isMenuOpen
    //
    // Return whether or not a menu is currently open
    //
    /////////////////////////////////////////////////////////
    function isMenuOpen()
    {
        return _gDs.visibility != "hidden";
    }
}

