<%page args="error, list, artist, stylesheet, datasource" />
<html> <head> <meta http-equiv="content-type" content="text-html; charset=utf-8">
<%!
    import re
    import cgi
    import rfc822

    def cleanup(text):
        return re.sub(r'\([^\)]*\)', '', text)

    def sec2hms(time):
        hr = time / 3600
        if hr > 0:
            time %= 3600
        mn = time / 60
        sec = time % 60
        if hr > 0:
            return _("%d:%02d:%02d") % (hr,mn,sec)
        else:
            return _("%d:%02d") %(mn,sec)

    def format_year(date):
        try:
            parsed = rfc822.parsedate(date)
        except Exception, e:
            return ""
        if parsed is None:
            return ""
        else:
            return '[' + str(parsed[0]) + ']'

%>  
<link rel="stylesheet" href="${stylesheet}" type="text/css" />
<script language="javascript">
    function swapClass (element, klass1, klass2) {
        elt = document.getElementById(element);
        elt.className = (elt.className == klass1) ? klass2 : klass1;
    }
    function swapText (element, text1, text2) {
        elt = document.getElementById(element);
        elt.innerHTML = (elt.innerHTML == text1) ? text2 : text1;
    }
    function toggle_vis (element) { 
        swapClass(element, 'hidden', 'shown');
        hide = ${ '"' + _("Hide all tracks") + '"' };
        show = ${ '"' + _("Show all tracks") + '"' };
        swapText('btn_'+element, hide, show);
    }
</script>
</head>
<body>
%if error is None:
<%  
    num_albums = min(8, len(list))
%>
    <h1>${ _("Top albums by %s") % ("<em>" + cgi.escape(artist, True) + "</em>") }</h1>
%for i, entry in enumerate(list) :
    <%
    if 'tracklist' not in entry or len(entry['tracklist']) == 0:
        continue
    if i == num_albums:
        break
    %>
    <div id="album${entry['id'] | h}" class="album">
    <img width="64" src="${entry['images'][1] | h}" alt="${entry['images'] | h}"/>
    <h2>${entry['title'] | h}
    %if 'releasedate' in entry:
    ${ format_year(entry['releasedate']) }
    %endif
    </h2>
    %if 'duration' in entry:
    <% 
        album_time = sec2hms(entry['duration'])
        tracks = len(entry['tracklist'])
    %>
    <p class="duration">${ _("%s (%d tracks)") % (album_time, tracks) }</p>
    %endif
    %if 'tracklist' in entry:
    <button id="btn_${entry['id'] | h}" onclick="toggle_vis(${entry['id'] | h})">
    ${ _("Show all tracks") }
    </button>
    <table class="hidden" id="${entry['id'] | h}">
        %for num, title, time in entry['tracklist'] :
            <% 
                time = sec2hms(time)
                title = cleanup(title)
                num = num+1
            %>
            <tr><td>${num}</td><td>${title | h}</td><td>${time}</td></tr>
        %endfor
    </table>
    %else:
    <p>${ _("Track list not available") }</p>
    %endif
    </div>
%endfor
    <p>${datasource}</p>
%else:
    <h1>${ _("Unable to retrieve album information:") }</h1>
    <p class="error">${error | h}</p>
%endif
</body>
</html>
