﻿Type.registerNamespace("ATI.Ajax.Controls.AutoComplete");

ATI.Ajax.Controls.AutoComplete.ValueController = function(element) {
    ATI.Ajax.Controls.AutoComplete.ValueController.initializeBase(this, [element]);
    this._autocompleteID = '';
    this._selectionCompletedCustomHandler = null;
    this._autocomplete = null;
    this._autocompleteElement = null;
    this._changeHandler = null;
    this._selectionCompletedHandler = null;
    this._lastTextBoxValue = null;
}

ATI.Ajax.Controls.AutoComplete.ValueController.prototype = {

    // value accessors.
    get_value: function() {
        return this.get_element().value;
    },
    set_value: function(value) {
        this.get_element().value = value;
    },

    //parent autocomplete ID accessors
    get_autocompleteID: function() {
        return this._autocompleteID;
    },
    set_autocompleteID: function(value) {
        this._autocompleteID = value;
    },

    //custom JS handler accessors
    get_selectionCompletedCustomHandler: function() {
        return this._selectionCompletedCustomHandler;
    },
    set_selectionCompletedCustomHandler: function(value) {
        this._selectionCompletedCustomHandler = value;
    },

    get_autocomplete: function() {
        return $find(this._autocompleteID);
    },

    OnAutocompleteElementChange: function() {
        if (this._lastTextBoxValue != this._autocompleteElement.value) {
            this._lastTextBoxValue = this._autocompleteElement.value;
            this.set_value(0);
        }
    },

    OnSelectionComplete: function(source, eventArgs) {
        var value = eventArgs.get_value();
        if (value != this.get_value())
            this.set_value(value);
    },

    // Release resources before control is disposed.
    dispose: function() {
        if (this._autocompleteElement)
            $removeHandler(this._autocompleteElement, 'change', this._changeHandler);
        if (this._autocomplete) {
            this._autocomplete.remove_selectionCompleted(this._selectionCompletedHandler);
            if (this.get_selectionCompletedCustomHandler() != null)
                this._autocomplete.remove_selectionCompleted(this.get_selectionCompletedCustomHandler());
        }
        ATI.Ajax.Controls.AutoComplete.ValueController.callBaseMethod(this, 'dispose');
    },

    initialize: function() {

        ATI.Ajax.Controls.AutoComplete.ValueController.callBaseMethod(this, 'initialize');

        this._autocomplete = this.get_autocomplete();
        this._autocompleteElement = this._autocomplete.get_element();
        this._lastTextBoxValue = this._autocompleteElement.value;

        this._changeHandler = Function.createDelegate(this, this.OnAutocompleteElementChange);
        this._selectionCompletedHandler = Function.createDelegate(this, this.OnSelectionComplete);
        $addHandler(this._autocompleteElement, 'change', this._changeHandler);

        this._autocomplete.add_selectionCompleted(this._selectionCompletedHandler);

        if (this.get_selectionCompletedCustomHandler() != null)
            this._autocomplete.add_selectionCompleted(this.get_selectionCompletedCustomHandler());
    }
}
ATI.Ajax.Controls.AutoComplete.ValueController.registerClass('ATI.Ajax.Controls.AutoComplete.ValueController', 
    Sys.UI.Control);



ATI.Ajax.Controls.AutoComplete.ValueController.descriptor = {
    properties: [{name: 'autocompleteID', type: String},
                    {name: 'selectionCompletedCustomHandler', type: Function}]
}

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();