## 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 static, format_number
   from r2.lib import js
   import datetime
   from r2.lib import promote
   import babel.numbers

   locale = c.locale

   def num(x):
      return format_number(x, locale)
   def money(x):
      return babel.numbers.format_currency(x, 'USD', locale)
 %>

<%namespace file="reddittraffic.html" import="load_timeseries_js"/>
${load_timeseries_js()}

<h1>${_("Sponsored link calendar")}</h1>

%if not c.user_is_sponsor:
<div class="instructions">
  <p>Below is a calendar of your scheduled and completed promotions (if you have any, of course), along with some site-wide averages to use as a guide for setting up future promotions.  These values are:</p>
  <dl>
    <dt>Count:</dt>
      <dd>Total number of sponsored links which either ran or are scheduled to run (site-wide).</dd>
    <dt>CPM:</dt>
      <dd>Cost per thousand impressions (site-wide).  Note that today's CPM is estimated.</dd>
    <dt>CPC:</dt>
      <dd>Cost per click, also site-wide and with the same caveat about today's value</dd>
    <dt>your commit:</dt>
      <dd>Total amount you have spent or have scheduled to spend on a given day</dd>
  </dl>
</div>

%endif

<% max_percent = 97. %>

<div class="calendar">
  <%
     today = thing.now.date()
   %>
    <div class="grid" 
         style="left:0; right:${'%.2f'%(100 - max_percent/(thing.total_size+1))}%">
      <div class="header">
        DATE
      </div>
      <div class="header total">
        COUNT
      </div>
      <div class="header total">
        %if c.user_is_sponsor:
        IMPRESSIONS<br/>
        %endif
        CPM
      </div>
      <div class="header total">
        %if c.user_is_sponsor:
        CLICKS<br/>
        %endif
        CPC
      </div>
      <div class="header total">
        %if c.user_is_sponsor:
          TOTAL COMMIT
        %else:
          YOUR COMMIT
        %endif
      </div>
    </div>
  %for i in xrange(thing.total_size):
    <%
       left = "%.2f" % (max_percent*float(i+1)/(thing.total_size+1))
       right = "%.2f" %  (100 - max_percent*float(i+2)/(thing.total_size+1))
       day = thing.start_date + datetime.timedelta(i)
       CPC = CPM = imp_traffic = cli_traffic = "---"
       if thing.promo_traffic.has_key(day):
           imp_traffic, cli_traffic = thing.promo_traffic[day]
           if thing.market.has_key(i) and day < today:
              CPM = "$%.2f" % (thing.market[i] * 1000./max(imp_traffic, 1))
              CPC = "$%.2f" % (thing.market[i] * 1./max(cli_traffic, 1))
           imp_traffic = num(imp_traffic)
           cli_traffic = num(cli_traffic)
           if day == today:
              imp_traffic = "(%s)" % imp_traffic
              cli_traffic = "(%s)" % cli_traffic
     %>
    <div class="grid ${'today' if day == today else ''}" 
         style="left:${left}%; right:${right}%">
      <div class="header">
        ${day}
      </div>
      <div class="header total">
        ${thing.promo_counter.get(i, unsafe("&nbsp;"))}
      </div>
      <div class="header total">
        %if c.user_is_sponsor:
          ${imp_traffic}<br/>
        %endif
        ${CPM}
      </div>
      <div class="header total">
        %if c.user_is_sponsor:
          ${cli_traffic}<br/>
        %endif
        ${CPC}
      </div>
      <div class="header total">
        ${"$%.2f" % thing.my_market[i] if thing.my_market.has_key(i) else "---"}
      </div>      
    </div>
  %endfor

<% 
   prev_end = 0
 %>
%for link, bid, start, end, indx in thing.promote_blocks:
  <% 
     start += 1
     end += 1
     sr = '' 
     if indx in getattr(link, "campaigns", {}):
         sr = link.campaigns[indx][promote.CAMPAIGN.sr]
     if sr:
        sr += ':'
   %>
 %if start != end:
  %if prev_end > start:
    <% prev_end = 0 %>
    <div class="clearleft"></div>
  %endif
  <%
     margin = "%.2f" % (float(max_percent*(start-prev_end))/(thing.total_size+1))
     width  = "%.2f" % (float(max_percent*(end-start))/(thing.total_size+1))
     prev_end = end 
   %>  
  <div class="blob promotedlink ${link.rowstyle} rounded"
       title="${link.title}"  
       style="width: ${width}%; margin-left: ${margin}%">
    <div class="bid">
      ${sr}$${int(bid)}
    </div>
    <a class="title" 
       href="/promoted/edit_promo/${link._id36}"
       title="${link.title}">
      ${link.author.name}
    </a>
  </div>
 %endif
%endfor
</div>
<div class="clear"></div>

<h1>${_("historical site performance")}</h1>
<div id="charts"></div>

${thing.performance_table}

<script type="text/javascript">
    r.timeseries.init()
</script>
</div>
