function TabContainer(parentElement)
{
	this.ParentElement = parentElement;
	
	this.mvarSelectedTabID = null;
	this.GetSelectedTabID = function()
	{
		return this.mvarSelectedTabID;
	};
	
	this.SetSelectedTab = function(tab)
	{
		var tabContainer = this.ParentElement;
		if (tabContainer == null) return;
		
		var tabs = tabContainer.childNodes[0];
		var tabPages = tabContainer.childNodes[1];
		var selectedIndex = -1;
		for (var i = 0; i < tabs.childNodes.length; i++)
		{
			if (System.ClassList.Contains(tabs.childNodes[i], "uwt-selected"))
			{
				System.ClassList.Remove(tabs.childNodes[i], "uwt-selected");
			}
			
			if (tabs.childNodes[i] === tab)
			{
				selectedIndex = i;
				System.ClassList.Add(tabs.childNodes[i], "uwt-selected");
			}
		}
		for (var i = 0; i < tabPages.childNodes.length; i++)
		{
			if (selectedIndex > -1 && selectedIndex < tabPages.childNodes.length && i == selectedIndex)
			{
				System.ClassList.Add(tabPages.childNodes[i], "uwt-selected");
			}
			else
			{
				System.ClassList.Remove(tabPages.childNodes[i], "uwt-selected");
			}
		}
		
		System.SetClientProperty(this.ID, "SelectedTabIndex", selectedIndex);
		
		if (tabs.childNodes[selectedIndex] != null && tabs.childNodes[selectedIndex].attributes["data-id"] != null)
		{
			this.mvarSelectedTabID = tabs.childNodes[selectedIndex].attributes["data-id"].value;
		}
		
		var attOnClientTabChanged = tabContainer.attributes["data-onclienttabchanged"];
		if (attOnClientTabChanged != null)
		{
			eval(attOnClientTabChanged.value);
		}
	};
	
	var tabContainer = this.ParentElement;
	var tabs = tabContainer.childNodes[0];
	for (var i = 0; i < tabs.childNodes.length; i++)
	{
		(function(i, tc)
		{
			tabs.childNodes[i].addEventListener("click", function(e)
			{
				tc.SetSelectedTab(tabs.childNodes[i]);
				
				e.preventDefault();
				e.stopPropagation();
				return false;
			});
		})(i, this);
	}
	
	//eval("window." + tabContainer.attributes["id"].value + " = this;");
}

TabContainer.parse = function(elem)
{
    var tabContainer = document.createElement("div");
    tabContainer.className = "uwt-tabcontainer";
    
    var ul = document.createElement("ul");
    ul.className = "uwt-tabcontainer-tabs";
	
	var divTabPages = document.createElement("div");
	divTabPages.className = "uwt-tabcontainer-tabpages";
    
	var selectedIndex = 0;

    var tabPages1 = elem.getElementsByTagName("tabpages");
    if (tabPages1.length == 1)
    {
        var pages = tabPages1[0].children;
        for (var i = 0; i < pages.length; i++)
        {
            var li = document.createElement("li");
            if (pages[i].getAttribute("Visible") != "false")
            {
                li.className = "uwt-visible";
            }
            var a = document.createElement("a");
            
            var spanText = document.createElement("span");
            spanText.innerHTML = pages[i].getAttribute("Title");
            
            a.appendChild(spanText);
            li.appendChild(a);
            
            ul.appendChild(li);

			var div = document.createElement("div");
			div.className = "uwt-tabpage";
			div.innerHTML = pages[i].innerHTML;
			divTabPages.appendChild(div);

			if (pages[i].getAttribute("Selected") == "true")
			{
				selectedIndex = i;
			}
        }
    }

	ul.children[selectedIndex].className = "uwt-visible uwt-selected";
	divTabPages.children[selectedIndex].className = "uwt-tabpage uwt-selected";
    
    tabContainer.appendChild(ul);
    tabContainer.appendChild(divTabPages);
    
    elem.replaceWith(tabContainer);
    return tabContainer;
};

window.addEventListener("load", function(e)
{
	var tbss = document.getElementsByClassName("uwt-tabcontainer");
	for (var i = 0; i < tbss.length; i++)
	{
		tbss[i].ObjectReference = new TabContainer(tbss[i]);
	}
	
	tbss = document.getElementsByTagName("uwt:tabcontainer");
	for (var i = 0; i < tbss.length; i++)
	{
		tbss[i].ObjectReference = new TabContainer(TabContainer.parse(tbss[i]));
	}
});
