﻿Type.registerNamespace("Telerik.Web.UI");

Telerik.Web.UI.PropertyBag = function (owner)
{
    Telerik.Web.UI.PropertyBag.initializeBase(this);
    this._data = {};
    this._owner = owner;
}

Telerik.Web.UI.PropertyBag.prototype = {
  getValue : function (propertyName, defaultValue)
  {
    var value = this._data[propertyName];
    
    if (typeof(value) === "undefined")
        return defaultValue;
    
    return value;
  },
  
  setValue : function (propertyName, value, notify)
  {
    this._data[propertyName] = value;
    
    if (notify)
        this._owner._notifyPropertyChanged(propertyName, value);
  },
  
  load : function (data)
  {
    this._data = data;
  }
};

Telerik.Web.UI.PropertyBag.registerClass('Telerik.Web.UI.PropertyBag');

Telerik.Web.UI.ControlItem = function()
{
	this._element = null;
	this._parent = null;
	this._text = null;
	this._children = null;
	this._childControlsCreated = false;
	this._itemData = null;
	this._control = null;
	this._properties = new Telerik.Web.UI.PropertyBag(this);
	this._attributes = new Telerik.Web.UI.AttributeCollection(this);
}

Telerik.Web.UI.ControlItem.prototype = 
{
	_shouldNavigate: function()
	{
		var navigateUrl = this.get_navigateUrl();
		if (!navigateUrl) return false;
		
		return !navigateUrl.endsWith("#");
	},
	
	_getNavigateUrl : function ()
	{
		if (this.get_linkElement())
		    return this._properties.getValue("navigateUrl", this.get_linkElement().getAttribute("href", 2));
		
		return this._properties.getValue("navigateUrl", null);
	},
	
	_initialize: function(json, element)
	{
		this.set_element(element);
		
		this._properties.load(json);
		
		if (typeof(json["attributes"]) != "undefined")
			this._attributes._load(json["attributes"]);
			
		this._itemData = json["items"];
	},
		
	_dispose: function()
	{
		if (this._children) 
			this._children.forEach(function(child) { child._dispose(); } );
		
		if (this._element) 
		{
			this._element._item = null;
			this._element = null;
		}
		
		if (this._control) 
			this._control = null;
	},


	// Public methods

	_initializeRenderedItem: function()
	{
		var childItems = this._children;
		if (!childItems || childItems.get_count() < 1) 
			return;
		
		var childElements = this._getChildElements();
		Sys.Debug.assert(childItems.get_count() == childElements.length, "Length of elements and child items must be the same!");
		
		for (var i = 0, length = childItems.get_count(); i < length; i++) 
		{
			var childItem = childItems.getItem(i);
			if (!childItem.get_element()) 
			{
				childItem.set_element(childElements[i]);
				if (this._shouldInitializeChild(childItem)) 
				{
					childItem._initializeRenderedItem();
				}
			}
		}
	},
	
	get_attributes : function ()
	{
		return this._attributes;
	},
	
	// Public properties
	
	get_element: function()
	{
		return this._element;
	},
	
	set_element: function(value)
	{
		this._element = value;
		this._element._item = this;
		this._element._itemTypeName = Object.getTypeName(this);
	},
	
	get_parent: function()
	{
		return this._parent;
	},
	
	set_parent: function(value)
	{
		this._parent = value;
	},
	
	get_text : function ()
	{
		 if (this._text !== null) return this._text;

        if (this._text = this._properties.getValue("text", ""))
            return 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._text;
	},

	set_text : function (text)
	{ 
	    var textElement = this.get_textElement();
	    if (textElement)
			textElement.innerHTML = text;
	    
	    this._text = text;
	    this._properties.setValue("text", text, true);	    
	},
		
	get_value: function()
	{
		return this._properties.getValue("value", null);
	},
	
	set_value: function(value)
	{
		this._properties.setValue("value", value, true);
	},
	
	get_itemData: function()
	{
		return this._itemData;
	},
	
	get_index : function ()
	{
		if (!this.get_parent())
			return -1;
			
		return this.get_parent()._getChildren().indexOf(this);
	},
	
	set_enabled: function(value)
	{
		this._properties.setValue("enabled", value, true);
	},
	
	get_enabled: function()
	{
		return this._properties.getValue("enabled", true) == true;
	},
	
	get_isEnabled : function()
	{
		var control = this._getControl();
		if (control)
			return control.get_enabled() && this.get_enabled();
		
		return this.get_enabled();
	},
	
	set_visible: function(value)
	{
		this._properties.setValue("visible", value);
	},
	
	get_visible: function()
	{
		return this._properties.getValue("visible", true);
	},
	
	get_level : function ()
	{
		var parent = this.get_parent();
		var level = 0;
			
		while (parent) 
		{
			
			if (Telerik.Web.UI.ControlItemContainer.isInstanceOfType(parent)) 
			{
				return level;
			}
			
			level++;
			parent = parent.get_parent();
		}
		
		return level;
	},
	
	get_isLast : function()
	{
		return this.get_index() == this.get_parent()._getChildren().get_count() - 1;
	},
	
	get_isFirst : function()
	{
		return this.get_index() == 0;
	},
	
	get_nextSibling : function ()
	{
		if (!this.get_parent())
			return null;
		return this.get_parent()._getChildren().getItem(this.get_index() + 1);
	},
	
	get_previousSibling : function ()
	{
		if (!this.get_parent())
			return null;
		
		return this.get_parent()._getChildren().getItem(this.get_index() - 1);
	},
	
	// Private methods
	_getHierarchicalIndex : function () 
	{
		var indexes = [];
		var current = this;
		
		while (!Telerik.Web.UI.ControlItemContainer.isInstanceOfType(current))
		{
			Array.insert(indexes, 0, current.get_index());
			current = current.get_parent();
		}
		
		return indexes.join(":");
	},
	
	_getChildren : function()
	{
		this._ensureChildControls();
		return this._children;
	},
	
	_ensureChildControls: function()
	{
		if (!this._childControlsCreated) 
		{
			this._createChildControls();
			this._childControlsCreated = true;
		}
	},

	_setCssClass : function(element, cssClass)
	{
		if (element.className != cssClass)
			element.className = cssClass;
	},

	_createChildControls: function()
	{
		this._children = this._createItemCollection();
	},
	
	_createItemCollection : function()
	{
	},
	
	_getControl: function()
	{
		if (!this._control) 
		{
			var parent = this.get_parent();
			
			if (parent) 
			{
				if (Telerik.Web.UI.ControlItemContainer.isInstanceOfType(parent)) 
				{
					this._control = parent;
				}
				else 
				{
					this._control = parent._getControl();
				}
			}
		}
		
		return this._control;
	},
	
	_getAllItems: function()
	{
		var items = [];
		this._getAllItemsRecursive(items, this);
		return items;
	},
	
	_getAllItemsRecursive: function(items, currentItem)
	{
		var children = currentItem._getChildren();
		for (var i = 0; i < children.get_count(); i++) 
		{
			var child = children.getItem(i);
			Array.add(items, child);
			this._getAllItemsRecursive(items, child);
		}
	},
	
	_getData: function()
	{
		var data = this._properties._data;
		
		//we don't need this - it comes from the initial JSON
		delete data.items;	
		
		data["text"] = this.get_text()
		
		if (this.get_attributes().get_count() > 0)
			data["attributes"] = this.get_attributes()._data;
		
		return data;
	},
	
	_notifyPropertyChanged: function(property, value)
	{
		var control = this._getControl();
		if (control) 
			control._itemPropertyChanged(this, property, value);
	},
	
	_loadFromDictionary : function (data)
	{
		if (typeof(data.Text) != "undefined")
			this.set_text(data.Text);

		if (typeof(data.Value) != "undefined" && data.Value !== "")
			this.set_value(data.Value);
		
		if (typeof(data.Enabled) != "undefined" && data.Enabled !== true)
			this.set_enabled(data.Enabled);

		var attributes = this.get_attributes();
		for (var attributeKey in data.Attributes)
		{
			attributes.setAttribute(attributeKey, data.Attributes[attributeKey]);
		}
	},
	
	_createDomElement: function()
	{
		var itemElementParent = document.createElement("ul");
		var html = [];
		
		this._render(html);
		
		itemElementParent.innerHTML = html.join("");
		return itemElementParent.firstChild;
	}
}

Telerik.Web.UI.ControlItem.registerClass('Telerik.Web.UI.ControlItem');


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();