var left_init = 0;
var reset_history_mask = function() {
   $(".history_years").animate(
      {
         left: 0
      },
      {
         duration: 500,
         step: function(now, fx) {
            
         }
      }
   )
}
function link_active(index, elem) {
   link_deactive();
   switch (index) {
      case 0:
         $(elem).parent().addClass('active');
         $(elem).parent().addClass('first_active');
      break;
      case 3:
         $(elem).parent().addClass('active');
         $(elem).parent().addClass('last_active');
      break;
      default:
         $(elem).parent().addClass('active');
         $(elem).parent().addClass('inner_active');
      break;
   }
}
function link_deactive() {
   $("#sub_navigation a").each(function(index, elem) {
      //console.log(index + " " + protect_link);
      //if (index == protect_link)
         
      $(elem).parent().removeClass('active');
      $(elem).parent().removeClass('first_active');
      $(elem).parent().removeClass('last_active');
      $(elem).parent().removeClass('inner_active');
   });
}
var reset_handler = false;
var timeout = 1000;
var protect_link = null;
$(document).ready(function() {  
   var has_history_mask = $(".history_mask").length;
   if (has_history_mask > 0) {
      if ($(".history_years").hasClass("page06")) {
         left_init = -1010;
         protect_link = 0;
      }
      if ($(".history_years").hasClass("page07")) {
         left_init = -2020;
         protect_link = 1;
      }
      if ($(".history_years").hasClass("page08")) {
         left_init = -3030;
         protect_link = 2;
      }
      if ($(".history_years").hasClass("page09")) {
         left_init = -4040;
         protect_link = 3;
      }
      if ($(".history_years").hasClass("page10")) {
         left_init = -5050;
         protect_link = 4;
      }

      $("#sub_navigation ul").hover(function() {}, function() {
         reset_handler = window.setTimeout("reset_history_mask()", timeout);
      });

      $(".history_years").hover(function() {
         if (reset_handler)
            window.clearTimeout(reset_handler);
         reset_handler = false;
      }, function () {
         reset_handler = window.setTimeout("reset_history_mask()", timeout);
      });

      $("#sub_navigation a, #sub_navigation span").each(function(index, item) {
         var pos = ((index+1) * -1010);
         if (Math.abs(pos) > Math.abs(left_init))
            pos = pos + Math.abs(left_init);
         else
            pos = pos - left_init;

         $(this).hover(
            function() {
               link_active(index, item);
               if (reset_handler)
                  window.clearTimeout(reset_handler);
               reset_handler = false;
               $(".history_years").animate(
                  {
                     left: pos
                  },
                  {
                     duration: 500
                  }
               )
            }, function () {
               link_deactive();
            }
         );
      });
   }
});
