( function( $ ) {

$( function() {
  // placeholders for non-supporting browsers
  $( 'form.listbuilder-optin input[placeholder]' ).placeholder();

  $( 'form.listbuilder-optin' ).submit( function() {
    var form = this;

    // cookie user as opted in when they submit a form
    var expires = new Date(); expires.setYear( expires.getFullYear() + 1 ); expires.toUTCString();
    document.cookie = 'listbuilder_opted_in=yes;expires=' + expires.toUTCString() + ';path=/';

    // make a page request to do optin tracking
    var optin_location = $( form ).attr( 'data-listbuilder-location' );
    $.get( '?', { listbuilder_source: optin_location }, function() {
      form.submit();
    } );

    return false;
  });

  // close button for footer optin
  $( 'div.listbuilder-footer-optin' ).each( function() {
    var close = $( '<div class="listbuilder-footer-optin-close">Close [x]</div>' );

    close.click( function() {
      $( 'div.listbuilder-footer-optin, div.listbuilder-footer-optin-shim' ).remove();
    } );

    $( this ).append( close );
  } );

  if ( $( 'meta[property="exitpopup:message"]' ).length ) {
    // exit popup
    window.onbeforeunload = function() {
      window.onbeforeunload = null;

      var e = e || window.event,
          msg = $( 'meta[property="exitpopup:message"]' ).attr( 'content' );

      setTimeout( function() {
        var url = $( 'meta[property="exitpopup:url"]' ).attr( 'content' );
        setTimeout( function() { window.location = url; }, 500 );
      }, 0 );

      if ( e ) {
        e.returnValue = msg;
      }

      return msg;
    };

    $( 'form' ).submit( function() { window.onbeforeunload = null; } );
    $( 'a' ).click( function() { window.onbeforeunload = null; } );
  }
} );

} )( jQuery );

