$(document).ready(function(){
   /* 
    * Navigation Menu
    */
   $("#helios_menu .entry_popupable_prodotti").click(function(){
      if($("#sub_prodotti").css("display") == "none")
      {
         hideAllPopup();
         resetAllTabsSelectionStatus();
         $("#sub_prodotti").fadeIn("normal");
      }
      else
         $("#sub_prodotti").toggle();

      toggleTabsSelectionStatus($(this));      
   });
   $("#helios_menu .entry_popupable_azienda").click(function(){
      if($("#sub_azienda").css("display") == "none")
      {
         hideAllPopup();
         resetAllTabsSelectionStatus();
         $("#sub_azienda").fadeIn("normal");
      }
      else
         $("#sub_azienda").toggle();

      toggleTabsSelectionStatus($(this));      
   });
   $("#helios_menu .entry_popupable_rete").click(function(){
      if($("#sub_rete").css("display") == "none")
      {
         hideAllPopup();
         resetAllTabsSelectionStatus();
         $("#sub_rete").fadeIn("normal");
      }
      else
         $("#sub_rete").toggle();

      toggleTabsSelectionStatus($(this));      
   });
   $("#helios_menu .entry_popupable_realizzazioni").click(function(){
      if($("#sub_realizzazioni").css("display") == "none")
      {
         hideAllPopup();
         resetAllTabsSelectionStatus();
         $("#sub_realizzazioni").fadeIn("normal");
      }
      else
         $("#sub_realizzazioni").toggle();

      toggleTabsSelectionStatus($(this));      
   });
   
   function hideAllPopup()
   {
      $("#sub_prodotti").hide();
      $("#sub_azienda").hide();
      $("#sub_rete").hide();
      $("#sub_realizzazioni").hide();
   }
   function resetAllTabsSelectionStatus()
   {
      resetAllTabsSelectionStatus_doWork($("#helios_menu .entry_home"));
      resetAllTabsSelectionStatus_doWork($("#helios_menu .entry_popupable_prodotti"));
      resetAllTabsSelectionStatus_doWork($("#helios_menu .entry_popupable_azienda"));
      resetAllTabsSelectionStatus_doWork($("#helios_menu .entry_popupable_rete"));
      resetAllTabsSelectionStatus_doWork($("#helios_menu .entry_popupable_realizzazioni"));
      resetAllTabsSelectionStatus_doWork($("#helios_menu .entry_contatti"));
   }
   function toggleTabsSelectionStatus(wjSelector)
   {
      wjSelector.children("div:first").toggleClass("tabs_start_active");
      wjSelector.children("a").toggleClass("active");
      wjSelector.children("div:last").toggleClass("tabs_end_active");
   }
   function resetAllTabsSelectionStatus_doWork(wjSelector)
   {
      wjSelector.children("div:first").removeClass("tabs_start_active");
      wjSelector.children("a").removeClass("active");
      wjSelector.children("div:last").removeClass("tabs_end_active");
   }

   
   /*
    * Highlight the menu entries
    */
   $("#helios_menu .sub_menu ul li a").hover(
         function(){ $(this).find("span:first").addClass("entry_hovered"); },
         function(){ $(this).find("span:first").removeClass("entry_hovered"); }
   );
   $("#helios_menu .sub_menu #sub_rete_sedi_list span.cell").hover(
         function(){ $(this).addClass("entry_hovered"); },
         function(){ $(this).removeClass("entry_hovered"); }
   );



   /*
    * Highlight the menu entries
    */
   $("#header_menu > .entry").hover(
         function(){ $(this).addClass("entry_hovered"); },
         function(){ $(this).removeClass("entry_hovered"); }
   );
   $("#header_menu .sub_menu ul li a").hover(
         function(){ $(this).find("span:first").addClass("entry_hovered"); },
         function(){ $(this).find("span:first").removeClass("entry_hovered"); }
   );
   $("#header_menu .sub_menu #sub_rete_sedi_list span.cell").hover(
         function(){ $(this).addClass("entry_hovered"); },
         function(){ $(this).removeClass("entry_hovered"); }
   );
   

   /*
    * Page Side Menu
    */
   var wjCurrentEntry = $("#" + g_currentPageId);
   wjCurrentEntry.addClass("selected");
   if(wjCurrentEntry.next().hasClass("sc_hidden"))
   {
      wjCurrentEntry.next().toggle();
      if(wjCurrentEntry.next().next().hasClass("separator"))
         wjCurrentEntry.next().next().toggle();
   }
   
   wjCurrentEntry.parents().map(function() {
      if($(this).hasClass("sc_hidden"))
      {
         $(this).toggle();
         if($(this).next().hasClass("separator"))
            $(this).next().toggle();
      }
   });
   
});

