(function(){

var num_files = 0;
var num_pending = 0;
var num_errors = 0;

jQuery.fn.pandaUploader = function(signed_params, options, swfupload_options) {

    var $video_field = this;

    if (signed_params === undefined) {
        alert("There was an error setting up the upload form. (The upload parameters were not specified).");
        return false;
    }
    
    options = options === undefined ? {} : options;
    swfupload_options = swfupload_options === undefined ? {} : swfupload_options;
    
    if (options.upload_button_id === undefined) {
        alert("You have to specify the button id");
        return false;
    }
    
    if ($video_field.size() == 0) {
        alert("The jQuery element is empty. Method pandaUploader() cannot be executed");
        return false;
    }
    
    if ( ! form()) {
        alert("Could not find a suitable form. Please place the call to pandaUploader() after the form, or to be executed onload().");
        return false;
    }
    
    if ($(form()).find('[name=submit], #submit').length != 0) {
        alert("An element of your video upload form is incorrect (most probably the submit button). Neither NAME nor ID can be set to \"submit\" on any field.");
        return false;
    }
    
    options = jQuery.extend({
        upload_filename_id: null,
        upload_progress_id: null,
        api_host: 'api.eu.pandastream.com',
        progress_handler: null,
        uploader_dir: "/panda_uploader"
    }, options);
    options['api_url'] = options['api_url'] || 'http://' + options['api_host'] + '/v2';
    
    if ( ! options.progress_handler) {
        options.progress_handler = new ProgressUpload(options);
    }
    
    var uploader = this.swfupload(jQuery.extend({
        upload_url: options.api_url + '/videos.json',
        file_size_limit : 0,
        file_types : "*.*",
        file_types_description : "All Files",
        file_upload_limit : 0,
        flash_url : options.uploader_dir + "/swfupload.swf",
        button_image_url : options.uploader_dir + "/choose_file_button.png",
        button_width : 87,
        button_height : 27,
        button_placeholder_id : options.upload_button_id,
        post_params : signed_params,
        file_post_name: "file",
        debug: false
    }, swfupload_options));
    
    uploader.bind('swfuploadLoaded', onLoad);
    uploader.bind('fileQueued', onFileQueued);
    uploader.bind('uploadStart', onStart);
    uploader.bind('uploadProgress', onProgress);
    uploader.bind('uploadSuccess', onSuccess);
    uploader.bind('uploadError', onError);
    uploader.bind('uploadComplete', onComplete);
    
    return uploader;
    
    
    //
    // Event handlers
    //
    
    function onLoad() {
        var form = $video_field.closest("form");
        form.submit(onSubmit);
    }

    function onFileQueued(event, file) {
        num_files++;
        num_pending++;
        var $field = $('#' + options.upload_filename_id);
        if ($field.size() == 0) {
            return;
        }
        $field.val(file.name);
    }

    function onSubmit(event) {
        uploader.swfupload('startUpload');
        return false;
    }

    function onStart(event, file) {

        if (options.progress_handler) {
            options.progress_handler.start(file);
        }
    }

    function onProgress(event, file, bytesLoaded, bytesTotal) {
        try {
            if (options.progress_handler) {
                options.progress_handler.setProgress(file, bytesLoaded, bytesTotal);
            }
        } catch (ex) {
        }
    	
    	
    }

    function onSuccess(event, file, response) {
    	//alert($video_field + " " + " " + file + " " + event + " resp:" + response);
        $video_field.val(eval('(' + response + ')').id);
        postNewVideoId(eval('(' + response + ')').id,$video_field.attr("id"));
        num_pending--;
        

    }

    function onError(event, file, code, message, more) {
    	//alert("message:" + message + " code:" + code + "event:" + event + " more: " + more);

        alert("There was an error uploading the file.\n\nHTTP error code " + message);
        num_pending--;
        num_errors++;
    }

    function onComplete(event, num_uploads) {
        if (num_pending > 0 && num_files != num_errors || num_files == num_errors) {
            return;
        }
        if ( ! $video_field.val()) {
            alert('The video ID was not stored on the form');
            return;
        }
       // form().submit();
    }
    
    
    //
    // Utils
    //
    
    function form() {
        return $video_field.closest("form").get(0);
    }
}


//
// A simple progress bar
//

function ProgressUpload(options) {
    this.options = options;
    this.$p = jQuery('#' + this.options.upload_progress_id);
    this.$p.css('display', 'none');
}

ProgressUpload.prototype = {
    start: function(file) {
        if (this.$p.size() == 0) {
            return;
        }
        
        if (this.$p.find('.progress-inside').size() == 0) {
            this.$p.append('<div class="progress-inside"></div>');
        }
        this.progress = this.$p.find('.progress-inside');
        this.setProgress(file, 0, file.size);
        this.$p.css('display', 'block');
    },
    
    setProgress: function(file, bytesLoaded, bytesTotal) {
        if ( ! this.progress) {
            return;
        }
        var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
        $(this.progress).css('width', percent + '%');
    }
}

})();