<%! from urllib import urlencode %>

<%def name="link_to_sort(key, title)">
  % if key == sort_key:
    % if sort_order.lower() == 'desc':
      ${_link_to_sort(key, 'asc', title + ' &#x25BC;', 'sorted')}
    % else:
      ${_link_to_sort(key, 'desc', title + ' &#x25B2;', 'sorted')}
    % endif
  % else:
    ${_link_to_sort(key, 'desc', title)}
  % endif
</%def>

<%def name="_link_to_sort(key, order, title, cls='')">
  <% 
  def merge(dic1, dic2):
    return dict(dic1.items() + dic2.items())

  def url_for_sort(key, order):
    return base_url + '?' + urlencode(merge(query_params, {'page': 1, 'sort_key': key, 'sort_order': order}))
  %>
  <a class="discussion-sort-link discussion-inline-sort-link ${cls}" href="javascript:void(0)" sort-url="${url_for_sort(key, order)}">${title}</a>
</%def>
    
<div class="discussion-sort">
  Sort by:
  ${link_to_sort('date', 'date')}
  |
  ${link_to_sort('votes', 'votes')}
  |
  ${link_to_sort('comments', 'comments')}
</div>
