function TabControl(pane)
{
	this.pane = pane;
	this.anchorsPane;
	this.tabs = new Array();
	this.activeTab;
	this.newContainer = false;
	
	this.initialize = function()
	{
		if (this.pane == undefined)
		{
			var id = "tabContainer" + Math.floor(Math.random() * 1000001);
            document.write("<div id=\"" + id + "\" class=\"tabContainer\"></div>");
			this.pane = document.getElementById(id);
			this.newContainer = true;
		}
		
		this.anchorsPane = document.createElement("div");
		this.anchorsPane.className = "tabAnchorContainer";
        this.pane.insertBefore(this.anchorsPane, this.pane.firstChild);
	}
	
	this.createTab = function(element, caption)
	{
		if (this.newContainer)
		{
            element.parentNode.removeChild(element);
            this.pane.appendChild(element);
        }
		return new Tab(element, caption, this);
	}
	
	this.register = function(tab)
	{
		var active = this.tabs.push(tab) == 1;
		if (active)
		{
			this.activeTab = tab;
		}
		tab.toggle(active);
	}
	
	this.activate = function(tab)
	{
		this.activeTab.toggle(false);
		tab.toggle(true);
		this.activeTab = tab;
	}
	
	this.activateByCaption = function(caption)
	{
		for (var iTabs = 0 ; iTabs < this.tabs.length ; iTabs++)
		{
			if (this.tabs[iTabs].caption == caption)
			{
				this.activate(this.tabs[iTabs]);
				break;
			}
		}
	}

	this.createTabsFromChildren = function(nodeName, captionNodeName)
	{
		var elements = document.getElementsByTagName(nodeName);
		for (iElements = 0 ; iElements < elements.length ; iElements++)
		{
			var element = elements[iElements];
			var caption = null;
			for (var iChildNodes = 0 ; iChildNodes < element.childNodes.length ; iChildNodes++)
			{
				var childNode = element.childNodes[iChildNodes];
				if (childNode.nodeName.toLowerCase() == captionNodeName)
				{
					caption = childNode.firstChild.nodeValue;
					element.removeChild(childNode);
					break;
				}
			}
			this.createTab(element, caption);
		}
	}
	
	this.initialize();
}

function Tab(pane, caption, tabControl)
{
	this.pane = pane;
	this.caption = caption;
    this.tabControl = tabControl;
    this.anchor;
	this.active = false;
	
	this.initialize = function()
	{
		this.pane.className += " tabPane";

        this.anchor = document.createElement("a");
		this.anchor.href = "#";
        this.anchor.onclick = "return false";
        this.anchor.appendChild(document.createTextNode(this.caption));

		this.tabControl.anchorsPane.appendChild(this.anchor);

        this.tabControl.register(this);

        Event.attach(this.anchor, "click", this.onclick, this);
	}
	
	this.toggle = function(active)
	{
		this.pane.style.display = (active)? "block" : "none";

        //this.pane.style.position = (active)? "relative" : "absolute";
        //this.pane.style.visibility = (active)? "visible" : "hidden";

        this.anchor.className = "tabAnchor";
		if (active)
		{
			this.anchor.className += " tabAnchorActive";
		}
        this.active = active;
	}
	
	this.onclick = function()
	{
		if (!this.visible)
		{
			this.tabControl.activate(this);
		}
	}

	this.initialize();
}
