var CUR_REGEX = /^[^#]*#([^?]*)?.*$/
var TAB_REGEX = /\bvtab\b/;
var CONTENT_REGEX = /\bvtabcontent\b/;

var getTabName = function(url, id)
{
	var str = CUR_REGEX.exec(url);
	if(str && str.length > 1)
	{
		var entries = str[1].split(',');
		var regex = new RegExp('^' + id + ':');
		for(var i=0; i<entries.length; ++i)
		{
			if(regex.test(entries[i]))
			{
				return entries[i];
			}
		}
	}
	return '';
};



function VTab()
{
// private variables
	var _tabs = null;
	var _contents = null;
	var _current = null;


// private functions
	var _click = function(e)
	{
		e = e || event;
		var target = e.target || e.srcElement;

		_current = getTabName(target.href, _tabs.id);
		_refresh();

		if(e["preventDefault"]) { e.preventDefault(); }
		return false;
	};
	var _refresh = function()
	{
		var li = getFirstChild(_tabs);
		while(li != null)
		{
			var anchor = getFirstChild(li);
			if(anchor && getTabName(anchor.href, _tabs.id) == _current)
			{
				anchor.style.color = "#013c7d";
				li.style.backgroundPosition = "0px -192px";

				li = getPrevSibling(li);
				if(li){ li.style.backgroundPosition = "left -216px" };

				li = getNextSibling(getNextSibling(li));
				if(li){ li.style.backgroundPosition = "right -240px" };
			}
			else if(anchor)
			{
				anchor.style.color = "white";
				anchor.style.fontWeight = "normal";
				li.style.backgroundPosition = "0px -30px";

				li = getPrevSibling(li);
				if(li){ li.style.backgroundPosition = "left -114px" };

				li = getNextSibling(getNextSibling(li));
				if(li){ li.style.backgroundPosition = "right -138px" };
			}

			li = getNextSibling(li);
		}

		li = getFirstChild(_contents);
		while(li != null)
		{
			var anchor = getFirstChild(li);
			if(anchor && anchor.name != _current)
			{
				li.style.display = "none";
			}
			else
			{
				li.style.display = "inline";
			}
			li = getNextSibling(li);
		}
	};


// accessors
	this.setTabs = function(tabs)
	{
		_tabs = tabs;
	};
	this.setContents = function(contents)
	{
		_contents = contents;
	};


// set up
	this.initialize = function()
	{
		_current = getTabName(document.location.href, _tabs.id);
		if(_current == '' && _tabs && _tabs.children.length > 0)
		{
			var li = getFirstChild(_tabs);
			while(li != null)
			{
				var anchor = getFirstChild(li);
				if(anchor)
				{
					_current = getTabName(anchor.href, _tabs.id);
					li = null;
				}
				else
				{
					li = getNextSibling(li);
				}
			}
		}

		_refresh();

		addEventListener(_tabs, "click", _click, false);
	};
}



var vtabs = {};
var lists = document.getElementsByTagName("ul");
for(var i=0; i<lists.length; ++i)
{
	var id = lists[i].id.replace("content", '');

	if(TAB_REGEX.test(lists[i].className))
	{
		var vtab = new VTab();
		vtab.setTabs(lists[i]);
		vtabs[id] = vtab;
	}

	else if(CONTENT_REGEX.test(lists[i].className) && !undef(vtabs[id]))
	{
		vtabs[id].setContents(lists[i]);
		vtabs[id].initialize();
	}
}
vtabs = null;
lists = null;


/*
var vtab =
{
	tabs : document.getElementById("vtabs"),
	contents : document.getElementById("vtabcontents"),
	current : 0,

	init : function()
	{
		var href = document.location.href;
		var section = href.substring(href.lastIndexOf('#'));
		section = section.charAt(0) === '#' ? section.substring(1) : '';

		var sections = this.contents.getElementsByTagName("li");
		for(var node in this.contents.childNodes)
		{
			node.style.display = "none";
		}

		addEventListener(this.tabs, "click", this.click, false);
	}
};

vtab.init();
*/
