// Primary navigation drop down
$('nav.primary li').hover(
  function() { $(this).children('ul').stop(false, true).fadeIn(250);  },
  function() { $(this).children('ul').stop(false, true).fadeOut(500); }
)

// Allow primary accordion anchor to be used as a link
$('nav.sub a.direct_link').click(
   function() { location.href = $(this).attr('href'); }
);

// Activate accordian
$('nav.sub').accordion({ autoHeight: false, active: '.active', collapsible: true });

// Close all submenu classes
$('.submenu').siblings('ul').css('display', 'none');

// If a submenu is active then keep it open
$('.sub ul li .active').siblings('ul').css('display', 'block');

// Active sections should show down arrows
$('.active').children('img').replaceWith('<img src="/resources/images/arrow_down.png" alt="" />');

// Arrow left of accordion text points down if section is open, right if closed
$('nav.sub p').click(
  function() {
    // Return all other indicators to closed position
    $(this).siblings().children('img').replaceWith('<img src="/resources/images/arrow_right.png" alt="" />');
    // Clicked indicator
    if ($(this).hasClass('ui-state-active') == true) { // Section is open, so indicator should show as open
      $(this).children('img').replaceWith('<img src="/resources/images/arrow_down.png" alt="" />'); 
    } else { // Section is closed, so indicator should show as closed
      $(this).children('img').replaceWith('<img src="/resources/images/arrow_right.png" alt="" />');
    }
  }
);

// Secondary submenu navigation drop down
// toggle() is not used, as its starting status is not always the same
$('.submenu').click(
  function() {
    if ($(this).siblings().is(':hidden')) {
      $(this).siblings('ul').slideDown(350);
      // Show selected indicator
      $(this).children('img').replaceWith('<img src="/resources/images/arrow_down.png" alt="" />');
    } else {
      $(this).siblings('ul').slideUp(200);
      // Return selected indicator
      $(this).children('img').replaceWith('<img src="/resources/images/arrow_right.png" alt="" />');
    }
  }
)

// Identify external links
$('#info a[href^=http]').not('a[href*=.stsci.edu]').not('a[class="no_ext_ident"]').filter(
  function(){
    // Return true if text is within the anchor (not an image only link)
    if ($(this).text() != '') { return true; }
  }
).append('<img src="/resources/images/external.png" style="padding-left:2px;outline:0;float:none !important;" alt="" title="External link" />');

// Manually identify external links
$('.external_link').append('<img src="/resources/images/external.png" style="padding-left:2px;outline:0;float:none !important;" alt="" title="External link" />');

// Identify links to Intranet
$('.link_to_intranet').append('<img src="/resources/images/link-to-intranet.png" style="padding-left:2px;outline:0;float:none !important;" alt="" title="Link to Intranet" />');

// Show anchor linked location in title
$('#info a[href^=http]').not('a[href^=http://www.stsci.edu]').attr('title', 
  function() { return $(this).attr('href'); }
)

// Search engine form
$('#search_link').toggle(
  function() { $('#search_form').slideDown(350); },
  function() { $('#search_form').slideUp(250);   }
)
  
// Catalog quick search
$('#catalog_search_link').toggle(
  function() { $('#search_form_catalog').slideDown(350); },
  function() { $('#search_form_catalog').slideUp(250);   }
)

// Left justify the third column in /institute/sd/staff
$('.staff_directory tr').each(
  function() { $(this).children('td').eq(2).attr('class', 'left'); }
)

// Stripe rows in table for stripped_rows class
$('.stripped_rows tr:even').each(
  function() { $(this).css('background-color', '#ddd'); }
)
$('.stripped_rows tr:odd').each(
  function() { $(this).css('background-color', '#fff'); }
)

// Page specific functions

// Functionality for /jwst/internal/IandT/content, /jwst/internal/IandT/document/gsit/, /jwst/internal/IandT/document/soit, and /jwst/internal/IandT/document/testinfra
$('.show_list_js h2').next().css('display', 'none'); // Hide div under h2

e = $('h2').next().html(); // Get code for initial default
$('#h2_content').html(e);  // Display initial default code
$('.show_list_js h2').eq(0).css('backgroundColor', '#eca'); // Color initially defaulted selector

$('.show_list_js h2').click(
  function() {
    $('h2').css('backgroundColor', '#e1dcce'); // Reset background color
    $(this).css('backgroundColor', '#eca');    // Identify selection
    e = $(this).next().html(); // Get code for the selected area
    $('#h2_content').html(e);  // Place saved code in display area
  }
)

// Sliding display for FAQ-type information
// Uses dl tag with class of faq_info, displaying dt as the question and hiding/showing dd
$('.faq_info').children('dt').prepend('<span class="open_closed">+</span>&nbsp;');
$('.faq_info dd').css('display', 'none');
$('.faq_info dt').toggle(
  function() {
    e = $(this).parent().attr('class'); // Get class of dl to determine type of transition
    if (e.indexOf('fadein') != -1) { $(this).next().fadeIn(250);  } else { $(this).next().slideDown(500); }
    $(this).children('span.open_closed').text('-');
  },
  function() {
    e = $(this).parent().attr('class');
    if (e.indexOf('fadein') != -1) { $(this).next().fadeOut(250); } else { $(this).next().slideUp(500);   }
    $(this).children('span.open_closed').text('+');
  }
)

// AJAX functionality for /jwst/internal/IandT/acronyms/content
$('#letter_select a').click(
  function(link) {
    link.preventDefault();
    var new_info = $(this).text();
    $('#acronym_list').load('snippets/' + new_info);
  }
)

// Make the article height equal to the side navigation: /institute/coi/login_form and /institute/coi/thankyou
$('.coi_form article').height($('nav#sideNav').height()-17);

