(function(){
    dojo.addOnLoad(function(){
        var frm = dojo.byId("comic_submit");
        dojo.connect(frm, "click", function(){
            dojo.io.iframe.send({
                form: "new_comic",
                handleAs:"json",
                method:"post",
                preventCache: true
            });
			
            var interval = -1;
            var progress = function()
            {
                dojo.xhrGet({
                    url: "/progress",
                    handleAs: "json",
                    headers: {'X-Progress-ID': dojo.attr("progress_key",'value')},
                    load: function(data){
                        if( data.state == 'uploading')
                        {
                            jsProgress.update({
                               maximum:100,
                               progress: Math.round( (data.received / data.size) * 100)
                            });
                        }
                        else if(data.state == 'done')
                        {
                            jsProgress.update({
                               maximum:100,
                               progress: 100
                            });
                            clearInterval(interval);
                        }
                    }
                });
            };
            interval = setInterval(progress, 1000);
        });
    });
})();
