/*
 * jquery.uploadProgress
 *
 * Copyright (c) 2008 Piotr Sarnacki (drogomir.com)
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 */
(function($) {
  $.fn.uploadProgress = function(options) {
	options = $.extend({
		interval: 1000,
		progressUrl: "/progress",
		start: function() {},
		uploading: function() {},
		complete: function() {},
		success: function() {},
		error: function() {},
		uploadProgressPath: '/design/js/jquery.uploadProgress.js',
		jqueryPath: 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js',
                timer: ""
	}, options);

        $(function() {
            if($.browser.safari && top.document == document) {
                /* iframe to send ajax requests in safari       thanks to Michele Finotto for idea */
                iframe = document.createElement('iframe');
                iframe.name = "progressFrame";
                $(iframe).css({width: '0', height: '0', position: 'absolute', top: '-3000px'});
                document.body.appendChild(iframe);
                var d = iframe.contentWindow.document;
                d.open();      /* weird - safari won't load scripts without this lines... */
                d.write('<html><head></head><body></body></html>');
                d.close();
                var b = d.body;
                var s = d.createElement('script');
                s.src = options.jqueryPath;      /* must be sure that jquery is loaded */
                s.onload = function() {
                    var s1 = d.createElement('script');
                    s1.src = options.uploadProgressPath;
                    b.appendChild(s1);
                }
                b.appendChild(s);
            }
        });

	return this.each(function(){
            $(this).bind('submit', function() {
                var uuid = "";
                for (i = 0; i < 32; i++) { uuid += Math.floor(Math.random() * 16).toString(16); }            /* update uuid */
                options.uuid = uuid;
                options.start();
                $(this).attr("action", jQuery(this).attr("action") + "?X-Progress-ID=" + uuid);
                var uploadProgress = $.browser.safari ? progressFrame.jQuery.uploadProgress : jQuery.uploadProgress;
                options.timer = window.setInterval(function() { uploadProgress(this, options) }, options.interval);
            });
	});
  };

  jQuery.uploadProgress = function(e, options) {
	$.ajax({
            type: "GET",
            url: options.progressUrl,
            dataType: "json",
            beforeSend: function(xhr) {
                    xhr.setRequestHeader("X-Progress-ID", options.uuid);
            },
            success: function(upload) {
                if(upload){
                    if (upload.state == 'uploading') {
                        upload.percents = Math.floor((upload.received / upload.size)*100);
                        upload.speed = Math.floor(upload.received / upload.during);
                        options.uploading(upload);
                    }

                    if (upload.state == 'done' || upload.state == 'error') {
                        window.clearTimeout(options.timer);
                        options.complete(upload);
                    }

                    if (upload.state == 'done') {
                        options.success(upload);
                    }

                    if (upload.state == 'error') {
                        options.error(upload);
                    }
                }
            }
	});
  };

})(jQuery);

