jQuery( document ).ready( function () {

  // if working locally...
  if (jQuery( 'base' ).exists() && jQuery( 'base' ).attr( 'href' ).match( '127.0.0.1' )) {

    var sPath = jQuery( 'base' ).attr( 'href' ).replace( /\/application\/.+\//, '' );

    // prepend absolute links with (abbreviated) local path
    jQuery( 'a[href^=/]' ).each( function () { jQuery( this ).attr( 'href', sPath + jQuery( this ).attr( 'href' ) ); } );

    // prepend form actions with (abbreviated) local path
    jQuery( 'form' ).each( function () {
      if (jQuery( this ).attr( 'action' ).match( /^\// )) { jQuery( this ).attr( 'action', sPath + jQuery( this ).attr( 'action' ) ); }
    } );

/*  // in progress...

    // prepend absolute links with (abbreviated) local path
    jQuery( 'a[href^=/]' ).each( function () { jQuery( this ).attr( 'href' ).replace( /^\/(.+)/, sPath + '$1' ); } );

    // prepend form actions with (abbreviated) local path
    jQuery( 'form' ).each( function () { jQuery( this ).attr( 'action' ).replace( /^\/(.+)/, sPath + '$1' ); } );

    // prepend meta refresh with (abbreviated) local path
    jQuery( 'meta[http-equiv=refresh]' ).attr( 'content' ).replace( /\//, sPath );
*/
  }

  // remove focus border
  jQuery( 'a' ).focus( function () { this.blur(); } );

  // remove focus border
  jQuery( 'a.external' ).click( function () {
    window.open( jQuery( this ).attr( 'href' ) );
    return false;
  } );

  // assign name (to avoid: id="txtFoo" name="txtFoo")
  jQuery( 'form :input[id]' ).each( function () {
    if (jQuery( this ).attr( 'name' ) == '' || jQuery( this ).attr( 'name' ) == null) {
      jQuery( this ).attr( 'name', jQuery( this ).attr( 'id' ) );
    }
  } );

  // remove textarea resize (for safari)
  jQuery( 'textarea' ).css( 'resize', 'none' );

  // initialize header stuff
  //jQuery( 'div#divHeader_1_content div.internal dl.portrait' ).randomChild();
  jQuery( 'div#divHeader_1_content div.internal div.link dl dt[class!=current]' ).hover(

    function () {
      setHeaderContent( jQuery( this ), 'reset' );
      setHeaderContent( jQuery( this ) );
    },

    function () {
      setHeaderContent( jQuery( this ), 'reset' );
      jQuery( 'div#divHeader_1_content div.internal div.link dl dt.current' ).each( function () { setHeaderContent( jQuery( this ) ); } );
    }
  );

  jQuery( 'div#divHeader_1_content div.internal div.link dl dt.current' ).each( function () { setHeaderContent( jQuery( this ) ); } );

  // initialize subnav click
  jQuery( 'ul.menu a.open' ).click( function () {

    var oElement = jQuery( this ).closest( 'li' ).find( 'ul:first' );

    // if closed...
    if (oElement.css( 'display' ) == 'none') {
      jQuery( this ).text( '\u2013' );
      oElement.slideDown( 'fast' );
    } else {
      jQuery( this ).text( '+' );
      oElement.slideUp( 'fast' );
    }

    return false;

  } );

  // initialize menu
  jQuery( 'ul.menu li.current' ).closest( 'ul' ).show();
  jQuery( 'ul.menu li.current' ).find( 'ul:first' ).show();

  // initialize textsize
  jQuery( 'div.textsize a.plus' ).click( function ()  { setTextSize( 'plus' ); return false; } );
  jQuery( 'div.textsize a.minus' ).click( function () { setTextSize( 'minus' ); return false; } );

  // initialize global search
  jQuery( 'div#divButton_Search' ).click( function () { jQuery( this ).closest( 'form' ).submit(); } );

  // initialize calendar
  jQuery( 'div#divDatepicker' ).datepicker( {

    inline: true,

    beforeShowDay: function ( oDate ) {

      if (aEvent != null) {

        for (i = 0; i < aEvent.length; i++) {
          var aDate = aEvent[i].split( '-' );
          if (oDate.getFullYear() == aDate[0] &&
              oDate.getMonth()    == aDate[1] - 1 &&
              oDate.getDate()     == aDate[2]) {
            return [ true, '' ];
          }
        }

        return [ false, '' ];
      }
    },

    onSelect: function( sDate ) {

      var aDate = sDate.split( '/' );
      document.location = jQuery( 'base' ).attr( 'href' ).replace( /application.+/, '' ) + 'helpful-stuff/event-calendar/' + aDate[2] + '-' + aDate[0] + '-' + aDate[1];
    }

  } );

} );

/*
 * Custom jQuery methods
 */
jQuery.fn.exists = function() { return jQuery( this ).length > 0; }

function setHeaderContent( oElement, sMode ) {

  if (sMode == null) { sMode = 'set'; }

  // if displaying header...
  if (sMode == 'set') {

    var nID = (oElement.attr( 'id' ).split( '_' ))[1];

    oElement.css( 'background-image', 'url( ' + jQuery( 'base' ).attr( 'href' ).replace( /application.+/, '' ) + '/asset/images/header/button_blank.png )' );
    jQuery( 'div#divCopy_' + nID ).show();

    if (nID == 0) {
      jQuery( 'div#divHeader_1_content div.internal div.man img#imgMan_1' ).show();
    } else if (nID == 1 || nID == 3 || nID == 4) {
      jQuery( 'div#divHeader_1_content div.internal div.man img#imgMan_6' ).show();
    } else if (nID == 2) {
      jQuery( 'div#divHeader_1_content div.internal div.man img#imgMan_2' ).show();
    } else if (nID == 5) {
      jQuery( 'div#divHeader_1_content div.internal div.man img#imgMan_3' ).show();
    } else if (nID == 6) {
      jQuery( 'div#divHeader_1_content div.internal div.man img#imgMan_4' ).show();
    }

  } else {

    oElement.css( 'background-image', '' );
    jQuery( 'div#divHeader_1_content div.internal div.copy div' ).hide();
    jQuery( 'div#divHeader_1_content div.internal div.man img' ).hide();
    jQuery( 'div#divHeader_1_content div.internal div.man img#imgMan_0' ).show();
  }
}

function setTextSize( sMode ) {

  var oElement = jQuery( 'div#divContent div.center div.content p' );

  if (oElement.css( 'font-size' ) != null) {

    var nFontSize   = oElement.css( 'font-size' ).replace( 'px', '' );
    var nLineHeight = oElement.css( 'line-height' ).replace( 'px', '' );

    if (nFontSize != null && nLineHeight != null) {

      if (sMode == 'plus') {
        nFontSize   = ++nFontSize   < 24 ? nFontSize   : 24;
        nLineHeight = ++nLineHeight < 28 ? nLineHeight : 28;
      } else {
        nFontSize   = --nFontSize   > 10 ? nFontSize   : 10;
        nLineHeight = --nLineHeight > 14 ? nLineHeight : 14;
      }

      jQuery( 'div#divContent div.center div.content a, div#divContent div.center div.content li, div#divContent div.center div.content p' ).css( 'font-size',   nFontSize   + 'px' );
      jQuery( 'div#divContent div.center div.content a, div#divContent div.center div.content li, div#divContent div.center div.content p' ).css( 'line-height', nLineHeight + 'px' );
    }
  }
}

