/**
* Classes included in this file:
*     TabControl : Control
*     TabPage : VisibleControl
*     TabFiller : SimpleControl
* Enumerators included in this file:
*     TabScrollbars
*     TabLocation
* This file requires the use of the following files:
*     base.js
*     base_classes.js
*
* TODO: Add multi-line tab support.
* TODO: Get the vertical-align working for the text in the tabs.
* TODO: Add left and right column tab support.
**/
TabScrollbars = {};
TabScrollbars.None   = 'hidden';
TabScrollbars.Always = 'scroll';
TabScrollbars.Auto   = 'auto';
TabScrollbars.NoClip = 'visible';
TabLocation = {};
TabLocation.TopLeft      = 1;
TabLocation.TopCenter    = 2;
TabLocation.TopRight     = 3;
TabLocation.BottomLeft   = 4;
TabLocation.BottomCenter = 5;
TabLocation.BottomRight  = 6;
//TabLocation.Left         = 7;
//TabLocation.Right        = 8;
TabLocation.AnyTop = function(p_tabLocation) { return (p_tabLocation == TabLocation.TopLeft || p_tabLocation == TabLocation.TopCenter || p_tabLocation == TabLocation.TopRight); }
TabLocation.AnyBottom = function(p_tabLocation) { return (p_tabLocation == TabLocation.BottomLeft || p_tabLocation == TabLocation.BottomCenter || p_tabLocation == TabLocation.BottomRight); }
TabLocation.AnyTBLeft = function(p_tabLocation) { return (p_tabLocation == TabLocation.TopLeft || p_tabLocation == TabLocation.BottomLeft); }
TabLocation.AnyTBRight = function(p_tabLocation) { return (p_tabLocation == TabLocation.TopRight || p_tabLocation == TabLocation.BottomRight); }
TabLocation.AnyCenter = function(p_tabLocation) { return (p_tabLocation == TabLocation.TopCenter || p_tabLocation == TabLocation.BottomCenter); }
/****************************************************************************************/
function TabDisplayInfo(p_background, p_cursor, p_font, p_foreColor, p_textAlign, p_border, p_padding)
{
	this.Background = p_background;
	this.Border     = p_border;
	this.Cursor     = p_cursor;
	this.Font       = p_font;
	this.ForeColor  = p_foreColor;
	this.Padding    = p_padding;
	this.TextAlign  = p_textAlign;
	return;
}
TabDisplayInfo.prototype.GetType = function() { return 'TabDisplayInfo'; }
TabDisplayInfo.prototype.GetHashCode = function() { return this.Background.GetHashCode() + this.Border.GetHashCode() + this.Cursor + this.Font.GetHashCode() + this.ForeColor + this.Padding.GetHashCode() + this.TextAlign; }
TabDisplayInfo.prototype.toString = function() { return this.ToString(); }
TabDisplayInfo.prototype.ToString = function() { return '[Object: TabDisplayInfo]'; }
TabDisplayInfo.prototype.SetCssDomProperties = function(p_tab, p_tabText)
{
	p_tab.style.background = this.Background.ToCssString();
	p_tab.style.cursor = this.Cursor;
	p_tab.style.color = this.ForeColor;
	this.Border.SetCssDomProperties(p_tab);

	p_tabText.style.font = this.Font.ToCssString();
	p_tabText.style.padding = this.Padding.ToCssString();
	ContentAlignment.SetCssDomProperties(p_tabText, this.TextAlign);
	return;
}
/****************************************************************************************/
function TabControl(p_elementId, p_instanceName)
{
	TabControl.baseConstructor.call(this, p_elementId, p_instanceName);
	this.SelectedIndex  = 0;
	this.Scrollbars     = TabScrollbars.Auto;
	this.Tabs           = Array();
	this.TabsFillTop    = false;
	this.TabLocation    = TabLocation.TopLeft;
	this.TabSpacing     = 10;
	this.TabSize        = new Size(0, 20);
	this.TabLeftFiller  = new TabFiller(this, 'TabLeftFiller', HorizontalAlignment.Left);
	this.TabFiller      = new TabFiller(this, 'TabFiller', HorizontalAlignment.Center);
	this.TabRightFiller = new TabFiller(this, 'TabRightFiller', HorizontalAlignment.Right);

	this.UseTabMouseOverEffects = true;
	this.TabArea              = new TabDisplayInfo(new Background('#FFFFFF'), 'default', this.DefaultFont(), '#000000', ContentAlignment.TopLeft, new RectangleBorder(0, BorderStyle.None, '#FFFFFF'), new Padding(0, 0, 0, 0));
	this.TabActive            = new TabDisplayInfo(new Background('#FFFFFF'), 'pointer', this.DefaultFont(), '#000000', ContentAlignment.MiddleCenter, this.DefaultBorder(), new Padding(2, 2, 0, 2));
	this.TabInactive          = new TabDisplayInfo(new Background('#CCCCCC'), 'pointer', this.DefaultFont(), '#666666', ContentAlignment.MiddleCenter, this.DefaultBorder(), new Padding(2, 2, 0, 2));
	this.TabActiveMouseOver   = new TabDisplayInfo(new Background('#FFFFFF'), 'pointer', this.DefaultFont(), '#000000', ContentAlignment.MiddleCenter, this.DefaultBorder(), new Padding(2, 2, 0, 2));
	this.TabInactiveMouseOver = new TabDisplayInfo(new Background('#CCCCCC'), 'pointer', this.DefaultFont(), '#666666', ContentAlignment.MiddleCenter, this.DefaultBorder(), new Padding(2, 2, 0, 2));
	this.TabActive.Border.Bottom.Set(0, BorderStyle.None, '#FFFFFF');

	this.TabPageChanged = null;
	return;
}
Extend(TabControl, Control);
TabControl.prototype.GetType = function() { return 'TabControl'; }
TabControl.prototype.ToString = function() { return 'TabControl: ' + this.Name; }
TabControl.prototype.DefaultSize = function() { return new Size(0, 300); }
TabControl.prototype.OnTabPageChanged = function(p_oldPage, p_newPage) { if(this.TabPageChanged) { this.TabPageChanged(this, p_oldPage, p_newPage); } return; }
TabControl.prototype.AddTabFromDiv = function(p_elementId, p_text, p_toolTipText, p_visible, p_identifier)
{
	if(!p_elementId || p_elementId.length <= 0) { throw ArgumentNullException('p_elementId'); }
	if(!p_text || p_text.length <= 0) { throw ArgumentNullException('p_text'); }
	p_visible = p_visible ? true : false;
	var v_element = Utils.GetElement(p_elementId);
	return this.AddTab(p_text, v_element.innerHTML, p_toolTipText, p_visible, p_identifier);
}
TabControl.prototype.AddTab = function(p_tabPageOrText, p_content, p_toolTipText, p_visible, p_identifier)
{
	if(!p_tabPageOrText) { throw ArgumentNullException('p_tabPageOrText'); }
	if(TypeOf(p_tabPageOrText) == 'String' && p_tabPageOrText.length <= 0) { throw ArgumentNullException('p_tabPageOrText'); }
	p_toolTipText = p_toolTipText ? String(p_toolTipText) : '';
	p_visible = p_visible ? true : false;
	if(TypeOf(p_tabPageOrText) == 'String')
	{
		var v_tab = new TabPage(p_tabPageOrText, p_content, p_toolTipText, p_identifier);
		this.TabArea.Background.CopyTo(v_tab.Background);
		this.TabArea.Border.CopyTo(v_tab.Border);
		this.TabArea.Font.CopyTo(v_tab.Font);
		this.TabArea.Padding.CopyTo(v_tab.Padding);
		v_tab.Cursor    = this.TabArea.Cursor;
		v_tab.ForeColor = this.TabArea.ForeColor;
		v_tab.TextAlign = this.TabArea.TextAlign;
		return v_tab.AssignParent(this, p_visible);
	}
	else if(TypeOf(p_tabPageOrText) == 'TabPage') { return p_tabPageOrText.AssignParent(this, p_visible); }
	throw InvalidArgumentException('p_tabPageOrText');
	return -1;
}
TabControl.prototype.FindTabPage = function(p_tabPageOrIdentifier)
{
	if(!p_tabPageOrIdentifier) { throw ArgumentNullException('p_tabPageOrIdentifier'); }
	var v_tabmode = TypeOf(p_tabPageOrIdentifier) == 'TabPage';
	for(var i = 0; i < this.Tabs.length; i++)
	{
		if(v_tabmode && this.Tabs[i] == p_tabPageOrIdentifier) { return i; }
		else if(this.Tabs[i].Identifier == p_tabPageOrIdentifier) { return i; }
	}
	return -1;
}
TabControl.prototype.SetCursor = function(p_cursor)
{
	if(TypeOf(p_cursor) != 'String') { throw InvalidArgumentException('p_cursor'); }
	this.Cursor = p_cursor;
	if(this.IsRendered) { Utils.GetElement(this.ElementId + '_tabarea').style.cursor = this.Cursor; }
	return;
}
TabControl.prototype.SetActiveTabCursor = function(p_cursor)
{
	if(TypeOf(p_cursor) != 'String') { throw InvalidArgumentException('p_cursor'); }
	this.TabActive.Cursor = p_cursor;
	if(this.IsRendered) { for(var i = 0; i < this.Tabs.length; i++) { if(this.Tabs[i].Active) { Utils.GetElement(this.Tabs[i].ElementId + '_tab').style.cursor = this.TabActive.Cursor; } } }
	return;
}
TabControl.prototype.SetInactiveTabCursor = function(p_cursor)
{
	if(TypeOf(p_cursor) != 'String') { throw InvalidArgumentException('p_cursor'); }
	this.TabInactive.Cursor = p_cursor;
	if(this.IsRendered) { for(var i = 0; i < this.Tabs.length; i++) { if(!this.Tabs[i].Active) { Utils.GetElement(this.Tabs[i].ElementId + '_tab').style.cursor = this.TabInactive.Cursor; } } }
	return;
}
TabControl.prototype.Refresh = function()
{
    if(TypeOf(this.Scrollbars) == 'Boolean') { this.Scrollbars = this.Scrollbars ? TabScrollbars.Always : TabScrollbars.None; }
	var v_tab_filler_width = this._GetTabFillerWidth();

	// Refresh the left tab filler if it is visible.
	if(!this.TabsFillTop && (TabLocation.AnyTBRight(this.TabLocation) || TabLocation.AnyCenter(this.TabLocation)))
	{
		this.TabLeftFiller.Size.Set(v_tab_filler_width, this.TabSize.Height);
		this.TabLeftFiller.Refresh();
	}

	// Refresh the right tab filler if it is visible.
	else if(!this.TabsFillTop && (TabLocation.AnyTBLeft(this.TabLocation) || TabLocation.AnyCenter(this.TabLocation)))
	{
		this.TabRightFiller.Size.Set(v_tab_filler_width, this.TabSize.Height);
		this.TabRightFiller.Refresh();
	}

	// Refresh the global tab control properties (tab control main table).
	var v_tabarea = Utils.GetElement(this.ElementId + '_tabarea');
	v_tabarea.style.width = this.Size.Width <= 0 ? '100%' : this.Size.Width.ToCssString();
	v_tabarea.style.background = this.Background.ToCssString();
	v_tabarea.style.font = this.Font.ToCssString();
	v_tabarea.style.color = this.ForeColor;
	v_tabarea.style.cursor = this.Cursor;

	// Refresh the tab content area container.
	var v_contentarea = Utils.GetElement(this.ElementId + '_contentarea');
	v_contentarea.style.padding = this.Padding.ToCssString();
	if(TabLocation.AnyTop(this.TabLocation)) { v_contentarea.style.borderBottom = this.Border.Bottom.ToCssString(); }
	else { v_contentarea.style.borderTop = this.Border.Top.ToCssString(); }
	v_contentarea.style.borderLeft = this.Border.Left.ToCssString();
	v_contentarea.style.borderRight = this.Border.Right.ToCssString();
	v_contentarea.style.height = (this.Size.Height <= 0 ? '' : (this.Size.Height - this.TabSize.Height).ToCssString());

	// Setup the tab filler properties for refreshing all of the tab fillers.
	this.TabFiller.Size.Set(1, this.TabSize.Height);
	this.TabFiller.Text = '<div style="overflow: hidden; width: ' + this.TabSpacing.ToCssString() + '">&nbsp;</div>';

	// Refresh all of the tabs, tab fillers, and tab content.
	for(var i = 0; i < this.Tabs.length; i++)
	{
		if(this.Tabs[i].Visible)
		{
			this.Tabs[i].Refresh();
			if(i != this.Tabs.length - 1)
			{
				this.TabFiller.ElementId = this.ElementId + '_tab_filler_' + i.toString();
				this.TabFiller.Refresh();
			}
		}
	}
	return;
}
TabControl.prototype.Render = function()
{
    if(TypeOf(this.Scrollbars) == 'Boolean') { this.Scrollbars = this.Scrollbars ? TabScrollbars.Always : TabScrollbars.None; }
	if(!this.Tabs[this.SelectedIndex].Visible)
	{
		// Find the first visible tab and make it the active tab.
		for(var i = 0; i < this.Tabs.length; i++) { if(this.Tabs[i].Visible) { this.Tabs[i].MakeActive(); break; } }
	}

	// Setup the tab filler properties for render operations.
	this.TabFiller.Size.Set(1, this.TabSize.Height);
	this.TabFiller.Text = '<div style="overflow: hidden; width: ' + this.TabSpacing.ToCssString() + '">&nbsp;</div>';

	// Create the tab HTML table.
	var v_sb = new StringBuilder('<table border="0" cellpadding="0" cellspacing="0" style="');
	v_sb.Append(Utils.CreateCssPropertyString('width', this.Size.Width <= 0 ? '100%' : this.Size.Width.ToCssString()))
		.Append(Utils.CreateCssPropertyString('height', this.Size.Height <= 0 ? '100%' : this.Size.Height.ToCssString()))
		.Append(Utils.CreateCssPropertyString('background', this.Background.ToCssString()))
		.Append(Utils.CreateCssPropertyString('font', this.Font.ToCssString()))
		.Append(Utils.CreateCssPropertyString('color', this.ForeColor))
		.Append(Utils.CreateCssPropertyString('cursor', this.Cursor))
		.Append('" id="').Append(this.ElementId).Append('_tabarea"');
	this.GetEventAttributeString(v_sb);
	v_sb.Append('>');

	// Render the tab bar if it is displayed on top.
	if(TabLocation.AnyTop(this.TabLocation)) { this._RenderTabBarHorizontal(v_sb); }

	// Render the tab content area container.
	v_sb.Append('<tr><td id="').Append(this.ElementId).Append('_contentarea" style="vertical-align: top;')
		//.Append(Utils.CreateCssPropertyString('width', this.Size.Width <= 0 ? '100%' : this.Size.Width.ToCssString()))
		//.Append(Utils.CreateCssPropertyString('height', this.Size.Height <= 0 ? '100%' : (this.Size.Height - this.TabSize.Height).ToCssString()))
		.Append(Utils.CreateCssPropertyString('border-left', this.Border.Left.ToCssString()));
	if(TabLocation.AnyTop(this.TabLocation)) { v_sb.Append(Utils.CreateCssPropertyString('border-bottom', this.Border.Bottom.ToCssString())); }
	else { v_sb.Append(Utils.CreateCssPropertyString('border-top', this.Border.Top.ToCssString())); }
	v_sb.Append(Utils.CreateCssPropertyString('border-right', this.Border.Right.ToCssString()))
		.Append(Utils.CreateCssPropertyString('padding', this.Padding.ToCssString()))
		.Append('" colspan="').Append(((this.GetVisibleTabCount() * 2) - (this.TabsFillTop ? 1 : 0) + (this.TabLocation == TabLocation.TopCenter || this.TabLocation == TabLocation.BottomCenter ? 1 : 0)).toString()).Append('">');

	// Render all of the tab content divs.
	for(var i = 0; i < this.Tabs.length; i++) { if(this.Tabs[i].Visible) { this.Tabs[i].RenderContent(v_sb); } }

	v_sb.Append('</td></tr>');

	// Render the tab bar if it is displayed on the bottom.
	if(TabLocation.AnyBottom(this.TabLocation)) { this._RenderTabBarHorizontal(v_sb); }

	v_sb.Append('</table>');

	var v_tc = Utils.GetElement(this.ElementId);
	v_tc.innerHTML = v_sb.ToString();
	this.IsRendered = true;
	return;
}
TabControl.prototype.GetVisibleTabCount = function() { var v_count = 0; for(var i = 0; i < this.Tabs.length; i++) { if(this.Tabs[i].Visible) { v_count++; } } return v_count; }
TabControl.prototype.GetContentAreaWidth = function() { if(this.Size.Width > 0) { return this.Size.Width - this.Padding.Left - this.Padding.Right - this.Border.Left.Width - this.Border.Right.Width; } return 0; }
TabControl.prototype.GetContentAreaHeight = function() { if(this.Size.Height > 0) { return this.Size.Height - this.TabSize.Height - this.Border.Bottom.Width; } return 0; }
TabControl.prototype._RenderTabBarHorizontal = function(p_stringBuilder)
{
	var v_tab_filler_width = this._GetTabFillerWidth();
	p_stringBuilder.Append('<tr>');
	if(!this.TabsFillTop && (TabLocation.AnyTBRight(this.TabLocation) || TabLocation.AnyCenter(this.TabLocation)))
	{
		this.TabLeftFiller.Size.Set(v_tab_filler_width, this.TabSize.Height);
		this.TabLeftFiller.Render(p_stringBuilder);
	}
	for(var i = 0; i < this.Tabs.length; i++)
	{
		if(this.Tabs[i].Visible)
		{
			this.Tabs[i].RenderTab(p_stringBuilder);
			if(i != this.Tabs.length - 1)
			{
				this.TabFiller.ElementId = this.ElementId + '_tab_filler_' + i.toString();
				this.TabFiller.Render(p_stringBuilder);
			}
		}
	}
	if(!this.TabsFillTop && (TabLocation.AnyTBLeft(this.TabLocation) || TabLocation.AnyCenter(this.TabLocation)))
	{
		this.TabRightFiller.Size.Set(v_tab_filler_width, this.TabSize.Height);
		this.TabRightFiller.Render(p_stringBuilder);
	}
	p_stringBuilder.Append('</tr>');
	return;
}
TabControl.prototype._GetTabFillerWidth = function()
{
	if(!this.TabsFillTop && (TabLocation.AnyTBLeft(this.TabLocation) || TabLocation.AnyTBRight(this.TabLocation))) { return 100 - (this.GetVisibleTabCount() * 2); }
	else if(!this.TabsFillTop && TabLocation.AnyCenter(this.TabLocation)) { return (100 - (this.GetVisibleTabCount() * 2)) / 2; }
	return 0;
}
/****************************************************************************************/
function TabFiller(p_parent, p_instanceNameWithinParent, p_placement)
{
	TabFiller.baseConstructor.call(this, p_parent.ElementId + '_' + p_instanceNameWithinParent, p_parent.Name + '.' + p_instanceNameWithinParent);
	this.Parent = p_parent;
	this.Placement = p_placement;
	return;
}
Extend(TabFiller, SimpleControl);
TabFiller.prototype.GetType = function() { return 'TabFiller'; }
TabFiller.prototype.ToString = function() { return 'TabFiller: ' + this.Name; }
TabFiller.prototype.DefaultBorder = function() { return new RectangleBorder(0, BorderStyle.None, '#FFFFFF'); }
TabFiller.prototype.SetText = function(p_value)
{
	if(p_value && TypeOf(p_value) != 'String') { throw InvalidArgumentException('p_value'); }
	this.Text = p_value;
	if(this.Parent.IsRendered) { try { Utils.GetElement(this.ElementId).innerHTML = this.Text; } catch(e) {  } }
	return;
}
TabFiller.prototype.Refresh = function()
{
	if(this.IsRendered == true)
	{
		var v_element = Utils.GetElement(this.ElementId);
		if(this.Placement == HorizontalAlignment.Left) { v_element.style.borderLeft = this.Border.Left.ToCssString(); }
		if(this.Placement == HorizontalAlignment.Right) { v_element.style.borderRight = this.Border.Right.ToCssString(); }
		if(TabLocation.AnyTop(this.Parent.TabLocation))
		{
			v_element.style.borderTop    = this.Border.Top.ToCssString();
			v_element.style.borderBottom = this.Parent.Border.Top.ToCssString();
		}
		else
		{
			v_element.style.borderTop    = this.Parent.Border.Bottom.ToCssString();
			v_element.style.borderBottom = this.Border.Bottom.ToCssString();
		}
		v_element.style.width      = this.Size.Width.toString() + '%';
		v_element.style.height     = this.Size.Height.ToCssString();
		v_element.style.background = this.Background.ToCssString();
		v_element.style.padding    = this.Padding.ToCssString();
		v_element.style.font       = this.Font.ToCssString();
		v_element.style.color      = this.ForeColor;
		v_element.style.cursor     = this.Cursor;
		ContentAlignment.SetCssDomProperties(v_element, this.TextAlign);
		v_element.title     = this.ToolTipText;
		v_element.className = this.CssClassName;
		v_element.innerHTML = this.Text == '' ? '&nbsp;' : this.Text;
	}
	return;
}
TabFiller.prototype.Render = function(v_stringBuilder)
{
	v_stringBuilder.Append('<td style="')
		.Append(Utils.CreateCssPropertyString('width', this.Size.Width.toString() + '%'))
		.Append(Utils.CreateCssPropertyString('height', this.Size.Height.ToCssString()));
	if(TabLocation.AnyTop(this.Parent.TabLocation))
	{
		v_stringBuilder.Append(Utils.CreateCssPropertyString('border-bottom', this.Parent.Border.Top.ToCssString()))
			.Append(Utils.CreateCssPropertyString('border-top', this.Border.Top.ToCssString()));
	}
	else
	{
		v_stringBuilder.Append(Utils.CreateCssPropertyString('border-top', this.Parent.Border.Bottom.ToCssString()))
			.Append(Utils.CreateCssPropertyString('border-bottom', this.Border.Bottom.ToCssString()));
	}
	if(this.Placement == HorizontalAlignment.Left) { v_stringBuilder.Append(Utils.CreateCssPropertyString('border-left', this.Border.Left.ToCssString())); }
	if(this.Placement == HorizontalAlignment.Right) { v_stringBuilder.Append(Utils.CreateCssPropertyString('border-right', this.Border.Right.ToCssString())); }
	v_stringBuilder.Append(Utils.CreateCssPropertyString('background', this.Background.ToCssString()))
		.Append(Utils.CreateCssPropertyString('padding', this.Padding.ToCssString()))
		.Append(Utils.CreateCssPropertyString('font', this.Font.ToCssString()))
		.Append(Utils.CreateCssPropertyString('color', this.ForeColor))
		.Append(Utils.CreateCssPropertyString('cursor', this.Cursor))
		.Append(ContentAlignment.CreateCssString(this.TextAlign))
		.Append(';"')
		.Append(Utils.CreateAttributeString('title', this.ToolTipText))
		.Append(Utils.CreateAttributeString('class', this.CssClassName))
		.Append(Utils.CreateAttributeString('id', this.ElementId));
	this.GetEventAttributeString(v_stringBuilder);
	v_stringBuilder.Append('>')
		.Append(this.Text == '' ? '&nbsp;' : this.Text)
		.Append('</td>');
	this.IsRendered = true;
	return;
}
/****************************************************************************************/
function TabPage(p_text, p_content, p_toolTipText, p_identifier)
{
	TabPage.baseConstructor.call(this, 'tabpage', 'tabpage');
	if(p_text && TypeOf(p_text) != 'String') { throw InvalidArgumentException('p_text'); }
	if(p_content && TypeOf(p_content) != 'String') { throw InvalidArgumentException('p_content'); }
	if(p_toolTipText && TypeOf(p_toolTipText) != 'String') { throw InvalidArgumentException('p_toolTipText'); }
	this.Identifier     = p_identifier;
	this.Text           = p_text ? String(p_text) : '';
	this.ToolTipText    = p_toolTipText ? String(p_toolTipText) : '';
	this.Content        = p_content ? String(p_content) : '';
	this.Active         = false;
	this.TabClick       = null;
	this.TabDoubleClick = null;
	this.TabMouseDown   = null;
	this.TabMouseUp     = null;
	this.TabMouseOver   = null;
	this.TabMouseMove   = null;
	this.TabMouseOut    = null;
	this.TabKeyPress    = null;
	this.TabKeyDown     = null;
	this.TabKeyUp       = null;
	return;
}
Extend(TabPage, VisibleControl);
TabPage.prototype.GetType = function() { return 'TabPage'; }
TabPage.prototype.ToString = function() { return this.Text; }
TabPage.prototype.DefaultBorder = function() { return new RectangleBorder(0, BorderStyle.None, '#FFFFFF'); }
TabPage.prototype.Render = function() { return; }
TabPage.prototype.OnTabClick = function(e, p_element) { this.MakeActive(); if(this.Click) { this.Click(e, this); } return; }
TabPage.prototype.OnTabDoubleClick = function(e, p_element) { if(this.DoubleClick) { this.DoubleClick(e, this); } return; }
TabPage.prototype.OnTabMouseDown = function(e, p_element) { if(this.MouseDown) { this.MouseDown(Utils.GetMouseLocation(e), e, this); } return; }
TabPage.prototype.OnTabMouseUp = function(e, p_element) { if(this.MouseUp) { this.MouseUp(Utils.GetMouseLocation(e), e, this); } return; }
TabPage.prototype.OnTabMouseMove = function(e, p_element) { if(this.MouseMove) { this.MouseMove(Utils.GetMouseLocation(e), e, this); } return; }
TabPage.prototype.OnTabKeyPress = function(e, p_element) { if(this.KeyPress) { this.KeyPress(e, this); } return; }
TabPage.prototype.OnTabKeyDown = function(e, p_element) { if(this.KeyDown) { this.KeyDown(e, this); } return; }
TabPage.prototype.OnTabKeyUp = function(e, p_element) { if(this.KeyUp) { this.KeyUp(e, this); } return; }
TabPage.prototype.OnTabMouseOver = function(e, p_element)
{
	if(this.Parent != null && this.Parent.IsRendered && this.Parent.UseTabMouseOverEffects)
	{
		var v_tab     = Utils.GetElement(this.ElementId + '_tab');
		var v_tabtext = Utils.GetElement(this.ElementId + '_tab_text');
		if(this.Active) { this.Parent.TabActiveMouseOver.SetCssDomProperties(v_tab, v_tabtext); }
		else { this.Parent.TabInactiveMouseOver.SetCssDomProperties(v_tab, v_tabtext); }
	}
	if(this.TabMouseOver) { this.TabMouseOver(this); }
	return;
}
TabPage.prototype.OnTabMouseOut = function(e, p_element)
{
	if(this.Parent != null && this.Parent.IsRendered && this.Parent.UseTabMouseOverEffects)
	{
		var v_tab     = Utils.GetElement(this.ElementId + '_tab');
		var v_tabtext = Utils.GetElement(this.ElementId + '_tab_text');
		if(this.Active) { this.Parent.TabActive.SetCssDomProperties(v_tab, v_tabtext); }
		else { this.Parent.TabInactive.SetCssDomProperties(v_tab, v_tabtext); }
	}
	if(this.TabMouseOut) { this.TabMouseOut(this); }
	return;
}
TabPage.prototype.AssignParent = function(p_parent, p_visible)
{
	if(!p_parent) { throw ArgumentNullException('p_parent'); }
	p_visible = p_visible ? true : false;
	var v_index = p_parent.Tabs.length;
	if(v_index == 0) { p_visible = true; }
	this.Parent = p_parent;
	this.ElementId = p_parent.ElementId + '_' + v_index.toString();
	this.Name = p_parent.Name + '.Tabs[' + v_index.toString() + ']';
	p_parent.Tabs.push(this);
	if(p_visible) { this.MakeActive(); }
	else { this.Deactivate(); }
	return v_index;
}
TabPage.prototype.MakeActive = function()
{
	if(this.Parent != null && !this.Active)
	{
		var v_oldpage = null;
		var v_index = -1;
		var v_tab = null;
		for(var i = 0; i < this.Parent.Tabs.length; i++)
		{
			v_tab = this.Parent.Tabs[i];
			if(v_tab.Active && v_tab != this) { v_oldpage = v_tab; }
			v_tab.Deactivate();
			if(v_tab == this) { v_index = i; }
		}
		this.Parent.SelectedIndex = v_index;
		this.Active = true;
		if(this.Parent.IsRendered)
		{
			this.Parent.OnTabPageChanged(v_oldpage, this)
			this.Parent.Refresh();
		}
	}
	return;
}
TabPage.prototype.Deactivate = function() { this.Active = false; return; }
TabPage.prototype.SetContentFromDiv = function(p_elementId, p_doNotRemove)
{
	if(!p_elementId || p_elementId.length <= 0) { throw ArgumentNullException('p_elementId'); }
	var v_source = Utils.GetElement(p_elementId);
	this.SetContent(v_source.innerHTML);
	if(!p_doNotRemove) { v_source.innerHTML = ''; }
	return;
}
TabPage.prototype.SetContent = function(p_value)
{
	if(p_value && TypeOf(p_value) != 'String') { throw InvalidArgumentException('p_value'); }
	this.Content = p_value;
	if(this.Parent != null && this.Parent.IsRendered && this.Visible)
	{
		var v_content = Utils.GetElement(this.ElementId + '_content');
		v_content.innerHTML = this.Content;
	}
	return;
}
TabPage.prototype.SetCursor = function(p_cursor)
{
	if(TypeOf(p_cursor) != 'String') { throw InvalidArgumentException('p_cursor'); }
	this.Cursor = p_cursor;
	if(this.Active) { Utils.GetElement(this.ElementId + '_content').style.cursor = this.Cursor; }
	return;
}
TabPage.prototype.Refresh = function()
{
	if(this.Parent != null && this.Visible)
	{
	    if(TypeOf(this.Parent.Scrollbars) == 'Boolean') { this.Parent.Scrollbars = this.Parent.Scrollbars ? TabScrollbars.Always : TabScrollbars.None; }
		var v_display_info = this.Active ? this.Parent.TabActive : this.Parent.TabInactive;

		var v_tab = Utils.GetElement(this.ElementId + '_tab');
		v_tab.title = this.ToolTipText;
		if(TabLocation.AnyTop(this.Parent.TabLocation))
		{
			v_tab.style.borderTop = v_display_info.Border.Top.ToCssString();
			if(this.Active) { v_tab.style.borderBottom = '0px none white'; }
			else { v_tab.style.borderBottom = this.Parent.Border.Top.ToCssString(); }
		}
		else
		{
			v_tab.style.borderBottom = v_display_info.Border.Bottom.ToCssString();
			if(this.Active) { v_tab.style.borderTop = '0px none white'; }
			else { v_tab.style.borderTop = this.Parent.Border.Bottom.ToCssString(); }
		}
		v_tab.style.borderLeft = v_display_info.Border.Left.ToCssString();
		v_tab.style.borderRight = v_display_info.Border.Right.ToCssString();
		v_tab.style.background = v_display_info.Background.ToCssString();
		v_tab.style.color = v_display_info.ForeColor;
		v_tab.style.cursor = v_display_info.Cursor;
		v_tab.style.padding = '0px 0px 0px 0px';

		var v_tabtext = Utils.GetElement(this.ElementId + '_tab_text');
		v_tabtext.innerHTML = this.Text;
		v_tabtext.style.padding = v_display_info.Padding.ToCssString();
		v_tabtext.style.font = v_display_info.Font.ToCssString();
		v_tabtext.style.height = this.Parent.TabSize.Height.ToCssString();
		ContentAlignment.SetCssDomProperties(v_tabtext, v_display_info.TextAlign);
		if(this.Parent.TabsFillTop)
		{
			this.Parent.TabSize.Width = 0;
			v_tab.style.width = Math.floor(100 / this.Parent.GetVisibleTabCount()).toString() + '%';
			v_tabtext.style.width = '';
			v_tabtext.style.whiteSpace = 'normal';
			v_tabtext.style.overflow = 'hidden';
		}
		else if(this.Parent.TabSize.Width > 0)
		{
			v_tab.style.width = '1%';
			v_tabtext.style.width = this.Parent.TabSize.Width.ToCssString();
			v_tabtext.style.whiteSpace = 'normal';
			v_tabtext.style.overflow = 'hidden';
		}
		else
		{
			v_tab.style.width = '1%';
			v_tabtext.style.width = '';
			v_tabtext.style.whiteSpace = 'nowrap';
			v_tabtext.style.overflow = 'visible';
		}

		var v_content = Utils.GetElement(this.ElementId + '_content');
		if(this.Active)
		{
			var v_width = this.Parent.GetContentAreaWidth();
			var v_height = this.Parent.GetContentAreaHeight();
			v_content.innerHTML = this.Content;
			v_content.style.width = v_width <= 0 ? '100%' : v_width.ToCssString();
			v_content.style.height = v_height <= 0 ? '100%' : v_height.ToCssString();
			v_content.style.overflow = this.Parent.Scrollbars;
			this.Border.SetCssDomProperties(v_content);
			v_content.style.background = this.Background.ToCssString();
			v_content.style.padding = this.Padding.ToCssString();
			v_content.style.font = this.Font.ToCssString();
			v_content.style.color = this.ForeColor;
			v_content.style.cursor = this.Cursor;
			v_content.style.display = 'block';
			v_content.style.position = 'static';
			v_content.style.left = 'auto';
			v_content.style.top = 'auto';
		}
		else
		{
			v_content.style.display = 'none';
			v_content.style.position = 'absolute';
			v_content.style.left = '0px';
			v_content.style.top = '0px';
		}
	}
	return;
}
TabPage.prototype.RenderTab = function(p_stringBuilder)
{
	if(this.Parent != null && this.Visible)
	{
		var v_display_info = this.Active ? this.Parent.TabActive : this.Parent.TabInactive;

		p_stringBuilder.Append('<td style="')
			.Append(Utils.CreateCssPropertyString('border-left', v_display_info.Border.Left.ToCssString()))
			.Append(Utils.CreateCssPropertyString('border-right', v_display_info.Border.Right.ToCssString()));
		if(TabLocation.AnyTop(this.Parent.TabLocation))
		{
			p_stringBuilder.Append(Utils.CreateCssPropertyString('border-top', v_display_info.Border.Top.ToCssString()));
			if(this.Active) { p_stringBuilder.Append(' border-bottom: 0px solid white;'); }
			else { p_stringBuilder.Append(Utils.CreateCssPropertyString('border-bottom', this.Parent.Border.Top.ToCssString())); }
		}
		else
		{
			p_stringBuilder.Append(Utils.CreateCssPropertyString('border-bottom', v_display_info.Border.Bottom.ToCssString()));
			if(this.Active) { p_stringBuilder.Append(' border-top: 0px solid white;'); }
			else { p_stringBuilder.Append(Utils.CreateCssPropertyString('border-top', this.Parent.Border.Bottom.ToCssString())); }
		}
		if(this.Parent.TabsFillTop)
		{
			this.Parent.TabSize.Width = 0;
			p_stringBuilder.Append(' width: ').Append(Math.floor(100 / this.Parent.GetVisibleTabCount()).toString()).Append('%;');
		}
		else { p_stringBuilder.Append(' width: 1%;'); }
		p_stringBuilder.Append(Utils.CreateCssPropertyString('background', v_display_info.Background.ToCssString()))
			.Append(Utils.CreateCssPropertyString('color', v_display_info.ForeColor))
			.Append(Utils.CreateCssPropertyString('cursor', v_display_info.Cursor))
			.Append('"')
			.Append(Utils.CreateAttributeString('title',       this.ToolTipText))
			.Append(Utils.CreateAttributeString('id',          this.ElementId + '_tab'))
			.Append(Utils.CreateAttributeString('onclick',     this.Name + '.OnTabClick(event, this);'))
			.Append(Utils.CreateAttributeString('ondblclick',  this.Name + '.OnTabDoubleClick(event, this);'))
			.Append(Utils.CreateAttributeString('onmousemove', this.Name + '.OnTabMouseMove(event, this);'))
			.Append(Utils.CreateAttributeString('onkeypress',  this.Name + '.OnTabKeyPress(event, this);'))
			.Append(Utils.CreateAttributeString('onkeydown',   this.Name + '.OnTabKeyDown(event, this);'))
			.Append(Utils.CreateAttributeString('onkeyup',     this.Name + '.OnTabKeyUp(event, this);'))
			.Append(Utils.CreateAttributeString('onmouseover', this.Name + '.OnTabMouseOver(event, this);'))
			.Append(Utils.CreateAttributeString('onmouseout',  this.Name + '.OnTabMouseOut(event, this);'))
			.Append(Utils.CreateAttributeString('onmousedown', this.Name + '.OnTabMouseDown(event, this);'))
			.Append(Utils.CreateAttributeString('onmouseup',   this.Name + '.OnTabMouseUp(event, this);'))
			.Append('><div style="height: ').Append(this.Parent.TabSize.Height.ToCssString())
			.Append('; padding: ').Append(v_display_info.Padding.ToCssString())
			.Append('; font: ').Append(v_display_info.Font.ToCssString()).Append(';')
			.Append(ContentAlignment.CreateCssString(v_display_info.TextAlign));
		if(this.Parent.TabsFillTop)
		{
			p_stringBuilder.Append(' white-space: normal; overflow: hidden;')
		}
		else if(this.Parent.TabSize.Width > 0)
		{
			p_stringBuilder.Append(' white-space: normal; overflow: hidden; width: ').Append(this.Parent.TabSize.Width.ToCssString()).Append(';');
		}
		else
		{
			p_stringBuilder.Append(' white-space: nowrap; overflow: visible;');
		}
		p_stringBuilder.Append('" id="').Append(this.ElementId).Append('_tab_text">').Append(this.Text).Append('</div></td>');
	}
	return;
}
TabPage.prototype.RenderContent = function(p_stringBuilder)
{
	if(this.Parent != null && this.Visible)
	{
	    if(TypeOf(this.Parent.Scrollbars) == 'Boolean') { this.Parent.Scrollbars = this.Parent.Scrollbars ? TabScrollbars.Always : TabScrollbars.None; }
		p_stringBuilder.Append('<div style="');
		if(this.Active)
		{
			var v_width = this.Parent.GetContentAreaWidth();
			var v_height = this.Parent.GetContentAreaHeight();
			p_stringBuilder.Append(Utils.CreateCssPropertyString('height', v_height <= 0 ? '100%' : v_height.ToCssString()))
				.Append(Utils.CreateCssPropertyString('width', v_width <= 0 ? '100%' : v_width.ToCssString()))
				.Append(Utils.CreateCssPropertyString('overflow', this.Parent.Scrollbars))
				.Append(this.Border.ToCssString())
				.Append(Utils.CreateCssPropertyString('background', this.Background.ToCssString()))
				.Append(Utils.CreateCssPropertyString('padding', this.Padding.ToCssString()))
				.Append(Utils.CreateCssPropertyString('font', this.Font.ToCssString()))
				.Append(Utils.CreateCssPropertyString('color', this.ForeColor))
				.Append(Utils.CreateCssPropertyString('cursor', this.Cursor));
		}
		else
		{
			p_stringBuilder.Append('display: none; position: absolute; left: 0px; top: 0px;');
		}
		p_stringBuilder.Append('"').Append(Utils.CreateAttributeString('id', this.ElementId + '_content'));
		this.GetEventAttributeString(p_stringBuilder);
		p_stringBuilder.Append('>').Append(this.Content).Append('</div>');
	}
	return;
}