// remove the flash of unstyled content
document.documentElement.className = "js-tertiary-nav";

var openFlag = "no";

$(function() {
  // show the main container for products
  $("#navigation-products").show();
  // switch the arrow to opened position
  $('#navigation-tertiary-products .main-control').addClass('opened');
  
  //alert(openFlag);
  
  if(openFlag=="yes"){
  // hide the benefits containers
  $("#navigation-benefits").show();
  // switch the arrow to opened position
  $('#navigation-tertiary-benefits .main-control').addClass('opened');
  }else{
  // hide the benefits containers
  $("#navigation-benefits").hide();
  }
  
  // used for showing selected category
  var main = ".main-control";
  var headers = ".products-heading";

  // show the selected page's container
  var selectedSubject = $('li.products-selected')[0];
  var parentElm = $(selectedSubject).parent();
  var parentParentElm = $(parentElm).parent();
  $(parentParentElm).addClass("category-expanded")
  $(parentElm).show();
  
  if (typeof ($('li.products-selected')[0]) != 'undefined') {
    var selectedParent = $(selectedSubject).parents().get(2).id;
    if (selectedParent == "navigation-tertiary-benefits") {
      $('#navigation-tertiary-products ul').hide();
      $('#navigation-tertiary-products .main-control').removeClass('opened');
      $('#navigation-tertiary-benefits .main-control').addClass('opened');
    }
  }

  $(main).click(function() {
    $(this).next().toggle("fast");
  });
  $(main).toggle(function() {
    $(this).children('.control').addClass("expanded");
  }, function() {
    $(this).children('.control').removeClass("expanded")
  });
  $(".heading-text").click(function() {
    $(this).next().slideToggle("fast");
  });
  // functions to swap out the up/down arrow elements
  $(".heading-text").toggle(function() {
    if ($(this).parent()
            .is(".category-expanded")) {
      $(this).parent()
                    .removeClass("category-expanded");
    }
    else {
      $(this).parent().addClass("category-expanded");
    }
  }, function() {
    if (!($(this)
            .parent().is(".category-expanded"))) {
      $(this).parent()
                    .addClass("category-expanded");
    }
    else {
      $(this).parent()
                .removeClass("category-expanded");
    }
  });
  $(main).toggle(function() {
    if ($(this).is('.opened')) {
      $(this).removeClass("opened");
    }
    else {
      $(this).addClass("opened");
    }
  }, function() {
    if (!($(this).is('.opened'))) {
      $(this).addClass("opened");
    }
    else {
      $(this).removeClass("opened");
    }
  });

  // functions to show and hide all product links           
  $(".open-all").click(function() {
    $(".products-heading ul").show(150);
    $(headers).addClass("category-expanded");
    if ($.browser.msie && $.browser.version <= 7) {
      $(".products-heading ul li").css("font-weight", "700");
    }
    return false;
  });
  $(".close-all").click(function() {
    $(".products-heading ul").hide(150);
    $(headers).removeClass("category-expanded");
    return false;
  });
});
