 # This code was shamelessly stolen from:
 # https://github.com/jaubourg/ajaxHooks/blob/master/src/ajax/xdr.js
 if $.browser.msie and parseInt($.browser.version, 10) in [8, 9]
+  console?.log "Hacking AJAX CORS for Internet Explorer"
   jQuery.ajaxTransport (s) ->
     if s.crossDomain and s.async
       if s.timeout

# jQuery.ajaxSettings.xhr was breaking regular IE7 loading. Better to localize
# this anyway to solve that problem and loading order issues.
hackAroundFirefoxXhrHeadersBug = ->
  xhr = jQuery.ajaxSettings.xhr()
  originalFun = xhr.getAllResponseHeaders
  xhr.getAllResponseHeaders = ->
    XHR_HEADERS = [
      "Cache-Control", "Content-Language", "Content-Type"
      "Expires", "Last-Modified", "Pragma"
    ]
    return allHeaders if (allHeaders = originalFun.call xhr)?
    lines = for name in XHR_HEADERS when xhr.getResponseHeader(name)?
      "#{name}: #{xhr.getResponseHeader name}"
    lines.join '\n'

    data.last_message_sequence = @_lastMessageSequence
    # TODO: Some of these options will be deprecated in jQuery 1.8
    #       See: http://api.jquery.com/jQuery.ajax/#jqXHR
    opts =
      crossDomain:  true
      data:         data
      timeout:      @_timeout
      success:      @_success
      error:        @_error
      complete:     @_xhrComplete
    opts.xhr = hackAroundFirefoxXhrHeadersBug if $.browser.mozilla
    $.ajax @config.longPoll.url, opts

  _xhrComplete: (jqXhr) =>
    console?.log "XHR complete", arguments
    # Get the last sequence from the server if specified.
    console?.log "status #{jqXhr.status}"
    if jqXhr.status == 200
      val = jqXhr.getResponseHeader @messageSequenceHeader
      @_lastMessageSequence = val if val?
      console?.log "Header #{@messageSequenceHeader} => #{val}"
      if @_lastMessageSequence == null
        console?.log 'ERROR: Unable to get last message sequnce from header'

