﻿Type.registerNamespace('LinkInfotec.CustomControls');

LinkInfotec.CustomControls.AutoCompletePlusBehavior = function(element)
{
LinkInfotec.CustomControls.AutoCompletePlusBehavior.initializeBase(this, [element]);
}

LinkInfotec.CustomControls.AutoCompletePlusBehavior.prototype =
{

	get_itemTemplate : function()
	{
		return this._itemTemplate;
	},

	set_itemTemplate : function(value)
	{
		this._itemTemplate = value;
	},
	
	_update: function(prefixText, completionItems, cacheResults)
	{
        if (cacheResults && this.get_enableCaching()) {
            if (!this._cache) {
                this._cache = {};
            }
            this._cache[prefixText] = completionItems;
        }
       
        if (completionItems && completionItems.length) {
            this._completionListElement.innerHTML = '';
            
            this._selectIndex = -1;
            var _firstChild = null;
        
            for (var i = 0; i < completionItems.length; i++) {
                var itemElement = null;

                if (this._completionListElementID) { 
                    // the completion element has been created by the user and li won't necessarily work
                    itemElement = document.createElement('div');
                } else {
                    itemElement = document.createElement('li');
                }
                
                // set the first child if it is null
                if( _firstChild == null ){
                    _firstChild = itemElement;
                }

				if (this._itemTemplate && Array.isInstanceOfType(completionItems[i]))
				{
					itemElement.innerHTML = this._getItemHtml(completionItems[i]);
					this._usingItemTemplate = true;
					itemElement._text = completionItems[i][0];
                }
                else
                {
					itemElement.appendChild(document.createTextNode(this._getTextWithInsertedWord(completionItems[i])));
					this._usingItemTemplate = false;
                }
				
                // itemElement.__item = '';
                
                if (this._completionListItemCssClass) {
                    Sys.UI.DomElement.addCssClass(itemElement, this._completionListItemCssClass);
                } else {
                    var itemElementStyle = itemElement.style;
                    itemElementStyle.padding = '0px';
                    itemElementStyle.textAlign = 'left';
                    itemElementStyle.textOverflow = 'ellipsis';
                    // workaround for safari since normal colors do not
                    // show well there.
                    if (Sys.Browser.agent === Sys.Browser.Safari) {
                        itemElementStyle.backgroundColor = 'white';
                        itemElementStyle.color = 'black';
                    } else {
                        itemElementStyle.backgroundColor = this._textBackground;
                        itemElementStyle.color = this._textColor;
                    }
                }
                this._completionListElement.appendChild(itemElement);
            }
            var elementBounds = $common.getBounds(this.get_element());        
            this._completionListElement.style.width = Math.max(1, elementBounds.width - 2) + 'px';
            this._completionListElement.scrollTop = 0;
            
            this.raisePopulated(Sys.EventArgs.Empty);
            
            var eventArgs = new Sys.CancelEventArgs();
            this.raiseShowing(eventArgs);
            if (!eventArgs.get_cancel()) {
                this.showPopup();
                // Check if the first Row is to be selected by default and if yes highlight it and updated selectIndex.
                if (this._firstRowSelected && (_firstChild != null)) {
                    this._highlightItem( _firstChild );
                    this._selectIndex = 0;
                }
            }            
        } else {
            this._hideCompletionList();
        }
    },
    
    _onListMouseDown: function(ev) {
return false;
   },
    
    _onLostFocus: function() {
return false;
    },

    _onKeyDown: function(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._completionListElement.childNodes[this._selectIndex].firstChild.click()
                ev.preventDefault();
                return false;
           }
        }
        else if (k === Sys.UI.Key.tab) {
            if (this._selectIndex !== -1) {
            this._completionListElement.childNodes[this._selectIndex].firstChild.click()
                return false;         
              }
        }
        else {
         this._timer.set_enabled(true);
        }
    },

    
    _getItemHtml : function(content)
    {
		var args = [this._itemTemplate];
		var length = content.length;
		for (var i = 0; i < length; i++)
		{
			args.push(content[i]);
		}
		return String.format.apply(null, args);
    },
    
    _setText: function(item)
    {
		var text = null;
		
        if (this._usingItemTemplate)
        {
			while (!item._text)
			{
				item = item.parentNode;
			}
	        
			text = this._getTextWithInsertedWord(item._text);
		}
		else
		{
			text = (item && item.firstChild) ? item.firstChild.nodeValue : null;
		}
        
        this.raiseItemSelected(new AjaxControlToolkit.AutoCompleteItemEventArgs(item, text));
        
        this._timer.set_enabled(false);

        var element = this.get_element();
        var control = element.control;
        if (control && control.set_text) {
            control.set_text(text);
            $common.tryFireEvent(control, "change");
        }
        else {
            element.value = text;
            $common.tryFireEvent(element, "change");
        }
        this._currentPrefix = this._currentCompletionWord();
        this._hideCompletionList();
    }
}

LinkInfotec.CustomControls.AutoCompletePlusBehavior.registerClass('LinkInfotec.CustomControls.AutoCompletePlusBehavior', AjaxControlToolkit.AutoCompleteBehavior);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();