{% load insert_tags %}
{% load uni_form_tags %}
{% load thumbnail %}
	<h5 id="srv_insert_gallery_title">{{ object.title }}</h5>
	<form id="srv_upload_image" method="POST" enctype="multipart/form-data" class="servee uniform" action="{% url servee:insert_servee_gallery_gallery_image_add %}">
        {% csrf_token %}
        {{ image_form|as_uni_form }}
        <input type="hidden" value="{{ object.pk }}" name="gallery_id" id="id_gallery_id" />
        <div class="buttonHolder">
            <input type="submit" class="srv_button insert button" id="srv_upload_image_action" value="Upload">
        </div>
    </form>
    <script type="text/javascript" src="{{ STATIC_URL }}uploadify/swfobject.js"></script>
    <script type="text/javascript" src="{{ STATIC_URL }}uploadify/jquery.uploadify.3.0.0.min.js"></script>
    
    <script type="text/javascript" src="{{ STATIC_URL }}jquery-ui/js/jquery-ui-1.8.11.custom.min.js"></script>
    
    <script type="text/javascript">
    $(document).ready(function(){
        $('html').ajaxSend(function(event, xhr, settings) {
            function getCookie(name) {
                var cookieValue = null;
                if (document.cookie && document.cookie != '') {
                    var cookies = document.cookie.split(';');
                    for (var i = 0; i < cookies.length; i++) {
                        var cookie = jQuery.trim(cookies[i]);
                        // Does this cookie string begin with the name we want?
                        if (cookie.substring(0, name.length + 1) == (name + '=')) {
                            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                            break;
                        }
                    }
                }
                return cookieValue;
            }
            if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                // Only send the token to relative URLs i.e. locally.
                xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
            }
        });
        
        // uploadify
        
    	$("form#srv_upload_image input[type=file]").uploadify({
    		swf: "{{ STATIC_URL }}uploadify/uploadify.swf",
            uploader: $("form#srv_upload_image").attr("action"),
            cancelImage: "{{ STATIC_URL }}uploadify/uploadify-cancel.png",
            auto: true,
            buttonImage: "{{ STATIC_URL }}servee/images/buttonBG.png",
            width: 90,
            height: 24,
            fileObjName: "image",
            checkExisting: false,
            multi: true,
            postData: {
                "csrfmiddlewaretoken": $("form#srv_upload_image [name=csrfmiddlewaretoken]").val(),
                "gallery_id": "{{ object.pk }}"
            },
    		onUploadSuccess: function(file, response){
    			$("ul.galleryThumbs").prepend(response);
            	
            	$("#id_galleryThumbs").sortable("destroy");
            	$("#id_galleryThumbs").sortable();
    		}
    	});
    	$("input#srv_upload_image_action").hide();
    	
    	$("#id_galleryThumbs").sortable();
        $("#id_galleryThumbs").disableSelection();
        
        $("#id_galleryThumbs").live("sortstop", function(event, ui){
            var $sortable = $(this);
            $.ajax({
                url: "{% url servee:insert_servee_gallery_gallery_reorder %}",
                type: "post",
                data: $sortable.sortable("serialize", {key:"itemid"})
            });
        });
    });
    </script>
	<ul class="galleryThumbs" id="id_galleryThumbs">
	    {% for item in object.items.all %}
	        {% include "servee/wysiwyg/insert/servee_gallery/_add_image.html" %}
	    {% endfor %}
	</ul>
	
	
	
	<input type="hidden" name="srv_insert_gallery_pk" id="srv_insert_gallery_pk" value="{{ object.pk }}">
	<input type="button" class="srv_button insert" id="srv_insert_gallery_action" value="Place Gallery at Cursor">
<script type="text/javascript">
$(document).ready(function(){
	$("input#srv_insert_gallery_action").click(function(e){
        $.ajax({
		    url: "{% insert_render_url insert object.pk %}",
		    success: function(data, text){
		        srv_hide_adminBox("srv_insertMedia");
        		srv_wysiwyg_insert_html_at_cursor(data);
		    }
		});
		e.preventDefault();
		return false;
	});
});
</script>
