// Generated by CoffeeScript 1.6.1

/*
# Backwards compatible dynamic disclaimers.
#
# does most of the filtering work up front so that we
# aren't doing expensive stuff in the ajax call, and
# uses the ajax deferred.done and fail api so that
# we aren't mixing handling concerns into the
# ajax configuration.
#
# For ajax failures (ex, we don't want to be running a long request query just for
# disclaimers)
# It default times out in 200 ms.
# we can use a globally defined defaultDisclaimerMessage. An ultimate fallback
# is predefined here JIC.
*/


(function() {

  $(function() {
    var $, Modernizr, bootbox, confirm, defaultMessage, disclaimerData, global, location, open, _base;
    global = global;
    global || (global = window);
    $ = global.$;
    Modernizr = global.Modernizr;
    bootbox = global.bootbox;
    open = global.open;
    location = global.location;
    confirm = global.confirm;
    defaultMessage = global.defaultDisclaimerMessage;
    defaultMessage || (defaultMessage = 'You will be linking to another website not owned or operated by ' + location.hostname + '. ' + location.hostname + ' is not responsible for the availability or content of this website and does not represent either the linked website or you, should you enter into a transaction. We encourage you to review their privacy and security policies which may differ from ' + location.hostname + '.');
    if (!$.expr[':'].external) {
      $.expr[':'].external = function(obj) {
        return (!obj.href.match(/^mailto\:/)) && (obj.hostname !== location.hostname) && (!obj.href.match(/^javascript/)) && (!obj.href.match(/^tel\:/)) && (!obj.href.match(/^https\:\/\/pass\.t8webware\.com/));
      };
    }
    global.com || (global.com = {});
    (_base = global.com).banno || (_base.banno = {});
    disclaimerData = $.ajax({
      type: 'GET',
      contentType: 'application/json',
      url: '/_/api/disclaimer',
      timeout: 2000
    });
    global.com.banno.watchDisclaimers = function() {
      var bootboxConfirm, confirmer, disclaimedAnchors, disclaimerMessage, doDisclaimer, doneHandler, failedHandler, getSpeedbump, nativeConfirm, speedbumpConfirm;
      disclaimedAnchors = $('a[data-disclaimer-id]');
      disclaimedAnchors.map(function(index, element) {
        var $element, currentHandlers, handlers, _ref;
        currentHandlers = (_ref = $._data(element, 'events')) != null ? _ref.click : void 0;
        if ((currentHandlers != null)) {
          handlers = $.map($._data(element, 'events').click, function(handler, handlersIndex) {
            if (handler.handler.toString().indexOf("bootbox.confirm") === -1 && handler.handler.toString().indexOf('#speedbump') === -1 && handler.handler.toString().indexOf("popDisclaimer") === -1) {
              return handler.handler;
            }
          });
          $element = $(element);
          $element.off('click');
          return $.map(handlers, function(handler, handlerIndex) {
            return $element.on('click', handler);
          });
        }
      });
      getSpeedbump = function(elem) {
        return $(elem).attr('href');
      };
      nativeConfirm = function(confirmationCallback, disclaimer) {
        return confirmationCallback(confirm(disclaimer));
      };
      bootboxConfirm = function(confirmationCallback, disclaimer, header) {
        var disclaimerWithHeader;
        try {
          if ((header != null)) {
            disclaimerWithHeader = "<h4>" + header + "</h4>" + disclaimer;
          } else {
            disclaimerWithHeader = disclaimer;
          }
          return bootbox.confirm(disclaimerWithHeader, 'Cancel', 'Proceed', confirmationCallback);
        } catch (e) {
          return nativeConfirm(confirmationCallback, disclaimer);
        }
      };
      confirmer = function(confirmationCallback, disclaimer, header) {
        if (Modernizr != null ? typeof Modernizr.mq === "function" ? Modernizr.mq('only all and (max-width: 767px)') : void 0 : void 0) {
          return nativeConfirm(confirmationCallback, disclaimer);
        } else {
          return bootboxConfirm(confirmationCallback, disclaimer, header);
        }
      };
      speedbumpConfirm = function(speedbump, disclaimer, header, target) {
        target = target != null ? target : "_self";
        return confirmer(function(result) {
          if (result !== false) {
            return open(speedbump, target);
          }
        }, disclaimer, header);
      };
      doDisclaimer = function(data) {
        return function(elem) {
          var header, message, obj, target, _i, _len;
          header = null;
          message = defaultMessage;
          target = null;
          for (_i = 0, _len = data.length; _i < _len; _i++) {
            obj = data[_i];
            if (obj.id === $(elem).attr('data-disclaimer-id')) {
              header = obj.header;
              message = obj.message;
              target = $(elem).attr('target');
            }
          }
          if ((header != null)) {
            header = disclaimerMessage(header);
          }
          message = disclaimerMessage(message);
          return speedbumpConfirm(getSpeedbump(elem), message, header, target);
        };
      };
      doneHandler = function(data, status, jqXHR) {
        var popDisclaimer;
        popDisclaimer = doDisclaimer(data);
        return disclaimedAnchors.map(function(index, element) {
          var $element;
          $element = $(element);
          if ($element.attr('data-disclaimer-id') !== "null") {
            $(element).on('click', function(event) {
              event.preventDefault();
              return popDisclaimer(this);
            });
          }
          return element;
        });
      };
      failedHandler = function(jqXHR, textStatus, errorThrown) {
        var disclaimer, popDisclaimer;
        disclaimer = disclaimerMessage(defaultDisclaimerMessage);
        popDisclaimer = doDisclaimer([
          {
            id: 'default',
            title: 'Default',
            header: null,
            message: disclaimer
          }
        ]);
        return disclaimedAnchors.map(function(index, element) {
          var $element;
          $element = $(element);
          if ($element.attr('data-disclaimer-id') !== "null") {
            $(element).attr('data-disclaimer-id', 'default').on('click', function(event) {
              event.preventDefault();
              return popDisclaimer(this);
            });
          }
          return element;
        });
      };
      disclaimerMessage = function(disclaimer) {
        return $('<div>').text(disclaimer).html();
      };
      return disclaimerData.done(doneHandler).fail(failedHandler);
    };
    return global.com.banno.watchDisclaimers();
  });

}).call(this);
