                 return b.length - a.length
         lastKeyword = null
         keywordsUnique = []
-        for keyword in keywordsNormalized
+        for keyword in unorderedKeywords
                 if keyword != lastKeyword
                         keywordsUnique.push keyword
                 lastKeyword = keyword

createSearchIndex = (song) ->
        index = song.filename.toLowerCase()
        if song.album
                album = song.album.toLowerCase()
                index += " album:#{album}"
        if song.title
                title = song.title.toLowerCase()
                index += " title:#{title}"
        if song.artist
                artist = song.artist.toLowerCase()
                index += " artist:#{artist}"
        return index



Identifier =
        ALBUM: "a"
        TITLE: "t"
        ARTIST: "r"

normalizeKey = (value) ->
        normalized = value
        normalized = normalized.replace(/\s+/, '-')
        normalized = normalized.replace(/-+/, '-')
        normalized = normalized.replace(/^-+/, '')
        normalized = normalized.replace(/-+$/, '')

        normalized = normalized.toLowerCase()

        normalized = normalized.replace("album:", "#{Identifier.ALBUM}:")
        normalized = normalized.replace("title:", "#{Identifier.TITLE}:")
        normalized = normalized.replace("artist:", "#{Identifier.ARTIST}:")

        return normalized


parseKeywords = (searchValue) ->
        cleaned = searchValue.replace(/^\s+/, '')
        cleaned = cleaned.replace(/\s+$/, '')
        keywords = cleaned.split /\s+/
        keywordsNormalized = []
        for keyword in keywords
                if keyword == ""
                        continue
                keywordsNormalized.push normalizeKey keyword
        return keywordsNormalized


createSearchList = (keywords) ->
        unorderedKeywords = [keyword for keyword in keywords]
        unorderedKeywords.sort (a, b) ->
                if (b.length - a.length) == 0
                        if a == b
                                return 0
