var TAB_STATUS_FRONT = 1;
var TAB_STATUS_BACK = 2;

// Globals
var _Tab_Groups = new Object ();
var _Next_Id = 1;

/*
 * TextTabObject (group name, tab name, page name,
 *						on style, off style, hover style,
 *						onClick callback, the default)
 * ------------------------------------------------------
 * Create a Tab object.
 * group name must be the ID of the container div!
 */
function TextTabObject (groupName, tabName, pageName, onStyle, offStyle, hoverStyle, onClickCallback, theDefault)
{
	// Set properties
	this.groupName = groupName;
	this.tabName = tabName;
	this.pageName = pageName;
	this.onStyle = onStyle;
	this.offStyle = offStyle;
	this.hoverStyle = hoverStyle;
	this.theDefault = theDefault;
	this.id = _Next_Id++;
	this.status = 0;

	var topOffset = 0;
	var leftOffset = 0;

	// Grab the objects
	this.pageObject = GrabId (pageName);
	this.tabObject = GrabId (tabName);
	if (!this.pageObject)
	{
		alert ("this.pageObject is broken!");
	}
	if (!this.tabObject)
	{
		alert ("this.tabObject is broken!");
	}
	
	// Set methods
	this.toFront = TextTabToFront;
	this.toBack = TextTabToBack;
	this.onMouseOver = TextTab_OnMouseOver;
	this.onMouseOut = TextTab_OnMouseOut;
	this.onClick = TextTab_OnClick;

	// Set callbacks if specified
	if (onClickCallback)
	{
		this.has_onClick_Callback = true;
		this.onClick_Callback = onClickCallback;
	}
	else
	{
		this.has_onClick_Callback = false;
	}

	// Add the tab into the group
	if (!(_Tab_Groups[this.groupName]))
	{
		_Tab_Groups[this.groupName] = new Object ();
	}
	if (!(_Tab_Groups[this.groupName].tabs))
	{
		_Tab_Groups[this.groupName].tabs = new Object();
	}
	_Tab_Groups[this.groupName].tabs[this.tabName] = this;
	this.myGroup = _Tab_Groups[this.groupName];
	if (!(this.myGroup.container))
	{
		this.myGroup.container = GrabId (this.groupName);
	}

	topOffset = Number (this.myGroup.container.offsetTop) + pageYadjust;
	leftOffset = Number (this.myGroup.container.offsetLeft) + pageXadjust;
	this.pageObject.style.top = topOffset.toString() + "px";
	this.pageObject.style.left = leftOffset.toString() + "px";

	if (this.theDefault == true)
	{
		this.toFront ();
		this.myGroup.defaultTab = this;
	}
	else
	{
		this.toBack ();
	}
}

/*
 * TextTabToFront ()
 * ------------
 * Move this tab object to the front
 */
function TextTabToFront ()
{
	this.status = TAB_STATUS_FRONT;
	this.pageObject.style.visibility = "visible";
	this.tabObject.className = this.onStyle;
	
	if (this.myGroup.currentTab)
	{
		if (this.myGroup.currentTab.id == this.id)
		{
			return true;
		}
		this.myGroup.currentTab.toBack();
	}
	this.myGroup.currentTab = this;

	return true;
}

/*
 * TextTabToBack ()
 * ------------
 * Move this tab object to the back
 */
function TextTabToBack ()
{
	var defaultTab = null;
	
	this.status = TAB_STATUS_BACK;
	this.pageObject.style.visibility = "hidden";
	this.tabObject.className = this.offStyle;
	
	return true;
}

/*
 * TextTab_OnMouseOver ()
 * ------------
 * Event handler for mouse over
 */
function TextTab_OnMouseOver ()
{
	this.tabObject.className = this.hoverStyle;
	
	return true;
}

/*
 * TextTab_OnMouseOut ()
 * ------------
 * Event handler for mouse out
 */
function TextTab_OnMouseOut ()
{
	if (this.status == TAB_STATUS_FRONT)
	{
		this.tabObject.className = this.onStyle;
	}
	else
	{
		this.tabObject.className = this.offStyle;
	}
	
	return true;
}

/*
 * TextTab_OnClick ()
 * ------------
 * Event handler for mouse over
 */
function TextTab_OnClick ()
{
	this.toFront ();
	
	if (this.has_onClick_Callback)
	{
		return this.onClick_Callback (this);
	}
	else
	{
		return true;
	}
}

/* HideAllTabs */
function HideAllTabs ()
{
	for (i in _Tab_Groups)
	{
		_Tab_Groups[i].currentTab.toBack ();
	}
	
	return true;
}
