
var SelectDialog = new Class({
    
    Implements: [Options, Events],

    options: {
        callback_select: $empty,
        callback_cancel: $empty,
        type: 'users',
        presets: [],
        filter: false
    },
   
    container: null,

    // Initialize Instance
    initialize: function(options){

        this.setOptions(options);
        this.selection = [];
    },

    
    // Display dialog, as modal window
    display: function() {

        // build  frame
        container = new Element('div', { 'id': 'selectdialog' } );
        
        // load frame
        container.set('load', { onComplete: this.load.bind(this) });
        
    
        container.load('/dialog/selectdialog');

        // display frame
        container.inject(document.body );        
             
        // store into object
        this.container = container;

    },

    refresh: function() {
       
        var data;
        var resulttable = this.resulttable;

      
        if ( this.options.filter ) {
            data = { searchstring: this.options.filter };
        }
        
        var presets = this.options.presets;

        // load users
        var jsonRequest = new Request.JSON({
                url: "/dialog/usersearchownajax",
                noCache: true,
                onSuccess: function(users) {
            

                try {  resulttable.empty(); } 
                catch (e)  {}

             users.each( function(user) {

                        var checkbox = new Element('input', { 'type': 'checkbox', 'name': 'selectdialog_value', 'value': user.id });
                        
                        presets.each( function(p) {
                        
                            if ( p == user.id ) {

                                checkbox.checked = true;
                            }

                        });

                        resulttable.push( [ checkbox, user.fullname, user.email ] );

                    }.bind(this));
                }.bind(this)
             }
        ).get(data);
    },

    // 
    load: function() {

        $('selectdialog_cancel').addEvent('click', function(e) { e.stop(); this.cancel() }.bind(this) );
        $('selectdialog_select').addEvent('click', function(e) { e.stop(); this.select() }.bind(this) );

        $('selectdialog_filter').addEvent('keyup', function(e) { e.stop(); this.options.filter = $('selectdialog_filter').value; this.refresh() }.bind(this) );

        var resulttable = new HtmlTable($('selectdialog_table'), 
        
            { 
                
                selectable:   true, 
                onRowFocus:   this.addToSelection.bind(this), 
                onRowUnfocus: this.removeFromSelection.bind(this)
            }
            
        );
        
        // Clean Table
         try {  resulttable.empty(); } 
         catch (e)  {};


        this.resulttable = resulttable;


        // Intialload
        this.refresh();

      },


    // Add to selection-array
    addToSelection: function(row) {

        var checkbox = row.getElement("input[name=selectdialog_value]");
        checkbox.checked = true;
        this.selection.push(checkbox.value);
    },
    
    // Remove from selection-array
    removeFromSelection: function(row) {
      
        var checkbox = row.getElement("input[name=selectdialog_value]");
        checkbox.checked = false;
        this.selection.erase(checkbox.value);
    },    
    // Call select
    select: function() {

        this.container.destroy();
        this.options.callback_select(this.selection);
    },

    // Call cancel
    cancel: function() {

        this.container.destroy();
        this.options.callback_cancel();
    }

});

