
//+===============| HOTKEY 1.01 |===============+
//| serie di funzioni per accesso facilitato 	|
//| tramite scorciatoie da tastiera			|
//|								|
//+---------------------------------------------|
//| # versione 1.01 [FC 06092006] 			|
//+---------------------------------------------+
//| !# agguinta fusione con eventuale		|
//|	 funzione keypress giŕ presente		|
//+---------------------------------------------|
//| # versione 1.00 [FC 06082006] 			|
//+---------------------------------------------+
//| !# modifiche codice a generali.com		|
//|	- aggiunto id="menuNavX" x= 1 : n		|
//|	- aggiunto id="menuServX" x= 1 : n		|
//+---------------------------------------------+


//################# SETTAGGI ################# 

var arrowID='hotkey';
//id da assegnare al div che contiele il cursore di puntamento hotKey

var arrowPath='/'+window.w3gContex+'/img/hotkey.gif'
//ipercorso e nome immagine del cursore di puntamento hotKey

var arrowOffsetY=5;  //px
//decentramento partendo dall'alto rispetto alla posizione dell'ancora selezionata

var arrowOffsetX=13; //px
//decentramento partendo da destra rispetto alla posizione dell'ancora selezionata

var idPrefix= new Array('mMenu','menuServ','menuSub');
// prefissi degli id di menů (es. [nemuServ1,nemuServ2,nemuServ3] -> 'nemuServ')

var ctrlCombo=true;
var altCombo=false;
var shiftCombo=false;


var enableHotKeyHelp=false
// abilita la visualizzazione delle scorciatoie tramite (tasti combo)+F1
// 	solo IE 5.0+

var enableHideHotKey=false
// abilita il tasto alt+q (+invio in IE) per nascondere il menu di sottolivello e/o il puntatore (se visibili)

//################# VARIABILI ################# 

var hotKey=new Array();
var KEY=0;
var TYPE=1;
var MAX=2;
var index=new Array(1,0,0);
var types=new Array(0,1,2);
var NAV=0
var SERV=1
var SUB=2
var doSetMax=true;
var doHidePathMenu=false;
var pointedId='';
var escapeBrowserHotKey=false;


//################# FUNZIONI ################# 

// ritorna tutte le scorciatoie impostate
function getHotKeys(){
	var hks='';
	for(var i=0;i<hotKey.length;i++)
	{
		hks=hks+getHotKey(i)+'\n';
	}
	return hks;
}

// ritorna la scorciatoia da tastiera di un tipo di menů
function getHotKey(type){
	var hk='';
	var i=0;
        while(i<hotKey.length&&hk==''){
		if (hotKey[i][TYPE]==type){
			hk=String.fromCharCode(hotKey[i][KEY]);}
		i++;
	}
	if(hk!=''){
		hk=comboKeyConcat(hk);
	}
	return hk;	
}

// concatena i tasti funzioni seleziotati al carattere
function comboKeyConcat(hk)
{
	var ckc=hk;
	if(shiftCombo==true)
		ckc='Shift + '+ckc;
	if(altCombo==true)
		ckc='Alt + '+ckc;
	if(ctrlCombo==true)
		ckc='Ctrl + '+ckc;
	return ckc;
}

// inserice un menů sotto il controllo delle hotKey
//	keyChar: carattere associato (es 'a','b',ecc.)
//	type:	id tipo menu
//	max:	numero di elementi che compongono il menu
function insertMenuHotKey(keyChar,type){
	var keyCode;
	keyCode=keyChar.toLowerCase().charCodeAt();
	hotKey.push(new Array(keyCode,type,0));
}

// cattura e computa l'evento keypress
function hotKeyPressed(e){
	var ACHAR='a';
	var offset;
	offset=0;
	if(doSetMax){
		setMaxElements();
		doSetMax=false;
	}
	if (window.event){
		if ((ctrlCombo==event.ctrlKey) && (shiftCombo==event.shiftKey) && (altCombo==event.altKey))		
			{	
				event.returnValue=false;
				event.cancelBubble=true;				
				if(event.keyCode<ACHAR.charCodeAt()&&(ctrlCombo||shiftCombo)){
						offset=(ACHAR.charCodeAt())-1;
						//offset per catturare caratteri in combo con ctrl in IE
					}					
					for(i=0;i<hotKey.length;i++){			
						if(event.keyCode==((hotKey[i][KEY])-offset))
						{							
							scrollMenu(i,hotKey[i][TYPE]);							
							var escapeBrowserHotKey=true;
							
						}
					
				}
			}
	}
	else{	
		if ((ctrlCombo==e.ctrlKey) && (shiftCombo==e.shiftKey) && (altCombo==e.altKey))
		{		
				for(i=0;i<hotKey.length;i++){					
					if(e.which==hotKey[i][KEY])
					{
						scrollMenu(i,hotKey[i][TYPE]);
						escapeBrowserHotKey=true;
					}				
			}
		}

	}
	
}

// esegue lo scorrimento tra le voci di menů
function scrollMenu(i,type)
{	
	if(type==types[NAV])
	{
		if(hotKey[i][MAX]>0){
			var obj=document.getElementById(idPrefix[NAV]+index[NAV]);
			if(obj.style.display=='none' || obj.style.visibility=='hidden'){
				index[NAV]++;
				if(index[type]>hotKey[i][MAX])index[NAV]=1;
				scrollMenu(i,type);
			}else{
				see(index[NAV]);
				obj.focus();
				moveArrow(idPrefix[NAV]+index[NAV]);	
				index[NAV]++;
				if(index[type]>hotKey[i][MAX])index[NAV]=1;			
			}		
			
		}
	}else if(type==types[SERV])
	{
		if(hotKey[i][MAX]>0){
			var obj=document.getElementById(idPrefix[SERV]+index[SERV])
			if(obj.style.display=='none' || obj.style.visibility=='hidden'){
				index[SERV]++;
				if(index[SERV]>hotKey[i][MAX])index[SERV]=0;
				scrollMenu(i,type);
			}else{
				obj.focus();
				moveArrow(idPrefix[SERV]+index[SERV]);
				index[SERV]++;
				if(index[SERV]>hotKey[i][MAX])index[SERV]=0;
			}
		}

	}else if(type==types[SUB])
	{
		if(hotKey[i][MAX]>0){
			var child=document.getElementById(idPrefix[SUB]+index[SUB])
			if(child.style.display=='none' || child.style.visibility=='hidden'){
				index[SUB]++;
				if(index[SUB]>hotKey[i][MAX])index[SUB]=0;
				scrollMenu(i,type);
			}else{
				if(doHidePathMenu){
					seePathSubMenu(-1)
					doHidePathMenu=false;
				}	
				child.focus();			
				var parent=getParent(child, 'DIV')			
				if(parent.onmouseover!=null){			
					eval(getEvalCode(parent.onmouseover));
					doHidePathMenu=true;
				}
				moveArrow(idPrefix[SUB]+index[SUB]);
				document.getElementById(idPrefix[SUB]+index[SUB]).setActive;
				index[SUB]++;
				if(index[SUB]>hotKey[i][MAX])index[SUB]=0;
			}
		}
	}

}

// estraee il codice nel caso sia interno ad una funzione
function getEvalCode(code){
	var str=''+code;	
	if(str.substr(0,8)=='function')
	{
		str=str.substr(str.indexOf('{')+1,str.lastIndexOf('}')-(str.indexOf('{')+1));
	}
	return str;
}

// visualizza la lista di scorciatoie
function hotKeyHelp(e){
	var showHelp=false;
	if (window.event){
		if ((ctrlCombo==event.ctrlKey) && (shiftCombo==event.shiftKey) && (altCombo==event.altKey))	
			showHelp=true;	
	}else{ 	
		if ((ctrlCombo==e.ctrlKey) && (shiftCombo==e.shiftKey) && (altCombo==e.altKey))
			showHelp=true;	
	}
	if(showHelp){
		alert(getHotKeys());
		return false;
	}
}

// ritorn la posizone Y (attributo top assoluto dello style) di un oggetto
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// ritorna la posizone X (attributo left assoluto dello style) di un oggetto
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
	
// Sposta la freccia sul link che riceve il focus
function moveArrow(id){
	var obj=document.getElementById(id);
	var arrow=document.getElementById(arrowID);
	arrow.style.top=(findPosY(obj)+arrowOffsetY)+'px'
	arrow.style.left=(findPosX(obj)-arrowOffsetX)+'px'
	arrow.style.visibility='visible';
	pointedId=id;	
}

//aggiorna la posizione della freccia in caso di ridimensionamento
function refreshArrowPosition()
{
	var arrow=document.getElementById(arrowID);
	if(arrow.style.visibility=='visible'&&pointedId!=''){		
		moveArrow(pointedId);
	}
}

// Nasconde la freccia e l'eventuale sotto menu visualizzato
function hideHotKey(){
	if(enableHideHotKey){
		var arrow=document.getElementById(arrowID);
		if(arrow.style.visibility=='visible'){
			see(-1);
			arrow.style.visibility='hidden';
			pointedId='';
		}
	}
}

// Scrive il codice html necessari per gestire il puntatore ai link selezionati
function writeHotKeyTag(){
	var htmlOut=''+
	'<div id=\"'+arrowID+'\" name=\"'+arrowID+'\" style=\"visibility:hidden;position:absolute;top:0px;left:100px;z-index:9999\">'+
	'<img src=\"'+arrowPath+'\">'+
	'</div>';
	if(enableHideHotKey){
		htmlOut=htmlOut+'<a href=\"#\" onclick=\"javascript:hideHotKey();\" accessKey=\"q\"></a>';
	}
	htmlOut=htmlOut+'<a href=\"\" onClick=\"\" id=\"hotKeyFunctionBuffer\" name=\"hotKeyFunctionBuffer\"></a>'
	document.write(htmlOut);
}

// ritorna in numero di oggetto che hanno come id idPrefixN 
// dove N č un progressivo che va da 1 a infinito
function getMax(idPrefix,start){
	var i=start
	while(document.getElementById(idPrefix+i)!=null){
		i++}
	return i-1;
}

//setta il numero massimo di elemnti di un menů
function setMaxElements(){
	for(var i=0;i<hotKey.length;i++){
		hotKey[i][MAX]=getMax(idPrefix[hotKey[i][TYPE]],index[i]);		
	}
}
// ritorna il primo oggetto padre che corrisponde al tag passato di un elemento
function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())		
		return el;
	else
		return getParent(el.parentNode, pTagName);	// traverse up
}

//################# FUNZIONI DI MERGING PER LE FUNZIONI ONKEYPRESS ################# 

// contenitore del codice da eseguire
var funcBuffer=''

//riempimento funcBuffer con il codice da eseguire
function initFunctionBuffer(){
	funcBuffer=funcBuffer+writeParametersDeclaration(getFunctionParameterName(hotKeyPressed))
	funcBuffer=funcBuffer+getEvalCode(hotKeyPressed)
	if(document.onkeypress!=''&&document.onkeypress!=null){
		funcBuffer=funcBuffer+writeParametersDeclaration(getFunctionParameterName(document.onkeypress))
		funcBuffer=funcBuffer+getEvalCode(document.onkeypress)
	}

}

// trova i nomi dei paratri di una funzione
function getFunctionParameterName(functionCode){
	var code=''+functionCode;
	var pars=new Array();
	code=code.substr(code.indexOf('(')+1,code.indexOf(')')-code.indexOf('(')-1)
	pars=code.split(',');
	return pars;
}

// scrive le dichiariazioni dei parametri associandoli al primo argomento
// vista la natura delle funzioni (onkeypress) l'unico parametro č Event
function writeParametersDeclaration(pars){
	var dec='';
	for (var i=0;i<pars.length;i++)
		dec=dec+' var '+pars[i]+'= findEventArgument(arguments)';
	return dec;
}

// funzione contenito del codice fuso delle funzioni onkeypress
// e l'escape hotkey
function HotKey()
{
	eval(funcBuffer);
	return !escapeBrowserHotKey;
}

//scorre la lista di argomenti finchč non ne trova uno compatibile
//con l'Event Keypress
//non necessario con explorer
function findEventArgument(args){
	var ret=null;
	var i=0;
	if(window.event)
		ret=null;
	else{
		while(i<args.length||ret==null)		
		{			
			args[i].which!=null?ret=args[i]:ret=null;
			i++;
		}
	}
	return ret;
}

//################# INIZIALIZZAZIONE NEL CARICAMENTO DELLA PAGINA ################# 
if (isJSHotKeyActive()) alert ( "HotKey already included!") 

	writeHotKeyTag();
	initFunctionBuffer()
	document.onkeypress=HotKey
	if(enableHotKeyHelp){document.onhelp=hotKeyHelp}



