﻿Type.registerNamespace('CMACGM');

CMACGM.AutoCompleteBehavior = function CMACGM$AutoCompleteBehavior(element) {
    /// <param name="element" domElement="true"></param>
    var e = Function._validateParams(arguments, [
        {name: "element", domElement: true}
    ]);
    if (e) throw e;

    CMACGM.AutoCompleteBehavior.initializeBase(this, [element]);
}

    function CMACGM$AutoCompleteBehavior$get_completionInterval() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._completionInterval;
    }
    function CMACGM$AutoCompleteBehavior$set_completionInterval(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._completionInterval = value;
    }

    function CMACGM$AutoCompleteBehavior$get_completionList() {
        /// <value domElement="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._completionListElement;
    }
    function CMACGM$AutoCompleteBehavior$set_completionList(value) {
        var e = Function._validateParams(arguments, [{name: "value"}]);
        if (e) throw e;

        this._completionListElement = value;
    }

    function CMACGM$AutoCompleteBehavior$get_completionSetCount() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._completionSetCount;
    }
    function CMACGM$AutoCompleteBehavior$set_completionSetCount(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._completionSetCount = value;
    }

    function CMACGM$AutoCompleteBehavior$get_minimumPrefixLength() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._minimumPrefixLength;
    }
    function CMACGM$AutoCompleteBehavior$set_minimumPrefixLength(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._minimumPrefixLength = value;
    }

    function CMACGM$AutoCompleteBehavior$get_serviceMethod() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._serviceMethod;
    }
    function CMACGM$AutoCompleteBehavior$set_serviceMethod(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;

        this._serviceMethod = value;
    }

    function CMACGM$AutoCompleteBehavior$get_serviceURL() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._serviceURL;
    }
    function CMACGM$AutoCompleteBehavior$set_serviceURL(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;

        this._serviceURL = value;
    }

    function CMACGM$AutoCompleteBehavior$get_enableCaching() {
        /// <value type="Boolean"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._enableCaching;
    }
    function CMACGM$AutoCompleteBehavior$set_enableCaching(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Boolean}]);
        if (e) throw e;

        this._enableCaching = value;
    }

    function CMACGM$AutoCompleteBehavior$_get_path() {
        return this.get_serviceURL();
    }

    function CMACGM$AutoCompleteBehavior$get_timeout() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return 0;
    }

    function CMACGM$AutoCompleteBehavior$dispose() {
        if(this._timer) {        
            this._timer.dispose();
            this._timer = null;
        }

        var element = this.get_element();
        if(element) {
            $removeHandler(element, "focus", this._focusHandler);
            $removeHandler(element, "blur", this._blurHandler);
            $removeHandler(element, "keydown", this._keyDownHandler);
            $removeHandler(this._completionListElement, 'mousedown', this._mouseDownHandler);
            $removeHandler(this._completionListElement, 'mouseup', this._mouseUpHandler);
            $removeHandler(this._completionListElement, 'mouseover', this._mouseOverHandler);
        }
        
        this._tickHandler = null;
        this._focusHandler = null;
        this._blurHandler = null;
        this._keyDownHandler = null;
        this._mouseDownHandler = null;
        this._mouseUpHandler = null;
        this._mouseOverHandler = null;
        
        CMACGM.AutoCompleteBehavior.callBaseMethod(this, 'dispose');
    }

    function CMACGM$AutoCompleteBehavior$initializeTimer(timer) {
        timer.set_interval(this._completionInterval);
        timer.add_tick(this._tickHandler);
    }

    function CMACGM$AutoCompleteBehavior$initializeTextBox(element) {
        element.autocomplete = "off";
        $addHandler(element, "focus", this._focusHandler);
        $addHandler(element, "blur", this._blurHandler);
        $addHandler(element, "keydown", this._keyDownHandler);
    }

    function CMACGM$AutoCompleteBehavior$initializeCompletionList(element) {
        var completionListStyle = element.style;
        completionListStyle.visibility = 'hidden';
        completionListStyle.backgroundColor = 'window';
        completionListStyle.color = 'windowtext';
        completionListStyle.border = 'solid 1px buttonshadow';
        completionListStyle.cursor = 'default';
        completionListStyle.unselectable = 'unselectable';
        completionListStyle.overflow = 'hidden';
        var elementBounds = Sys.UI.DomElement.getBounds(this.get_element());        
        completionListStyle.width = (elementBounds.width - 2) + 'px';
        
        $addHandler(element, "mousedown", this._mouseDownHandler);
        $addHandler(element, "mouseup", this._mouseUpHandler);
        $addHandler(element, "mouseover", this._mouseOverHandler);
    }

    function CMACGM$AutoCompleteBehavior$initialize() {
        CMACGM.AutoCompleteBehavior.callBaseMethod(this, 'initialize');

        this._tickHandler = Function.createDelegate(this, this._onTimerTick);
        this._focusHandler = Function.createDelegate(this, this._onGotFocus);
        this._blurHandler = Function.createDelegate(this, this._onLostFocus);
        this._keyDownHandler = Function.createDelegate(this, this._onKeyDown);
        this._mouseDownHandler = Function.createDelegate(this, this._onListMouseDown);
        this._mouseUpHandler = Function.createDelegate(this, this._onListMouseUp);
        this._mouseOverHandler = Function.createDelegate(this, this._onListMouseOver);
        
        this._timer = new Sys.Preview.Timer();
        this.initializeTimer(this._timer);
        
        var element = this.get_element();
        this.initializeTextBox(element);
                
        if (!this._completionListElement) {
            this._completionListElement = document.createElement('DIV');
            document.body.appendChild(this._completionListElement);
        }
        
        this.initializeCompletionList(this._completionListElement);
        
        var popupControl = new Sys.UI.Control(this._completionListElement);
        this._popupBehavior = new Sys.Preview.UI.PopupBehavior(this._completionListElement);
        this._popupBehavior.set_parentElement(element);
        this._popupBehavior.set_positioningMode(Sys.Preview.UI.PositioningMode.BottomLeft);
        this._popupBehavior.initialize();
        popupControl.initialize();
    }

    function CMACGM$AutoCompleteBehavior$_hideCompletionList() {
        this._popupBehavior.hide();
        this._completionListElement.innerHTML = '';
        this._selectIndex = -1;
    }

    function CMACGM$AutoCompleteBehavior$_highlightItem(item) {
        /// <param name="item"></param>
        var e = Function._validateParams(arguments, [
            {name: "item"}
        ]);
        if (e) throw e;

        var children = this._completionListElement.childNodes;
        
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child !== item) {
                child.style.backgroundColor = 'window';
                // *** Default Value
                child.style.color = 'windowtext';
            }
        }
        item.style.backgroundColor = 'highlight';
        item.style.color = 'highlighttext';
    }

    function CMACGM$AutoCompleteBehavior$_onListMouseDown(ev) {
        if (ev.target !== this._completionListElement) {
            this._setText(ev.target.firstChild.nodeValue);
        }
    }

    function CMACGM$AutoCompleteBehavior$_onListMouseUp(ev) {
        this.get_element().focus();
    }

    function CMACGM$AutoCompleteBehavior$_onListMouseOver(ev) {
        var item = ev.target;
        this._selectIndex = -1;
        this._highlightItem(item);
    }

    function CMACGM$AutoCompleteBehavior$_onGotFocus(ev) {
        this._timer.set_enabled(true);
    }

    function CMACGM$AutoCompleteBehavior$_onKeyDown(ev) {
        var k = ev.keyCode ? ev.keyCode : ev.rawEvent.keyCode;
        if (k === Sys.UI.Key.esc) {
            this._hideCompletionList();
            ev.preventDefault();
        }
        else if (k === Sys.UI.Key.up) {
            if (this._selectIndex > 0) {
                this._selectIndex--;
                this._highlightItem(this._completionListElement.childNodes[this._selectIndex]);
                ev.preventDefault();
            }
        }
        else if (k === Sys.UI.Key.down) {
            if (this._selectIndex < (this._completionListElement.childNodes.length - 1)) {
                this._selectIndex++;
                this._highlightItem(this._completionListElement.childNodes[this._selectIndex]);
                ev.preventDefault();
            }
        }
        else if (k === Sys.UI.Key.enter) {
            if (this._selectIndex !== -1) {
                this._setText(this._completionListElement.childNodes[this._selectIndex].firstChild.nodeValue);
                ev.preventDefault();
            }
        }
        
        else if (k === Sys.UI.Key.tab) {
            if (this._selectIndex !== -1) {
                if (this._completionListElement.childNodes[this._selectIndex] !== null
                    && typeof(this._completionListElement.childNodes[this._selectIndex]) != "undefined") {
                    this._setText(this._completionListElement.childNodes[this._selectIndex].firstChild.nodeValue);
                    this._timer.set_enabled(true);
                }
            }
        }
        
        if (k !== Sys.UI.Key.tab) {
            this._timer.set_enabled(true);
        }
    }

    function CMACGM$AutoCompleteBehavior$_onLostFocus() {
        this._timer.set_enabled(false);
        this._hideCompletionList();
    }

    function CMACGM$AutoCompleteBehavior$_onMethodComplete(result, context, methodName) {
        this._update(context, result,  true);
    }
    
    function CMACGM$AutoCompleteBehavior$_onMethodFailed(err, response, context) {
            }

    function CMACGM$AutoCompleteBehavior$_onTimerTick(sender, eventArgs) {
        if (this._serviceURL && this._serviceMethod) {
            var text = this.get_element().value;
            
            if (text.trim().length < this._minimumPrefixLength) {
                this._currentPrefix = null;
                this._update('', null,  false);
                return;
            }
            
            if (this._currentPrefix !== text) {
                this._currentPrefix = text;
                if (this._cache && this._cache[text]) {
                    this._update(text, this._cache[text],  false);
                    return;
                }
            
                                                                        Sys.Net._WebMethod._invoke(this, this._serviceMethod, this._serviceMethod, false,
                                        { prefixText : this._currentPrefix, count: this._completionSetCount },
                                        Function.createDelegate(this, this._onMethodComplete),
                                        Function.createDelegate(this, this._onMethodFailed),
                                        text);
            }
        }
    }

    function CMACGM$AutoCompleteBehavior$_setText(text) {
        this._timer.set_enabled(false);
        this._currentPrefix = text;
        var element = this.get_element();
        var control = element.control;
                if (control && Sys.Preview.UI.TextBox.isInstanceOfType(control)) {
            control.set_text(text);
        }
        else {
            element.value = text;
        }
        this._hideCompletionList();
    }

    function CMACGM$AutoCompleteBehavior$_update(prefixText, completionItems, cacheResults) {
        if (cacheResults && this.get_enableCaching()) {
            if (!this._cache) {
                this._cache = {};
            }
            this._cache[prefixText] = completionItems;
        }

        this._completionListElement.innerHTML = '';
        this._selectIndex = -1;
        if (completionItems && completionItems.length) {
            for (var i = 0; i < completionItems.length; i++) {
                var itemElement = document.createElement('div');
                itemElement.appendChild(document.createTextNode(completionItems[i]));
                itemElement.__item = '';
                
                var itemElementStyle = itemElement.style;
                itemElementStyle.padding = '2px';
                // ***
                itemElementStyle.textAlign = 'left';
                itemElementStyle.textOverflow = 'ellipsis';
                itemElementStyle.backgroundColor = 'window';
                itemElementStyle.color = 'windowtext';
                this._completionListElement.appendChild(itemElement);
            }
            //AutoSelect de la première valeur
            if (this._completionListElement.childNodes[0] != null) {
                this._selectIndex = 0;
                this._highlightItem(this._completionListElement.childNodes[this._selectIndex]);
            }
            this._popupBehavior.show();
        }
        else {
            this._popupBehavior.hide();
        }
    }
CMACGM.AutoCompleteBehavior.prototype = {
    _serviceURL: null,
    _serviceMethod: null,
    _minimumPrefixLength: 3,
    _completionSetCount: 10,
    _completionInterval: 1000,
    _completionListElement: null,
    _popupBehavior: null,
    _timer: null,
    _cache: null,
    _currentPrefix: null,
    _selectIndex: null,
    _focusHandler: null,
    _blurHandler: null,
    _keyDownHandler: null,
    _mouseDownHandler: null,
    _mouseUpHandler: null,
    _mouseOverHandler: null,
    _tickHandler: null,
    _enableCaching: true,
    
    get_completionInterval: CMACGM$AutoCompleteBehavior$get_completionInterval,
    set_completionInterval: CMACGM$AutoCompleteBehavior$set_completionInterval,
    
    get_completionList: CMACGM$AutoCompleteBehavior$get_completionList,
    set_completionList: CMACGM$AutoCompleteBehavior$set_completionList,
    
    get_completionSetCount: CMACGM$AutoCompleteBehavior$get_completionSetCount,
    set_completionSetCount: CMACGM$AutoCompleteBehavior$set_completionSetCount,
    
    get_minimumPrefixLength: CMACGM$AutoCompleteBehavior$get_minimumPrefixLength,
    set_minimumPrefixLength: CMACGM$AutoCompleteBehavior$set_minimumPrefixLength,
    
    get_serviceMethod: CMACGM$AutoCompleteBehavior$get_serviceMethod,
    set_serviceMethod: CMACGM$AutoCompleteBehavior$set_serviceMethod,
    
    get_serviceURL: CMACGM$AutoCompleteBehavior$get_serviceURL,
    set_serviceURL: CMACGM$AutoCompleteBehavior$set_serviceURL,
    
    get_enableCaching: CMACGM$AutoCompleteBehavior$get_enableCaching,
    set_enableCaching: CMACGM$AutoCompleteBehavior$set_enableCaching,
    
    _get_path: CMACGM$AutoCompleteBehavior$_get_path,
    get_timeout: CMACGM$AutoCompleteBehavior$get_timeout,

    dispose: CMACGM$AutoCompleteBehavior$dispose,
    
    initializeTimer: CMACGM$AutoCompleteBehavior$initializeTimer,
    
    initializeTextBox: CMACGM$AutoCompleteBehavior$initializeTextBox,
    
    initializeCompletionList: CMACGM$AutoCompleteBehavior$initializeCompletionList,
    
    initialize: CMACGM$AutoCompleteBehavior$initialize,
    
    _hideCompletionList: CMACGM$AutoCompleteBehavior$_hideCompletionList,
    
    _highlightItem: CMACGM$AutoCompleteBehavior$_highlightItem,
    
    _onListMouseDown: CMACGM$AutoCompleteBehavior$_onListMouseDown,
    
    _onListMouseUp: CMACGM$AutoCompleteBehavior$_onListMouseUp,
    
    _onListMouseOver: CMACGM$AutoCompleteBehavior$_onListMouseOver,

    _onGotFocus: CMACGM$AutoCompleteBehavior$_onGotFocus,
    
    _onKeyDown: CMACGM$AutoCompleteBehavior$_onKeyDown,
    
    _onLostFocus: CMACGM$AutoCompleteBehavior$_onLostFocus,
    
    _onMethodComplete: CMACGM$AutoCompleteBehavior$_onMethodComplete,
    _onMethodFailed: CMACGM$AutoCompleteBehavior$_onMethodFailed,
    
    _onTimerTick: CMACGM$AutoCompleteBehavior$_onTimerTick,
    
    _setText: CMACGM$AutoCompleteBehavior$_setText,
    
    _update: CMACGM$AutoCompleteBehavior$_update    
}
CMACGM.AutoCompleteBehavior.descriptor = {
    properties: [   {name: 'completionInterval', type: Number},
                    {name: 'completionList', type: Sys.UI.DomElement},
                    {name: 'completionSetCount', type: Number},
                    {name: 'minimumPrefixLength', type: Number},
                    {name: 'serviceMethod', type: String},
                    {name: 'serviceURL', type: String},
                    {name: 'enableCaching', type: Boolean } ]
}
CMACGM.AutoCompleteBehavior.registerClass('CMACGM.AutoCompleteBehavior', Sys.UI.Behavior);

Sys.Application.notifyScriptLoaded();