/*
98 - numpad2
104 - numpad8
100 - numpad4
102 - numpad6
111 - /
13 - enter
101 numpad5
*/

function objTeclas(){

    this.goFoward = 102;                // define tecla forward
    this.goBackward = 100;              // define tecla backward
    this.goDown = 98;                   // define tecla down
    this.goUp = 104;                    // define tecla up
    this.Access = 101;                  // define tecla access
    this.Help =111;                     // define tecla ajuda
    this.cursorTopPos = 0;              // posição do cursor de tela em relação topo da tela marco 0,0
    this.menuPos = 0;                   // posição atual do cursor nos links do menu ou conteudo ativo: default= 0
    this.contentPos = 99;               // posição atual do link de conteudo ativo.
    this.cursorContentPos = true;       // se true cursor no menu se false cursor no conteudo
    this.menuInit = 1;                  // valor do id do primeiro item do menu
    this.contentInit = 100;             // valor do id do primeiro item do conteudo
    this.totalMenuItem = 18;            // numero do ultimo item do menu
    
    return true;

}

objTeclas.prototype.Action = oTeclas_Action;
objTeclas.prototype.Scroller = oTeclas_Scroller;
objTeclas.prototype.setMenuActive = oTeclas_setMenuActive;
//objTeclas.prototype.setCursorPosition = oTeclas_setPosition;
objTeclas.prototype.setFocus = oTeclas_setFocus;

function oTeclas_Action(key) {

    switch (parseInt (event.keyCode)){

        case this.goDown:
            Teclas.Scroller ("up");
            break;
        case this.goUp:         
            Teclas.Scroller ("down")
            break;
        case this.goFoward:
            Teclas.setMenuActive("foward");
            break;
        case this.goBackward:
            Teclas.setMenuActive("backward");
            break;
        case this.Access:
            //Teclas.setCursorPosition();
            alert ("acessa o link");
            break;
        case this.Help:
            alert ("Lembrete:\nLembre-se de manter a tecla NUM LOCK do seu teclado acionada;\nTecla 8: \"Move tela para cima\"\nTecla 2: \"Move tela para baixo\"\nTecla 6: \"Escolhe menu para baixo\"\nTecla 4: \"Escolhe menu para cima\"\nTeclas Enter ou 5: \"Acessa opção do menu\"");
            break;
        }
        //alert (event.keyCode);
}


function oTeclas_setFocus (id,status){

    obj = document.images["menu"+id];

    if (status){
        obj.src= "../includes/images/bullet_fechado.gif";
    }
    else{
        obj.src = "../includes/images/bullet_aberto.gif";
    }

}



function oTeclas_setMenuActive(direction){


    counter = this.menuPos;
    
    if (this.cursorContentPos){
    // foco no menu
    
        try{ //gera erro se counter < que menuInit ou > que totalMenuItem
        
            if (direction == "foward"){
            
                counter = counter +1;
                if (counter != this.menuInit) Teclas.setFocus ((counter-1),false);
                
            }
            else{
            
                counter = counter - 1;
                if (counter != this.totalMenuItem) Teclas.setFocus ((counter+1),false);
                   
            }
            
            Teclas.setFocus (counter,true);
        
        }
        catch(e){
        
            this.cursorContentPos = false;
            if (counter > this.totalMenuItem){    
                counter = this.contentInit;
               document.anchors[counter].focus();
              vURL = window.location;
              vURL = vURL.toString();
              //alert (vURL);
              window.location = vURL
            }
            else{
                anchorArr = document.anchors;
                counter = 100 + (anchorArr.length - this.totalMenuItem -1);
                vURL = window.location;
                vURL = vURL.toString();
                alert (vURL);
                //document.anchors[counter].focus();
                
            }
        }
    
    }
    else{
    // foco no conteudo
        contentLimit = 100 + (anchorArr.length - this.totalMenuItem -1);
        try{ //gera erro se counter < que 100 (1º item do conteudo) ou > que contentLimit (ultimo)
        
            if (direction == "foward") counter = counter +1
            else counter = counter -1;
            vURL = window.location;
            vURL = vURL.toString();
            alert (vURL);
            //document.anchors[counter].focus();
        }
        catch(e){
            if (counter > contentLimit) counter = this.menuInit
            else counter = this.totalMenuItem;
            Teclas.setFocus (counter,true);
        }


    }
    this.menuPos = counter;
}
//////////////////////////////////////////////////
/*
try{

    if (direction == "foward"){

        

        Teclas.setFocus (counter,true);

    }
    else{

        counter = counter -1;

        if (this.cursorContentPos)
            if (counter != this.totalMenuItem) Teclas.setFocus ((counter+1),false);
        else
            if (counter != this.totalContentItem) Teclas.setFocus ((counter+1),false);

        Teclas.setFocus (counter,true);

    }
}
catch (e){
    x = e.valueOf();
    alert (e.name+" -- "+e.description+" -- "+e.number);
    juca = document.anchors;
    //for (a=0; a <juca.length;a++){
    alert (juca.length);
    
    
    if (direction =='foward' && this.cursorContentPos){

        counter = this.menuInit;
        Teclas.setFocus (counter,true);

    }
    else if (direction =='foward' && !this.cursorContentPos){

        counter = this.contentInit;
        Teclas.setFocus (counter,true);

    }
    else if (direction =='backward' && this.cursorContentPos){

        counter = this.totalMenuItem;
        Teclas.setFocus (counter,true);

    }
    else{

        counter = this.totalContentItem;
        Teclas.setFocus (counter,true);

    }


}

if (this.cursorContentPos) this.menuPos = counter;
else this.contentPos = counter;
}


*/

function oTeclas_Scroller (direction){

    if (direction == "up"){
    this.cursorTopPos = this.cursorTopPos + 25;
    }
    else {
    if (!this.cursorTopPos <= 0){
    this.cursorTopPos = this.cursorTopPos - 25;
    }
    }
    window.scrollTo (0,this.cursorTopPos);

}


/*


function oTeclas_setPosition (){

    if (this.cursorContentPos){

        this.cursorContentPos = false;

        //destivo o cursor do menu
        if (this.menuPos != 0) Teclas.setFocus (this.menuPos,false);

        //ativo o cursor conteudo
        if (this.contentPos != 99) Teclas.setFocus (this.contentPos,true);
    }
    else{

        this.cursorContentPos = true;

        //desativo o cursor conteudo
        Teclas.setFocus (this.contentPos,false);

        //ativo o cursor do menu
        Teclas.setFocus (this.menuPos,true);
    }
}

*/


Teclas = new objTeclas();

