{% extends "ldt/ldt_base.html" %}
{% load i18n %}
{% load static %}
{% block js_import %}
{{block.super}}
    <script type="text/javascript" src="{% url 'ldt.jsi18n' packages='ldt' %}" ></script>
    <script type="text/javascript" src='{% static "ldt/js/projectscontents.js" %}'></script>
    <script type="text/javascript" src='{% static "ldt/js/jquery.history.js" %}'></script>
{% endblock %}


{% block js_declaration %}
{{block.super}}
<script type="text/javascript">

var content_filter_url = "{% url 'ldt.ldt_utils.views.content.contents_filter' filter_c='__FILTER__' %}";
{% if request.user.is_superuser %}
var project_filter_url = "{% url 'ldt.ldt_utils.views.project.projects_filter' filter='__FILTER__' is_owner='false' status='0' %}";
{% else %}
var project_filter_url = "{% url 'ldt.ldt_utils.views.project.projects_filter' filter='__FILTER__' is_owner='true' status='0' %}";
{% endif %}
var publish_project_url = "{% url 'ldt.ldt_utils.views.project.publish' id='__PROJECT_ID__' %}";
var unpublish_project_url = "{% url 'ldt.ldt_utils.views.project.unpublish' id='__PROJECT_ID__' %}";

function init_events(base_node) {
	init_events_all(base_node, "{% url 'embed_config_v4' %}", content_filter_url, project_filter_url, publish_project_url, unpublish_project_url);
}

var global_csrf_token = "{{ csrf_token }}";

//Functions to manage history with parameters
function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}
function update_history(){
	s = "";
    // Content filter
    s += ($("#searchcontentsinput").val()===undefined || $("#searchcontentsinput").val()=="") ? "" : ("cf=" + $("#searchcontentsinput").val());
    // Content page
    s += ((s=="") ? "" : "&") + "cp=" + parseInt($("#current_content_page").val());
    // Content tag
    s += ((s=="" || (s.charAt(s.length-1)=="&")) ? "" : "&") + (($("#current_content_tag").val()===undefined || $("#current_content_tag").val()=="") ? "" : ("ct=" + $("#current_content_tag").val()));
    // Project filter
    s += ((s=="" || (s.charAt(s.length-1)=="&")) ? "" : "&") + (($("#searchprojectsinput").val()===undefined || $("#searchprojectsinput").val()=="") ? "" : ("pf=" + $("#searchprojectsinput").val()));
    // Project page
    s += ((s=="" || (s.charAt(s.length-1)=="&")) ? "" : "&") + "pp=" + parseInt($("#current_project_page").val());
    History.replaceState(null, null, "?" + s);
}

$(document).ready(function(){

	input_list_init = [
		{'input_selector':"#searchcontentsinput", 'container_selector':"#contentslistcontainer", 'url':content_filter_url},
		{'input_selector':"#searchprojectsinput", 'container_selector':"#projectslistcontainer", 'url':project_filter_url}
	];

	searchFieldInit(input_list_init);

	init_events(document);

	// If some filters parameters were set, we run the filter on contents and projects
	cf = getURLParameter("cf");
	ct = getURLParameter("ct");
	cp = parseInt(getURLParameter("cp"));
	if(cf=="null" || cf===null || cf===undefined){
		cf = "";
	}
    if(ct=="null" || ct===null || ct===undefined){
        ct = "";
    }
	if(cf!="" || ct!="" || cp>0){
		// Content filter with current page cp and current tag ct
		$("#searchcontentsinput").val(cf);
		searchCallback($("#searchcontentsinput"), "#contentslistcontainer", content_filter_url, 0, cp, ct);
	}
    pf = getURLParameter("pf");
    pp = parseInt(getURLParameter("pp"));
    if(pf=="null" || pf==null || pf=="undefined" || pf==undefined){
        pf = "";
    }
    if(pf!="" || pp>0){
    	// Project filter with current page pp
        $("#searchprojectsinput").val(pf);
        searchCallback($('#searchprojectsinput'), "#projectslistcontainer", project_filter_url, 0, pp);
    }

});
</script>

{% endblock %}

{% block css_import %}
{{block.super}}
    <link rel="stylesheet" href='{% static "ldt/css/workspace.css" %}' type="text/css"/>
{% endblock %}

{% block content %}
<div class="span-24 last" id="allcontentsdiv">
	<div class="span-12" id="contentsdiv">
	    <div class="span-12 last titlediv" >
			<div class="span-8 projectscontentstitle">{% trans "content list" %}
			<a class='content_link_create' href="{% url 'ldt.ldt_utils.views.content.write_content' %}" ><img class='icon_title' src='{% static "ldt/img/film_add.png" %}' alt="{% trans 'Create content' %}" title="{% trans 'Create content' %}" /></a></div>
			<div class="span-4 last searchfielddiv" >
			    <div class="searchfield rounded">
			    	<input id="searchcontentsinput" class="searchfieldinput searchfieldinputbase" placeholder="{% trans 'search' %}" type="text" />
			    	<img id="contentsajaxloader" class="searchajaxloader" src='{% static "ldt//img/ajax-loader-16x16.gif" %}' alt="loader"/>
			   		<img id="contentsclear" class="searchclear" src='{% static "ldt/img/clear-left.png" %}' />
		    	</div>
			</div>
		</div>
		<div class="span-12 last projectscontentsdiv" id="contentslistcontainer">
		{% include "ldt/ldt_utils/partial/contentslist.html" %}
		</div>
	</div>
	<div class="span-12 last" id="projectsdiv">
		<div class="span-12 last titlediv" >
			<div class="span-8 projectscontentstitle" >{% trans "My projects" %}
			<a class='ldt_link_create_project' href="{% url 'ldt.ldt_utils.views.project.create_ldt_view' %}" ><img class='icon_title' src='{% static "ldt/img/page_add_large.png" %}' alt="{% trans 'Create project' %}" title="{% trans 'Create project' %}" /></a></div>
			<div class="span-4 last searchfielddiv" >
			    <div class="searchfield rounded"><input id="searchprojectsinput" class="searchfieldinput searchfieldinputbase" placeholder="{% trans 'search' %}" type="text" /><img id="projectsajaxloader" class="searchajaxloader" src='{% static "ldt//img/ajax-loader-16x16.gif" %}' alt="loader"/><img id="projecsclear" class="searchclear" src='{% static "ldt/img/clear-left.png" %}'/></div>
			</div>
		</div>
		<div class="span-12 last projectscontentsdiv" id="projectslistcontainer">
		{% include "ldt/ldt_utils/partial/projectslist.html" %}
		</div>
	</div>
</div>
{% endblock %}
