// Make a literal string safe to include in regex patterns
function encodeForRegEx(s) { 
  return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1') 
}

// Add css class 'selected' to relevant banner menu links
//
// Example: If the current page is: http://mysite.com/about-us/our-markets/education,
// we need to add the css class to any anchor tags within an <li> that point to:
// http://mysite.com/about-us, or http://mysite.com/about-us/our-markets,
// or http://mysite.com/about-us/our-markets/education
function setSelected( anchors, currentUrl ) {
  for ( var i=0; i < anchors.length; i++ ) {
    var escapedHref = encodeForRegEx( anchors[i].href );
    var regex = new RegExp( '^' + escapedHref + '$|^' + escapedHref + '\\/', 'i' );
    if ( regex.test( currentUrl ) ) {
      if ( anchors[i].parentNode.tagName == "LI" ) { anchors[i].className += " selected"; }
    }
  }
}

// Pass the current page url and an array of all the 
// links within the banner menu to the setSelected function.
function highlightCurrentSection() {
  if ( document.getElementById( "bm" ) != null ) {
    var currentUrl = document.location.href ? document.location.href : document.location;
    currentUrl = currentUrl.replace(/\?.*/,''); // strip off any querystring parameters
    setSelected( document.getElementById( "bm" ).getElementsByTagName( "a" ), currentUrl ); 
  }
}


// Give IE a helping hand to enable hover like behaviour on <li> elements.
// Also, handle switching headlines between dropdowns
initBannerMenu = function() {
  var bmEls = document.getElementById( "bm" ).getElementsByTagName( "LI" );
  for ( var i=0; i<bmEls.length; i++ ) {
    bmEls[i].onmouseover=function() {
      this.className+=" bmhover";
      // Move the news headlines to each dropdown menu when it is opened.
      // This way, we only need one headlines active tag (rather than 9 or 10)
      if ( this.innerHTML.indexOf( 'dropdown' ) != -1 && this.innerHTML.indexOf( 'bmHeadlines' ) == -1 ) {
        var headlines = document.getElementById( 'bmHeadlines' );
        var column = this.childNodes[2].childNodes[1];
        column.appendChild( headlines );
      }
    }
    bmEls[i].onmouseout=function() {
      this.className=this.className.replace( new RegExp( " bmhover\\b" ), "" );
    }
  }
}


// Move the news headlines to each dropdown menu when it is opened.
// This way, we only need one headlines active tag (rather than 9 or 10)
// (IE is handled in the initBannerMenu function above)
initMoveHeadlines = function() {
  var bmEls = document.getElementById( 'bm' ).getElementsByTagName( 'LI' ); 
  for ( var i=0; i<bmEls.length; i++ ) {
    if ( window.addEventListener ) {
      bmEls[i].addEventListener( 'mouseover', moveHeadlines, false );
    }
  } 
} 
moveHeadlines = function() {
  if ( this.innerHTML.indexOf( 'dropdown' ) != -1 && this.innerHTML.indexOf( 'bmHeadlines' ) == -1 ) {
    var headlines = document.getElementById( 'bmHeadlines' );
    var column = this.childNodes[2].childNodes[3];
    column.appendChild( headlines );
  } 
} 


if ( window.addEventListener ) {
  window.addEventListener( "load", highlightCurrentSection, false ); 
  window.addEventListener( "load", initMoveHeadlines, false ); 
} else if ( window.attachEvent ) {
  window.attachEvent( "onload", initBannerMenu );
  window.attachEvent( "onload", highlightCurrentSection);
}

