{% extends "ldt/ldt_raw_base.html" %}

{% load i18n %} {# form of creation of content #}
{% load adminmedia %}

{% block js_import %}
	{{ block.super }}
    <script type="text/javascript" src="{{LDT_MEDIA_PREFIX}}/js/jquery.nyroModal.min.js"></script>    
	{{ content_form.media.js }}
{% endblock %}

{% block css_import %}
	{{ block.super }}
	{{ content_form.media.css }}
	<link rel="stylesheet" href="{{LDT_MEDIA_PREFIX}}css/ldt.css" />
	<link rel="stylesheet" href="{{LDT_MEDIA_PREFIX}}css/ldtform.css" />
	<link rel="stylesheet" href="{{LDT_MEDIA_PREFIX}}css/workspace.css" />
	<style type="text/css" >
		textarea {
			padding: 0px;
			width:390px;
			height: 100px;
		}
		
		#lefttable {
            float: left;
        }
        
        .choice {
            font-weight: bold;
        }
        
        .perm_read, .perm_write {
        	text-decoration: none;
        	color: black;
        }
        
        .permission {
            cursor: pointer;
        }
        
        .permcol {
            width: 110px;
        }
        	
	</style>
{% endblock %}

{% block js_declaration %}
	{{ block.super }}
	<script type="text/javascript">
	function check_uncheck_all(name) {
		var check_all = '{% trans "check all" %}';
		var uncheck_all = '{% trans "uncheck all" %}';
		var id_name = "#global_checkbox_" + name;
		
		$(id_name).attr('title', uncheck_all);
		
		$(id_name).change(function () {
			var checkbox_names = ".checkbox_" + name;
			if ($(id_name).is(":checked")) {
				$(checkbox_names).prop('checked', true);
				$(id_name).attr('title', uncheck_all );
			} else {
				$(checkbox_names).prop('checked', false);
				$(id_name).attr('title', check_all);
			}
			$(checkbox_names).trigger("change");
		});		
	}
	
	function check_initial_groups() {
		$(".checkbox_group").each(function (elem) {
			if ( $(".choice", $(this).closest('tr')).length ) {
				$(this).prop('checked', true);				
			}		
		});			
	}
	
	$(document).ready(function() {	
		
		$("#close_button").click(function (e) {
			e.preventDefault();
			parent.$.nmTop().close();
		});
		
		check_uncheck_all("content");
		check_uncheck_all("group");
		check_initial_groups();		

        $(".permission").live("click", function () {
            var group_name = $(this).attr('value');
        	var group_id = group_name.split('_').pop();
            
            $("a[value=\"" + group_name + "\"]").removeClass('choice');
            $(this).addClass('choice');
            
            if ($(this).hasClass('perm_read')) {
            	var perm = 'read';
            } else {
            	var perm = 'write';            	
            }
            
            $(".perm_field",$(this).parent().parent()).attr('value', perm);
        });
        
        $(".checkbox_group").bind("change", function() {
            var line = $(this).closest('td').next().next();
            
            if (!$(this).is(":checked")) {
                $(".choice", line).removeClass('choice');
                $(".perm_read, .perm_write", line).removeClass('permission');
            } else {
                $(".perm_read", line).addClass('choice');
                $(".perm_read, .perm_write", line).addClass('permission');
            }
        });
				
	});
	</script>
	<script type="text/javascript" src="{{LDT_MEDIA_PREFIX}}js/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript">
    tinyMCE.init({
        plugins : '-example', 
        mode : "textareas",
        theme : "advanced",
        language : "{{ LANGUAGE_CODE }}",
        theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,undo,redo,link,unlink",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        width: "470",
        plugins : "autoresize"        
	});


    </script>

{% endblock %}

{% block body %}
	<div id="add_contribution" class="span-23 last">
	
	<div class="projectscontentstitle span-23 last">{% if ldt_id %}{% trans "Update your project" %}{% else %}{% trans "Create your project" %}{% endif %}</div>
	<form action="{{create_project_action}}" method="POST" {% if target_parent %}target="_parent"{% endif %}>
	{% csrf_token %} 
	<input type="hidden" name="form_status" value="{{form_status}}" id="project_form_status" />
	<label for="title">{% trans "Title" %}:</label>
	{{form.title}}
	<label for="description" class="projectdesc">{% trans "Description :" %}</label>
	{{form.description}}
	
	<div id="lefttable" class="span-11">
	<label>{% trans "List of contents" %}</label>

		<div class="span-11 last" id="ldtcreatecontentslistcontainer">
			<div class="span-11 last projectscontentstablediv" id="ldtcreatecontentstablediv">
				<table class="projectscontentstable">
				
					<tr class="projectscontentsheader last" id="contentslistheader">
						<td class="cellcheckbox">
						{% if not ldt_id %}
							{% if contents|length > 1 %}
							<input class="selectallcontents" id="global_checkbox_content" type="checkbox" checked="true" />	 
							{% endif %}
						{% endif %}	
						</td>				
						<td class="projectcontentsheadertitle">{% trans "name" %}</td>
					</tr>
				
				    <tbody class="projectscontentsbody">
				{% for content in contents %}
					<tr class="imageline {% cycle 'projectscontentsoddline' 'projectscontentsevenline'%}">
					    <td class="cellcheckbox"><input type="checkbox" class="checkbox_content" name="contents" value="{{ content.id }}" checked="true" {% if ldt_id %}disabled="disabled"{% endif %} /></td>
					    <td class="contenttitle">{{ content.title }}</td>
					</tr>
				{% endfor %}
					</tbody>
				</table>			
			</div>
		</div>
	</div>	
     
     {{ form.errors }}
     {{group_form.errors}}
   <div id="righttable" class="span-11">
	    <label>{% trans "group list"%}</label>
	
        <div class="span-10 last" id="ldtcreatecontentslistcontainer">
            <div class="span-10 last projectscontentstablediv" id="ldtcreatecontentstablediv">
                <table class="projectscontentstable">
                    {{ management_form }}
                
                    <tr class="projectscontentsheader last" id="contentslistheader">
                        <td class="cellcheckbox">
                        {% if groups|length > 1 %}
                        	<input class="selectallgroups" id="global_checkbox_group" type="checkbox" checked="true"/>
                        {% endif %}                            
                        </td>				
                        <td class="projectcontentsheadertitle">{% trans "nom" %}</td>
                        <td class="projectcontentsheadertitle permcol" >{% trans "Permissions" %}</td>
                    </tr>
                
                    <tbody class="projectscontentsbody">
                
				    {% for form, group in group_form %}			    			    
				    <tr class="imageline projectscontentsoddline">
				    	<td class="cellcheckbox">{{ form.share }}</td>
				    	<td class="projecttitle">{{ group.name }}</td>
				    	<td>
				    	<a value="group_{{group.id}}" class="perm_read permission {% if group.member and not group.change%}choice{% endif %}" title="{% trans "This group can read the project" %}">{% trans "perm.read" %}</a>
				        <a value="group_{{group.id}}" class="perm_write permission {% if group.member and group.change %}choice{% endif %}" title="{% trans "This group can change the project" %}">{% trans "perm.write" %}</a>
				       	</td>
				       	{{ form.perms }}
				       	{{ form.group }}
				    </tr>
				    {% endfor %} 
				                
                    </tbody>
                </table>		
            </div>		
        </div>       
    </div> 
               
               
    <div id="submitcontent-buttons" class="span-11 last">
		<button type="button" id="close_button"  value="close">{% trans 'close_cancel' %}</button>
		{% if ldt_id %}
		<button class="button" id="ldt_submit" type="submit" value="prepare_delete" name="submit_button">{% trans "delete_project" %}</button>
		<button class="button" id="ldt_submit" type="submit" value="update" name="submit_button">{% trans "update_project" %}</button>
		{% else %}
		<button class="button" id="ldt_submit" type="submit" value="create" name="submit_button">{% trans "create_project" %}</button>
		{% endif %}
    </div>
    
    </form> 
    </div>
          
    </div>    
		
{% endblock %}

