                 channel = stream.channel
                 msg.send "#{channel.display_name} (\"#{channel.status}\"): #{channel.url} [Viewers: #{stream.viewers}]"
 
-            if object._total > maxResults
-                msg.reply "There are #{object._total - maxResults} other \"#{category}\" live streams."
+            if object._total > TWITCH_MAX_RESULTS
+                msg.reply "There are #{object._total - TWITCH_MAX_RESULTS} other \"#{category}\" live streams."
 
     robot.respond /ttv search (.+)/i, (msg) ->
         query = msg.match[1]
-        twitchRequest msg, "/search/streams", { q: query, limit: maxResults }, (object) ->
+        GetTwitchResult msg, "/search/streams", { q: query }, (object) ->
             if object._total is 0
                 msg.reply "No live streams were found using search query: \"#{query}\". Try a different query or try again later."
                 return

            msg.send "Viewers: #{object.stream.viewers}"

    robot.respond /ttv top/i, (msg) ->
        createURL = (game) ->
            "http://www.twitch.tv/directory/game/#{encodeURIComponent(game)}"

        twitchRequest msg, "/games/top", limit: maxResults, (object) ->
            for gameObj, i in object.top
                msg.send "#{i + 1}. #{gameObj.game.name} | Viewers: #{gameObj.viewers} | Channels: #{gameObj.channels} | #{createURL(gameObj.game.name)}"

twitchRequest = (msg, api, params = {}, handler) ->
    msg.http("https://api.twitch.tv/kraken#{api}")
        .query(params)
        .get() (err, res, body) ->

# Todo:
# - Save favourites?

#
# Config
#
TWITCH_MAX_RESULTS = 5
TWITCH_STORAGE_KEY = process.env.HUBOT_TWITCH_KEY or "_twitch"

module.exports = (robot) ->
    GetTTVData = ->
        robot.brain.data[TWITCH_STORAGE_KEY] or= {}

    robot.respond /ttv fav/i, (msg) ->
        return
