{% 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__' %}";
var project_filter_url = "{% url 'ldt.ldt_utils.views.project.projects_filter' filter='__FILTER__' is_owner='false' status='2' %}";
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 = "";
    // Project filter
    s += ($("#searchprojectsinput").val()==undefined || $("#searchprojectsinput").val()=="") ? "" : ("pf=" + $("#searchprojectsinput").val());
    // Project page
    s += ((s=="") ? "" : "&") + "pp=" + $("#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
    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 last" id="searchprojectsdiv">
		<div class="span-12 last titlediv" >
			<div class="span-8 projectscontentstitle" >{% trans "Search" %}</div>
		</div>
		<div class="span-12 last projectscontentsdiv" id="autre">
		<form method="get" id="my_form"
			action="{% url 'ldt.ldt_utils.views.workspace.search_index' %}"
			accept-charset="utf-8">
		<table>
			{{ form.as_table }}
		</table>
		<input type="submit" class="search_submit" value="{% trans "Submit" %}"/></form>
		</div>
	</div>
	<div class="span-12 last" id="projectsdiv">
		<div class="span-12 last titlediv" >
			<div class="span-8 projectscontentstitle" >{% trans "Published 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/publishedprojectslist.html" %}
		</div>
	</div>
</div>
{% endblock %}
