$(function() {

    $('#uuid').attr('value', Math.uuid() + '.jpg');

    $('#status').ajaxError(function(e, xhr, settings, error) {
        $(this).text('error loading page: ' + error);
    });

    // webcam init
    webcam.set_api_url('/docroot/upload.pl');
    webcam.set_quality(90);
    webcam.set_shutter_sound(true, '/js/shutter.mp3');
    webcam.set_hook('onComplete', function(data) {
        $('#done').attr('value', '1');
        $('#url input').attr('value', data);
        $('#upload').submit();
    });
    webcam.set_hook('onError', function(data) {
        $('.webcamradio').hide();
        $('#fileradio').click();
        return false;
    });

    $('input[type="radio"]').click(function() {
        $('#done').attr('value', 0);
    });

    $('#webcam').html(webcam.get_html(320,240) );

    // agile uploader
    var flashvars = {
        firebug : 'true',
        max_height : 1024,
        max_width  : 1024,
        jpg_quality : 90,
        file_input_field_width : 200,
        file_post_var : 'photo',
        form_action : '/docroot/upload2.pl',
        js_get_form_data : function() {
            setTimeout(function() {
                if ($('#done').attr('value') == 1) {
                    return;
                }
                $('#status').text('File upload timed out.  Please try again.');
                $('#spinningDisk').hide();
                $('#fdMeButton').show();
            }, 30000);
            return $('#upload').serialize();
        },
        flash_submit : false,
        js_encode_progress_callback : function() {
            $('#status').text('');
            $('#fdMeButton').hide();
            $('#encoding').show();
            $('#done').attr('value', '0');
        },
        js_encode_callback : function() {
            $('#encoding').hide();
            $('#fdMeButton').show(); 
        },
        js_status_callback : function(status) {
            if (status == 200) {
                //alert('uploaded file successfully');
                $('#done').attr('value', 1);
                $('#upload').submit();
            }
            else {
                $('#status').text('Failed to upload image.  Please select an image.');
                $('#spinningDisk').hide();
                $('#fdMeButton').show();
            }
        }
    };

    var params = {
        allowscriptaccess : 'always'
    };

    var  attributes = { 
        id : 'ExternalInterface' 
    };
    swfobject.embedSWF("/js/agile-uploader.swf", "agile-upload", "310", "30", "10.0.0", "/js/expressInstall.swf", flashvars, params, attributes);

});

