class MatchBetBBCode(AppResponse):


    def __init__(self, client):
        AppResponse.__init__(self, client)
        self.dependency(js='jquery')
        self.dependency(css='fontawesome')
        self.__idx = int(client.arg('idx'))
        self.data = Data()


    def get_bets(self, idx):
        bets = []
        match = self.data.get_matches_section()[idx]
        homegame = match['game'].startswith('RBK')
        for bet in match['bets']:
            user = bet['user']
            hg, ag = bet['result'].split('-')
            if not homegame: hg, ag = ag, hg
            hg = int(hg)
            ag = int(ag)
            advantage = hg - ag
            rbkgoals = hg
            if advantage < 0: rbkgoals = -rbkgoals
            result = bet['result']
            halftime = bet['halftime'] if 'halftime' in bet else ''
            goalees = len(bet['goals']) if 'goals' in bet else 0
            goals = ','.join(bet['goals']) if 'goals' in bet else ''
            bets += [(advantage, rbkgoals, goalees, user, result, halftime, goals)]
        return sorted(bets, reverse=True)


    def generate_table(self, bets):
        players = self.data.get_players_section()
        html = []
        html += [
            u'<div align="center">\n\n\n',
            u'<table width="480" cellpadding="1" cellspacing="0" border="0" bordercolor="black">',
            u'']
        for idx, bet in enumerate(bets):
            if idx > 0:
                html += [u'<tr height="1" bgcolor="black"><td colspan="5"></td></tr>']
            html += [
                u'<tr height="34">',
                u'<td align="center">[size=12]%d[/size]&nbsp;</td>' % (idx + 1),
                u'<td>&nbsp;[size=12]%s[/size]</td>' % bet[3],
                u'<td align="center">[size=12]%s[/size]</td>' % bet[4],
                u'<td align="center">[size=12]%s[/size]</td>' % bet[5],
                u'<td>&nbsp;']

            for goal in bet[6].split(','):
                if goal == '': continue
                goal = self.data.get_playerid(goal)
                name = goal
                image = players['default']['image']
                if goal in players:
                    name = players[goal]['name']
                    if 'image' in players[goal]:
                        image = players[goal]['image']
                image = self.data.get_imageurl(image)
                html += [
                    u'<img title="%s" src="%s"/> ' % (name, image)]

            html += [
                u'</td></tr>']
        html += [
            u'</table>\n',
            u'</div>']


        contents = ''.join(html)
        contents = contents.replace("<", "&lt;").replace(">", "&gt;")
        return contents


    def html(self):
        self.data.load()

        matches = self.data.get_matches_section()
        match = matches[self.__idx]

        bets = self.get_bets(self.__idx)
        contents = self.generate_table(bets)

        body = []
        body += self.html_heading()
        body += self.html_body_open()
        body += [u'<pre>']

        body += [contents]

        body += [u'</pre>']
        body += self.html_body_close()
        return body
class MatchBetEditor(AppResponse):


    def __init__(self, client):
        AppResponse.__init__(self, client)
        self.dependency(js='jquery')
        self.dependency(js='/apps/rbkweb/MatchBetEditor.js')
        self.dependency(css='fontawesome')

        self.__idx = int(client.arg("idx"))
        self.data = Data()


    def get_round(self, idx):
        return idx


    def html(self):
        self.data.load()

        matches = self.data.get_matches_section()
        players = self.data.get_players_section()
        matchdata = matches[self.__idx]

        title = "%d: %s" % (self.get_round(self.__idx), matchdata['game'])

        html = []
        html += self.html_heading(title)
        html += self.html_body_open()
        html += self.html_header(title, sections=[{'link':'MatchBetting.py','title':'Kamp-indeks'}])
        html += self.html_content_open()

        html += [
            u'<div align="center" style="white-space:nowrap;">',
        ]

        html += [
            u'<table width="480" border="0" cellspacing="0" cellpadding="0"><tr>'
        ]

        if self.__idx > 1:
            html += [
                u'<td align="left"><a href="MatchBetEditor.py?idx=%d"><i class="fa fa-arrow-left"></i></a> <a href="MatchBetEditor.py?idx=%d">%s</a></td>' % (self.__idx-1, self.__idx-1, matches[self.__idx-1]['game'])
            ]
        else:
            html += [
                u'<td align="left"></td>'
            ]
        if self.__idx < (len(matches)-1):
            html += [
                u'<td align="right"><a href="MatchBetEditor.py?idx=%d">%s</a> <a href="MatchBetEditor.py?idx=%d"><i class="fa fa-arrow-right"></i></a></td>' % (self.__idx+1, matches[self.__idx+1]['game'], self.__idx+1)
            ]
        else:
            html += [
                u'<td align="right"></td>'
            ]
        html += [
            u'</tr></table><br/><p>'
        ]
 

        html += [
            u'<table width="480" border="1" bordercolor="black" cellspacing="0" cellpadding="5px">'
        ]

        idx = 0
        html += [
            u'<tr id="row%d">' % idx,
            u'<td align="right">%d</td>' % idx,
            u'<td colspan=4>',
            u'<input id="bet_top" type="text" name="bet_top" value="" size="53"/>',
            u'</td><td>',
            u'<a title="Submit bet" href="javascript:rbkweb.bets.editor.add_bet2(%d);"><i class="fa fa-download"></i></a>&nbsp;&nbsp;' % self.__idx,
            u'<a title="Set result" style="color:red;" href="javascript:rbkweb.bets.editor.set_result2(%d);"><i class="fa fa-gavel"></i></a>' % self.__idx,
            u'</td>',
            u'</tr>'
        ]

        if not 'bets' in matchdata:
            matchdata['bets'] = []

        for bet in matchdata['bets']:
            previous = self.data.get_bets_for_user(bet['user'], self.__idx)

            idx += 1
            halftime = ""
            if 'halftime' in bet:
                halftime = bet['halftime']

            color = u'white'
            if len(previous) == 0:
                color = u'orange'

            html += [
                u'<tr id="row%d">' % idx,
                u'<td align="right">%d</td>' % idx,
                u'<td bgcolor="%s">%s</td>' % (color, bet['user']),
                u'<td align="center">%s</td>' % bet['result'],
                u'<td align="center">%s</td>' % halftime
            ]
            if 'goals' in bet:
                html += [u'<td style="padding:0; spacing=0; margin:0;" valign="bottom">']
                for goalee in bet['goals']:
                    img = players['default']['image']
                    goalee = self.data.get_playerid(goalee)
                    name = goalee
                    if goalee in players:
                        name = players[goalee]['name']
                        if 'image' in players[goalee]:
                            img = players[goalee]['image']

                    html += [
                        u'<img style="padding:0;" src="%s" title="%s" alt="%s"/> ' % (img, name, goalee)
                    ]

                html += [u'</td>']
            else:
                html += [u'<td></td>']

            html += [
                u'<td align="center">',
                u'<a id="edit%d" href="javascript:rbkweb.bets.editor.edit_bet(%d);" title="Edit"><i class="fa fa-edit"></i></a>&nbsp;&nbsp;' % (idx, idx),
                u'<a id="del%d" title="Delete" style="color:red;" href="javascript:rbkweb.bets.editor.delete_bet(%d,%d);"><i class="fa fa-times"></i></a></td>' % (idx, self.__idx, idx)
            ]
            html += [
                u'</tr>'
            ]

        html += [
            u'<tr id="row%d">' % idx,
            u'<td>%d</td>' % (idx + 1),
            u'<td colspan=4>',
            u'<input id="bet" type="text" name="bet" value="" size="53"/>',
            u'</td><td>',
            u'<a id="submit" title="Submit bet" href="javascript:rbkweb.bets.editor.add_bet(%d);"><i class="fa fa-upload"></i></a>&nbsp;&nbsp;' % self.__idx,
            u'<a id="submitres" title="Set result" style="color:red;" href="javascript:rbkweb.bets.editor.set_result(%d);"><i class="fa fa-gavel"></i></a>' % self.__idx,
            u'</td>',
            u'</tr>',
            u'<tr id="row%d">' % (idx+1),
            u'<td></td>',
            u'<td colspan=4>',
            u'<textarea id="multibet" rows="4" cols="51" name="multibet"></textarea>',
            u'</td><td>',
            u'<a id="submit" title="Submit bet" href="javascript:rbkweb.bets.editor.add_multibet(%d);"><i class="fa fa-upload"></i></a>&nbsp;&nbsp;' % self.__idx,
            u'</td>',
            u'</tr>',
            u'</table>']

        html += [
            u'<table width="480">',
            u'<tr>',
            u'<td width="10%">',
            u'</td>',
            u'<td width="80%" align="center">',
            u'<a href="MatchView.py?idx=%d" id="stats" title="Stats Page"><i class="fa fa-bar-chart-o"></i></a>' % self.__idx,
            u'<a href="PlayerSettings.py" id="config" title="Configure Players" style="color:grey;"><i class="fa fa-gear"></i></a>',
            u'</td>',
            u'<td width="10%" align="right">',
            u'<a target="_blank" href="MatchBetBBCode.py?idx=%d" title="BBCode"><i class="fa fa-code"></i></a>' % self.__idx,
            u'</td>',
            u'</tr>',
            u'</table>'
        ]

        html += [
            u'</div>'
        ]

        html += self.html_content_close()
        html += self.html_footer()
        html += self.html_body_close()
        return html
class StandingsView(AppResponse):


    def __init__(self, client):
        AppResponse.__init__(self, client)
        self.dependency(js='jquery2')
        self.dependency(css='fontawesome')
        self.__idx = int(client.arg('idx'))
        self.__mode = client.arg('mode')
        assert self.__mode in ['round', 'series', 'form'], 'invalid mode arg'

        self.__part = 0
        if self.__mode == 'series':
            self.__part = int(client.arg('part'))

        self.data = Data()
        self.standings = Standings(self.data)


    def get_round(self, idx):
        return idx


    def gen_table(self, results, title, separator=False, missing=None, cutoff=1000):
        header = [
            u'<table width="480" border="1" cellspacing="0" cellpadding="5" bordercolor="black" background="%s">' % self.data.get_imageurl('img/pattern.jpg'),
            u'<tr><td border="0">',
            u'<table width="100%" cellpadding="2" cellspacing="0" border="0">',
            u'<tr><td align="center" colspan="8">[b][size=22][color=white]',
            u'%sFILLER' % title,
            u'[/color][/size][/b]</td></tr>',

            u'<tr height="5"></tr>',
            u'<tr height="0" bgcolor="black"><td colspan="8"></td></tr>',
            u'<tr>',
            u'<td bgcolor="white" align="center"><img title="Plassering" src="%s"/></td>' % self.data.get_imageurl("img/icon-trophy.png"),
            u'<td bgcolor="white"><img title="Deltagernavn" src="%s"/> <img title="Jackpot" src="%s"/></td>' % (self.data.get_imageurl("img/icon-player.png"), self.data.get_imageurl("img/icon-ball-crop-high.png")),
            u'<td bgcolor="white" align="center"><img title="Sluttresultat" src="%s"/></td>' % self.data.get_imageurl("img/icon-whistle.png"),
            u'<td bgcolor="white" align="center"><img title="Pauseresultat" src="%s"/></td>' % self.data.get_imageurl("img/icon-clock.png"),
            u'<td bgcolor="white" align="center"><img title="M&aring;lscorere" src="%s"/></td>' % self.data.get_imageurl("img/icon-goal.png"),
            u'<td bgcolor="white" align="center"><img title="Tippetegn HUB" src="%s"/></td>' % self.data.get_imageurl("img/icon-tipping.png"),
            u'<td bgcolor="white" align="center"><img title="Poeng" src="%s"/></td>' % self.data.get_imageurl("img/icon-score.png"),
            u'<td bgcolor="white"></td></tr>',
            u'</tr>',
            u'<tr height="0" bgcolor="black"><td colspan="8"></td></tr>'
        ]

        html = []
        html += [ u'<div align="center">\n' ]
        html += header

        idx = 1
        plass = 1
        prevscore = 0
        block = 0
        for line, score in enumerate(results):

            if score[0] != prevscore:
                if line > 50 and block == 0:
                    block += 1
                    html += [
                        u'</td></tr>',
                        u'</table>',
                        u'</td></tr>',
                        u'</table>\n</div>\n',
                    ]
                    html += [ u'<div align="center">\n' ]
                    html += header
                if separator and plass <= 3 and idx > 3:
                    html += [
                        u'<tr height="5"></tr>',
                        u'<tr height="0" bgcolor="black"><td colspan="8"></td></tr>'
                    ]
                plass = idx

            if plass > cutoff:
                continue

            html += [
                u'<tr height="5"></tr>',
                u'<tr>',
                u'<td bgcolor="white" align="center">[size=12][b]%d[/b][/size]</td>' % plass
            ]

            html += [
                u'<td bgcolor="white">[size=12][b]%s[/b][/size]' % score[5]
            ]
            for jackpot in score[7].split('\n'):
                if not jackpot: continue
                if jackpot == u'': continue
                if not separator: jackpot = u'Jackpot!'
                html += [
                    u' <img title="%s" src="%s"/>' % (jackpot, self.data.get_imageurl("img/icon-ball-crop.png"))
                ]
            html += [
                u'</td>'
            ]

            html += [
                u'<td bgcolor="white" align="center">[size=12][b]%d[/b][/size]</td>' % score[1],
                u'<td bgcolor="white" align="center">[size=12][b]%d[/b][/size]</td>' % score[2],
                u'<td bgcolor="white" align="center">[size=12][b]%d[/b][/size]</td>' % score[3],
                u'<td bgcolor="white" align="center">[size=12][b]%d[/b][/size]</td>' % score[4],
                u'<td bgcolor="white" align="center">[size=12][b]%d[/b][/size]</td>' % score[0]
            ]
            if score[1] > 0:
                html += [
                    u'<td bgcolor="white" align="center"><img src="%s" title="%s"/></td>' % (self.data.get_imageurl("img/icon-stats-crop.png"), score[6].replace('\n', ' || ')),
                ]
            else:
                html += [
                    u'<td bgcolor="white"></td>'
                ]
            html += [
                u'</tr>'
            ]
            idx += 1
            prevscore = score[0]

        html += [
            u'</table>',
            u'</td></tr>',
            u'</table>',
            u'\n</div>'
        ]

        contents = ''.join(html)
        contents = contents.replace(r'<', u'&lt;')
        contents = contents.replace(r'>', u'&gt;')

        return contents


    def html(self):
        self.data.load()

        matches = self.data.get_matches_section()
        match = matches[self.__idx]
        missing = {}

        if self.__mode == 'round':
            separator = False
            results = self.standings.get_score(self.__idx, missing)
            title = u'Runde %d:\n%s' % (self.get_round(self.__idx), match['game'])

        elif self.__mode == 'series':
            separator = True
            title = u'Tabell, runde %d' % (self.get_round(self.__idx))
            results = self.standings.get_score(1, missing)
            matchname = matches[1]['game']
            for i in range(0, len(results)):
                results[i] = (results[i][0],
                              results[i][1],
                              results[i][2],
                              results[i][3],
                              results[i][4],
                              results[i][5],
                              u'%d: %s (%d)' % (self.get_round(1), matchname, results[i][0]),
                              results[i][7],
                              results[i][8])

            if self.__idx > 1:
                for i in range(2, self.__idx+1):
                    matchname = u'%d: %s' % (self.get_round(i), matches[i]['game'])
                    newscore = self.standings.get_score(i, missing)
                    results = self.standings.merge_scores(results, newscore, matchname)

            for i in range(0, len(results)):
                results[i] = (results[i][0],
                              results[i][1],
                              results[i][2],
                              results[i][3],
                              results[i][4],
                              results[i][5],
                              u'Fra %d tips:\n%s' % (results[i][8] + missing.get(results[i][5], 0), results[i][6]),
                              results[i][7],
                              results[i][8])

        elif self.__mode == 'form':
            kamper = 6
            separator = True
            first = max(self.__idx - kamper + 1, 1)
            title = u'Formtabell, runde %d-%d' % (self.get_round(first), self.get_round(self.__idx))
            results = self.standings.get_score(first, missing)
            matchname = matches[first]['game']
            for i in range(0, len(results)):
                results[i] = (results[i][0],
                              results[i][1],
                              results[i][2],
                              results[i][3],
                              results[i][4],
                              results[i][5],
                              u'%d: %s (%d)' % (self.get_round(1), matchname, results[i][0]),
                              results[i][7],
                              results[i][8])

            if self.__idx > 1:
                for i in range(first+1, self.__idx+1):
                    matchname = u'%d: %s' % (self.get_round(i), matches[i]['game'])
                    newscore = self.standings.get_score(i, missing)
                    results = self.standings.merge_scores(results, newscore, matchname)
            for i in range(0, len(results)):
                results[i] = (results[i][0],
                              results[i][1],
                              results[i][2],
                              results[i][3],
                              results[i][4],
                              results[i][5],
                              u'Fra %d tips:\n%s' % (results[i][8] + missing.get(results[i][5], 0), results[i][6]),
                              results[i][7],
                              results[i][8])

        cutoff = 1000
        if self.__mode == 'form': cutoff = 10

        contents = self.gen_table(results, title, separator, missing, cutoff)

        if self.__mode == 'round':
            contents = contents.replace('FILLER', '')
        elif self.__mode == 'series':
            parts = contents.split(u'&lt;/div&gt;')
            for idx, part in enumerate(parts):
                parts[idx] = part.replace('FILLER', ', %d/2' % (idx+1))
            if self.__part > 0:
                contents = parts[self.__part-1] + u'&lt;/div&gt;'
            else:
                contents = u'&lt;/div&gt;'.join(parts)

        elif self.__mode == 'form':
            contents = contents.replace('FILLER', ', topp 10')

        body = []
        body += self.html_heading()
        body += self.html_body_open()
        body += [u'<pre>']

        body += [contents]

        body += [u'</pre>']
        body += self.html_body_close()
        return body
class MainView(AppResponse):


    def __init__(self, client):
        AppResponse.__init__(self, client)
        self.dependency(js='jquery2')
        self.dependency(css='fontawesome')
        self.dependency(js='/apps/rbkweb/MatchEditor.js')
        self.data = Data()


    def get_round(self, idx):
        return idx


    def html(self):
        self.data.load()

        html = []
        html += self.html_heading()
        html += self.html_body_open()
        html += self.html_header()
        html += self.html_content_open()

        teams = self.data.get_teams_section()

        html += [u'<div align=center>']
        html += [u'<table border=1 bordercolor=black cellspacing=0 cellpadding=5>']

        matches = self.data.get_matches_section()
        prevdate = matches[1]['date']
        ongoing = False
        for idx in range(1, len(matches)):
            match = matches[idx]
            havestats = 'bets' in match and len(match['bets'])
            haveresults = 'result' in match
            if havestats and not haveresults:
                ongoing = True
                matchname = match['game']
                hometeam, awayteam = matchname.split(' - ', 2)
                hometeamname = hometeam
                if hometeam in teams:
                    if 'name' in teams[hometeam]:
                        hometeamname = teams[hometeam]['name']
                awayteamname = awayteam
                if awayteam in teams:
                    if 'name' in teams[awayteam]:
                        awayteamname = teams[awayteam]['name']
                havestats = 'bets' in match and len(match['bets'])
                haveresults = 'result' in match

                matchname = u'%s - %s' % (hometeamname, awayteamname)
                matchdate = match['date']
                color = "beige"
                html += [u'<tr bgcolor="%s">' % color,
                         u'<td align="right">%d</td>' % self.get_round(idx)]
                html += [u'<td><a href="MatchView.py?idx=%d">%s</a></td>' % (idx, matchname)]
                html += [u'<td>%s</td>' % matchdate,
                         u'<td><a href="MatchBetEditor.py?idx=%d">bets</a></td>' % idx]
                html += [u'<td><a href="MatchView.py?idx=%d">stats</a></td>' % idx]
                html += [u'<td>results</td>']

                html += ['</tr>']

        if ongoing:
            html += [u'<tr height=2 bgcolor="%s"><td colspan=6></td></tr>' % 'white']

        for idx in range(1, len(matches)):
            match = matches[idx]
            matchname = match['game']
            hometeam, awayteam = matchname.split(' - ', 2)
            hometeamname = hometeam
            if hometeam in teams:
                if 'name' in teams[hometeam]:
                    hometeamname = teams[hometeam]['name']
            awayteamname = awayteam
            if awayteam in teams:
                if 'name' in teams[awayteam]:
                    awayteamname = teams[awayteam]['name']
            havestats = 'bets' in match and len(match['bets'])
            haveresults = 'result' in match

            matchname = u'%s - %s' % (hometeamname, awayteamname)
            matchdate = match['date']
            if matchdate.find(' ') != -1:
                matchdate = matchdate.split(' ',2)[0]

            color = "white"
            if not haveresults:
                color = "beige"
                if not havestats:
                    color = "lightgrey"

            if prevdate[:7] != matchdate[:7]: # new month
                html += [u'<tr height=2 bgcolor="%s"><td colspan=6></td></tr>' % color]

            html += [u'<tr bgcolor="%s">' % color,
                     u'<td align="right">%d</td>' % self.get_round(idx)]
            if havestats:
                html += [u'<td><a href="MatchView.py?idx=%d">%s</a></td>' % (idx, matchname)]
            else:
                html += [u'<td>%s</td>' % matchname]
            html += [u'<td>%s</td>' % matchdate,
                     u'<td><a href="MatchBetEditor.py?idx=%d">bets</a></td>' % idx]

            if havestats:
                html += [u'<td><a href="MatchView.py?idx=%d">stats</a></td>' % idx]
            else:
                html += [u'<td>stats</td>']

            if haveresults:
                html += [u'<td><a href="StandingsView.py?idx=%d">results</a></td>' % idx]
            else:
                html += [u'<td>results</td>']

            html += ['</tr>']
            prevdate = matchdate

        html += [u'<tr height=2 bgcolor="white"><td colspan=6></td></tr>']
        html += [u'<tr><td align="right">%d</td>' % (idx+1)]
        html += [u'<td colspan=4><input id="match" type="text" value="" size="50"></td>']
        html += [u'<td align="center"><a id="submit" title="Add Match" href="#"><i class="fa fa-plus-square"></i></a>']
        html += [u'<a id="config" style="color:grey;" title="Configure Teams" href="TeamSettings.py"><i class="fa fa-gear"></i></a></td></tr>']
        html += [u'</table>']
        html += [u'<script>']
        html += [u'$("#submit").click(function() {']
        html += [u'rbkweb.matches.editor.add_match();']
        html += [u'});']
        html += [u'</script>']
        html += [u'</div>']

        html += self.html_content_close()
        html += self.html_footer()
        html += self.html_body_close()
        return html
class StandingsView(AppResponse):


    def __init__(self, client):
        AppResponse.__init__(self, client)
        self.dependency(js='jquery2')
        self.dependency(css='fontawesome')
        self.__idx = int(client.arg("idx"))
        self.data = Data()
        self.standings = Standings(self.data)


    def get_round(self, idx):
        return idx


    def html_tableheader(self):
        html = [
            u'<tr height="5"></tr>',
            u'<tr height="0" bgcolor="black"><td colspan="8"></td></tr>',
            u'<tr>',
            u'    <td bgcolor="white" align="center"><img title="Plassering" src="%s"/></td>' % self.data.get_imageurl(u'img/icon-trophy.png'),
            u'    <td bgcolor="white" align="left"><img title="Deltagernavn" src="%s"/> <img src="%s" title="Jackpot"/></td>' % (self.data.get_imageurl(u'img/icon-player.png'), self.data.get_imageurl(u'img/icon-ball-crop-high.png')),
            u'    <td bgcolor="white" align="center"><img title="Sluttresultat" src="%s"/></td>' % self.data.get_imageurl(u'img/icon-whistle.png'),
            u'    <td bgcolor="white" align="center"><img title="Pauseresultat" src="%s"/></td>' % self.data.get_imageurl(u'img/icon-clock.png'),
            u'    <td bgcolor="white" align="center"><img title="M&aring;lscorere" src="%s"/></td>' % self.data.get_imageurl(u'img/icon-goal.png'),
            u'    <td bgcolor="white" align="center"><img title="Tippetegn HUB" src="%s"/></td>' % self.data.get_imageurl(u'img/icon-tipping.png'),
            u'    <td bgcolor="white" align="center"><img title="Poeng" src="%s"/></td>' % self.data.get_imageurl(u'img/icon-score.png'),
            u'    <td bgcolor="white"></td></tr>',
            u'</tr>',
            u'<tr height="0" bgcolor="black"><td colspan="8"></td></tr>'
        ]
        return html


    def html(self):
        self.data.load()

        html = []
        html += self.html_heading()
        html += self.html_body_open()
        html += self.html_header('Resultater', sections=[{'link':'MatchBetting.py','title':'Kamp-indeks'}])
        html += self.html_content_open()

        html += [
            u'<div align="center">'
        ]

        matches = self.data.get_matches_section()
        match = matches[self.__idx]

        # round score

        html += [
            u'<table width="480" border="0" cellspacing="0" cellpadding="0"><tr>'
        ]

        if self.__idx > 1:
            html += [
                u'<td align="left"><a href="StandingsView.py?idx=%d"><i class="fa fa-arrow-left"></i></a> <a href="StandingsView.py?idx=%d">%s</a></td>' % (self.__idx-1, self.__idx-1, matches[self.__idx-1]['game'])
            ]
        else:
            html += [
                u'<td align="left"></td>'
            ]
        if self.__idx < (len(matches)-1) and 'result' in matches[self.__idx + 1]:
            html += [
                u'<td align="right"><a href="StandingsView.py?idx=%d">%s</a> <a href="StandingsView.py?idx=%d"><i class="fa fa-arrow-right"></i></a></td>' % (self.__idx+1, matches[self.__idx+1]['game'], self.__idx+1)
            ]
        else:
            html += [
                u'<td align="right"></td>'
            ]
        html += [
            u'</tr></table><br/><p>'
        ]
        html += [
            u'<table width=480 border="1" cellspacing="0" cellpadding="5" bordercolor="black" background="%s">' % self.data.get_imageurl(u'img/pattern.jpg'),
            u'<tr><td border="0">',
            u'<table width="100%" cellpadding="2" cellspacing="0" border="0">',
            u'<tr><td align="center" colspan="8"><b><font size="+3" color="white">',
            u'Runde %d:<br>' % self.get_round(self.__idx),
            match['game'],
            u'</font></b></td></tr>'
        ]

        html += self.html_tableheader()

        idx = 1
        plass = 1
        prevscore = 0
        for score in self.standings.get_score(self.__idx, dict()):
            if score[0] != prevscore:
                plass = idx

            html += [
                u'<tr height="5"></tr>',
                u'<tr>',
                u'<td bgcolor="white" align="center">%d</td>' % plass]
            if score[6]:
                html += [
                    u'<td bgcolor=white>%s' % score[5]
                ]
                for jackpot in score[7].split('\n'):
                    if jackpot != '':
                        html += [
                            ' <img src="%s" title="Jackpot!"/>' % self.data.get_imageurl(u'img/icon-ball-crop.png')
                        ]
                html += [u'</td>']
            else:
                html += [
                    u'<td bgcolor="white">%s</td>' % score[5]
                ]
            html += [
                u'<td bgcolor="white" align="center">%d</td>' % score[1],
                u'<td bgcolor="white" align="center">%d</td>' % score[2],
                u'<td bgcolor="white" align="center">%d</td>' % score[3],
                u'<td bgcolor="white" align="center">%d</td>' % score[4],
                u'<td bgcolor="white" align="center">%d</td>' % score[0]
            ]

            if score[0] == 1:
                html += [ u'<td bgcolor="white"></td>' ]
            else:
                html += [ u'<td bgcolor="white" align="center"><img src="%s" title="%s"/></td>' % (self.data.get_imageurl(u'img/icon-stats-crop.png'), score[6]) ]
            html += [
                u'</tr>'
            ]
            idx += 1
            prevscore = score[0]

        html += [
            u'</table>',
            u'</td></tr>',
            u'</table>',
        ]

        html += [
            u'<table width=480 border="1" cellspacing=0 cellpadding=5 bordercolor="white">',
            u'<tr><td align=right><a title="BBCode" target="_blank" href="StandingsBBCode.py?idx=%d&mode=round"><i class="fa fa-code"></i></a></td></tr>' % self.__idx,
            u'</table>',
            u'<p>'
        ]

        # series standings
        misses = {}
        score = self.standings.get_score(1, misses)
        matchname = matches[1]['game']
        for i in range(0, len(score)):
            score[i] = (score[i][0],
                        score[i][1],
                        score[i][2],
                        score[i][3],
                        score[i][4],
                        score[i][5],
                        u'%d: %s (%d)' % (self.get_round(1), matchname, score[i][0]),
                        score[i][7],
                        score[i][8])

        if self.__idx > 1:
            for i in range(2, self.__idx+1):
                matchname = u'%d: %s' % (self.get_round(i), matches[i]['game'])
                newscore = self.standings.get_score(i, misses)
                score = self.standings.merge_scores(score, newscore, matchname)

        html += [
            u'<table width=480 border="1" cellspacing=0 cellpadding=5 bordercolor="black" background="%s">' % self.data.get_imageurl(u'img/pattern.jpg'),
            u'<tr><td border=0>',
            u'<table width="100%" cellpadding="2" cellspacing=0 border="0">',
            u'<tr><td align="center" colspan="8"><b><font size=+3 color=white>',
            u'Tabell, runde %d' % self.get_round(self.__idx),
            u'</font></b></td></tr>'
        ]

        html += self.html_tableheader()

        idx = 1
        plass = 1
        prevscore = 0
        for entry in score:
            if entry[0] != prevscore:
                if plass <= 3 and idx > 3:
                    html += [
                        u'<tr height="5"></tr>',
                        u'<tr height="0" bgcolor="black"><td colspan="8"></td></tr>'
                    ]
                plass = idx

            html += [
                u'<tr height="5"></tr>',
                u'<tr>',
                u'<td bgcolor="white" align="center">%d</td>' % plass
            ]
            if entry[7]:
                html += [
                    u'<td bgcolor="white">%s' % entry[5]
                ]
                for jackpot in entry[7].split('\n'):
                    html += [
                        ' <img src="%s" title="%s"/>' % (self.data.get_imageurl(u'img/icon-ball-crop.png'), jackpot)
                    ]
                html += [u'</td>']
            else:
                html += [
                    u'<td bgcolor="white">%s</td>' % entry[5]
                ]
            html += [
                u'<td bgcolor="white" align="center">%d</td>' % entry[1],
                u'<td bgcolor="white" align="center">%d</td>' % entry[2],
                u'<td bgcolor="white" align="center">%d</td>' % entry[3],
                u'<td bgcolor="white" align="center">%d</td>' % entry[4],
                u'<td bgcolor="white" align="center">%d</td>' % entry[0]
            ]
            if entry[1] == 0:
                html += [
                    u'<td bgcolor="white"></td>'
                ]
            else:
                html += [
                    u'<td bgcolor="white" align="center"><img src="%s" title="%s"/></td>' % (self.data.get_imageurl(u'img/icon-stats-crop.png'), u'Fra %d tips:\n%s' % (entry[8] + misses.get(entry[5], 0), entry[6]))
                ]
            html += [
                u'</tr>'
            ]
            idx += 1
            prevscore = entry[0]


        html += [
            u'</table>',
            u'</td></tr>',
            u'</table>'
        ]
        html += [
            u'<table width=480 border="1" cellspacing=0 cellpadding=5 bordercolor="white">',
            u'<tr><td align=right><a title="BBCode 1/2" target="_blank" href="StandingsBBCode.py?idx=%d&mode=series&part=1"><i class="fa fa-code"></i></a> ' % self.__idx,
            u'<a title="BBCode 2/2" target="_blank" href="StandingsBBCode.py?idx=%d&mode=series&part=2"><i class="fa fa-code"></i></a></td></tr>' % self.__idx,
            u'</table>',
            u'<p>'
        ]

        # formtabell

        formlength = 6
        first = max(self.__idx - formlength + 1, 1)

        misses = {}
        score = self.standings.get_score(first, misses)
        matchname = matches[first]['game']
        for i in range(0, len(score)):
            score[i] = (score[i][0],
                        score[i][1],
                        score[i][2],
                        score[i][3],
                        score[i][4],
                        score[i][5],
                        u'%d: %s (%d)' % (self.get_round(first), matchname, score[i][0]),
                        score[i][7],
                        score[i][8])

        if self.__idx > 1:
            for i in range(first+1, self.__idx+1):
                matchname = u'%d: %s' % (self.get_round(i), matches[i]['game'])
                newscore = self.standings.get_score(i, misses)
                score = self.standings.merge_scores(score, newscore, matchname)

        html += [
            u'<table width=480 border="1" cellspacing=0 cellpadding=5 bordercolor="black" background="%s">' % self.data.get_imageurl(u'img/pattern.jpg'),
            u'<tr><td border=0>',
            u'<table width="100%" cellpadding="2" cellspacing=0 border="0">',
            u'<tr><td align="center" colspan="8"><b><font size=+3 color=white>',
            u'Formtabell, runde %d-%d' % (self.get_round(first), self.get_round(self.__idx)),
            u'</font></b></td></tr>'
        ]

        html += self.html_tableheader()

        idx = 1
        plass = 1
        prevscore = 0
        for entry in score:
            if entry[0] != prevscore:
                if plass <= 3 and idx > 3:
                    html += [
                        u'<tr height="5"></tr>',
                        u'<tr height="0" bgcolor="black"><td colspan="8"></td></tr>'
                    ]
                plass = idx

            html += [
                u'<tr height="5"></tr>',
                u'<tr>',
                u'<td bgcolor=white align=center>%d</td>' % plass
            ]
            if entry[7]:
                html += [
                    u'<td bgcolor=white>%s' % entry[5]
                ]
                for jackpot in entry[7].split('\n'):
                    html += [
                        ' <img src="%s" title="%s"/>' % (self.data.get_imageurl(u'img/icon-ball-crop.png'), jackpot)
                    ]
                html += [u'</td>']
            else:
                html += [
                    u'<td bgcolor=white>%s</td>' % entry[5]
                ]
            html += [
                u'<td bgcolor=white align=center>%d</td>' % entry[1],
                u'<td bgcolor=white align=center>%d</td>' % entry[2],
                u'<td bgcolor=white align=center>%d</td>' % entry[3],
                u'<td bgcolor=white align=center>%d</td>' % entry[4],
                u'<td bgcolor=white align=center>%d</td>' % entry[0]
            ]
            if entry[1] == 0:
                html += [
                    u'<td bgcolor=white></td>'
                ]
            else:
                html += [
                    u'<td bgcolor=white align=center><img src="%s" title="%s"/></td>' % (self.data.get_imageurl(u'img/icon-stats-crop.png'), u'Fra %d tips:\n%s' % (entry[8] + misses.get(entry[5], 0), entry[6]))
                ]
            html += [
                u'</tr>'
            ]
            idx += 1
            prevscore = entry[0]


        html += [
            u'</table>',
            u'</td></tr>',
            u'</table>'
        ]
        html += [
            u'<table width=480 border="1" cellspacing=0 cellpadding=5 bordercolor="white">',
            u'<tr><td align=right><a title="BBCode top10" target="_blank" href="StandingsBBCode.py?idx=%d&mode=form"><i class="fa fa-code"></i></a></td></tr>' % self.__idx,
            u'</table>'
        ]

        html += [
            u'</div>'
        ]

        html += self.html_content_close()
        html += self.html_footer()
        html += self.html_body_close()
        return html