   self = this
   self.searchedGames = ko.observableArray([])
   self.selectedGame = ko.observable()
+  # Indicates that information is currently loading
   self.searching = ko.observable(null)
+  # 1 is ascending, -1 is descending
   sortDirection = -1
   currentSort = 'brating'
 
-  #Sorting of search results
+  # Sorting of search results by name/title
+  # direction [Number] (1 is ascending, -1 is descending) OPTIONAL
   self.sortByName = (direction) ->
     sortDirection = direction if direction?
     self.searchedGames.sort (a, b) ->

      $(event.toElement).addClass "headerSortUp"
    return  

  #get information from boardgame object

  self.getGoodComments = (comments) ->
    gc = comments.comment.filter((el) ->
      el.value.length > 119 and parseInt(el.rating) > 0 and el.value.length < 600
    )
    gc[Math.floor(Math.random() * gc.length)]

  self.getRankLink = (name, id, value) ->
    return "http://boardgamegeek.com/browse/boardgame?sort=rank&rankobjecttype=subtype&rankobjectid=" + id + "&rank=" + value + "#" + value  if name is "boardgame"
    "http://boardgamegeek.com/" + name + "/browse/boardgame?sort=rank&rankobjecttype=subtype&rankobjectid=" + id + "&rank=" + value + "#" + value

  self.getRanks = (stats) ->
    stats.ratings.ranks.rank

  self.getAverageRating = (stats) ->
    stats.ratings.average.value

  self.getBRating = (stats) ->
    stats.ratings.bayesaverage.value

  self.getCategoriesFromLinks = (link) ->
    categories = []
    i = 0

    while i < link.length
      categories.push link[i]["value"]  if link[i]["type"] is "boardgamecategory"
      i++
    categories

  self.getDesignerFromLinks = (link) ->
    i = 0

    while i < link.length
      return link[i]["value"]  if link[i]["type"] is "boardgamedesigner"
      i++
    return

  self.getName = (name) ->
    return name[0].value  if Array.isArray(name)
    name.value

  self.getShortDescription = (description) ->
    description.slice(0, 100) + "..."

  #utilities

  self.parseDescription = (description) ->
    paragraphs = 1
    contenthid = false
    regex = new RegExp("&#10;&#10;&#10;    ", "g")
    description = description.replace(regex, "<ul><li>")
    regex = new RegExp("&#10;&#10;&#10;", "g")
    description = description.replace(regex, "</li></ul>")
    regex = new RegExp("&#10;    ", "g")
    description = description.replace(regex, "</li><li>")
    description = "<p>" + description
    regex = new RegExp("&#10;&#10;", "g")
    description = description.replace(regex, "</p><p>")
    description += "</p>"
    i = 0

    while i < description.length
      if description.slice(i, i + 3) is "<p>" or description.slice(i - 5, i) is "</ul>"
        paragraphs += 1
        if (paragraphs > 3 and i > 600 and description.length - i > 7) and contenthid is false
          description = description.slice(0, i) + "<div class='full-description' style='display:none'>" + description.slice(i, description.length)
          contenthid = true
          break
      i++
    description += "</div><a onclick=$('.full-description').toggle(function(){$('.show-more').toggleClass('ion-chevron-up')});><i class='show-more icon ion-chevron-down'></i></a>"  if contenthid
    regex = new RegExp(self.getName(self.selectedGame().name), "g")
    description = description.replace(regex, "<b>" + self.getName(self.selectedGame().name) + "</b>")
    description

  self.searchGames = (str) ->
    self.searchedGames.removeAll()
    return  if str is ""
    regex = new RegExp(" ", "g")
    str = str.replace(regex, "+")
    self.searching true
    if Modernizr.sessionstorage
      ids = eval("(" + sessionStorage["searched_bg_ids_" + str] + ")")
      if ids
        self.getGamesDetails ids, str
        self.searching null
        return
    else

    url = "http://www.boardgamegeek.com/xmlapi/search?search=" + str
    $.getJSON self.getYQLurl(url), (data) ->
      ids = self.extractIdsFromSearch(data)
      sessionStorage["searched_bg_ids_" + str] = JSON.stringify(ids)
      self.getGamesDetails ids, str
      return

    return

  self.extractIdsFromSearch = (data) ->
    console.log data
    if data.query.results
      jdata = data.query.results.boardgames["boardgame"]
      ids = []
      if Array.isArray(jdata)
        i = 0

        while i < jdata.length
          ids.push jdata[i]["objectid"]
          i++
      else
        ids.push jdata["objectid"]
      ids

  self.getSomething = ->
    console.log "here"


    return

  # Returns the YQL query
  # str [String]
  self.getYQLurl = (str) ->
    q = "select * from xml where url="
    url = "'#{str}'"
    return "http://query.yahooapis.com/v1/public/yql?q=#{encodeURIComponent(q + url)}&format=json&callback=?"

  # Loads data for a given boardgame id
  # id [String]
  self.getGameDetails = (id) ->
    if Modernizr.sessionstorage
      gdata = eval("(" + sessionStorage["bg" + id] + ")")
