﻿$(function() {
  $('.nrefV3_serviceNav li a').mouseover(function(e) {
    e.preventDefault();
    $(this).parent().addClass('hover');
    $(".menuMask").show();
  });
  $('.nrefV3_undernav').mouseover(function() {
    $(this).parent().addClass('hover');
    $(".menuMask").show();
  });
  $('.nrefV3_undernav').mouseleave(function() {
    $(this).parent().removeClass('hover');
    $(".menuMask").hide();
  });
  $('.nrefV3_serviceNav li a').mouseleave(function(e) {
    e.preventDefault();
    $(this).parent().removeClass('hover');
    $(".menuMask").hide();
  });
  $('.nrefV3_serviceNav li').click(function() {
    $('.nrefV3_activeItem').removeClass('nrefV3_activeItem');
    $(this).addClass('nrefV3_activeItem');
  });
  var listPos = $(".nrefV3_serviceNav").position();
  var listPosLeft = listPos.left;
  var listPosRight = listPosLeft + $(".nrefV3_serviceNav").width();
  $(".nrefV3_serviceNav li").each(function() {
    var itemPos = $(this).position();
    var itemWidth = $(this).width();
    var IEVers = parseFloat(navigator.appVersion.split("MSIE")[1]);
    if (IEVers == 6) {
      var subItemWidth = $(this).find("ul.nrefV3_undernav").width() + 200;
    } else {
      var subItemWidth = $(this).find("ul.nrefV3_undernav").width();
    }
    $(this).find("ul.nrefV3_undernav").attr('subItemWidth', subItemWidth);
    $(this).find("ul.nrefV3_undernav").css({ width: subItemWidth });
    var subItemLeftPosTemp = itemPos.left + itemWidth / 2 - subItemWidth / 2;
    var subItemRightPosTemp = subItemLeftPosTemp + subItemWidth;
    if (subItemLeftPosTemp < listPosLeft) {
      $(this).find("ul.nrefV3_undernav").css({ left: listPosLeft, textAlign: 'left' });
      $(this).attr('pos', 'left');
    } else if (subItemRightPosTemp > listPosRight) {
      $(this).find("ul.nrefV3_undernav").css({ left: listPosRight - subItemWidth, textAlign: 'right' });
      $(this).attr('pos', 'right');
    } else {
      $(this).find("ul.nrefV3_undernav").css({ left: subItemLeftPosTemp });
      $(this).attr('pos', 'middle');
    }
    $(this).attr('subItemLeftPosTemp', subItemLeftPosTemp);
    $(this).attr('listPosLeft', listPosLeft);
    $(this).attr('listPosRight', listPosRight);
    $(this).attr('subItemRightPosTemp', subItemRightPosTemp);
  });
});