{% load staticfiles %}
{% load hash %}
{% load jsonify %}

<script src="{% static 'sloval/jquery-1.11.1.min.js' %}"></script>
<script src="https://raw.githubusercontent.com/trentrichardson/jQuery-Impromptu/master/dist/jquery-impromptu.min.js"></script>
<script>

    var schemas = new Object;
    var default_order = new Object;

	{% for characteristic in characteristics %}
	    default_order[{{ characteristic }}] = []
		{% for i, status, colspan, id, categories, rowspan, arguments in schemas_display|hash:characteristic %}
		    schemas["{{ id }}"] = '<table class="InactiveFrameTable">'
		                      + '<tr><td class="ColumnHeader">Ocena schematu:</td><td class=u"schema_{{ id }}_" colspan="{{ colspan }}">'
		                      + '<span class="Opinion">{{ status }}[{{ id }}]</span>'
		                      + '<span class="CheckComp"></span></td><tr>'
		                      + '<tr>'
		                      + '<td class="ColumnHeader">Kategoria:</td>'
		    {% for id, class, category in categories %}
		                      + '<td id="{{ id }}" class="{{ class }}"  onclick="schemaClick(\'{{ class }}\')">'
		                      + '{{ category }}'
		                      + '</td>'
		    {% endfor %}
		                      + '</tr>'
		                      + '<tr>'
		                      + '<td class="ColumnHeader" rowspan="{{ rowspan }}">Argumenty:</td>'
		    {% for row in arguments %}
		        {% for id, class, argument in row %}
		                      + '<td id="{{ id }}" class="{{ class }}"  onclick="schemaClick(\'{{ class }}\')">'
		                      + '{{ argument }}'
		                      + '</td>'
		        {% endfor %}
		                      + '</tr><tr>'
		    {% endfor %}
		                      + '</tr>'
		                      + '</table>';
		                      
		    default_order[{{ characteristic }}][{{ i }} - 1] = {{ id }}	    
		{% endfor %}
	{% endfor %}
    
    var ranks = new Object;
    
    {% for schema_id in schema_unit_rank %}
        ranks["{{ schema_id }}"] = new Object;
        {% for unit_id in schema_unit_rank|hash:schema_id %}
            ranks["{{ schema_id }}"]["{{ unit_id }}"] = {{ schema_unit_rank|hash:schema_id|hash:unit_id }};
        {% endfor %}
    {% endfor %}
    
    function reorder(frame_id) {
    
        if (frame_id >= 0) {
    
            lus = lexical_units[frame_id]
            
            rank_values = new Object;
            for (var schema_id in ranks) { 
                rank_values[schema_id] = 0.0
                for (i = 0; i < lus.length; i++) {
                    rank_values[schema_id] += ranks[schema_id][lus[i]]
                }
            }
            
            ranks_to_sort = []
            for (var schema_id in rank_values) { 
                ranks_to_sort.push({ id: schema_id, value: rank_values[schema_id] })
            }
            
            ranks_to_sort.sort(value_compare);
            
            for (i = 0; i < ranks_to_sort.length; i++) {
                $("#"+(i+1)).html(schemas[ranks_to_sort[i].id])
            }
        
        } else {
        
            for (i = 0; i < default_order.length; i++) {
                $("#"+(i+1)).html(schemas[default_order[i]])
            }
        
        }
    
    }
    
    function value_compare(a, b) {
        return b.value - a.value;
    }
    
    $(document).ready(function(){
        for (var char in default_order) {
            for (i = 0; i < default_order[char].length; i++) {
                $("#"+char+"-"+(i+1)).html(schemas[default_order[char][i]])
            }
        }
    })
    
</script>

{% for characteristic in characteristics %}
	{% for c in characteristics_info|hash:characteristic %}
    	<div><strong>{{ lemma.entry }} {{ c.reflexiveness.reflexiveness }}({{ c.negativeness.negativeness }}, {{ c.predicativeness.predicativeness }}, {{ c.aspect.aspect }}):</strong></div>
    {% endfor %}
    {% for i, status, colspan, id, categories, rowspan, arguments in schemas_display|hash:characteristic %}
        <table><tr id="{{ i }}"><td id="{{ characteristic }}-{{ i }}">
        </td></tr></table>
    {% endfor %}
{% endfor %}
