/*
 * jQuery File Upload Plugin JS Example 6.0
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

/*jslint nomen: true, unparam: true, regexp: true */
/*global $, window, document */

$(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload();
	$('#fileupload').fileupload('option', {
		maxFileSize: 2000000000,
	});
	
    // Enable iframe cross-domain access via redirect page:
    var redirectPage = window.location.href.replace(
        /\/[^\/]*$/,
        '/result.html?%s'
    );
    $('#fileupload').bind('fileuploadsend', function (e, data) {
        if (data.dataType.substr(0, 6) === 'iframe') {
            var target = $('<a/>').prop('href', data.url)[0];
            if (window.location.host !== target.host) {
                data.formData.push({
                    name: 'redirect',
                    value: redirectPage
                });
            }
        }
    });
	$('#fileupload').bind('fileuploadstart', function () {
		var widget = $(this),
			progressElement = $('#fileupload-progress').fadeIn(),
			interval = 500,
			total = 0,
			loaded = 0,
			loadedBefore = 0,
			progressTimer,
			progressHandler = function (e, data) {
				loaded = data.loaded;
				total = data.total;
			},
			stopHandler = function () {
				widget
					.unbind('fileuploadprogressall', progressHandler)
					.unbind('fileuploadstop', stopHandler);
				window.clearInterval(progressTimer);
				progressElement.fadeOut(function () {
					progressElement.html('');
				});
			},
			formatTime = function (seconds) {
				var date = new Date(seconds * 1000);
				return ('0' + date.getUTCHours()).slice(-2) + ':' +
					('0' + date.getUTCMinutes()).slice(-2) + ':' +
					('0' + date.getUTCSeconds()).slice(-2);
			},
			formatBytes = function (bytes) {
				if (bytes >= 1000000000) {
					return (bytes / 1000000000).toFixed(2) + ' Go';
				}
				if (bytes >= 1000000) {
					return (bytes / 1000000).toFixed(2) + ' Mo';
				}
				if (bytes >= 1000) {
					return (bytes / 1000).toFixed(2) + ' Ko';
				}
				return bytes + ' o';
			},
			formatPercentage = function (floatValue) {
				return (floatValue * 100).toFixed(2) + ' %';
			},
			updateProgressElement = function (loaded, total, bps) {
				progressElement.html(
					formatBytes(bps) + '/s | ' +
						formatTime((total - loaded) / bps) + ' | ' +
						formatPercentage(loaded / total) + ' | ' +
						formatBytes(loaded) + ' / ' + formatBytes(total)
				);
			},
			intervalHandler = function () {
				var diff = loaded - loadedBefore;
				if (!diff) {
					return;
				}
				loadedBefore = loaded;
				updateProgressElement(
					loaded,
					total,
					diff * (1000 / interval)
				);
			};
		widget
			.bind('fileuploadprogressall', progressHandler)
			.bind('fileuploadstop', stopHandler);
		progressTimer = window.setInterval(intervalHandler, interval);
	});
    // Open download dialogs via iframes,
    // to prevent aborting current uploads:
    /*$('#fileupload .files').delegate(
        'a:not([rel^=gallery])',
        'click',
        function (e) {
            e.preventDefault();
            $('<iframe style="display:none;"></iframe>')
                .prop('src', this.href)
                .appendTo(document.body);
        }
    );*/

    // Initialize the Bootstrap Image Gallery plugin:
    //$('#fileupload .files').imagegallery();

});

