// UDMv4.44 // Keyboard Navigation Help extension v1.12 //
/***************************************************************\

  ULTIMATE DROP DOWN MENU Version 4.44 by Brothercake
  http://www.udm4.com/
  
\***************************************************************/

/***************************************************************\
 * Set help text: must be plain-text only, no entitites or HTML 
 * Do not change where it says "<hotkey>"
\***************************************************************/

var helpText = ''
             + 'The "F12" key sends focus to the navigation bar. '
             + 'You can move around the menus with the Arrow Keys,  '
             + 'activate a link by pressing "Enter", '
             + 'or jump back onto the page with "F12" again. '
             + '';
		
/***************************************************************\
 * Show a permanent hotkey icon ("yes")
 * Or only show it until instructions have been displayed ("no")
\***************************************************************/

var permanentIcon = 'yes';

/***************************************************************\
\***************************************************************/







//container object
var kn=new Object;

//help layer object
kn.hlp=null;




//add receiver for navbar initialised event
um.addReceiver(addButton,'010');


//add receiver for link focus event
um.addReceiver(showHelp,'040');



//add hotkey button indicator to first link
function addButton(navbar)
{
	//if keyboard navigation is fully supported
	if(um.kb)
	{
		//modifier key names
		kn.mod={'metaKey':'Cmd-','altKey':'Alt-','ctrlKey':'Ctrl-','shiftKey':'Shft-','none':''};
			
		//default hotkey label text
		kn.txt=kn.mod[um.keys[5]]+'F'+(um.keys[4]-111);
		
		//check for user preference cookie to see if key-assignments are different
		if(um.m.cookie&&/udmKeyPrefs/.test(um.m.cookie))
		{
			//get relevant data
			kn.ck=um.m.cookie.split('udmKeyPrefs=');
			kn.ck=kn.ck[1].split(';');
			kn.ck=kn.ck[0].split(',');
			
			//create hotkey label text from modifier and function key number
			kn.txt=kn.mod[kn.ck[1]]+'F'+(parseInt(kn.ck[0],10)-111);
			
		}
		
		//if there's no cookie 
		//or no cookie that says this is done already
		//or permaIcon is set to "yes"
		if(!um.m.cookie ||!/udmKeyHelp=done/.test(um.m.cookie)||permanentIcon != 'no')
		{
			//get first link
			kn.fl=navbar.getElementsByTagName('a')[0];
			
			//create button
			kn.at={'text':kn.txt};
			kn.but=um.createElement('kbd',kn.at);
			
			//append it inside first link
			kn.fl.appendChild(kn.but);
		}
	}
};




//create and show help layer
function showHelp(link)
{
	//if keyboard navigation is fully supported
	//and this is the first link in the navbar
	if(um.kb && link == um.gc(umTree))
	{
		//if there's no cookie, or no cookie that says this is done already
		if(!um.m.cookie||!/udmKeyHelp=done/.test(um.m.cookie))
		{
			//create a layer
			kn.at={'id':'udmKeyHelp'};
			kn.hlp=um.createElement('p',kn.at);
			
			//convert hotkey text to actual value
			helpText=helpText.replace(/<hotkey>/g,kn.txt);
			
			//add text to layer
			kn.hlp.appendChild(um.m.createTextNode(helpText));	
			
			//add it to the page
			um.m.getElementsByTagName('body')[0].appendChild(kn.hlp);
			
			//move it next to, but slightly offset from, the navbar
			kn.hlp.style.left=(um.getRealPosition(umTree,'x')+((um.h)?0:((um.a)?(0-kn.hlp.offsetWidth):umTree.offsetWidth)))+'px';
			kn.hlp.style.top=(um.getRealPosition(umTree,'y')+((um.h)?umTree.offsetHeight:0))+'px';
			
			//set cookie expiry date a month from now
			//if a visitor doesn't come back for a month
			//they've probably forgotten what the keys are by now
			kn.dd=new Date();
			kn.dd.setTime(kn.dd.getTime()+(30*24*60*60*1000));
			
			//set a cookie so this doesn't happen again
			um.m.cookie='udmKeyHelp=done; expires='+kn.dd.toGMTString()+'; path=/';
		}
	}
	//otherwise clear help layer
	else
	{
		//remove help layer
		removeHelp();
	}
};




//listen for all events 
um.addReceiver(checkRemoval,'');

//check for removal of help layer
function checkRemoval(eobj,eco)
{
	//if layer exists
	if(kn.hlp != null)
	{
	
		//turn code into number
		eco=parseInt(eco,10);
		
		//if code is in the range of arrow key events
		//or it's a keyboard unmount command
		//or a link mouseover event
		if((eco >= 100&&eco <= 103)||eco == 90||eco == 20)
		{
			//remove help layer
			removeHelp();
		}
	}
};


//add a mousedown handler to remove the layer as well
if(typeof um.m.addEventListener!=um.un)
{
	//mozilla
	um.m.addEventListener('mousedown',removeHelp,false);
}
else if(typeof um.m.attachEvent!=um.un)
{
	//internet explorer
	um.m.attachEvent('onmousedown',removeHelp);
}



//actually remove the layer
function removeHelp()
{
	//if layer exists
	if(kn.hlp != null)
	{
		//remove help layer
		um.m.getElementsByTagName('body')[0].removeChild(kn.hlp);
		
		//if not using a permanent icon
		if(permanentIcon=='no')
		{
			//remove icon
			kn.fl.removeChild(kn.but);
		}	
		
		//set back to null
		kn.hlp=null;
	}
};


