$(function () {
	'use strict';
	var is_canceled=false;
	var is_paused=false;
	var data_resume = null;
	$('#start_upload').attr('disabled', true);
	$('#pause_upload').attr('disabled', true);
	$('#resume_upload').attr('disabled', true);
	$('#cancel_upload').attr('disabled', true);
	
	$('#fileupload').fileupload({        
        url: chunked_uploads_endpoints.upload_url,
	   	dataType: 'json',
	   	maxChunkSize: 1048576,
	   	maxNumberOfFiles: 1,
	    multipart: false,
	    xhrFields: {
	        withCredentials: true
	    },
	    add: function (e, data) {
	    	$('#start_upload').attr('disabled', false);
	    	
	    	$('#start_upload').one('click', function (e) {
	            e.preventDefault();
	            chunked_uploads_endpoints.jqXHR = data.submit();
	        });
	    },
	    done: function (e, data) {
	    	chunked_uploads_endpoints.done_url = chunked_uploads_endpoints.done_url.replace('00000000-0000-0000-0000-000000000000', data.result[0].upload_uuid);
	    	$.getJSON(chunked_uploads_endpoints.done_url, function(upload) {
	    		if (typeof chunked_uploads_video_url === "function") {
	    			chunked_uploads_video_url(upload[0].video_url);
	    		}
	    	});
	        $('#start_upload').hide();
    		$('#pause_upload').hide();
    		$('#resume_upload').hide();
    		$('#cancel_upload').hide();
    		
	    },
	    progressall: function (e, data) {
	    	var progress = parseInt(data.loaded / data.total * 100, 10);
	        $('.progress').css(
	            'width',
	            progress + '%'
	        );
    	},
	    send: function(e, data) {
	    	$("#fileupload").hide();
	    	$('#start_upload').hide();
            $('#pause_upload').attr('disabled', false);
        	$('#cancel_upload').attr('disabled', false);
	    },
	    fail: function(e, data) {
	    	data_resume = data;
	    	if (typeof chunked_uploads_error === "function") {
    			chunked_uploads_error(true);
    		}
	    }
    });
	   
    $('#cancel_upload').click(function (e) {
		if (!is_paused){
			is_canceled = true;
			chunked_uploads_endpoints.jqXHR.abort();
	    	$('#pause_upload').attr('disabled', true);
			$('#resume_upload').attr('disabled', true);
			setTimeout(function() {
				$.ajax({
					  dataType: "json",
					  url: chunked_uploads_endpoints.upload_url,
					  xhrFields: {withCredentials: true},
					  success: function(current_upload){
						  $.ajax({
			    	    		type: current_upload[0].delete_type,
			    	    		url: current_upload[0].delete_url
			    		  });
					  }
				});
			},1000);
		}
		else{
			$.ajax({
				  dataType: "json",
				  url: chunked_uploads_endpoints.upload_url,
				  xhrFields: {withCredentials: true},
				  success: function(current_upload){
					  $.ajax({
		    	    		type: current_upload[0].delete_type,
		    	    		url: current_upload[0].delete_url
		    		  });
				  }
			});
		}
	});
	
	$('#pause_upload').click(function (e) {
		is_paused = true;
		chunked_uploads_endpoints.jqXHR.abort();
		$('#pause_upload').attr('disabled', true);
		$('#resume_upload').attr('disabled', false);
	});
	
	$('#resume_upload').click(function (e) {
		is_paused = false;
		$.ajax({
			  dataType: "json",
			  url: chunked_uploads_endpoints.upload_url,
			  xhrFields: {withCredentials: true},
			  success: function(current_upload){
				  data_resume.uploadedBytes = current_upload[0].size;
				  chunked_uploads_endpoints.jqXHR = data_resume.submit();
			  }
		});
		$('#resume_upload').attr('disabled', true);
		$('#pause_upload').attr('disabled', false);
	});
	
	if ('onLine' in navigator) {
        //event listener on connection found
		window.addEventListener('online', function(){
    		is_paused = false;
    		$.ajax({
    			  dataType: "json",
    			  url: chunked_uploads_endpoints.upload_url,
    			  xhrFields: {withCredentials: true},
    			  success: function(current_upload){
    				  data_resume.uploadedBytes = current_upload[0].size;
    				  chunked_uploads_endpoints.jqXHR = data_resume.submit();
    			  }
    		});
    		$('#resume_upload').attr('disabled', true);
    		$('#pause_upload').attr('disabled', false);
        });
        
		//event listener on connection lost
        window.addEventListener('offline', function(){
        	if (chunked_uploads_endpoints.jqXHR != null){
	    		is_paused = true;
	    		$('#pause_upload').attr('disabled', true);
	    		$('#resume_upload').attr('disabled', true);
	    		chunked_uploads_endpoints.jqXHR.abort();
        	}
        });
    };
	
});
@@ -978,51 +978,142 @@
  %09$.
-getJSON(chunked_uploads_endpoints.done_url,
+ajax(%7B%0A%09%09%09%09  dataType: %22json%22,%0A%09%09%09%09  url: chunked_uploads_endpoints.done_url,%0A%09%09%09%09  xhrFields: %7BwithCredentials: true%7D,%0A%09%09%09%09  success:
  fun
@@ -1122,25 +1122,32 @@
 ion(
+current_
 upload)
- 
 %7B%0A%09
-    %09%09
+%09%09%09%09  
 if (
@@ -1193,24 +1193,26 @@
 unction%22) %7B%0A
+%09%09
 %09    %09%09%09chun
@@ -1233,16 +1233,24 @@
 deo_url(
+current_
 upload%5B0
@@ -1268,25 +1268,37 @@
 );%0A%09
-    %09%09%7D
+%09%09%09%09  %7D%0A%09%09%09%09  %7D%0A%09%09%09%7D);
 %0A%09    %09
-%7D);
 %0A%09  

update done request to send credentials (it allows backend to properly remove session-uuid from request.session)