{% extends "ldt/ldt_base.html" %}
{% load i18n %}

{% block js_import %}
{{block.super}}
    <script type="text/javascript" src="{% url ldt.jsi18n packages='ldt' %}" ></script>
	<script type="text/javascript" src="{{LDT_MEDIA_PREFIX}}js/projectscontents.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 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 '__PROJECT_ID__' %}";
var unpublish_project_url = "{% url ldt.ldt_utils.views.project.unpublish '__PROJECT_ID__' %}";
var get_group_projects_url = "{% url ldt.ldt_utils.views.group.get_group_projects %}";

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)
	init_events_groups(base_node, "{% url ldt.ldt_utils.views.workspace.popup_embed %}", groups_filter_url);
}
	
var global_csrf_token = "{{ csrf_token }}";

$(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("");
        $(".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');
		}
    	
    });
    
});
</script>
{% endblock %}

{% block css_import %}
{{block.super}}
    <link rel="stylesheet" href="{{LDT_MEDIA_PREFIX}}/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="{{LDT_MEDIA_PREFIX}}img/film_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="{{LDT_MEDIA_PREFIX}}/img/ajax-loader-16x16.gif" alt="loader"/><img id="projecsclear" class="searchclear" src="{{LDT_MEDIA_PREFIX}}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="{{LDT_MEDIA_PREFIX}}/img/ajax-loader-16x16.gif" alt="loader"/><img id="projecsclear" class="searchclear" src="{{LDT_MEDIA_PREFIX}}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 %}

