{% 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 groups_filter_url = "{% url 'ldt.ldt_utils.views.group.groups_filter' filter='__FILTER__' %}";
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='0' id_group='__ID_GROUP__' %}";
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__' %}";
var get_group_projects_url = "{% url 'ldt.ldt_utils.views.group.get_group_projects_view' %}";

function init_events(base_node) {
	init_events_projects(base_node, "{% url 'ldt.ldt_utils.views.workspace.popup_embed' %}", 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(){
    // Group id
    s = "g=" + $("#id_group").val();
    // Project filter
    s += ($("#searchprojectsinput").val()===undefined || $("#searchprojectsinput").val()=="") ? "" : ("&pf=" + $("#searchprojectsinput").val());
    History.replaceState(null, null, "?" + s);
}

$(document).ready(function(){
	
	input_list_init = [
		{'input_selector':"#searchprojectsinput", 'container_selector':"#projectslistcontainer", 'url':project_filter_url},
		{'input_selector':"#searchgroupsinput", 'container_selector':"#groupslistcontainer", 'url':groups_filter_url}
	];
	
		
	searchFieldInit(input_list_init);
	
	init_events(document);
	
    $(".update_group_projects").live("click", function(){
        //alert("group id = " + $(this).attr('id') + ", url = " + get_group_projects_url);
        var id_group = $(this).attr('id');
        // Remove icons from all the lines
        $(".next_icon").hide();
        // Display icons from the good line
        $(".next_icon, .grouplink",this).show();
        // Show the search textfield and update the id_group in the hidden input
        $('#search_div').show();
        $('#id_group').val(id_group);
        $('#searchprojectsinput').val("");
        update_history();
        $(".searchclear",$('#search_div')).hide();
    	// Send the request to update the projects list
        $.ajax({
            url: get_group_projects_url,
            type: 'POST',
            data: {csrfmiddlewaretoken:global_csrf_token,
                   id_group:id_group
                   },
            success: function(msg, textStatus, XMLHttpRequest) {
                $('#projectslistcontainer').html(msg);
                init_events(document);
            },
            error: function (jqXHR, textStatus, errorThrown) {
            	alert(jqXHR.responseText());            	
            }
        });
    });
    
    var leave_msg = '{% trans "Do you want to leave this group ?" %}';
    
    $(".leave_group").click(function (e) {
		e.preventDefault();
		var group_name = $(this).html();
		var leave = confirm(leave_msg);
		if (leave) {
			window.location = $(this).attr('href');
		}
    });
    
    // If some filters parameters were set, we run the filter on contents and projects
    g = getURLParameter("g");
    pf = getURLParameter("pf");
    if(pf=="null" || pf==null || pf=="undefined" || pf==undefined){
        pf = "";
    }
    if(pf!="" || g>=0){
        // Project filter with current group g
        $("#searchprojectsinput").val(pf);
        $('#id_group').val(g);
        searchCallback($('#searchprojectsinput'), "#projectslistcontainer", project_filter_url, 0, 0);
    }
    
});
</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 "My groups" %}
			{% if can_add_group %}
				<a class="create_group" href="{% url 'ldt.ldt_utils.views.group.create_group' %}"><img class='icon_title' src='{% static "ldt/img/group_add.png" %}' alt="{% trans 'Create group' %}" title="{% trans 'Create group' %}" /></a>
			{% endif %}
			</div>
			<div class="span-4 last searchfielddiv" >
				
				<div class="span-4 last searchfielddiv" >
			    	<div class="searchfield rounded"><input id="searchgroupsinput" 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>
		<div class="span-12 last projectscontentsdiv" id="groupslistcontainer">
			{% include "ldt/ldt_utils/partial/groupslist.html" %}
		</div>
	</div>
	<div class="span-12 last" id="projectsdiv">
		<div class="span-12 last titlediv" >
			<div class="span-8 projectscontentstitle" >{% trans "The group's projects" %}</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='{% absstatic "ldt/img/ajax-loader-16x16.gif" %}' alt="loader"/><img id="projecsclear" class="searchclear" src='{% absstatic "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 %}

