//  @import com.hedgehoglab.application.view.BaseView;
//  @import com.hedgehoglab.ui.LabBox;


function TagDialogview(scope){
    BaseView.call(this);
    var _this=this;
    var _tag_box=new LabBox();
    var _callback=null;
    var _field=null;

    //  Override tag_box methods

    _tag_box.on_confirm=function(e){
        _this.raise_event('on_confirm_clicked',{dialog:e, value:$(_field).val()});

    };

    _tag_box.on_cancel=function(e){
        _this.raise_event('on_cancel_clicked',e);
    };


    /**
     * Show the model
     * @param model
     */
    this.show=function(model){
        _this.render_template_with_view('tags/tag_contacts',model,{},
        function(rendered_template){
            _tag_box.show(rendered_template,function(el){
                // add the behaviour
                _field=$(".Field input",el);
                 $(".Field input",el).autocomplete(null, {
                    max: 20,
                    highlight: false,
                    multiple: true,
                    multipleSeparator: ",",
                    scroll: true,
                    scrollHeight: 200,
                    selectFirst:false,
                    minChars:1,
                    service:getData,
                    cacheLength:0,
                    result:on_result
                });
            });
        });
    };

    /**
     * Update the matches with new data
     * @param matches
     */
    this.update_matches=function(matches){
        _callback(matches);
    };

    /**
     * handle a request by the auto completer to get data ! THIS REQUIRES A CALLBACK TO UPDATE_MATCHES !
     * @param term
     * @param callback
     */
    function getData(term,callback){
        _callback=callback;
        _this.raise_event('on_tag_changed',{term:term,value:_field.val()});
    };

    function on_result(e){
        _this.raise_event('on_tag_selected',{value:e});
    };
};