## The contents of this file are subject to the Common Public Attribution
## License Version 1.0. (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License at
## http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
## License Version 1.1, but Sections 14 and 15 have been added to cover use of
## software over a computer network and provide for limited attribution for the
## Original Developer. In addition, Exhibit A has been modified to be
## consistent with Exhibit B.
##
## Software distributed under the License is distributed on an "AS IS" basis,
## WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
## the specific language governing rights and limitations under the License.
##
## The Original Code is reddit.
##
## The Original Developer is the Initial Developer.  The Initial Developer of
## the Original Code is reddit Inc.
##
## All portions of the code written by reddit are Copyright (c) 2006-2012
## reddit Inc. All Rights Reserved.
###############################################################################

<%!
   from r2.lib.template_helpers import get_domain, static, style_line, choose_width
   from r2.lib.utils import query_string
   from r2.lib.strings import Score
 %>

<%namespace file="printable.html" import="arrow, score" />
<%namespace file="utils.html" import="img_link" />

<%def name="submiturl(url, title='')">${("http://%s/submit" % get_domain(cname = c.cname, subreddit = not c.cname)) + query_string(dict(url=url, title=title))}</%def>

<%def name="submitlink(url, title='', text='submit')">
  <a href='${submiturl(url, title)}' target='${thing.target}'>${text}</a>
</%def>

<%def name="class_def(class_number, width=None)">
  <% 
     like_cls = "unvoted"
     if getattr(thing, "likes", None):
         like_cls = "likes"
     elif getattr(thing, "likes", None) is False:
         like_cls = "dislikes"
   %>
  <div class="blog blog${class_number} entry ${like_cls}" 
    %if style_line(width):
       style="${style_line(width)}"
    %endif
    >
    ${caller.body()}
  </div>
</%def>

<%def name="button1(thing)">
<%self:class_def class_number="1" 
                 width="${choose_width(thing, thing.width)}">
  <div class="headimgcell">
    <a href="${submiturl(thing.url, thing.title)}" target="${thing.target}">
      <img src="http://www.reddit.com/static/blog_head.png" alt=""/>
    </a>
  </div>
  %if thing._fullname:
    %if thing.vote:
      ${arrow(thing, 1, thing.likes)}
      ${arrow(thing, 0, thing.likes == False)}
      ${score(thing, thing.likes, 
              scores = map(Score.safepoints, thing.voting_score), 
              tag='div')}
    %else:
      ${thing.score}
    %endif
  %else:
    ${submitlink(thing.url, thing.title)}
  %endif
  <div class="clear"></div>
</%self:class_def>
</%def>

<%def name="button2(thing)">
<%self:class_def class_number="2">
    %if thing._fullname:
      %if thing.vote:
        ${arrow(thing, 1, thing.likes)}
        ${score(thing, thing.likes, tag='div')}
        ${arrow(thing, 0, thing.likes == False)}
      %else:
        &nbsp;<br />
        ${thing.score}
        &nbsp;<br />
      %endif
    %else:
      <img src="http://www.reddit.com/static/blog_head.png" alt=""/>
      ${submitlink(thing.url, thing.title, 'submit to')}
    %endif
      %if thing.bgcolor:
      <div class="bottomreddit" style="background-color: #${thing.bgcolor}">
      %else:
      <div class="bottomreddit">
      %endif
      <a href="${submiturl(thing.url, thing.title)}" target="${thing.target}">reddit</a>
    </div>
</%self:class_def>
</%def>


<%def name="button3(thing)">
<%self:class_def class_number="3">
  <div class="left">
    %if thing._fullname:
      %if thing.vote:
      ${arrow(thing, 1, thing.likes)}
      ${score(thing, thing.likes, tag='div')}
      ${arrow(thing, 0, thing.likes == False)}
      %else:
      &nbsp;<br />
      ${thing.score}
      &nbsp;<br />
      %endif
    %else:
      ${submitlink(thing.url, thing.title,  unsafe('<div class="arrow upmod"></div>'))}
      ${submitlink(thing.url, thing.title, '?')}
      ${submitlink(thing.url, thing.title,  unsafe('<div class="arrow downmod"></div>'))}
    %endif
  </div>
  <div class="right">
    ${img_link('submit', '/static/blog_snoo.gif', capture(submiturl, thing.url, thing.title), target=thing.target)}
  </div>
  <div class="clear"></div>
</%self:class_def>
</%def>

<%def name="button4(thing)">
<%self:class_def class_number="2"> 
    %if thing._fullname:
      %if thing.vote:
      ${arrow(thing, 1, thing.likes)}
      ${score(thing, thing.likes, tag='div')}
      ${arrow(thing, 0, thing.likes == False)}
      %else:
      &nbsp;<br />
      ${thing.score}
      &nbsp;<br />
      %endif
    %else:
      <img src="http://www.reddit.com/static/blog_head.png" alt=""/>
      ${submitlink(thing.url, thing.title, 'submit to')}
    %endif
</%self:class_def>
</%def>

<%def name="button5(thing)">
<%self:class_def class_number="5" 
                 width="${choose_width(thing, thing.width)}">
  ${img_link('submit', '/static/blog_snoo.gif', capture(submiturl, thing.url, thing.title), _class="left")}
<%
     # this button style submits to the cname as a default
     c.cname = hasattr(c.site, "domain") and bool(c.site.domain)
     submitlink = capture(submiturl, thing.url, thing.title)
     if thing._fullname:
        fullname = thing._fullname
        ups = thing.upvotes
        downs = thing.downvotes
        link = "http://%s%s" % (get_domain(cname = c.cname, subreddit = False),
                                thing.make_permalink_slow())
     else:
        fullname = ""
        ups = 0
        downs = 0
        link = submitlink
     if c.user_is_loggedin and thing._fullname:
        dir = 1 if thing.likes else 0 if thing.likes is None else -1
        ups   = ups - (1 if dir > 0 else 0)
        downs = downs - (1 if dir < 0 else 0)
     else:
        dir = 0

     other_votes = float(ups + downs)
     ups   = [ups, ups, ups + 1]
     downs = [downs + 1, downs, downs]
     votes = [other_votes + 1, other_votes, other_votes + 1]
     up_pct = [int( 100 * ups[i] / votes[i]) if votes[i] else "--" for i in range(len(votes))]
     down_pct = [int( 100 * downs[i] / votes[i]) if votes[i] else "--" for i in range(len(votes))]
     def controversy_text(_up_pct):
        if isinstance(_up_pct, str):
           return ""
        p = abs(50 - _up_pct)
        msg = "<b class='controversy%d'>%s</b>"                                    
        if p <= 5:
           return msg % (3, _("quite controversial"))
        elif p <= 15:
           return msg % (2, _("controversial"))
        elif p <= 25:
           return msg % (1, _("uncontroversial"))
        else:
           return msg % (0, _("quite uncontroversial"))
     controversy = map(controversy_text, up_pct)
   %>
  <script type="text/javascript">
    function set_score(id, dir) {
      id = $(id).filter(":first").thing_id();
      for(var i = 0; i < 3; i++) {
         var fn =  (i == dir + 1) ? 'show': 'hide';
         $("#down" + i + "_" + id)[fn]();
         $("#up" + i + "_" + id)[fn]();
         $("#controversy" + i + "_" + id)[fn]();
      }
    }
  </script>
  <div class="container">
    <ul>
      <li class="selected">
        ${_("Vote on this article")}
      </li>
      <li>
        <a href="${link}" target="_new">
          %if thing._fullname:
            ${_("Discuss at the %(name)s reddit") % dict(name = c.site.name)}
          %else:
            ${_("Submit to the %(name)s reddit") % dict(name = c.site.name)}
          %endif
        </a>
      </li>
    </ul>
    <div class="votes ${'' if thing._fullname else 'disabled'}">
      %for i in range(3):
        <div class="right" id="controversy${i}_${fullname}"
           ${"" if i == dir + 1 else "style='display:none'"}>
          ${unsafe(controversy[i])}
        </div>
      %endfor
      <a class="arrow up${'mod' if dir==1 else ''}" 
         id="up_${fullname}"
         %if c.user_is_loggedin and thing._fullname:
            href="#" onclick="$(this).vote('', set_score, event); return false;"
         %else:
           href="${submitlink}" target="_new"
         %endif
      >
        %for i in range(3):
          <span id="up${i}_${fullname}"
                ${"" if i == dir + 1 else "style='display:none'"}
                ><b>${up_pct[i]}%</b>&#32;liked it (${ups[i]})</span>
        %endfor
      </a>
      <a class="arrow down${'mod' if dir==-1 else ''}" id="down_${fullname}"
         %if c.user_is_loggedin and thing._fullname:
           href="#" onclick="$(this).vote('', set_score, event); return false;"
         %else:
           href="${submitlink}" target="_new"
         %endif
         >
        %for i in range(3):
          <span id="down${i}_${fullname}"
                ${"" if i == dir + 1 else "style='display:none'"}
                ><b>${down_pct[i]}%</b>&#32;disliked it (${downs[i]})</span>
        %endfor
      </a>
    </div>
    <div class="clearleft"><!--IEsux--></div>
  </div>
 </%self:class_def>
</%def>
