﻿Type.registerNamespace("Telerik.Web.UI");

Telerik.Web.UI.RadComboBoxItem = function()
{
	Telerik.Web.UI.RadComboBoxItem.initializeBase(this);
}

Telerik.Web.UI.RadComboBoxItem.prototype =
{
	_shouldInitializeChild : function (childNode)
	{
		return false;
	},

    get_text: function()
    {
        if (this._text !== null) return this._removeEmTags(this._text);

        if ((this._text = this._properties.getValue("text", null)) != null)
            return this._removeEmTags(this._text);
		
		if (!this.get_element())
			return "";
		
		var textElement = this.get_textElement();
		if (!textElement)
			return "";

		if (typeof(textElement.innerText) != "undefined")
			this._text = textElement.innerText;
		else
			this._text = textElement.textContent;
			
		if($telerik.isSafari2)
		{
			this._text = textElement.innerHTML;
		}
		
        return this._removeEmTags(this._text);
    },

    get_baseText: function()
    {
        return Telerik.Web.UI.RadComboBoxItem.callBaseMethod(this, 'get_text');
    },

    _removeEmTags: function(text)
    {
        var indexEm=text.indexOf("<em>");
	    var indexEndEm=text.indexOf("</em>");
    	        
	    if(indexEm >= 0 && indexEndEm > indexEm)
	     {
	         text = String.format("{0}{1}{2}",text.substr(0,indexEm),text.substr(indexEm+4,indexEndEm-indexEm-4),text.substr(indexEndEm+5));  
	     }
	     return text;
    },

    set_visible: function(value)
    {
        var valueChanged = this.get_visible() != value;
		if (!valueChanged)
		{
			return;
		}
		
		Telerik.Web.UI.RadComboBoxItem.callBaseMethod(this, 'set_visible', [value]);
		if(value)
            this.get_element().style.display="";
        else
            this.get_element().style.display="none";
    },

	_markText : function(text)
	{
        var combo = this.get_comboBox();
        
	    var itemText = this.get_baseText();
	    var newText = this.get_text();
	    if (!combo.get_isTemplated())
        {
	        this.set_text(newText);
	        
	        this.get_element().innerHTML = Telerik.Web.UI.RadComboBox.htmlEncode(newText);
	    }
	    itemText =  newText;
	    var index = itemText.toLowerCase().indexOf(text.toLowerCase());
	    
	    var firstCondition = combo.get_filter()==Telerik.Web.UI.RadComboBoxFilter.Contains && index >= 0;
	    var secondCondition = combo.get_filter()==Telerik.Web.UI.RadComboBoxFilter.StartsWith && index == 0;
	    if(firstCondition ||secondCondition)
	    {
	        if(text!="" && !combo.get_isTemplated())
	        {
	            var markedTextTemplate = "{0}<em>{1}</em>{2}";
	            
	            var textBeforeFirstEmTag = itemText.substr(0, index);
	            var textBetweenEmTags = itemText.substring(index, index + text.length);
	            var textAfterSecondEmTag = itemText.substr(index + text.length);
	            
	            var newUnencodedText = String.format(markedTextTemplate, textBeforeFirstEmTag, textBetweenEmTags, textAfterSecondEmTag);
	            
	            this.set_text(newUnencodedText);
	            
	            this.get_element().innerHTML = 
	                String.format(
	                    markedTextTemplate,
	                    Telerik.Web.UI.RadComboBox.htmlEncode(textBeforeFirstEmTag),
	                    Telerik.Web.UI.RadComboBox.htmlEncode(textBetweenEmTags),
	                    Telerik.Web.UI.RadComboBox.htmlEncode(textAfterSecondEmTag)
	                );
	        }
	        
	        this.set_visible(true);
	    }
	    else 
	    {
	        this.set_visible(false);
	    
	    }
	},
	
	_render : function (html)
	{
		html[html.length] = "<li class='";
		if (this.get_enabled())
		{
			if(this.get_isSeparator())
			{
				html[html.length] = "rcbItem rcbSeparator'>";
			}
			else
			{
				html[html.length] = "rcbItem'>";
			}
		}
		else
		{
			html[html.length] = "rcbDisabled'>";
		}
		if (this.get_imageUrl()) 
		{
			this._renderImage(html);
		}
		html[html.length] = this.get_text();
		html[html.length] = "</li>";
	},
	
	_renderImage: function(html)
	{
		html[html.length] = "<img alt='' src='" + this.get_imageUrl() + "' class='rcbImage'";
		
		if (!this.get_enabled()) 
		{
			html[html.length] = " disabled='disabled'";
		}
		
		html[html.length] = "/>";
		
		return html;
	},
	
	_updateImageSrc : function ()
	{
		var newSrc = this.get_imageUrl();
	
		
		if (!this.get_enabled() && this.get_disabledImageUrl())
		{
			newSrc = this.get_disabledImageUrl();
		}
			
		if (newSrc && this.get_element())
		{
			var imageElement = this.get_imageElement();		
			if (!imageElement)
			{
				imageElement = this._createImageElement();
			}

		    newSrc = newSrc.replace(/&amp;/ig, "&");
			if (newSrc != imageElement.src)
			{
				imageElement.src = newSrc;
			}
		}
	},

	_createImageElement : function()
	{
		this._imageElement = document.createElement("img");		
		this._imageElement.className = "rcbImage";	
		if (!this.get_enabled())
		{
			this._imageElement.disabled = "disabled";
		}
		
		var imageParent = this.get_element();
		if (imageParent.firstChild)
		{
			imageParent.insertBefore(this._imageElement, imageParent.firstChild);
		}
		else
		{
			imageParent.appendChild(this._imageElement);
		}
		
		return this._imageElement;
	},
	
	get_imageElement: function()
	{
		if (!this._imageElement)
		{
			var element = this.get_element();
			
			this._imageElement = $telerik.getFirstChildByTagName(element, "img", 0);
		}
		
		return this._imageElement;
	},
	get_disabledImageUrl : function ()
	{
		return this._properties.getValue("disabledImageUrl", null);
	},
	
	set_disabledImageUrl : function (value)
	{
		this._properties.setValue("disabledImageUrl", value, true);
		this._updateImageSrc();
	},
	
	get_imageUrl: function()
	{
		if(this._imageUrl = this._properties.getValue("imageUrl", null))
			return this._imageUrl;
	
		if (!this._imageUrl) 
		{
			var imageElement = this.get_imageElement();
			if (imageElement) 
			{
				this._imageUrl = imageElement.src;
			}
		}
		
		return this._imageUrl;
	},
	
	set_imageUrl : function (value)
	{
		this._imageUrl = value;
		this._properties.setValue("imageUrl", value, true);
		this._updateImageSrc();
	},
	get_value : function ()
	{
		return this._properties.getValue("value", "");
	},
	
	select : function ()
	{
		
		this._select(null);
	},
	
	hide: function()
	{
		this.set_visible(false);
	},
	
	show: function()
	{
		this.set_visible(true);
	},
	
	_select : function (e)
	{
		
		if (!this.get_isEnabled()|| this.get_isSeparator())
			return;
			
		var comboBox = this.get_comboBox();
	    if(comboBox.raise_selectedIndexChanging(this, e) == true)
		{
            return;
        }
        
        var text = comboBox.get_text();
        
		var lastSeparatorIndex = comboBox._getLastSeparatorIndex(text);
		//Append the item text after the last separator
		var textToSet = text.substring(0, lastSeparatorIndex + 1) + this.get_text();
		
		comboBox.set_text(textToSet);
		comboBox.set_originalText(textToSet);
		comboBox.set_value(this.get_value());
		comboBox.set_selectedItem(this);  
		comboBox.set_selectedIndex(this.get_index());
		
		this.set_selected(true);
		this.highlight();
    
		comboBox.raise_selectedIndexChanged(this, e);
        var command = {Command:"Select",Index: this.get_index()};
        comboBox.postback(command);
	},
	_createChildControls : function ()
	{
		//this._items = this._createItemCollection();
	},
	
	unHighlight : function ()
	{   
	    var combobox = this.get_comboBox();
	    if (!combobox.get_isTemplated() || combobox.get_highlightTemplatedItems())
        {
            this._replaceCssClass(this.get_element(),"rcbHovered","rcbItem")	
	    }
    	
	    combobox.set_highlightedItem(null);
	},
	
	highlight : function ()
	{
	    if (!this.get_isEnabled()|| this.get_isSeparator())
			return;
			
		var combobox = this.get_comboBox();
	    if (!combobox.get_isTemplated() || combobox.get_highlightTemplatedItems())
        {
                var highlightedItem = combobox.get_highlightedItem();
                
		        if (highlightedItem)
		        {
			        highlightedItem.unHighlight();
		        }
    		    
                var element = this.get_element();
                
			    if (element)
			    {	
				    this._replaceCssClass(element,"rcbItem","rcbHovered")	
			    }
    		
        }
	    combobox.set_highlightedItem(this);
	},
	
	scrollOnTop : function ()
	{
		var itemScroll = this.get_element().offsetTop;
		var combo = this.get_comboBox();
		var header = combo._getHeaderElement();
		if(header)
		{
		  itemScroll= itemScroll - header.offsetHeight;
		}
		combo.get_childListElementWrapper().scrollTop = itemScroll;
	},
	
	scrollIntoView : function ()
	{
		var itemOffset = this.get_element().offsetTop;
		var itemHeight = this.get_element().offsetHeight;
		var dropDownWrapper = this.get_comboBox().get_childListElementWrapper();
		var dropDownOffset = dropDownWrapper.scrollTop;
		var dropDownVisibleHeight = dropDownWrapper.offsetHeight;
		
		if (itemOffset + itemHeight > dropDownOffset + dropDownVisibleHeight)
		{
			dropDownWrapper.scrollTop = itemOffset + itemHeight - dropDownVisibleHeight;

            //If a horizontal scrollbar exists, add its height to the scrollTop.
			if (dropDownWrapper.clientWidth < dropDownWrapper.scrollWidth)
			{
			    var horizontalScrollBarHeight = 
			        Telerik.Web.UI.RadComboBox._getScrollBarWidth(); //The scrollbar's width is equal to its height.
			    
			    dropDownWrapper.scrollTop += horizontalScrollBarHeight;
			}
		}
		else if (itemOffset + itemHeight <= dropDownOffset)
		{
			dropDownWrapper.scrollTop = itemOffset;
		}
	},
	
	nextItem : function ()
	{
		return this.get_comboBox().get_items().getItem(this.get_index() + 1);
	},
	
	_replaceCssClass : function (element, oldClass, newClass)
	{
		element.className = element.className.replace(oldClass, newClass);
	},
	
	_createChildListElement : function ()
	{
		var childListElement = document.createElement("ul");
		this.get_combobox().get_dropDownElement().appendChild(childListElement);
		
	},	
	
	set_selected : function (value)
	{
		this._properties.setValue("selected", value);
	},
	
	get_selected : function ()
	{
		return this._properties.getValue("selected", false);
	},	
	
	get_highlighted : function ()
	{
		var comboBox = this.get_comboBox();
		if (!comboBox) return false;
		
		return comboBox.get_highlightedItem() == this;
	},
	
	disable: function()
	{
		this.set_enabled(false);
		this.get_element().className = "rcbDisabled";
	},
	
	enable: function()
	{
		this.set_enabled(true);
		this.get_element().className = "rcbItem";
	},

    set_enabled: function(value)
    {
        this._properties.setValue("enabled", value, true);
        this._updateImageSrc();
    },
    
	get_textElement : function ()
	{
		return this.get_element();
	},
	
	get_comboBox : function ()
	{
		return this._parent;
	},
	
	_getHierarchicalIndex : function()
	{
		return this.get_index();
	},	
	
	get_isSeparator: function()
	{
		return this._properties.getValue("isSeparator", false);
	},
	
	set_isSeparator : function (value)
	{
		this._properties.setValue("isSeparator", value, true);
		if(this.get_element())
	      Sys.UI.DomElement.toggleCssClass(this.get_element(),"rcbSeparator");
	}
}

Telerik.Web.UI.RadComboBoxItem.registerClass('Telerik.Web.UI.RadComboBoxItem', Telerik.Web.UI.ControlItem);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();