


$.fn.uploader = function(conf) {

    
    var markup = '' +
    '<div class="uploader">' +
        '<div class="toolbar">' +
            '<div id="browse" style="width: 200px; height: 20px;"></div>' + 
        '</div>' +
        '<ul></ul>' + 
    '</div>'

    function redraw(state, ul) {
        ul.html('')
        for(var i in state) {
            var item = state[i]
            $('<li><b>' + item.name + '</b></li>').appendTo(ul)
        }
    }
    
    function serializeState(state) {
        return '["'+state.map(function(a) { return a.id }).join('","')+'"]';
    }
    
    this.each(function() {
        // create markup and insert in document
        var uploaderDiv = $(markup).appendTo(this)
        var ul = uploaderDiv.children('ul')
        var textarea = $(this).children('textarea')
        var form = $(this).parents('form')
        var state = []
        
        // Populate 'state' from value of hidden textarea
        // Update textarea.value to a simple list of id's
        // [code here]
        if(textarea[0].value) {
            eval('state = ' + textarea[0].value)
        }
        textarea[0].value = serializeState(state)
        redraw(state, ul)
        
        // Attach onsubmit listener to form
        var onsubmit = function() {
            // Start upload
            if(swfu.getStats().files_queued > 0) {
                form.data('stop', true)
                swfu.startUpload()
            
                // This does not stop other event listeners from 
                // getting executed, hence using a "stop"-flag above
                return false
            }
        }
        form.bind('submit', onsubmit)
        
        
        // create an uploader instance
        var swfu = new SWFUpload({
            upload_url: "/acm/document/upload_attachment",
            flash_url: "/acm/script/swfupload/swfupload.swf",
            file_size_limit: "200 MB",
            button_placeholder_id: "browse",
            button_text: 'Browse..',
            button_width: 100,
            button_height: 20,
            file_queued_handler: function(file) {
                var id = '' + Math.random().toString(16).substring(2) + '_' + file.name
                swfu.addPostParam(file.name, id)
                state.push({
                    id: id,
                    name: file.name
                })
                
                redraw(state, ul)
            }, 
            upload_complete_handler: function(file, server_data, received_response) {
                // If there are no more files in queue, submit form
                var stats = swfu.getStats()
                if(stats.files_queued == 0) {
                    form.data('stop', false)
                    form.unbind('submit', onsubmit)
                    // Create a json-array string of id's
                    textarea[0].value = serializeState(state)
                    form.submit()
                    form.bind('submit', onsubmit)
                    
                }
            }
        });
        
        
    })
    


}
