<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/">
<head>
  <title>${request.path_info}</title>
  <script src="/jquery.js"></script>
  <script type="text/javascript">
$(document).ready(function() {  
        $("td").click(function() { 
    
                if ( $(this).find('input').length == 0 ) {
    
                    $(this).html('<input type="text" value="' + $(this).text() + '"/>');
                    $(this).find('input').focus();
                    $(this).find('input').blur(function() {
dict = {};
column = $(this).closest("td").attr('class');
row = $(this).closest("tr").attr('id');
if ( column == 'section' ) {
key = '[' + row + ']';
}
else {
key = '[' + row + ']' + column;
}
dict[key] = $(this).val();
$.post("${request.path_info}", dict);
                            $(this).parent().html($(this).val());
                        });
                }
            });
$(":button").click(function() {
var headers = $(this).closest("table").find("th").map(function() {
return $(this).attr('class');
});
var row_html = '<tr><td class="section"><input type="text" /></td>';
for ( var i=1; i != headers.length; i++ ) {
  row_html += '<td class="';
  row_html += headers[i];
  row_html += '"></td>';
}
row_html += '</tr>';
$(this).closest("tr").before(row_html);
});
    });
  </script>
</head>
<body>

  <table py:with="columns=sorted(set(sum([section.keys() for section in sections.values()], [])))">
    
    <tr>
      <th class="section"></th>
      <th class="${column}" py:for="column in columns">${column}</th>
    </tr>
    
    <tr py:for="section in sections" id="${section}">
      <td class="section">${section}</td>
      <td py:for="column in columns" class="${column}">${sections[section].get(column)}</td>
    </tr>
    
    <tr>
      <td><input type="button" id="add_section" value="+"/></td>
    </tr>
  </table>

</body>
</html>
