{% extends "ldt/ldt_utils/workspace_base.html" %}
{% load i18n %}
{% load absstatic %}
{% load absurl %}

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

{% block js_import %}
	{{ block.super }}
    <script type="text/javascript" src="{{ADMIN_MEDIA_PREFIX}}js/core.js" ></script>
    <script type="text/javascript" src="{{ADMIN_MEDIA_PREFIX}}js/jquery.init.js"></script>
    <script type="text/javascript" src='{% absstatic "ldt/js/jquery.min.js" %}'></script>
    <script type="text/javascript" src="{% url ldt.jsi18n packages='ldt' %}" ></script>	
    <script type="text/javascript" src="{% absurl ldt.jsi18n packages='ldt' %}" ></script>
    <script type="text/javascript" src='{% absstatic "ldt/js/projectscontents.js" %}'></script>
    <script type="text/javascript" src='{% absstatic "ldt/js/jquery.textchange.min.js" %}'></script>
    <script type="text/javascript" src='{% absstatic "ldt/js/jquery-ui.min.js" %}'></script>
	{{ content_form.media.js }}
{% endblock %}

{% block js_declaration %}
	<script type="text/javascript">
	window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";
	
	$(document).ready(function() {
		// url_upload, media_prefix, post_added_params, btn_label, success_label, wait_label, cancel_label, remove_url, get_dur_url
		post_added_params = {"csrfmiddlewaretoken" : "{{csrf_token}}",
                    		"{{cookie_name}}" : "{{session_key}}"};
		
	     onCreateContentReady("{% url ldt.ldt_utils.views.content.upload %}",
	    	     "{% absstatic 'ldt' %}",
	    	     post_added_params,
	    	     "{% trans 'Browse' %}",
	    	     "{% trans 'File uploaded' %}",
	    	     "{% trans 'Please wait, the upload is not finished yet' %}",
	    	     "{% trans 'Cancel upload' %",
	    	     "{% url ldt.ldt_utils.views.content.remove_temp_file %}",
	    	     "{% url ldt.ldt_utils.views.content.get_duration %}");
	});
	// the function stopRKey prevents from the validation of the form if the user tape enter button while entering a field 
	function stopRKey(evt) {
		  var evt = (evt) ? evt : ((event) ? event : null);
		  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
		  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
	}

	document.onkeypress = stopRKey;

	chunked_uploads_endpoints = {
		upload_url: '{{WEB_URL}}{% url upload %}',
		done_url: '{{WEB_URL}}{% url upload_done uuid='00000000-0000-0000-0000-000000000000' %}',
    };
	
	$(function() {		
    	//gets video_url and returns it to the "scr" field
    	chunked_uploads_video_url = function(video_url) {
    		$('#id_content-media_input_type').val("create");
    		$('#id_media-src').val(video_url);
    		$('#id_media-src').attr('readOnly', true);
    		$(".media_fields").hide();
       		$("#media_field_create").show();
    		$('#upload_url').append(video_url);
	    };
	    
	    //callback of chunked upload start
		chunked_uploads_start = function(){
		};
		
		//callback of chunked upload stop
		chunked_uploads_stop = function(){
		};
		
		//callback of chunked upload error
		chunked_uploads_error = function(){
			alert("The upload failed. Please reload the page, and try again.");
		};
		
		//callback of chunked upload complete
		chunked_uploads_complete = function(){
		};
    });
    </script>
{% endblock %}

{% block css_import %}
    {{ content_form.media.css }}
	{{ block.super }}
	<link rel="stylesheet" href='{% absstatic "ldt/css/ldtform.css" %}'/>
{% endblock %}

{% block content %}
	{% if media_form.errors %}
	<div id="media-form-error">
		<p>{% trans "The operation could not be performed because one or more error(s) occurred.<br />Please resubmit the media form after making the following changes:" %}</p>
		<ul>
		{% for field in media_form %}
		{% if field.errors%}<li>{{field.name}} : {{ field.errors|striptags }}</li>{% endif %}
		{% endfor %}
		</ul>
	</div>
	{% endif %}

	{{ media_form.errors }}
	{{ picture_form.errors }}
	{{ content_form.errors }}

	<div id="add_content" class="span-18 last">
	{% if iri_id %}
		<div class="projectscontentstitle">{% trans "Modify this content" %}</div>
	{% else %}
		<div class="projectscontentstitle">{% trans "Create content" %}</div>
	{% endif %}
	<form action="{{create_content_action}}" enctype="multipart/form-data" id="my_form" method="POST" enctype="multipart/form-data" >
	
	<div id="contentformhidden">
		{% csrf_token %}
		<input type="hidden" name="form_status" value="{{form_status}}" id="content_form_status" />
		{% for hidden in form.hidden_fields %}
	    	{{ hidden }}
	    {% endfor %}
	    {{content_form.iri_id}}
    </div>
    <div id="contentformfields" class="span-18 last">
	<div id="contentleft" class="span-9">
		<div>{{ content_form.src.errors }}{{ content_form.src.label_tag }}{{content_form.src}}</div>
		<div>{{ content_form.videopath.errors }}{{ content_form.videopath.label_tag }}{{content_form.videopath}}</div>
		<div>{{ content_form.title.errors }}{{ content_form.title.label_tag }}{{content_form.title}}</div>
		<div>{{ content_form.description.errors }}{{ content_form.description.label_tag }}{{content_form.description}}</div>
	</div>
	<div id="contentright" class="span-9 last" >
		<div>{{ content_form.external_id.errors }}{{ content_form.external_id.label_tag }}{{content_form.external_id}}</div>
		<div>{{ content_form.duration.errors }}{{ content_form.duration.label_tag }}{{content_form.duration}}</div>
		<div>{{ content_form.content_creation_date.errors }}{{ content_form.content_creation_date.label_tag }}{{content_form.content_creation_date}}</div>
		<div>{{ content_form.tags.errors }}{{ content_form.tags.label_tag }}{{content_form.tags}}</div>
		<div>{{ content_form.media_input_type.errors }}{{ content_form.media_input_type.label_tag }}{{content_form.media_input_type}}</div>
		<div id="media_fields_div">
			<div id="media_field_link" class="media_fields">{{ content_form.media_obj.errors }}{{ content_form.media_obj.label_tag }}{{content_form.media_obj}}</div>
			
			<div id="media_field_upload" class="media_fields">
				<!-- refers to chunked_upload template -->
				{% include "chunked_uploads.html"%}
			</div>
			
			<div id="media_field_url" class="media_fields">{{ media_form.external_src_url.errors }}{{ media_form.external_src_url.label_tag }}{{ media_form.external_src_url }}</div>
			
			<div id="media_field_create" class="media_fields">{{ media_form.src.errors }}{{ media_form.src.label_tag }}{{ media_form.src }}</div>
		</div>
	</div>	 
	
		{% if iri_id and user.is_staff %}
		<div>{% trans 'Actual front project' %} : {{ current_front_project }}
		<button type="submit" value="prepare_reset" name="submit_button">{% trans "reset_front_project" %}</button>
		{{ content_form.front_project.label_tag }}{{ content_form.front_project }}
		</div>
		
		{% else %}
		<div style="display:none">{{ content_form.front_project }}</div>
		{% endif %}
	{% include "ldt/ldt_utils/partial/picture.html"%}
	{% include "ldt/ldt_utils/partial/permissions.html" %}
		
	<div id="submitcontent" class="span-18 last">
		<div id="submitcontent-loader" class="span-10">
			<div id="submitcontent-loader-img" class="submitcontent-loader-content span-10 last"><img alt="loader" src='{% absstatic "ldt/img/ajax-loader-220x19.gif" %}'/></div>
			<div id="submitcontent-loader-msg" class="submitcontent-loader-content span-10 last">{% trans "media file is being processed please wait." %}</div>
			<span>&nbsp;</span>
		</div>
		<div id="submitcontent-buttons" class="span-8 last">
			<button type="submit" value="close" name="submit_button" id="submit_button_close">{% trans 'close_cancel' %}</button>
			{% if iri_id %}<button type="submit" value="prepare_delete" name="submit_button">{% trans "delete" %}</button>{% endif %}
			<button type="submit" value="write" name="submit_button" id="submit_button_write">{% trans "write" %}</button>
		</div>
	</div>
	
	</form>
	</div>
	<script type="text/javascript" src='{% absstatic "chunked_uploads/js/jquery.ui.widget.js" %}'></script>
	<script type="text/javascript" src='{% absstatic "chunked_uploads/js/jquery.iframe-transport.js" %}'></script>
	<script type="text/javascript" src='{% absstatic "chunked_uploads/js/jquery.fileupload.js" %}'></script>
	<script type="text/javascript" src='{% absstatic "chunked_uploads/js/jquery.chunked_uploads.js" %}'></script>
{% endblock %}
