// Avoid `console` errors in browsers that lack a console.
(function() {
    var method;
    var noop = function () {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
        'timeline', 'timelineEnd', 'timeStamp', 'trace', 'warn'
    ];
    var length = methods.length;
    var console = (window.console = window.console || {});

    while (length--) {
        method = methods[length];

        // Only stub undefined methods.
        if (!console[method]) {
            console[method] = noop;
        }
    }
}());

// Place any jQuery/helper plugins in here.
function HandleControls() {
// Trig some javascript to handle New Dialog content
    $.each( $('.fileupload'), 
        function( NumCtrl, Ctrl ) {
            $("#"+Ctrl.id).fileupload({
                        // Uncomment the following to send cross-domain cookies:
                        //xhrFields: {withCredentials: true},
                        //url: '/uploader/proceed/'
                        url: this.action
                    });
            // Enable iframe cross-domain access via redirect option:
            $("#"+Ctrl.id).fileupload(
                'option',
                'redirect',
                window.location.href.replace(
                    /\/[^\/]*$/,
                    '/cors/result.html?%s'
                )
            );
            $("#"+Ctrl.id).addClass('fileupload-processing');
            $.ajax({
                // Uncomment the following to send cross-domain cookies:
                //xhrFields: {withCredentials: true},
                //url: this.action,
                url: $("#"+Ctrl.id).fileupload('option', 'url'),
                //url: "uploader/proceed/",
                dataType: 'json',
                context: $("#"+Ctrl.id)[0]
            }).always(function () {
                $(this).removeClass('fileupload-processing');
            }).done(function (result) {
                $(this).fileupload('option', 'done')
                    .call(this, $.Event('done'), {result: result}); //$(this)});
            });
        }
    );
}