var main_container = "";

var parent_pos = 0;
function ParentPos(container) {
   if (container.tagName == "LI"){
      parent_pos = parent_pos + 1;
   }
   var pnodes = container.parentNode
   if (pnodes){
      ParentPos(pnodes);
   }
}
function GetParentPos(container) {
   ParentPos(container);
   var p_pos = parent_pos;
   parent_pos = 0;
   return p_pos;
}

var opt_cnt = 0;
function GrpCnt(container) {

   var pnodes = container.parentNode
   if (pnodes){

   var grp_cnt = 0;
   var cnodes = pnodes.childNodes
   if (cnodes){
      for (var i = 0; i < cnodes.length; ++i){
         if (cnodes[i].tagName){
             if (cnodes[i].tagName == "LI"){
                grp_cnt = grp_cnt + 1;
                if (cnodes[i] == container){
                   opt_cnt = grp_cnt;
                }
             }
         }
      }   
   }

   }
   return grp_cnt;
}

var is_active = false;
function IsActive(container) {
   if (container == main_container){
      is_active = true;
   }
   var pnodes = container.parentNode
   if (pnodes){
      IsActive(pnodes);
   }
   return is_active;
}
function IsMenuActive(container) {
   var active = IsActive(container);
   is_active = false;
   return active;
}

var has_child = false;
function FindChild(container) {
   var cnodes = container.childNodes
   if (cnodes){
      for (var i = 0; i < cnodes.length; ++i){
         if (cnodes[i].tagName){
             if (cnodes[i].tagName == "LI"){
                has_child = true;
             }
             FindChild(cnodes[i]);
         }
      }   
   }
}
function HasChildPos(container) {
   var found = false;
   FindChild(container);
   if (has_child == true){
      found = true;
   }
   has_child = false
   return found;
}

var doset = false;
var arr_opt = "";
var prv_elm = "";
var fst_elm = "";
var parnt_li = "";
var prv_p_pos = 0;
var group_cnt = 0;
var g_cnt = 0;

function SetFocus(container, p_pos, direction) {

   var cur_p_pos = 0;

   var cnodes = container.childNodes
   if (cnodes){
      for (var i = 0; i < cnodes.length; ++i){
         if (cnodes[i].tagName){

             if (cnodes[i].tagName == "LI"){
                parnt_li = cnodes[i];
             }

             if (cnodes[i].tagName == "A"){

                cur_p_pos = GetParentPos(container);
                group_cnt = GrpCnt(parnt_li);

                switch (direction){
                   case "R":

                   if (p_pos == 1){

                      if (doset == true){
                         if (p_pos == cur_p_pos){
                            arr_opt = cnodes[i];
                            doset = false;
                         }
                      }
                      if (cnodes[i] == document.activeElement){
                         doset = true;
                      }

                   }else{

                      if (doset == true){
                         arr_opt = cnodes[i];
                         doset = false;
                      }
                      if (cnodes[i] == document.activeElement){
                         if (HasChildPos(parnt_li) == true){
                            doset = true;
                         }
                      }

                   }

                   break;
                   case "L":

                   if (p_pos == 1){

                      if (cnodes[i] == document.activeElement){
                         arr_opt = prv_elm;
                      }
                      if (cur_p_pos == 1){
                         prv_elm = cnodes[i];
                      }

                   }else{

                      if (cnodes[i] == document.activeElement){
                         if (cur_p_pos != prv_p_pos){
                            if (cur_p_pos > 2){
                               arr_opt = prv_elm;
                            }
                         }
                      }

                      prv_elm = cnodes[i];

                    }

                   break;
                   case "D":

                   if (p_pos == 1){

                      if (doset == true){
                         arr_opt = cnodes[i];
                         doset = false;
                      }
                      if (cnodes[i] == document.activeElement){
                         if (HasChildPos(parnt_li) == true){
                            doset = true;
                         }  
                      }

                   }else{

                      if (doset == true){
                         if (p_pos == cur_p_pos){
                            arr_opt = cnodes[i];
                            doset = false;
                         }
                      }
                      if (cnodes[i] == document.activeElement){
                         if (p_pos == cur_p_pos && opt_cnt < group_cnt){
                            doset = true;
                         }
                      }  

                   }

                   break;
                   case "U":

                   if (p_pos > 1){

                      if (cnodes[i] == document.activeElement){
                         if (cur_p_pos <= prv_p_pos){
                            arr_opt = prv_elm;
                         }
                         if (prv_p_pos == 1){
                            arr_opt = fst_elm;
                         }
                      }

                      if (p_pos == cur_p_pos){
                         prv_elm = cnodes[i];
                      }  
                      fst_elm = cnodes[i];

                   }

                   break;
                }

                prv_p_pos = cur_p_pos;
             }
             SetFocus(cnodes[i], p_pos, direction);
         }
      }   
   }
}

function SetMenuActions(direction) {
   if (document.activeElement.tagName == "A"){
      var p_pos = GetParentPos(document.activeElement);
      SetFocus(main_container, p_pos, direction);
      doset = false;
      prv_elm = "";
      prv_elm = "";
      prv_p_pos = 0;
      group_cnt = 0;
      opt_cnt = 0;

      if (arr_opt){
         arr_opt.focus();
         arr_opt = "";
      }

   }
}

function UnFocusAll(container) {
   var cnodes = container.childNodes
   if (cnodes){
      for (var i = 0; i < cnodes.length; ++i){
         if (cnodes[i].tagName){
             if (cnodes[i].tagName == "LI"){
                cnodes[i].className=cnodes[i].className.replace(" sfhover", "");
             }
             UnFocusAll(cnodes[i]);
         }
      }   
   }
}

function SetLIFocus(container) {

   if (container.tagName == "LI"){
      container.className+=" sfhover";
   }

   var pnodes = container.parentNode
   if (pnodes){
      SetLIFocus(pnodes);
   }

}

var pLI = "";
function LoopULs(container) {

   var cnodes = container.childNodes
   if (cnodes){
      for (var i = 0; i < cnodes.length; ++i){
         if (cnodes[i].tagName){

             if (cnodes[i].tagName == "LI"){

                pLI = cnodes[i];

                cnodes[i].onmouseover=function() {
                    SetLIFocus(this);
                }
                cnodes[i].onmouseout=function() {
                   UnFocusAll(main_container);
                }

             }else if (cnodes[i].tagName == "A"){
   
                var par_LI = pLI;
                cnodes[i].onfocus=function() {
                   SetLIFocus(par_LI);
                }
                cnodes[i].onblur=function() {
                   UnFocusAll(main_container);
                }

             }
             LoopULs(cnodes[i]);
         }
      }   
   }
}

function sfHover() {
   main_container = document.getElementById("suckerfishnav");
   LoopULs(main_container);
}

function InitHover() {
   if (!document.getElementById) return;

   if (window.attachEvent){
      window.attachEvent("onload", sfHover);
   }else{
      window.addEventListener("DOMContentLoaded", sfHover, false);
   }
}
InitHover();

function KeyActions(evt){

   var evt = (evt) ? evt : ((event) ? event : null);
   var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

   var r_value = true;

   if (IsMenuActive(document.activeElement) == true){
      switch (evt.keyCode){
         case 39:
            SetMenuActions("R");
            r_value = false;
         break;
         case 40:
            SetMenuActions("D");
            r_value = false;
         break;
         case 38:
            SetMenuActions("U");
            r_value = false;
         break;
         case 37:
            SetMenuActions("L");
            r_value = false;
         break;
      }
   }

   if (r_value == false){
      evt.returnValue = false; 
      return false;
   }

}
document.onkeydown = KeyActions;
