 
   build: (buildWithEmptyParameters) =>
     return if not @_init(@build)
-    job = @_getJob(true)
+    job = @_getJob()
     server = @_serverManager.getServerByJobName(job)
     command = if buildWithEmptyParameters then "buildWithParameters" else "build"
     path = if @_params then "job/#{job}/buildWithParameters?#{@_params}" else "job/#{job}/#{command}"
     if !server
       @msg.send "I couldn't find any servers with a job called #{@_getJob()}.  Try `jenkins servers` to get a list."
       return
-    @_requestFactorySingle server, path, @_handleBuild, "post"
+    @_requestFactorySingle server, null, path, @_handleBuild, "post"
 
   describeById: =>
     return if not @_init(@describeById)

  # Utility Methods
  # ---------------

  _addJobsToJobsList: (jobs, server, outputStatus = false) =>
    response = ""
    filter = new RegExp(@msg.match[2], 'i')

    for job in jobs
      # Add the job to the @_jobList
      server.addJob(job)
      index = @_jobList.indexOf(job.name)
      if index == -1
        @_jobList.push job.name
        index = @_jobList.indexOf(job.name)

      state = if job.color == "red" then "FAIL" else "PASS"
      if filter.test job.name
        response += "[#{index + 1}] #{state} #{job.name} on #{server.url}\n"

    @send response if outputStatus

  _configureRequest: (request, server = null) =>
    defaultAuth = process.env.HUBOT_JENKINS_AUTH
    return if not server and not defaultAuth
    selectedAuth = if server then server.auth else defaultAuth
    #auth = new Buffer(selectedAuth).toString('base64')
    #request.headers Authorization: "Basic #{auth}"
    request.header('Content-Length', 0)
    request


  url: null
  auth: null
  _hasListed: false
  _rootFolder: null
  _querystring: null

  constructor: (url, auth) ->
    @url = url
    @auth = auth
    @_querystring = require 'querystring'

  hasInitialized: ->
    @_hasListed

  setFolder: (folder) =>
    @_hasListed = true
    @_rootFolder = folder

  getFolder: =>
    @_rootFolder

  hasFolder: =>
    true if @_rootFolder

  getFolderByName: (folderName) =>
    @_rootFolder.getFolderByName(folderName)

  hasFolderByName: (folderName) =>
    @_rootFolder.hasFolderByName(folderName)

  getJobByName: (jobName) =>
    @_rootFolder.getJobByName(jobName)

  hasFolderByName: (jobName) =>
    @_rootFolder.hasJobByName(jobName)

class JenkinsFolder
  name: null
  path: null
  depth: null
  _jobs: null
  _folders: null
  _querystring: null

  constructor: (name, path, depth) ->
    @name = name
    @path = path
    @depth = depth
    @_jobs = []
    @_folders = []
    @_querystring = require 'querystring'

  hasInitialized: ->
    @_hasListed

  addJob: (job) =>
    @_hasListed = true
    @_jobs.push job if not @hasJobByName(job.name, false)

  getJobs: =>
    @_jobs
