<%! from django.utils.translation import ugettext as _ %>
<%! from django.template.defaultfilters import escapejs %>
<%! from microsite_configuration import page_title_breadcrumbs %>
<%inherit file="/main.html" />
<%namespace name='static' file='/static_content.html'/>
<%def name="course_name()">
 <% return _("{course_number} Courseware").format(course_number=course.display_number_with_default) %>
</%def>

<%block name="bodyclass">courseware ${course.css_class or ''}</%block>
<%block name="title"><title>
    % if section_title:
${page_title_breadcrumbs(section_title, course_name())}
    % else:
${page_title_breadcrumbs(course_name())}
    %endif
</title></%block>

<%block name="header_extras">
% for template_name in ["image-modal"]:
<script type="text/template" id="${template_name}-tpl">
    <%static:include path="js/${template_name}.underscore" />
</script>
% endfor
</%block>


<%block name="headextra">
<%static:css group='style-course-vendor'/>
<%static:css group='style-course'/>

<%block name="nav_skip">${"#seq_content" if section_title else "#course-content"}</%block>

<%include file="../discussion/_js_head_dependencies.html" />

  % if show_chat:
    <link rel="stylesheet" href="${static.url('css/vendor/ui-lightness/jquery-ui-1.8.22.custom.css')}" />
    ## It'd be better to have this in a place like lms/css/vendor/candy,
    ## but the candy_res/ folder contains images and other junk, and it
    ## all needs to stay together for the Candy.js plugin to work.
    <link rel="stylesheet" href="${static.url('candy_res/candy_full.css')}" />
  % endif
  ${fragment.head_html()}
</%block>

<%block name="js_extra">
  <script type="text/javascript" src="${static.url('js/vendor/jquery.scrollTo-1.4.2-min.js')}"></script>
  <script type="text/javascript" src="${static.url('js/vendor/flot/jquery.flot.js')}"></script>

  ## codemirror
  <script type="text/javascript" src="${static.url('js/vendor/codemirror-compressed.js')}"></script>

  <%static:js group='courseware'/>
  <%static:js group='discussion'/>

  <%include file="../discussion/_js_body_dependencies.html" />
  % if staff_access:
  	<%include file="xqa_interface.html"/>
  % endif

  <script type="text/javascript">
    var $$course_id = "${course.id | escapejs}";

    $(function(){
        $(".ui-accordion-header a, .ui-accordion-content .subtitle").each(function() {
          var elemText = $(this).text().replace(/^\s+|\s+$/g,''); // Strip leading and trailing whitespace
          var wordArray = elemText.split(" ");
          var finalTitle = "";
          if (wordArray.length > 0) {
            for (i=0;i<=wordArray.length-1;i++) {
              finalTitle += wordArray[i];
              if (i == (wordArray.length-2)) {
                finalTitle += "&nbsp;";
              } else if (i == (wordArray.length-1)) {
                // Do nothing
              } else {
                finalTitle += " ";
              }
            }
          }
          $(this).html(finalTitle);
        });
      });
  </script>

% if timer_expiration_duration:
  <script type="text/javascript">
    var timer = {
      timer_inst : null,
      end_time : null,
      get_remaining_secs : function(endTime) {
        var currentTime = new Date();
        var remaining_secs = Math.floor((endTime - currentTime)/1000);
        return remaining_secs;
      },
      get_time_string : function() {
        function pretty_time_string(num) {
          return ( num < 10 ? "0" : "" ) + num;
        }
        // count down in terms of hours, minutes, and seconds:
        var hours = pretty_time_string(Math.floor(remaining_secs / 3600));
        remaining_secs = remaining_secs % 3600;
        var minutes = pretty_time_string(Math.floor(remaining_secs / 60));
        remaining_secs = remaining_secs % 60;
        var seconds = pretty_time_string(Math.floor(remaining_secs));

        var remainingTimeString = hours + ":" + minutes + ":" + seconds;
        return remainingTimeString;
      },
      update_time : function(self) {
        remaining_secs = self.get_remaining_secs(self.end_time);
        if (remaining_secs <= 0) {
          self.end(self);
        }
        $('#exam_timer').text(self.get_time_string(remaining_secs));
      },
      start : function() { var that = this;
        // set the end time when the template is rendered.
        // This value should be UTC time as number of milliseconds since epoch.
        this.end_time = new Date((new Date()).getTime() + ${timer_expiration_duration});
        this.timer_inst = setInterval(function(){ that.update_time(that); }, 1000);
      },
      end : function(self) {
        clearInterval(self.timer_inst);
        // redirect to specified URL:
        window.location = "${time_expired_redirect_url}";
      }
    }
    // start timer right away:
    timer.start();
  </script>
% endif

% if show_chat:
  <script type="text/javascript" src="${static.url('js/vendor/candy_libs/libs.min.js')}"></script>
  <script type="text/javascript" src="${static.url('js/vendor/candy.min.js')}"></script>

  <script type="text/javascript">
    // initialize the Candy.js plugin
    $(document).ready(function() {
      Candy.init("http://${chat['domain']}:5280/http-bind/", {
        core: { debug: true, autojoin: ["${chat['room']}@conference.${chat['domain']}"] },
        view: { resources: "${static.url('candy_res/')}"}
      });
      Candy.Core.connect("${chat['username']}", "${chat['password']}");

      // show/hide the chat widget
      $('#chat-toggle').click(function() {
        var toggle = $(this);
        if (toggle.hasClass('closed')) {
          $('#chat-block').show().animate({height: '400px'}, 'slow', function() {
            $('#chat-open').hide();
            $('#chat-close').show();
          });
        } else {
          $('#chat-block').animate({height: '0px'}, 'slow', function() {
            $('#chat-open').show();
            $('#chat-close').hide();
            $(this).hide(); // do this at the very end
          });
        }
        toggle.toggleClass('closed');
      });
    });
  </script>
% endif

${fragment.foot_html()}

</%block>

% if timer_expiration_duration:
<div class="timer-main">
  <div id="timer_wrapper">
    % if timer_navigation_return_url:
    <a href="${timer_navigation_return_url}" class="timer_return_url">${_("Return to Exam")}</a>
    % endif
    <div class="timer_label">Time Remaining:</div> <div id="exam_timer" class="timer_value">&nbsp;</div>
  </div>
</div>
% endif

<%include file="/dashboard/_dashboard_prompt_midcourse_reverify.html" />
% if default_tab:
  <%include file="/courseware/course_navigation.html" />
% else:
  <%include file="/courseware/course_navigation.html" args="active_page='courseware'" />
% endif

<div class="container">
  <div class="course-wrapper">

% if disable_accordion is UNDEFINED or not disable_accordion:
    <div class="course-index" role="navigation">
      <header id="open_close_accordion">
        <a href="#">${_("close")}</a>
      </header>

      <div id="accordion" style="display: none">
        <nav aria-label="${_('Course Navigation')}">
          % if accordion.strip():
            ${accordion}
          % else:
            <div class="chapter">${_("No content has been added to this course")}</div>
          % endif
        </nav>
      </div>
    </div>
% endif

    <section class="course-content" id="course-content">
      ${fragment.body_html()}
    </section>
  </div>
</div>

% if show_chat:
  <div id="chat-wrapper">
    <div id="chat-toggle" class="closed">
      <span id="chat-open">Open Chat <em class="icon-chevron-up"></em></span>
      <span id="chat-close">Close Chat <em class="icon-chevron-down"></em></span>
    </div>
    <div id="chat-block">
      ## The Candy.js plugin wants to render in an element with #candy
      <div id="candy"></div>
    </div>
  </div>
% endif

% if course.show_calculator:
  <div class="calc-main">
    <a title="${_('Open Calculator')}" href="#" role="button" aria-controls="calculator_wrapper" aria-expanded="false" class="calc">${_("Open Calculator")}</a>

    <div id="calculator_wrapper">
      <form id="calculator">
        <div class="input-wrapper">
          <input type="text" id="calculator_input" title="${_('Calculator Input Field')}" tabindex="-1" />

          <div class="help-wrapper">
            <p class="sr" id="hint-instructions">${_('Use the arrow keys to navigate the tips or use the tab key to return to the calculator')}</p>

            <a id="calculator_hint" href="#" role="button" aria-haspopup="true" tabindex="-1" aria-describedby="hint-instructions">${_("Hints")}</a>

            <ul id="calculator_input_help" class="help" aria-activedescendant="hint-moreinfo" role="tooltip" aria-hidden="true">
              <li class="hint-item" id="hint-moreinfo" tabindex="-1">
                <p><span class="bold">${_("For detailed information, see <a href='http://edx-guide-for-students.readthedocs.org/en/latest/SFD_mathformatting.html'>Entering Mathematical and Scientific Expressions</a> in the <a href='http://edx-guide-for-students.readthedocs.org/en/latest/index.html'>edX Guide for Students</a>.")}</span></p>
              </li>

              <li class="hint-item" id="hint-tips" tabindex="-1"><p><span class="bold">${_("Tips")}:</span> </p>
                <ul>
                  <li class="hint-item" id="hint-paren" tabindex="-1"><p>Use parentheses () to make expressions clear. You can use parentheses inside other parentheses.</p></li>
                  <li class="hint-item" id="hint-spaces" tabindex="-1"><p>Do not use spaces in expressions.</p></li>
                  <li class="hint-item" id="hint-howto-constants" tabindex="-1"><p>For constants, indicate multiplication explicitly (example: 5*c).</p></li>
                  <li class="hint-item" id="hint-howto-maffixes" tabindex="-1"><p>For affixes, type the number and affix without a space (example: 5c).</p></li>
                  <li class="hint-item" id="hint-howto-functions" tabindex="-1"><p>For functions, type the name of the function, then the expression in parentheses.</p></li>
                </ul>
              </li>

              <li class="hint-item" id="hint-list" tabindex="-1">
                <table class="calculator-input-help-table">
                  <tbody>
                    <tr>
                      <th scope="col">${_("To Use")}</th>
                      <th scope="col">${_("Type")}</th>
                      <th scope="col">${_("Examples")}</th>
                    </tr>
                    <tr>
                      <th scope="row">${_("Numbers")}</th>
                      <td>Integers<br />
                        Fractions<br />
                        Decimals
                      </td>
                      <td>2520<br />
                        2/3<br />
                        3.14, .98
                      </td>
                    </tr>
                    <tr>
                      <th scope="row">${_("Operators")}</th>
                      <td>+ - * / (add, subtract, multiply, divide)<br />
                        ^ (raise to a power)<br />
                        _ (add a subscript)<br />
                        || (parallel resistors)
                      </td>
                      <td>x+(2*y)/x-1
                        x^(n+1)<br />
                        v_IN+v_OUT<br />
                        1||2
                      </td>
                    </tr>
                    <tr>
                      <th scope="row">${_("Greek letters")}</th>
                      <td>Name of letter</td>
                      <td>alpha<br />
                        lambda
                      </td>
                    </tr>
                    <tr>
                      <th scope="row">${_("Constants")}</th>
                      <td>c, e, g, i, j, k, pi, q, T</td>
                      <td>20*c<br />
                        418*T
                      </td>
                    </tr>
                    <tr>
                      <th scope="row">${_("Affixes")}</th>
                      <td>Percent sign (%) and metric affixes (d, c, m, u, n, p, k, M, G, T)</td>
                      <td>20%<br />
                        20c<br />
                        418T
                      </td>
                    </tr>
                    <tr>
                      <th scope="row">${_("Basic functions")}</th>
                      <td>abs, exp, fact or factorial, ln, log2, log10, sqrt</td>
                      <td>abs(x+y)<br />
                        sqrt(x^2-y)
                      </td>
                    </tr>
                    <tr>
                      <th scope="row">${_("Trigonometric functions")}</th>
                      <td>sin, cos, tan, sec, csc, cot<br />
                        arcsin, sinh, arcsinh, etc.<br />
                      </td>
                      <td>sin(4x+y)<br />
                        arccsch(4x+y)
                      </td>
                      <td></td>
                    </tr>
                    <tr>
                      <th scope="row">${_("Scientific notation")}</th>
                      <td>10^ and the exponent</td>
                      <td>10^-9</td>
                    </tr>
                    <tr>
                      <th scope="row">${_("e notation")}</th>
                      <td>1e and the exponent</td>
                      <td>1e-9</td>
                    </tr>
                  </tbody>
                </table>
              </li>
            </ul>
          </div>
        </div>

        <input id="calculator_button" type="submit" title="${_('Calculate')}" value="=" aria-label="${_('Calculate')}" value="=" tabindex="-1" />
        <input type="text" id="calculator_output" title="${_('Calculator Output Field')}" readonly tabindex="-1" />
      </form>
    </div>
  </div>

% endif
<%include file="../modal/accessible_confirm.html" />
