try {
 var $j = jQuery.noConflict();
}
catch(e) {
 alert('JAEGER requires jQuery to be loaded first!');
}
$j(document).ready(function() {
 
 // get the "current page" value
 if (typeof currentPage != "undefined") {
  
  var currLoc = currentPage;
  
  // check for existence of left nav items:
  if ( $j('#left_nav > div').length) {
   
   // hide all the subnav <div> items initially:
   $j('#left_nav > div').css('display', 'none');
   
   // collapse "second" and "third" level <ul> elements
   //$j('#left_nav > div > ul ul, #left_nav > div > ul ul ul').css('display', 'none');
   
   // only collapse "third" level <ul> elements only
   $j('#left_nav > div > ul ul ul').css('display', 'none');
   
   // set a hit flag to false - if a link is found, it's set to true (this allows something to be displayed in the event of no hits)
   // does NOT prevent multiple "hits" - safer that way
   var hitFlag = false;
   
   // with each <a> tag in the entire left nav
   $j('#left_nav a').each(function(index) {
    // with this <a> get the href value:
    var linkVal = $j(this).attr('href');
    // compare the href value with currLoc (the page number, ie 1234)
    if (linkVal.indexOf(currLoc) != -1) {
     // if it's a hit, add the .selected class:
     $j(this).addClass('selected');
     // make sure the parent <div> (ie the nav "section") is set to display:
     $j(this).parents('div').css('display', 'block');
     // ensure that the link's parent <ul> is displayed:
     //$j(this).parents('ul').css('display', 'block');
     if ($j(this).parent().parent().css('display') != 'block') {
      $j(this).parent().parent().css('display', 'block');
     }
     // next do a test for sibling <ul> elements - if there are any, they need to be displayed:
     if ($j(this).siblings('ul').length) {
      $j(this).siblings('ul').css('display', 'block');
     }
     // set the hit flag to true
     hitFlag = true;
    }
   });
   
   // if we haven't had a hit, just roll back to the first <div> (with collapsed sublinks) block as safety:
   if (hitFlag != true) {
    $j('#left_nav div:first-child').css('display', 'block');
   }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   // NEW DEV - bolt on accordion ///////////////////////////////////////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
   var $myDivs = $j('#left_nav div');
   $myDivs.each(function() {
    if ($j(this).css('display') == 'block') {
     // add the active class to the div (used as a listener for timouts)
     $j(this).addClass('active_div'); 
     // add the "active accordion" class to the child <ul> of the active div:
     $j(this).children('ul:first-child').addClass('active_accord');
     // and only activate <a> elements with a sibling <ul>:
     // selector goes: get all <a> tags with a class of rollover with a sibling <ul>, then add the .child_present class to that <a>
     $j('ul.active_accord > li > a.rollover + ul').prev().addClass('child_present');
    }
   });
   
   var $actives = $j('ul.active_accord > li > a.child_present');
   // now do a test - there need to be a.child_present items to initiate an accordion:
   // if there's only 1 accordion-possible element, then simply leave as is <ul> (don't intialise an accordion):
   if ($actives.length > 1) {
    // this is tricky - to determine what item should open by default, we go:
    // find the parents LI of the selected A
    // then filter to find the LI with a first-child A with a class of .child_present - this is the "header" element in the accordion
    var $defaultOpen = $j('ul.active_accord a.selected').parents('li').children('a.child_present:first-child');
    if ($defaultOpen.length == 0) {
     // if there is no .selected (ie no page ref match) but we have accordion potential, get the first "a.child_present" element and enable that as default:
     $defaultOpen = $j('ul.active_accord li a.child_present').eq(0);
    } else if ($defaultOpen.length > 1) {
     // SAFETY CATCH:
     // If there are more than 1 <a class="selected"> tags, only use the first one.
     // This means that possibly the wrong list is expanded, but it's better than the alternatives.
     $defaultOpen = $j('ul.active_accord a.selected').eq(0).parents('li').children('a.child_present:first-child');
    }
    //define the custom animation:
    $j.ui.accordion.animations.jag = function(options) {
     this.slide(options, {
      duration: 450
     });
     };
    // activate:
    $j('ul.active_accord').accordion({
     // the drawer handle - the <a> tags with "children present"
     header: 'a.child_present',
     // our selected class
     selectedClass: 'open',
     autoheight: true,
     active: $defaultOpen,
     animated: 'jag',
     event: 'mouseover'
    });
   
    // setup a listener on the container DIV, which on mouseout resets the accordion to default display after a timeout
    var timer = null;
    $j('div.active_div').bind('mouseleave', function() {
     clearTimeout(timer);
     timer = setTimeout(
      function() {
       $j('ul.active_accord').accordion("activate", $defaultOpen);
      },
      1500
     );
    });
   
   }
   
   
  }
  
 }
 
});