{% extends "front/front_base.html" %}
{% load i18n %}
{% load thumbnail %}
{% load front_tags %}

{% block title %}Lignes de temps : {% trans 'search' %} "{{search}}" {% endblock %}

{% block js_import %}
{{block.super}}
<script src="{{LDT_MEDIA_PREFIX}}js/jquery.min.js" type="text/javascript"/></script>
<script src="{{LDT_MEDIA_PREFIX}}js/raphael.js" type="text/javascript"/></script>
{% endblock %}

{% block css_import %}
{{block.super}}
<link rel="stylesheet" href="{{LDT_MEDIA_PREFIX}}css/front_search.css" type="text/css"/>
{% endblock %}

{% block body %}
{{block.super}}
<script type="text/javascript">
	var _width = 340,
		_height = 25,
		_colorRange = [ "#89e1ff", "#c800ff", "#85ff3b", "#ffff17", "#ff2f7e" ];
    var _medias = [ {% for res in results.object_list %}
        {
            "iri_id" : '{{ res.content.iri_id }}',
            "duration" : {{ res.content.duration }},
            "segments" : [ {% for segment in res.list %}
                {
                    "id" : '{{segment.element_id}}',
                    "begin" : {{segment.begin}},
                    "duration" : {{segment.duration}},
                },{% endfor %}
            ],
            "stats" : [{{ res.content.stat_annotation }}],
        },{% endfor %}
    ];
    $(document).ready(function () {
    	var _k = 0;
        $('.graph_annotation').each(function(_i) {
            var _values = _medias[_i].stats,
                _nb = _values.length,
                _max = _values.reduce(function(_a,_b) {
                    return Math.max(_a,_b);
                }, 1),
                _x = function(_p) {
                    return _p * _width / _nb;
                },
                _y = function(_p) {
                    return _height * ( 1 - _p / _max );
                },
                _d = _values.map(function(_v,_k) {
                    return ( _k == 0 ?
                        "M" + _x(_k) + " " + _y(_v) :
                        "C" + _x(_k-.5) + " " + _y(_values[_k - 1])
                        + " " + + _x(_k-.5) + " " + _y(_v)
                        + " " + + _x(_k) + " " + _y(_v)
                        ) ;
                }).join("") + "L" + _width + " " + _height + "L0 " + _height;
            this.innerHTML = '';
            var _paper = new Raphael(this,_width,_height);
            _paper.path(_d).attr({
                "fill" : "#adadad",
                "stroke" : "none"
            });
            var _x = function(_p) {
                return _p * 340 / _medias[_i].duration;
            };
            for (var _j = 0; _j < _medias[_i].segments.length; _j++) {
                var _s = _medias[_i].segments[_j],
                	_coul = _colorRange[_k % _colorRange.length],
                	_rect = _paper.rect(_x(_s.begin),0,Math.max(1,_x(_s.duration)),25).attr({
	                    "fill" : _coul,
	                    "opacity" : .5,
	                    "stroke" : "none",
	                });
	                _rect.node.id = "re_" + _s.id;
                $("#li_" + _s.id + " .color_zone").css({
                	"background" : _coul,
                	"opacity" : .5,
               	});
            	_k ++;
            }
        });
        $("li.li_segment, .graph_annotation rect").mouseover(function() {
            var _id = this.id.substr(3);
            $("#re_" + _id + ", #li_" + _id + " .color_zone").css({
            	"opacity" : ".9",
            });
            $("#re_" + _id).attr({
            	"opacity" : ".9",
            });
        }).mouseout(function() {
            var _id = this.id.substr(3);
            $("#re_" + _id + ", #li_" + _id + " .color_zone").css({
            	"opacity" : ".5",
            });
            $("#re_" + _id).attr({
            	"opacity" : ".5",
            });
        });
    });
</script>

{% if msg %}
{{ msg }}
{% else %}
  {% ifequal nb_results 0 %}
    {% blocktrans %} No results for <b>{{ search }}</b>.{% endblocktrans %}
  {% else %}
<!-- TABLE POUR ALIGNER À LA VERTICALE -->
    <table id="tbl_rech">
    <tr>
<!-- LISTE DES RÉSULTATS -->
        <td>
        <ul class="floatlist" id="liste_resultats">
    <!-- titre "Résultats de recherche" et options -->
            <li id="li_haut_resultats">
                <ul class="floatlist" id="ul_haut_resultats">
                    <li id="title_resultats">
                        <h2>{% trans "Search results for " %} <b> {{ search }}</b></h2>
                    </li>
                    <li id="li_nb_resultats">
                        <p id="p_nb_resultats">{{ nb_results }} {% trans "Result" %}{{ nb_results|pluralize }}</p>
                    </li>
                    <!--li id="resultats_options">options</li-->
                </ul>
            </li>
    <!-- Médias trouvés -->
    
        <!-- Média -->
        {% autoescape off %}
        {% for res in results.object_list %}
            <li class="li_result_media">
                <div class="titre_result_media">
                    <div class="h3_result_media">
                        <h3>{% if res.content_title|length > 20 %}{{res.content_title|capfirst|slice:"20"}}...{% else %}{{res.content_title}}{% endif %}</h3>
                    </div>
                    <div class="duree_result_media">
                        <p>{{res.content.duration|str_duration:"::"}}</p>
                    </div>
                    <div class="graphe_result_media">
                    	<span id="{{ res.content.iri_id }}" class="graph_annotation" title="{% trans 'annotation distribution' %}">
                    	</span>
                    </div>
                </div>
        
            <!-- Segments du média -->
                <ul class="floatlist list_segments">
                {% for segment in res.list %}
                    <li class="li_segment" id="li_{{segment.element_id}}">
                        <div class="left_segment">
                            <div class="color_zone"></div>
                            <a href="{% url ldt.ldt_utils.views.front.annot_content segment.iri_id segment.project_id segment.decoupage_id %}#id={{segment.element_id}}"><img src="{{LDT_MEDIA_PREFIX}}img/annot_icon_80x45.png" width="80" height="45" alt="" /></a>
                            <!--p class="duree_segment">{{ segment.duration|str_duration:"::" }}</p-->
                        </div>
                        <h4 class="title_segment"><a class="blue under" href="{% url ldt.ldt_utils.views.front.annot_content segment.iri_id segment.project_id segment.decoupage_id %}#id={{segment.element_id}}" title="{% trans 'view this annotation in the player' %}">
                          {% if segment.title %}{{ segment.title }}{% else %}{% trans "No title" %}{% endif %}</a></h4>
                        <p class="text_segment">{% if segment.context %}{{ segment.context }}{% endif %}<br/>{% trans "Begin" %} : {{ segment.begin|str_duration:"::" }} - {% trans "duration" %} : {{ segment.duration|str_duration:"::" }}</h4>
                    </li>
                {% endfor %}
                </ul>
            </li>
      {% endfor %}
      {% endautoescape %}
      <!-- Fin des médias -->
      <!-- Pagination -->
        <li id="result_pagination">
            <p>
            {% if results.has_previous %}
	            <a class="blue under" href="{% url ldt.ldt_utils.views.front.search_listing %}?page={{ results.previous_page_number }}" title="{% trans 'previous' %}">{% trans "previous" %}</a>
	            .
	        {% endif %}
	        {% if results.paginator.num_pages > 1 %}
	        <span class="current">
	            {% for i in results.paginator.num_pages|get_range %}
	            <span class="current">
	                {% if i|add:'1' == results.number %}
	                <span class="pink">{{i|add:'1'}}</span>
	                {% else %}
	                <a class="blue under" href="{% url ldt.ldt_utils.views.front.search_listing %}?page={{i|add:'1'}}">{{i|add:'1'}}</a>
	                {% endif %}
	                {% if i|add:'1' < results.paginator.num_pages and 1 < results.paginator.num_pages %}
	                .
                    {% endif %}
	            </span>
	            {% endfor %}
	        </span>
	        {% endif %}
	        {% if results.has_next %}
	            .
	            <a class="blue under" href="{% url ldt.ldt_utils.views.front.search_listing %}?page={{ results.next_page_number }}" title="{% trans 'next' %}">{% trans "next" %}</a>
	        {% endif %}
            </p>
        </li>
      <!-- Fin Pagination -->
      </ul>
    

<!-- Right column -->
<!-- APERÇU DU RÉSULTAT -->
    <td id="result_preview">
        <h4>ICI PREVIEW ?</h4>
        <p class="bigmargin">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem …</p>
    </td>
  </tr>
</table>

  {% endifequal %}
{% endif %}

{% endblock %}

