﻿Type.registerNamespace('Klokservice.ServerControls');

Klokservice.ServerControls.StyledAutoCompleteBehavior = function(element)
{
	Klokservice.ServerControls.StyledAutoCompleteBehavior.initializeBase(this, [element]);
}

Klokservice.ServerControls.StyledAutoCompleteBehavior.prototype =
{
	/*initialize : function()
	{
		Klokservice.ServerControls.StyledAutoCompleteBehavior.callBaseMethod(this, 'initialize');
	},

	dispose : function()
	{
		lokservice.ServerControls.StyledAutoCompleteBehavior.callBaseMethod(this, 'dispose');
	},*/

	get_itemTemplate : function()
	{
		return this._itemTemplate;
	},

	set_itemTemplate : function(value)
	{
		this._itemTemplate = value;
	},
	
	get_completionListWidth : function()
	{
		return this._completionListWidth;
	},
	
	set_completionListWidth : function (value)
	{
		this._completionListWidth = 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());        
            if ( this._completionListWidth != '' ) {
				this._completionListElement.style.width = this._completionListWidth + 'px';
			} else {
				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();
        }
    },
    
    _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();
    }
}

Klokservice.ServerControls.StyledAutoCompleteBehavior.registerClass('Klokservice.ServerControls.StyledAutoCompleteBehavior', AjaxControlToolkit.AutoCompleteBehavior);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();