Type.registerNamespace('CustomAjax.UI');

CustomAjax.UI.AutoCompleteBehavior = function Sys$UI$AutoCompleteBehavior(element) {
    /// <param name="element" type="Sys.UI.DomElement"></param>
    var e = Function._validateParams(arguments, [
        {name: "element", domElement: true}
    ]);
    if (e) throw e;

    CustomAjax.UI.AutoCompleteBehavior.initializeBase(this, [element]);
}

    function Sys$UI$AutoCompleteBehavior$get_completionInterval() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._completionInterval;
    }
    function Sys$UI$AutoCompleteBehavior$set_completionInterval(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._completionInterval = value;
    }

    function Sys$UI$AutoCompleteBehavior$get_completionList() {
        /// <value type="Sys.UI.DomElement"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._completionListElement;
    }
    function Sys$UI$AutoCompleteBehavior$set_completionList(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Sys.UI.DomElement}]);
        if (e) throw e;

        this._completionListElement = value;
    }

    function Sys$UI$AutoCompleteBehavior$get_completionSetCount() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._completionSetCount;
    }
    function Sys$UI$AutoCompleteBehavior$set_completionSetCount(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._completionSetCount = value;
    }

    function Sys$UI$AutoCompleteBehavior$get_minimumPrefixLength() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._minimumPrefixLength;
    }
    function Sys$UI$AutoCompleteBehavior$set_minimumPrefixLength(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._minimumPrefixLength = value;
    }
    
    function Sys$UI$AutoCompleteBehavior$get_noMinimumPrefix() {
        /// <value type="Boolean"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._noMinimumPrefix;
    }
    function Sys$UI$AutoCompleteBehavior$set_noMinimumPrefix(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Boolean}]);
        if (e) throw e;

        this._noMinimumPrefix = value;
    }
    
    function Sys$UI$AutoCompleteBehavior$get_separatorChar() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._separatorChar;
    }
    function Sys$UI$AutoCompleteBehavior$set_separatorChar(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;

        this._separatorChar = value;
    }

    function Sys$UI$AutoCompleteBehavior$get_serviceMethod() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._serviceMethod;
    }
    function Sys$UI$AutoCompleteBehavior$set_serviceMethod(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;

        this._serviceMethod = value;
    }

    function Sys$UI$AutoCompleteBehavior$get_serviceURL() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._serviceURL;
    }
    function Sys$UI$AutoCompleteBehavior$set_serviceURL(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;

        this._serviceURL = value;
    }

    function Sys$UI$AutoCompleteBehavior$get_enableCaching() {
        /// <value type="Boolean"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._enableCaching;
    }
    function Sys$UI$AutoCompleteBehavior$set_enableCaching(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Boolean}]);
        if (e) throw e;

        this._enableCaching = value;
    }


    function Sys$UI$AutoCompleteBehavior$_get_path() {
        return this.get_serviceURL();
    }

    function Sys$UI$AutoCompleteBehavior$get_timeout() {
        /// <value type="Number"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return 0;
    }

    function Sys$UI$AutoCompleteBehavior$dispose() {
        if(!this._timer) { CustomAjax.UI.AutoCompleteBehavior.callBaseMethod(this, 'dispose'); return; }
        
        this._timer = null;

        var element = this.get_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;
        
        CustomAjax.UI.AutoCompleteBehavior.callBaseMethod(this, 'dispose');
    }

    function Sys$UI$AutoCompleteBehavior$initializeTimer(timer) {
        timer.set_interval(this._completionInterval);
        timer.add_tick(this._tickHandler);
    }

    function Sys$UI$AutoCompleteBehavior$initializeTextBox(element) {
        element.autocomplete = "off";
        $addHandler(element, "focus", this._focusHandler);
        $addHandler(element, "blur", this._blurHandler);
        $addHandler(element, "keydown", this._keyDownHandler);
    }

    function Sys$UI$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());        
        var w = elementBounds.width - 2;
        w= 200; // DHØ : Hack to make this work in IE6..crashes on width < 0
        completionListStyle.width = w + 'px';
        
        $addHandler(element, "mousedown", this._mouseDownHandler);
        $addHandler(element, "mouseup", this._mouseUpHandler);
        $addHandler(element, "mouseover", this._mouseOverHandler);
    }

    function Sys$UI$AutoCompleteBehavior$initialize() {
        CustomAjax.UI.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.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 AjaxControlToolkit.PopupBehavior(this._completionListElement);
        this._popupBehavior.set_parentElement(element);
        this._popupBehavior.set_positioningMode(AjaxControlToolkit.PositioningMode.BottomLeft);
        this._popupBehavior.initialize();
        popupControl.initialize();
    }

    function Sys$UI$AutoCompleteBehavior$_hideCompletionList() {
        this._popupBehavior.hide();
        this._completionListElement.innerHTML = '';
        this._selectIndex = -1;
    }

    function Sys$UI$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';
                child.style.color = 'windowtext';
            }
        }
        item.style.backgroundColor = 'highlight';
        item.style.color = 'highlighttext';
    }

    function Sys$UI$AutoCompleteBehavior$_onListMouseDown(ev) {
        if (ev.target !== this._completionListElement) {
                var tag = ev.target.firstChild.nodeValue;
                var newTag = tag.replace(/\s\([0-9]+\)/,"");
                this._setText(newTag);
                
                ev.preventDefault();

                var btn = document.getElementById("ctl00_ctl00_BodyCPH_MainCPH_SearchTagButton");
                if (btn) btn.click();                
        }
    }

    function Sys$UI$AutoCompleteBehavior$_onListMouseUp(ev) {
        this.get_element().focus();
    }

    function Sys$UI$AutoCompleteBehavior$_onListMouseOver(ev) {
        var item = ev.target;
        this._selectIndex = -1;
        this._highlightItem(item);
    }

    function Sys$UI$AutoCompleteBehavior$_onGotFocus(ev) {
        this._timer.set_enabled(true);
    }

    function Sys$UI$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"] || k === Sys.UI.Key["tab"]) {
            if (this._selectIndex !== -1) {
                var tag = this._completionListElement.childNodes[this._selectIndex].firstChild.nodeValue;
                var newTag = tag.replace(/\s\([0-9]+\)/,"");
                this._setText(newTag);
                
                ev.preventDefault();
            }
            var btn = document.getElementById("ctl00_ctl00_BodyCPH_MainCPH_SearchTagButton");
            if (btn) btn.click();
        }
        
        if (k !== Sys.UI.Key.tab) {
            this._timer.set_enabled(true);
        }
    }

    function Sys$UI$AutoCompleteBehavior$_onLostFocus() {
        this._timer.set_enabled(false);
        this._hideCompletionList();
    }

    function Sys$UI$AutoCompleteBehavior$_onMethodComplete(result, context, methodName) {
        this._update(context, result,  true);
    }
    function Sys$UI$AutoCompleteBehavior$_onMethodFailed(err, response, context) {
            }

    function Sys$UI$AutoCompleteBehavior$_onTimerTick(sender, eventArgs) {
        if (this._serviceURL && this._serviceMethod) {
            var text = this.get_element().value;
            
            /**************************************************/
            if ( text.lastIndexOf(this._separatorChar) > -1 ) 
            {
                // found separator char in the text
                var pos = text.lastIndexOf(this._separatorChar);
                pos++;
                text = text.substring(pos, (text.length));
                text = text.trim();
            }            
            
            if ( !this._noMinimumPrefix && (text.trim().length < this._minimumPrefixLength) ) {
                this._update('', null,  false);
                return;
            }
            
            /**************************************************/
            
            var updateText = false;
            if ( this._noMinimumPrefix && !this._popupOpened ) updateText = true;
            else if ( !this._noMinimumPrefix && this._currentPrefix != text ) updateText = true;
            else if ( this._noMinimumPrefix && this._popupOpened && this._currentPrefix != text ) updateText = true;
            
            if (updateText) {
                this._currentPrefix = text;
                if (this._cache && this._cache[text]) {
                    this._update(text, this._cache[text],  false);
                    return;
                }
                Sys.Net.WebServiceProxy.invoke
                    (this._serviceURL, this._serviceMethod, false,
                    { prefixText : this._currentPrefix, count: this._completionSetCount },
                    Function.createDelegate(this, this._onMethodComplete),
                    Function.createDelegate(this, this._onMethodFailed),
                    text);
            }
        }
    }

    function Sys$UI$AutoCompleteBehavior$_setText(text) {
        this._timer.set_enabled(false);
        this._currentPrefix = text;
        var element = this.get_element();
        
        var control = element.control;        
        if (control && Sys.UI.TextBox.isInstanceOfType(control)) {
            control.set_text(text);
        }
        else {
            //element.value = text;
            var currentValue = element.value;
            if ( currentValue.lastIndexOf(this._separatorChar) > -1 ) 
            {
                // found separator char in the text
                var pos = currentValue.lastIndexOf(this._separatorChar);
                pos++;
                currentValue = currentValue.substring(0, pos) + text;
            } 
            else 
            {
                // no separator char found
                currentValue = text;
            }
            // Change MalSmith 1/15/2007
            var s = this._separatorChar;
            if(s == '|') s = '';
            element.value = currentValue + s;
        }
        
        this._hideCompletionList();
    }

    function Sys$UI$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 = '1px';
                itemElementStyle.textAlign = 'left';
                itemElementStyle.textOverflow = 'ellipsis';
                itemElementStyle.backgroundColor = 'window';
                itemElementStyle.color = 'windowtext';
                
                this._completionListElement.appendChild(itemElement);
            }
            this._popupBehavior.show();
            this._popupOpened = true;
        }
        else {
            this._popupBehavior.hide();
            this._popupOpened = false;
        }
    }
CustomAjax.UI.AutoCompleteBehavior.prototype = {
    _popupOpened: false,
    _serviceURL: null,
    _serviceMethod: null,
    _minimumPrefixLength: 3,
    _completionSetCount: 10,
    _completionInterval: 1000,
    _completionListElement: null,
    _separatorChar: ',',
    _noMinimumPrefix: false,
    _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: Sys$UI$AutoCompleteBehavior$get_completionInterval,
    set_completionInterval: Sys$UI$AutoCompleteBehavior$set_completionInterval,
    
    get_completionList: Sys$UI$AutoCompleteBehavior$get_completionList,
    set_completionList: Sys$UI$AutoCompleteBehavior$set_completionList,
    
    get_completionSetCount: Sys$UI$AutoCompleteBehavior$get_completionSetCount,
    set_completionSetCount: Sys$UI$AutoCompleteBehavior$set_completionSetCount,
    
    get_minimumPrefixLength: Sys$UI$AutoCompleteBehavior$get_minimumPrefixLength,
    set_minimumPrefixLength: Sys$UI$AutoCompleteBehavior$set_minimumPrefixLength,
    
    get_noMinimumPrefix: Sys$UI$AutoCompleteBehavior$get_noMinimumPrefix,
    set_noMinimumPrefix: Sys$UI$AutoCompleteBehavior$set_noMinimumPrefix,
    
    get_separatorChar: Sys$UI$AutoCompleteBehavior$get_separatorChar,
    set_separatorChar: Sys$UI$AutoCompleteBehavior$set_separatorChar,
    
    get_serviceMethod: Sys$UI$AutoCompleteBehavior$get_serviceMethod,
    set_serviceMethod: Sys$UI$AutoCompleteBehavior$set_serviceMethod,
    
    get_serviceURL: Sys$UI$AutoCompleteBehavior$get_serviceURL,
    set_serviceURL: Sys$UI$AutoCompleteBehavior$set_serviceURL,
    
    get_enableCaching: Sys$UI$AutoCompleteBehavior$get_enableCaching,
    set_enableCaching: Sys$UI$AutoCompleteBehavior$set_enableCaching,
    
        _get_path: Sys$UI$AutoCompleteBehavior$_get_path,
        get_timeout: Sys$UI$AutoCompleteBehavior$get_timeout,

    dispose: Sys$UI$AutoCompleteBehavior$dispose,
    
    initializeTimer: Sys$UI$AutoCompleteBehavior$initializeTimer,
    
    initializeTextBox: Sys$UI$AutoCompleteBehavior$initializeTextBox,
    
    initializeCompletionList: Sys$UI$AutoCompleteBehavior$initializeCompletionList,
    
    initialize: Sys$UI$AutoCompleteBehavior$initialize,
    
    _hideCompletionList: Sys$UI$AutoCompleteBehavior$_hideCompletionList,
    
    _highlightItem: Sys$UI$AutoCompleteBehavior$_highlightItem,
    
    _onListMouseDown: Sys$UI$AutoCompleteBehavior$_onListMouseDown,
    
    _onListMouseUp: Sys$UI$AutoCompleteBehavior$_onListMouseUp,
    
    _onListMouseOver: Sys$UI$AutoCompleteBehavior$_onListMouseOver,

    _onGotFocus: Sys$UI$AutoCompleteBehavior$_onGotFocus,
    
    _onKeyDown: Sys$UI$AutoCompleteBehavior$_onKeyDown,
    
    _onLostFocus: Sys$UI$AutoCompleteBehavior$_onLostFocus,
    
    _onMethodComplete: Sys$UI$AutoCompleteBehavior$_onMethodComplete,
    _onMethodFailed: Sys$UI$AutoCompleteBehavior$_onMethodFailed,
    
    _onTimerTick: Sys$UI$AutoCompleteBehavior$_onTimerTick,
    
    _setText: Sys$UI$AutoCompleteBehavior$_setText,
    
    _update: Sys$UI$AutoCompleteBehavior$_update    
}
CustomAjax.UI.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: 'appURL', type: String },
                    {name: 'enableCaching', type: Boolean } ,
                    {name: 'separatorChar', type: String},
                    {name: 'noMinimumPrefix', type: Boolean},
    ]
}
CustomAjax.UI.AutoCompleteBehavior.registerClass('CustomAjax.UI.AutoCompleteBehavior', Sys.UI.Behavior);

if (Sys && Sys.Application) { Sys.Application.notifyScriptLoaded(); }