// General

function isFirefox()
{
	return (navigator.userAgent.indexOf("Firefox") != -1);
}

function isIE()
{
	return (navigator.userAgent.indexOf("MSIE") != -1);
}

function escapeString(value)
{
	return value.replace("'", "\\'");
}

function deriveID(clientID, elementName)
{
	return clientID + "_" + elementName;
}

function evalSharedState(elementID)
{
	element = document.getElementById(elementID);

	return eval(String(element.value));
}

function associateObjectWithEvent(object, methodName)
{
	return (function(event) { event = event || window.event; return object[methodName](event, this); })
}

function associateObjectWithMultiEvent(sourceElement, eventName, object, methodName)
{
	if (sourceElement.attachEvent)
	{
		sourceElement.attachEvent(eventName, associateObjectWithEvent(object, methodName));
	}
	else if (sourceElement.AttachEvent)
	{
		sourceElement.AttachEvent(eventName, associateObjectWithEvent(object, methodName));
	}
	else if (sourceElement.addEventListener)
	{
		if (eventName.indexOf("on") == 0)
		{
			eventName = eventName.substring(2);
		}

		sourceElement.addEventListener(eventName, associateObjectWithEvent(object, methodName), false);
	}
}

function cancelEventBubble(evt)
{
	evt.cancelBubble = true;

	if (evt.stopPropagation)
	{
		evt.stopPropagation();
	}
}

function getObjectByID(ID)
{
	try
	{
		instance = eval(ID);
	}
	catch (e)
	{
		instance = null;
	}

	return instance;
}

function isInElement(element, container)
{
	inElement = (element == container);

	if (!inElement)
	{
		owner = element.parentNode;

		while (owner != null)
		{
			inElement = (owner == container);

			if (inElement)
			{
				owner = null;
			}
			else
			{
				owner = owner.parentNode;
			}
		}
	}

	return inElement;
}

function getElementLocation(element)
{
	elementLeft = element.offsetLeft;
	elementTop = element.offsetTop;

	if (element.offsetParent != null)
	{
		while (element = element.offsetParent)
		{
			elementLeft += element.offsetLeft;
			elementTop += element.offsetTop;
		}
	}

	return [elementLeft, elementTop];
}

function elementHasMarkerClass(element)
{
	markerClassName = "ribbonPart";
	hasMarker = (element.className.indexOf(markerClassName) != -1);

	return hasMarker;
}


// Global Information

function RibbonInfo()
{
	this.applicationBar = null;
	this.activeButton = null;
	this.activeGroup = null;
	this.openGroup = null;
}

RibbonInfo.getApplicationBar = function()
{
	return this.applicationBar;
}

RibbonInfo.setApplicationBar = function(applicationBar)
{
	this.applicationBar = applicationBar;
}

RibbonInfo.getActiveButton = function()
{
	return this.activeButton;
}

RibbonInfo.setActiveButton = function(button)
{
	this.activeButton = button;
}

RibbonInfo.getActiveGroup = function()
{
	return this.activeGroup;
}

RibbonInfo.setActiveGroup = function(group)
{
	this.activeGroup = group;
}

RibbonInfo.getOpenGroup = function()
{
	return this.openGroup;
}

RibbonInfo.setOpenGroup = function(group)
{
	this.openGroup = group;
}


// Ribbon Resizing Manager

function RibbonResizingManager(clientID)
{
	this.clientID = clientID;
	this.sharedStateID = deriveID(clientID, "SharedState");
	this.windowWidth = 0;

	this.refreshSharedState();

	associateObjectWithMultiEvent(window, "onresize", this, "windowResized");

	this.windowResized();
	this.expandCollapseGroups();
}

RibbonResizingManager.prototype.getApplicationBarID = function()
{
	return String(this.sharedState[0]);
}

RibbonResizingManager.prototype.getApplicationMenuID = function()
{
	return String(this.sharedState[1]);
}

RibbonResizingManager.prototype.getRibbonTabStripID = function()
{
	return String(this.sharedState[2]);
}

RibbonResizingManager.prototype.getRibbonMultiPageViewID = function()
{
	return String(this.sharedState[3]);
}

RibbonResizingManager.prototype.getMinimumRibbonWidth = function()
{
	return Number(this.sharedState[4]);
}

RibbonResizingManager.prototype.getRibbonHidden = function()
{
	return Boolean(this.sharedState[5]);
}

RibbonResizingManager.prototype.setRibbonHidden = function(hidden)
{
	this.sharedState[5] = hidden;
	this.updateSharedState();

	if (RibbonInfo.getOpenGroup() != null)
	{
		RibbonInfo.getOpenGroup().setOpen(false);
	}

	this.setAppearance();
}

RibbonResizingManager.prototype.getControlSet = function()
{
	return String(this.sharedState[6]);
}

RibbonResizingManager.prototype.expandCollapseGroups = function()
{
	for (tabIndex = 0; tabIndex < this.tabsInfo.length; tabIndex++)
	{
		tabInfo = this.tabsInfo[tabIndex];

		for (groupsIndex = 0; groupsIndex < tabInfo.length; groupsIndex++)
		{
			windowWidth = document.documentElement.clientWidth;
			groupInfo = tabInfo[groupsIndex];
			groupID = groupInfo[0];
			group = getObjectByID(groupID);
			transitionWidth = groupInfo[3];
			expand = !(windowWidth < transitionWidth);

			group.setExpanded(expand);
		}
	}
}

RibbonResizingManager.prototype.lockWindowWidth = function(windowWidth)
{
	elementID = document.forms[0].id;
	element = document.getElementById(elementID);

	element.style.minWidth = windowWidth + "px";
}

RibbonResizingManager.prototype.windowResized = function()
{
	windowWidth = document.documentElement.clientWidth;

	if (windowWidth != this.windowWidth)
	{
		this.windowWidth = windowWidth;
		this.expandCollapseGroups();

		if (!isIE())
		{
			this.lockWindowWidth(windowWidth);
		}
	}

	hide = (windowWidth <= this.getMinimumRibbonWidth());

	if (hide != this.getRibbonHidden())
	{
		this.setRibbonHidden(hide);
	}

	RibbonInfo.getApplicationBar().refresh();

	if (RibbonInfo.getOpenGroup() != null)
	{
		RibbonInfo.getOpenGroup().refresh();
	}
}

RibbonResizingManager.prototype.refresh = function()
{
	this.setAppearance();
}

RibbonResizingManager.prototype.setAppearance = function()
{
	applicationBarElement = document.getElementById(this.getApplicationBarID());
	applicationMenuElement = document.getElementById(this.getApplicationMenuID());
	ribbonTabStripElement = document.getElementById(this.getRibbonTabStripID());
	ribbonMultiPageViewElement = document.getElementById(this.getRibbonMultiPageViewID());

	if (this.getRibbonHidden())
	{
		applicationBarElement.style.display = "none";
		applicationMenuElement.style.display = "none";
		ribbonTabStripElement.style.display = "none";
		ribbonMultiPageViewElement.style.display = "none";
	}
	else
	{
		applicationBarElement.style.display = "block";
		applicationMenuElement.style.display = "block";
		ribbonTabStripElement.style.display = "block";
		ribbonMultiPageViewElement.style.display = "block";
	}
}

RibbonResizingManager.prototype.refreshSharedState = function()
{
	this.sharedState = evalSharedState(this.sharedStateID);
	this.tabStrip = getObjectByID(this.getRibbonTabStripID());

	controlsInfo = eval(this.getControlSet());
	tabIndex = 0;
	index = 0;

	tabCount = this.tabStrip.GetAllTabs().length;
	this.tabsInfo = new Array(tabCount);

	while (index < controlsInfo.length)
	{
		numberOfItems = controlsInfo[index++];
		groupsInfo = new Array(numberOfItems);

		for (groupIndex = 0; groupIndex < numberOfItems; groupIndex++)
		{
			groupID = controlsInfo[index++];
			expandedWidth = controlsInfo[index++];
			collapsedWidth = controlsInfo[index++];
			transitionWidth = controlsInfo[index++];

			groupInfo = new Array(5);
			groupInfo[0] = groupID;
			groupInfo[1] = expandedWidth;
			groupInfo[2] = collapsedWidth;
			groupInfo[3] = transitionWidth;

			groupsInfo[groupIndex] = groupInfo;
		}

		this.tabsInfo[tabIndex] = groupsInfo;

		tabIndex++;
	}
}

RibbonResizingManager.prototype.updateSharedState = function()
{
	value = "['" + escapeString(this.sharedState[0]) + "','" + escapeString(this.sharedState[1]) + "','" + escapeString(this.sharedState[2]) + "','" + escapeString(this.sharedState[3]) + "'," + this.sharedState[4] + "," + this.sharedState[5] + ",'" + this.sharedState[6] + "']";
	document.getElementById(this.sharedStateID).value = value;
}


// Application Bar

function ApplicationBar(clientID)
{
	this.clientID = clientID;
	this.sharedStateID = deriveID(clientID, "SharedState");
	this.textID = deriveID(clientID, "Text");
	this.tabStrip = null;

	this.refreshSharedState();

	associateObjectWithMultiEvent(window, "onresize", this, "windowResized");
	associateObjectWithMultiEvent(document, "onclick", this, "dismissRibbon");

	this.windowResized();

	RibbonInfo.setApplicationBar(this);
}

ApplicationBar.prototype.attachTabStripEventHandler = function()
{
	this.tabStrip = getObjectByID(this.getRibbonTabStripID());

	if (this.tabStrip != null)
	{
		associateObjectWithMultiEvent(this.tabStrip, "OnClientTabSelected", this, "tabSelected");
	}
}

ApplicationBar.prototype.getApplicationName = function()
{
	return String(this.sharedState[0]);
}

ApplicationBar.prototype.setApplicationName = function(name)
{
	this.sharedState[0] = name;
	this.updateSharedState();
	this.refresh();
}

ApplicationBar.prototype.getDocumentName = function()
{
	return String(this.sharedState[1]);
}

ApplicationBar.prototype.setDocumentName = function(name)
{
	this.sharedState[1] = name;
	this.updateSharedState();
	this.refresh();
}

ApplicationBar.prototype.getApplicationMenuID = function()
{
	return String(this.sharedState[2]);
}

ApplicationBar.prototype.getRibbonTabStripID = function()
{
	return String(this.sharedState[3]);
}

ApplicationBar.prototype.getRibbonMultiPageViewID = function()
{
	return String(this.sharedState[4]);
}

ApplicationBar.prototype.getCustomizeMenuID = function()
{
	return String(this.sharedState[5]);
}

ApplicationBar.prototype.getMinimizeRibbon = function()
{
	return Boolean(this.sharedState[6]);
}

ApplicationBar.prototype.setMinimizeRibbon = function(minimize)
{
	this.setRibbonOpen(!minimize);
	this.sharedState[6] = minimize;
	this.updateSharedState();
	this.setAppearance();
}

ApplicationBar.prototype.getRibbonOpen = function()
{
	return Boolean(this.sharedState[7]);
}

ApplicationBar.prototype.setRibbonOpen = function(open)
{
	this.sharedState[7] = open;
	this.updateSharedState();
}

ApplicationBar.prototype.getOriginallySelectedTabIndex = function()
{
	return Number(this.sharedState[8]);
}

ApplicationBar.prototype.getOriginallySelectedTab = function()
{
	tab = null;

	if (this.tabStrip != null && (typeof (this.tabStrip.GetAllTabs) == "function"))
	{

		tab = this.tabStrip.GetAllTabs()[this.getOriginallySelectedTabIndex()];
	}

	return tab;
}

ApplicationBar.prototype.setOriginallySelectedTab = function(tab)
{
	index = -1;

	if (tab != null)
	{
		index = tab.Index;
	}

	this.sharedState[8] = index;
	this.updateSharedState();
}

ApplicationBar.prototype.refresh = function()
{
	text = "";
	applicationName = this.getApplicationName();
	documentName = this.getDocumentName();

	if ((documentName != null) && (documentName != ""))
	{
		text += documentName;
	}

	if ((documentName != null) && (documentName != "") && (applicationName != null) && (applicationName != ""))
	{
		text += " - ";
	}

	if ((applicationName != null) && (applicationName != ""))
	{
		text += applicationName;
	}

	document.getElementById(this.textID).innerHTML = text;

	this.setAppearance();
}

ApplicationBar.prototype.showRibbon = function()
{
	this.setRibbonOpen(true);
	this.updateSharedState();
	this.setAppearance();
}

ApplicationBar.prototype.hideRibbon = function()
{
	if (this.getMinimizeRibbon())
	{
		this.setRibbonOpen(false);
		this.updateSharedState();
		this.setAppearance();
	}
}

ApplicationBar.prototype.showCustomizeMenu = function(evt)
{
	menu = getObjectByID(this.getCustomizeMenuID());

	if ((menu != null) && ((!evt.relatedTarget) || (!menu.IsChildOf(menu.DomElement, evt.relatedTarget))))
	{
		menu.Show(evt);
	}

	cancelEventBubble(evt);
}

ApplicationBar.prototype.setAppearance = function()
{
	if (this.tabStrip != null)
	{
		if (this.getMinimizeRibbon())
		{
			tab = this.tabStrip.SelectedTab;

			if (tab == null)
			{
				tab = this.getOriginallySelectedTab();
			}

			if (tab != null)
			{
				multiPageView = document.getElementById(this.getRibbonMultiPageViewID());

				if (this.getRibbonOpen())
				{
					multiPageView.className = "ribbonBarFloat";

					tabStringElement = document.getElementById(this.getRibbonTabStripID());
					multiPageView.style.top = (tabStringElement.offsetTop + tabStringElement.offsetHeight) + "px";
				}
				else
				{
					this.setOriginallySelectedTab(tab);

					tab.UnSelect();
					multiPageView.className = "ribbonBarClosed";
				}
			}
		}
		else
		{
			originallySelectedTab = this.getOriginallySelectedTab();

			if (originallySelectedTab != null)
			{
				multiPageView = document.getElementById(this.getRibbonMultiPageViewID());

				multiPageView.className = "ribbonBar";
				originallySelectedTab.Select();
			}
		}
	}
}

ApplicationBar.prototype.windowResized = function()
{
	this.setAppearance();
}

ApplicationBar.prototype.tabSelected = function(sender, eventArgs)
{
	if ((this.getMinimizeRibbon()) && (!this.getRibbonOpen()))
	{
		this.showRibbon();
	}

	this.setOriginallySelectedTab(this.tabStrip.SelectedTab);
}

ApplicationBar.prototype.dismissRibbon = function(evt)
{
	if ((this.isClickAway(evt)) && (this.getMinimizeRibbon()) && (this.getRibbonOpen()))
	{
		this.hideRibbon();
	}
}

ApplicationBar.prototype.isClickAway = function(evt)
{
	clickedAway = false;
	tabStripId = this.getRibbonTabStripID();

	if ((tabStripId != null) && (this.tabStrip != null))
	{
		tabStripElement = document.getElementById(tabStripId);
		tab = this.tabStrip.SelectedTab;

		if (tab != null)
		{
			eventTarget = (typeof (evt.target) != 'undefined') ? evt.target : evt.srcElement;  // FF:IE
			clickedOn = this.isPartOfRibbon(eventTarget, tabStripElement);
			clickedAway = !clickedOn;
		}
	}

	return clickedAway;
}

ApplicationBar.prototype.isPartOfRibbon = function(eventTarget, tabStripElement)
{
	isRibbonPart = ((isInElement(eventTarget, tabStripElement)) ||
		(elementHasMarkerClass(eventTarget)));

	return isRibbonPart;
}

ApplicationBar.prototype.refreshSharedState = function()
{
	this.sharedState = evalSharedState(this.sharedStateID);
}

ApplicationBar.prototype.updateSharedState = function()
{
	value = "['" + escapeString(this.sharedState[0]) + "','" + escapeString(this.sharedState[1]) + "','" + escapeString(this.sharedState[2]) + "','" + escapeString(this.sharedState[3]) + "','" + escapeString(this.sharedState[4]) + "','" + escapeString(this.sharedState[5]) + "'," + this.sharedState[6] + "," + this.sharedState[7] + "," + this.sharedState[8] + "]";
	document.getElementById(this.sharedStateID).value = value;
}


// Quick Access Ribbon Button

function QuickAccessRibbonButton(clientID)
{
	this.clientID = clientID;
	this.sharedStateID = deriveID(clientID, "SharedState");
	this.imageID = deriveID(clientID, "Image");

	this.refreshSharedState();

	element = document.getElementById(this.clientID);
	associateObjectWithMultiEvent(element, "onmouseover", this, "mouseOver");
	associateObjectWithMultiEvent(element, "onmouseout", this, "mouseOut");
}

QuickAccessRibbonButton.prototype.mouseOver = function(event, element)
{
	this.setAppearance(true);
	RibbonInfo.setActiveButton(this);
}

QuickAccessRibbonButton.prototype.mouseOut = function(event, element)
{
	this.setAppearance(false);
	RibbonInfo.setActiveButton(null);
}

QuickAccessRibbonButton.prototype.getEnabled = function()
{
	return Boolean(this.sharedState[0]);
}

QuickAccessRibbonButton.prototype.setEnabled = function(enabled)
{
	this.sharedState[0] = enabled;
	this.updateSharedState();
	this.setAppearance(false);
}

QuickAccessRibbonButton.prototype.getChecked = function()
{
	return Boolean(this.sharedState[1]);
}

QuickAccessRibbonButton.prototype.setChecked = function(checked)
{
	this.sharedState[1] = checked;
	this.updateSharedState();
	this.setAppearance(false);
}

QuickAccessRibbonButton.prototype.getEnabledImageUrl = function()
{
	return String(this.sharedState[2]);
}

QuickAccessRibbonButton.prototype.getDisabledImageUrl = function()
{
	return String(this.sharedState[3]);
}

QuickAccessRibbonButton.prototype.refresh = function()
{
	this.setAppearance(false);
}

QuickAccessRibbonButton.prototype.setAppearance = function(over)
{
	element = document.getElementById(this.clientID);
	imageElement = document.getElementById(this.imageID);

	enabled = this.getEnabled();
	checked = this.getChecked();

	if ((enabled) && (over))
	{
		element.className = "quickAccessButtonHover";
	}
	else if ((enabled) && (checked))
	{
		element.className = "quickAccessButtonChecked";
	}
	else
	{
		element.className = "quickAccessButton";
	}

	if (enabled)
	{
		imageElement.src = this.getEnabledImageUrl();
	}
	else
	{
		imageElement.src = this.getDisabledImageUrl();
	}
}

QuickAccessRibbonButton.prototype.refreshSharedState = function()
{
	this.sharedState = evalSharedState(this.sharedStateID);
}

QuickAccessRibbonButton.prototype.updateSharedState = function()
{
	value = "[" + this.sharedState[0] + "," + this.sharedState[1] + ",'" + escapeString(this.sharedState[2]) + "','" + escapeString(this.sharedState[3]) + "']";
	document.getElementById(this.sharedStateID).value = value;
}


// Ribbon Group

function RibbonGroup(clientID)
{
	this.clientID = clientID;
	this.sharedStateID = deriveID(clientID, "SharedState");
	this.expandedID = deriveID(clientID, "E");
	this.expandedStartID = deriveID(clientID, "EStart");
	this.expandedItemID = deriveID(clientID, "EItem");
	this.expandedEndID = deriveID(clientID, "EEnd");
	this.expandedTextID = deriveID(clientID, "EText");
	this.collapsedID = deriveID(clientID, "C");
	this.collapsedStartID = deriveID(clientID, "CStart");
	this.collapsedItemID = deriveID(clientID, "CItem");
	this.collapsedEndID = deriveID(clientID, "CEnd");
	this.collapsedImageContainerID = deriveID(clientID, "CImageContainer");
	this.collapsedTextID = deriveID(clientID, "CText");

	this.refreshSharedState();

	associateObjectWithMultiEvent(document, "onclick", this, "dismissGroup");

	expandedElement = document.getElementById(this.expandedID);
	associateObjectWithMultiEvent(expandedElement, "onmouseover", this, "mouseOver");
	associateObjectWithMultiEvent(expandedElement, "onmouseout", this, "mouseOut");

	collapsedElement = document.getElementById(this.collapsedID);
	associateObjectWithMultiEvent(collapsedElement, "onmouseover", this, "mouseOver");
	associateObjectWithMultiEvent(collapsedElement, "onmouseout", this, "mouseOut");
}

RibbonGroup.prototype.mouseOver = function(event, element)
{
	this.setAppearance(true);
	RibbonInfo.setActiveGroup(this);
}

RibbonGroup.prototype.mouseOut = function(event, element)
{
	this.setAppearance(false);
	RibbonInfo.setActiveGroup(null);
}

RibbonGroup.prototype.getText = function()
{
	return String(this.sharedState[0]);
}

RibbonGroup.prototype.setText = function(text)
{
	this.sharedState[0] = text;
	this.updateSharedState();
	this.setAppearance(false);
}

RibbonGroup.prototype.getExpandedWidth = function()
{
	return Number(this.sharedState[1]);
}

RibbonGroup.prototype.getCollapsedWidth = function()
{
	return Number(this.sharedState[2]);
}

RibbonGroup.prototype.getExpanded = function()
{
	return Boolean(this.sharedState[3]);
}

RibbonGroup.prototype.setExpanded = function(expanded)
{
	this.sharedState[3] = expanded;
	this.updateSharedState();

	if ((RibbonInfo.getOpenGroup() == this) && (expanded))
	{
		this.setOpen(false);
	}

	this.setAppearance(false);
}

RibbonGroup.prototype.getOpen = function()
{
	return Boolean(this.sharedState[4]);
}

RibbonGroup.prototype.setOpen = function(open)
{
	this.sharedState[4] = open;
	this.updateSharedState();

	openGroup = RibbonInfo.getOpenGroup();

	if ((openGroup != null) && (openGroup != this))
	{
		RibbonInfo.getOpenGroup().setOpen(false);
		RibbonInfo.setOpenGroup(null);
	}

	this.refresh();
}

RibbonGroup.prototype.collapsedClicked = function(evt)
{
	this.setOpen(!this.getOpen());

	cancelEventBubble(evt);
}

RibbonGroup.prototype.dismissGroup = function(evt)
{
	if ((this.isClickAway(evt)) && (this.getOpen()))
	{
		this.setOpen(false);
		this.setAppearance(false);

		if (RibbonInfo.getActiveButton() != null)
		{
			RibbonInfo.getActiveButton().refresh();
			RibbonInfo.setActiveButton(null);
		}
	}
}

RibbonGroup.prototype.isClickAway = function(evt)
{
	clickedAway = true;

	eventTarget = (typeof (evt.target) != 'undefined') ? evt.target : evt.srcElement;  // FF:IE
	expandedElement = document.getElementById(this.expandedID);

	if ((isInElement(eventTarget, expandedElement)) && (elementHasMarkerClass(eventTarget)))
	{
		clickedAway = false;
	}

	return clickedAway;
}

RibbonGroup.prototype.refresh = function()
{
	this.setAppearance(RibbonInfo.getActiveGroup() == this);

	expandedElement = document.getElementById(this.expandedID);
	collapsedElement = document.getElementById(this.collapsedID);

	if (this.getOpen())
	{
		windowWidth = document.documentElement.clientWidth;
		groupLocation = getElementLocation(collapsedElement);
		groupLeft = groupLocation[0] - 3;
		groupRight = groupLeft + this.getCollapsedWidth();
		groupWidth = this.getExpandedWidth();
		openLeft = groupLeft;
		openTop = groupLocation[1] + collapsedElement.offsetHeight - 3;

		if (groupLeft + groupWidth + 6 > windowWidth)
		{
			openLeft = groupRight - groupWidth;

			if (openLeft < 0)
			{
				openLeft = 0;
			}
		}

		expandedElement.className = "ribbonGroupFloat ribbonPart";
		expandedElement.style.left = openLeft + "px";
		expandedElement.style.top = openTop + "px";

		parentElement = document.forms[0];
		parentElement.appendChild(expandedElement);

		RibbonInfo.setOpenGroup(this);
	}
	else
	{
		expandedElement.className = "ribbonGroupContainer ribbonPart";

		parentElement = document.getElementById(this.clientID);
		parentElement.appendChild(expandedElement);

		RibbonInfo.setOpenGroup(null);
	}
}

RibbonGroup.prototype.setAppearance = function(over)
{
	expandedElement = document.getElementById(this.expandedID);
	expandedStartElement = document.getElementById(this.expandedStartID);
	expandedItemElement = document.getElementById(this.expandedItemID);
	expandedEndElement = document.getElementById(this.expandedEndID);
	expandedTextElement = document.getElementById(this.expandedTextID);
	collapsedElement = document.getElementById(this.collapsedID);
	collapsedStartElement = document.getElementById(this.collapsedStartID);
	collapsedItemElement = document.getElementById(this.collapsedItemID);
	collapsedEndElement = document.getElementById(this.collapsedEndID);
	collapsedImageContanerElement = document.getElementById(this.collapsedImageContainerID);
	collapsedTextElement = document.getElementById(this.collapsedTextID);

	expandedTextElement.innerHTML = this.getText();
	collapsedTextElement.innerHTML = this.getText();

	if (over)
	{
		expandedStartElement.className = "ribbonGroupStartHover ribbonPart";
		expandedItemElement.className = "ribbonGroupItemHover ribbonPart";
		expandedEndElement.className = "ribbonGroupEndHover ribbonPart";

		collapsedStartElement.className = "ribbonGroupCollapsedStartHover ribbonPart";
		collapsedItemElement.className = "ribbonGroupCollapsedItemHover ribbonPart";
		collapsedEndElement.className = "ribbonGroupCollapsedEndHover ribbonPart";
		collapsedImageContanerElement.className = "ribbonGroupCollapsedImageContainerHover ribbonPart";
	}
	else
	{
		expandedStartElement.className = "ribbonGroupStart ribbonPart";
		expandedItemElement.className = "ribbonGroupItem ribbonPart";
		expandedEndElement.className = "ribbonGroupEnd ribbonPart";

		collapsedStartElement.className = "ribbonGroupCollapsedStart ribbonPart";
		collapsedItemElement.className = "ribbonGroupCollapsedItem ribbonPart";
		collapsedEndElement.className = "ribbonGroupCollapsedEnd ribbonPart";
		collapsedImageContanerElement.className = "ribbonGroupCollapsedImageContainer ribbonPart";
	}

	if (this.getExpanded())
	{
		expandedElement.style.display = "block";
		collapsedElement.style.display = "none";
	}
	else
	{
		expandedElement.style.display = "none";
		collapsedElement.style.display = "block";
	}
}

RibbonGroup.prototype.refreshSharedState = function()
{
	this.sharedState = evalSharedState(this.sharedStateID);
}

RibbonGroup.prototype.updateSharedState = function()
{
	value = "['" + escapeString(this.sharedState[0]) + "'," + this.sharedState[1] + "," + this.sharedState[2] + "," + this.sharedState[3] + "," + this.sharedState[4] + "]";
	document.getElementById(this.sharedStateID).value = value;
}


// Large Ribbon Button

function LargeRibbonButton(clientID)
{
	this.clientID = clientID;
	this.sharedStateID = deriveID(clientID, "SharedState");
	this.startID = deriveID(clientID, "Start");
	this.itemID = deriveID(clientID, "Item");
	this.endID = deriveID(clientID, "End");
	this.imageID = deriveID(clientID, "Image");
	this.dropDownImageID = deriveID(clientID, "DropDownMenuImage");
	this.textID = deriveID(clientID, "Text");

	this.refreshSharedState();

	element = document.getElementById(this.clientID);
	associateObjectWithMultiEvent(element, "onmouseover", this, "mouseOver");
	associateObjectWithMultiEvent(element, "onmouseout", this, "mouseOut");
}

LargeRibbonButton.prototype.mouseOver = function(event, element)
{
	this.setAppearance(true);
	RibbonInfo.setActiveButton(this);
}

LargeRibbonButton.prototype.mouseOut = function(event, element)
{
	this.setAppearance(false);
	RibbonInfo.setActiveButton(null);
}

LargeRibbonButton.prototype.getEnabled = function()
{
	return Boolean(this.sharedState[0]);
}

LargeRibbonButton.prototype.setEnabled = function(enabled)
{
	this.sharedState[0] = enabled;
	this.updateSharedState();
	this.setAppearance(false);
}

LargeRibbonButton.prototype.getChecked = function()
{
	return Boolean(this.sharedState[1]);
}

LargeRibbonButton.prototype.setChecked = function(checked)
{
	this.sharedState[1] = checked;
	this.updateSharedState();
	this.setAppearance(false);
}

LargeRibbonButton.prototype.getEnabledImageUrl = function()
{
	return String(this.sharedState[2]);
}

LargeRibbonButton.prototype.getDisabledImageUrl = function()
{
	return String(this.sharedState[3]);
}

LargeRibbonButton.prototype.getText = function()
{
	return String(this.sharedState[4]);
}

LargeRibbonButton.prototype.setText = function(text)
{
	this.sharedState[4] = text;
	this.updateSharedState();
	this.setAppearance(false);
}

LargeRibbonButton.prototype.getDropDownMenuID = function()
{
	return String(this.sharedState[5]);
}

LargeRibbonButton.prototype.showDropDownMenu = function(evt)
{
	menu = getObjectByID(this.getDropDownMenuID());

	if ((!evt.relatedTarget) || (!menu.IsChildOf(menu.DomElement, evt.relatedTarget)))
	{
		menu.Show(evt);
	}

	cancelEventBubble(evt);
}

LargeRibbonButton.prototype.refresh = function()
{
	this.setAppearance(false);
}

LargeRibbonButton.prototype.setAppearance = function(over)
{
	imageElement = document.getElementById(this.imageID);
	textElement = document.getElementById(this.textID);
	dropDownImageElement = document.getElementById(this.dropDownImageID);
	startElement = document.getElementById(this.startID);
	itemElement = document.getElementById(this.itemID);
	endElement = document.getElementById(this.endID);

	enabled = this.getEnabled();
	checked = this.getChecked();

	textElement.innerHTML = this.getText();

	if (enabled)
	{
		imageElement.src = this.getEnabledImageUrl();
		textElement.className = "largeRibbonButtonText";

		if (dropDownImageElement != null)
		{
			dropDownImageElement.className = "largeRibbonButtonDropDownMenuArrow";
		}
	}
	else
	{
		imageElement.src = this.getDisabledImageUrl();
		textElement.className = "largeRibbonButtonTextDisabled";

		if (dropDownImageElement != null)
		{
			dropDownImageElement.className = "largeRibbonButtonDropDownMenuArrowDisabled";
		}
	}

	if ((enabled) && (over))
	{
		startElement.className = "largeRibbonButtonStartHover";
		itemElement.className = "largeRibbonButtonItemHover";
		endElement.className = "largeRibbonButtonEndHover";
	}
	else if ((enabled) && (checked))
	{
		startElement.className = "largeRibbonButtonStartChecked";
		itemElement.className = "largeRibbonButtonItemChecked";
		endElement.className = "largeRibbonButtonEndChecked";
	}
	else
	{
		startElement.className = "largeRibbonButtonStart";
		itemElement.className = "largeRibbonButtonItem";
		endElement.className = "largeRibbonButtonEnd";
	}
}

LargeRibbonButton.prototype.refreshSharedState = function()
{
	this.sharedState = evalSharedState(this.sharedStateID);
}

LargeRibbonButton.prototype.updateSharedState = function()
{
	value = "[" + this.sharedState[0] + "," + this.sharedState[1] + ",'" + escapeString(this.sharedState[2]) + "','" + escapeString(this.sharedState[3]) + "','" + escapeString(this.sharedState[4]) + "','" + escapeString(this.sharedState[5]) + "']";
	document.getElementById(this.sharedStateID).value = value;
}


// Large Split Ribbon Button

function LargeSplitRibbonButton(clientID)
{
	this.clientID = clientID;
	this.sharedStateID = deriveID(clientID, "SharedState");
	this.topID = deriveID(clientID, "Top");
	this.topStartID = deriveID(clientID, "TopStart");
	this.topItemID = deriveID(clientID, "TopItem");
	this.topEndID = deriveID(clientID, "TopEnd");
	this.bottomID = deriveID(clientID, "Bottom");
	this.bottomStartID = deriveID(clientID, "BottomStart");
	this.bottomItemID = deriveID(clientID, "BottomItem");
	this.bottomEndID = deriveID(clientID, "BottomEnd");
	this.imageID = deriveID(clientID, "Image");
	this.dropDownImageID = deriveID(clientID, "DropDownMenuImage");
	this.textID = deriveID(clientID, "Text");

	this.refreshSharedState();

	topElement = document.getElementById(this.topID);
	bottomElement = document.getElementById(this.bottomID);
	associateObjectWithMultiEvent(topElement, "onmouseover", this, "mouseOverTop");
	associateObjectWithMultiEvent(topElement, "onmouseout", this, "mouseOut");
	associateObjectWithMultiEvent(bottomElement, "onmouseover", this, "mouseOverBottom");
	associateObjectWithMultiEvent(bottomElement, "onmouseout", this, "mouseOut");
}

LargeSplitRibbonButton.prototype.mouseOverTop = function(event, element)
{
	this.setAppearance(true, true);
	RibbonInfo.setActiveButton(this);
}

LargeSplitRibbonButton.prototype.mouseOverBottom = function(event, element)
{
	this.setAppearance(true, false);
	RibbonInfo.setActiveButton(this);
}

LargeSplitRibbonButton.prototype.mouseOut = function(event, element)
{
	this.setAppearance(false, false);
	RibbonInfo.setActiveButton(null);
}

LargeSplitRibbonButton.prototype.getEnabled = function()
{
	return Boolean(this.sharedState[0]);
}

LargeSplitRibbonButton.prototype.setEnabled = function(enabled)
{
	this.sharedState[0] = enabled;
	this.updateSharedState();
	this.setAppearance(false, false);
}

LargeSplitRibbonButton.prototype.getChecked = function()
{
	return Boolean(this.sharedState[1]);
}

LargeSplitRibbonButton.prototype.getEnabledImageUrl = function()
{
	return String(this.sharedState[2]);
}

LargeSplitRibbonButton.prototype.getDisabledImageUrl = function()
{
	return String(this.sharedState[3]);
}

LargeSplitRibbonButton.prototype.getText = function()
{
	return String(this.sharedState[4]);
}

LargeSplitRibbonButton.prototype.setText = function(text)
{
	this.sharedState[4] = text;
	this.updateSharedState();
	this.setAppearance(false, false);
}

LargeSplitRibbonButton.prototype.getDropDownMenuID = function()
{
	return String(this.sharedState[5]);
}

LargeSplitRibbonButton.prototype.showDropDownMenu = function(evt)
{
	menu = getObjectByID(this.getDropDownMenuID());

	if ((!evt.relatedTarget) || (!menu.IsChildOf(menu.DomElement, evt.relatedTarget)))
	{
		menu.Show(evt);
	}

	cancelEventBubble(evt);
}

LargeSplitRibbonButton.prototype.refresh = function()
{
	this.setAppearance(false, false);
}

LargeSplitRibbonButton.prototype.setAppearance = function(over, top)
{
	imageElement = document.getElementById(this.imageID);
	textElement = document.getElementById(this.textID);
	dropDownImageElement = document.getElementById(this.dropDownImageID);
	topStartElement = document.getElementById(this.topStartID);
	topItemElement = document.getElementById(this.topItemID);
	topEndElement = document.getElementById(this.topEndID);
	bottomStartElement = document.getElementById(this.bottomStartID);
	bottomItemElement = document.getElementById(this.bottomItemID);
	bottomEndElement = document.getElementById(this.bottomEndID);

	enabled = this.getEnabled();
	checked = this.getChecked();

	textElement.innerHTML = this.getText();

	if (enabled)
	{
		imageElement.src = this.getEnabledImageUrl();
		textElement.className = "largeSplitRibbonButtonText";

		dropDownImageElement.className = "largeSplitRibbonButtonDropDownMenuArrow";
	}
	else
	{
		imageElement.src = this.getDisabledImageUrl();
		textElement.className = "largeSplitRibbonButtonTextDisabled";

		dropDownImageElement.className = "largeSplitRibbonButtonDropDownMenuArrowDisabled";
	}

	if ((enabled) && (over))
	{
		if (top)
		{
			topStartElement.className = "largeSplitRibbonButtonTopStartHover";
			topItemElement.className = "largeSplitRibbonButtonTopItemHover";
			topEndElement.className = "largeSplitRibbonButtonTopEndHover";
			bottomStartElement.className = "largeSplitRibbonButtonBottomStartHoverInactive";
			bottomItemElement.className = "largeSplitRibbonButtonBottomItemHoverInactive";
			bottomEndElement.className = "largeSplitRibbonButtonBottomEndHoverInactive";
		}
		else
		{
			topStartElement.className = "largeSplitRibbonButtonTopStartHoverInactive";
			topItemElement.className = "largeSplitRibbonButtonTopItemHoverInactive";
			topEndElement.className = "largeSplitRibbonButtonTopEndHoverInactive";
			bottomStartElement.className = "largeSplitRibbonButtonBottomStartHover";
			bottomItemElement.className = "largeSplitRibbonButtonBottomItemHover";
			bottomEndElement.className = "largeSplitRibbonButtonBottomEndHover";
		}
	}
	else
	{
		topStartElement.className = "largeSplitRibbonButtonTopStart";
		topItemElement.className = "largeSplitRibbonButtonTopItem";
		topEndElement.className = "largeSplitRibbonButtonTopEnd";
		bottomStartElement.className = "largeSplitRibbonButtonBottomStart";
		bottomItemElement.className = "largeSplitRibbonButtonBottomItem";
		bottomEndElement.className = "largeSplitRibbonButtonBottomEnd";
	}
}

LargeSplitRibbonButton.prototype.refreshSharedState = function()
{
	this.sharedState = evalSharedState(this.sharedStateID);
}

LargeSplitRibbonButton.prototype.updateSharedState = function()
{
	value = "[" + this.sharedState[0] + "," + this.sharedState[1] + ",'" + escapeString(this.sharedState[2]) + "','" + escapeString(this.sharedState[3]) + "','" + escapeString(this.sharedState[4]) + "','" + escapeString(this.sharedState[5]) + "']";
	document.getElementById(this.sharedStateID).value = value;
}


// Small Ribbon Button

function SmallRibbonButton(clientID)
{
	this.clientID = clientID;
	this.sharedStateID = deriveID(clientID, "SharedState");
	this.startID = deriveID(clientID, "Start");
	this.itemID = deriveID(clientID, "Item");
	this.endID = deriveID(clientID, "End");
	this.imageID = deriveID(clientID, "Image");
	this.dropDownImageID = deriveID(clientID, "DropDownMenuImage");
	this.textID = deriveID(clientID, "Text");

	this.refreshSharedState();

	element = document.getElementById(this.clientID);
	associateObjectWithMultiEvent(element, "onmouseover", this, "mouseOver");
	associateObjectWithMultiEvent(element, "onmouseout", this, "mouseOut");
}

SmallRibbonButton.prototype.mouseOver = function(event, element)
{
	this.setAppearance(true);
	RibbonInfo.setActiveButton(this);
}

SmallRibbonButton.prototype.mouseOut = function(event, element)
{
	this.setAppearance(false);
	RibbonInfo.setActiveButton(null);
}

SmallRibbonButton.prototype.getEnabled = function()
{
	return Boolean(this.sharedState[0]);
}

SmallRibbonButton.prototype.setEnabled = function(enabled)
{
	this.sharedState[0] = enabled;
	this.updateSharedState();
	this.setAppearance(false);
}

SmallRibbonButton.prototype.getChecked = function()
{
	return Boolean(this.sharedState[1]);
}

SmallRibbonButton.prototype.setChecked = function(checked)
{
	this.sharedState[1] = checked;
	this.updateSharedState();
	this.setAppearance(false);
}

SmallRibbonButton.prototype.getEnabledImageUrl = function()
{
	return String(this.sharedState[2]);
}

SmallRibbonButton.prototype.getDisabledImageUrl = function()
{
	return String(this.sharedState[3]);
}

SmallRibbonButton.prototype.getText = function()
{
	return String(this.sharedState[4]);
}

SmallRibbonButton.prototype.setText = function(text)
{
	this.sharedState[4] = text;
	this.updateSharedState();
	this.setAppearance(false);
}

SmallRibbonButton.prototype.getDropDownMenuID = function()
{
	return String(this.sharedState[5]);
}

SmallRibbonButton.prototype.showDropDownMenu = function(evt)
{
	menu = getObjectByID(this.getDropDownMenuID());

	if ((!evt.relatedTarget) || (!menu.IsChildOf(menu.DomElement, evt.relatedTarget)))
	{
		menu.Show(evt);
	}

	cancelEventBubble(evt);
}

SmallRibbonButton.prototype.refresh = function()
{
	this.setAppearance(false);
}

SmallRibbonButton.prototype.setAppearance = function(over)
{
	imageElement = document.getElementById(this.imageID);
	textElement = document.getElementById(this.textID);
	dropDownImageElement = document.getElementById(this.dropDownImageID);
	startElement = document.getElementById(this.startID);
	itemElement = document.getElementById(this.itemID);
	endElement = document.getElementById(this.endID);

	enabled = this.getEnabled();
	checked = this.getChecked();

	textElement.innerHTML = this.getText();

	if (enabled)
	{
		imageElement.src = this.getEnabledImageUrl();
		textElement.className = "smallRibbonButtonText";

		if (dropDownImageElement != null)
		{
			dropDownImageElement.className = "smallRibbonButtonDropDownMenuArrow";
		}
	}
	else
	{
		imageElement.src = this.getDisabledImageUrl();
		textElement.className = "smallRibbonButtonTextDisabled";

		if (dropDownImageElement != null)
		{
			dropDownImageElement.className = "smallRibbonButtonDropDownMenuArrowDisabled";
		}
	}

	if ((enabled) && (over))
	{
		startElement.className = "smallRibbonButtonStartHover";
		itemElement.className = "smallRibbonButtonItemHover";
		endElement.className = "smallRibbonButtonEndHover";
	}
	else if ((enabled) && (checked))
	{
		startElement.className = "smallRibbonButtonStartChecked";
		itemElement.className = "smallRibbonButtonItemChecked";
		endElement.className = "smallRibbonButtonEndChecked";
	}
	else
	{
		startElement.className = "smallRibbonButtonStart";
		itemElement.className = "smallRibbonButtonItem";
		endElement.className = "smallRibbonButtonEnd";
	}
}

SmallRibbonButton.prototype.refreshSharedState = function()
{
	this.sharedState = evalSharedState(this.sharedStateID);
}

SmallRibbonButton.prototype.updateSharedState = function()
{
	value = "[" + this.sharedState[0] + "," + this.sharedState[1] + ",'" + escapeString(this.sharedState[2]) + "','" + escapeString(this.sharedState[3]) + "','" + escapeString(this.sharedState[4]) + "','" + escapeString(this.sharedState[5]) + "']";
	document.getElementById(this.sharedStateID).value = value;
}


// Small Split Ribbon Button

function SmallSplitRibbonButton(clientID)
{
	this.clientID = clientID;
	this.sharedStateID = deriveID(clientID, "SharedState");
	this.leftID = deriveID(clientID, "Left");
	this.leftStartID = deriveID(clientID, "LeftStart");
	this.leftItemID = deriveID(clientID, "LeftItem");
	this.rightID = deriveID(clientID, "Right");
	this.imageID = deriveID(clientID, "Image");
	this.textID = deriveID(clientID, "Text");

	this.refreshSharedState();

	leftElement = document.getElementById(this.leftID);
	rightElement = document.getElementById(this.rightID);
	associateObjectWithMultiEvent(leftElement, "onmouseover", this, "mouseOverLeft");
	associateObjectWithMultiEvent(leftElement, "onmouseout", this, "mouseOut");
	associateObjectWithMultiEvent(rightElement, "onmouseover", this, "mouseOverRight");
	associateObjectWithMultiEvent(rightElement, "onmouseout", this, "mouseOut");
}

SmallSplitRibbonButton.prototype.mouseOverLeft = function(event, element)
{
	this.setAppearance(true, true);
	RibbonInfo.setActiveButton(this);
}

SmallSplitRibbonButton.prototype.mouseOverRight = function(event, element)
{
	this.setAppearance(true, false);
	RibbonInfo.setActiveButton(this);
}

SmallSplitRibbonButton.prototype.mouseOut = function(event, element)
{
	this.setAppearance(false, false);
	RibbonInfo.setActiveButton(null);
}

SmallSplitRibbonButton.prototype.getEnabled = function()
{
	return Boolean(this.sharedState[0]);
}

SmallSplitRibbonButton.prototype.setEnabled = function(enabled)
{
	this.sharedState[0] = enabled;
	this.updateSharedState();
	this.setAppearance(false, false);
}

SmallSplitRibbonButton.prototype.getChecked = function()
{
	return Boolean(this.sharedState[1]);
}

SmallSplitRibbonButton.prototype.getEnabledImageUrl = function()
{
	return String(this.sharedState[2]);
}

SmallSplitRibbonButton.prototype.getDisabledImageUrl = function()
{
	return String(this.sharedState[3]);
}

SmallSplitRibbonButton.prototype.getText = function()
{
	return String(this.sharedState[4]);
}

SmallSplitRibbonButton.prototype.setText = function(text)
{
	this.sharedState[4] = text;
	this.updateSharedState();
	this.setAppearance(false, false);
}

SmallSplitRibbonButton.prototype.getDropDownMenuID = function()
{
	return String(this.sharedState[5]);
}

SmallSplitRibbonButton.prototype.showDropDownMenu = function(evt)
{
	menu = getObjectByID(this.getDropDownMenuID());

	if ((!evt.relatedTarget) || (!menu.IsChildOf(menu.DomElement, evt.relatedTarget)))
	{
		menu.Show(evt);
	}

	cancelEventBubble(evt);
}

SmallSplitRibbonButton.prototype.refresh = function()
{
	this.setAppearance(false, false);
}

SmallSplitRibbonButton.prototype.setAppearance = function(over, left)
{
	imageElement = document.getElementById(this.imageID);
	textElement = document.getElementById(this.textID);
	leftStartElement = document.getElementById(this.leftStartID);
	leftItemElement = document.getElementById(this.leftItemID);
	rightElement = document.getElementById(this.rightID);

	enabled = this.getEnabled();
	checked = this.getChecked();

	textElement.innerHTML = this.getText();

	if (enabled)
	{
		imageElement.src = this.getEnabledImageUrl();
		textElement.className = "smallSplitRibbonButtonText";
	}
	else
	{
		imageElement.src = this.getDisabledImageUrl();
		textElement.className = "smallSplitRibbonButtonTextDisabled";
	}

	if ((enabled) && (over))
	{
		if (left)
		{
			leftStartElement.className = "smallSplitRibbonButtonLeftStartHover";
			leftItemElement.className = "smallSplitRibbonButtonLeftItemHover";
			rightElement.className = "smallSplitRibbonButtonRightHoverInactive";
		}
		else
		{
			leftStartElement.className = "smallSplitRibbonButtonLeftStartHoverInactive";
			leftItemElement.className = "smallSplitRibbonButtonLeftItemHoverInactive";
			rightElement.className = "smallSplitRibbonButtonRightHover";
		}
	}
	else
	{
		leftStartElement.className = "smallSplitRibbonButtonLeftStart";
		leftItemElement.className = "smallSplitRibbonButtonLeftItem";

		if (enabled)
		{
			rightElement.className = "smallSplitRibbonButtonRight";
		}
		else
		{
			rightElement.className = "smallSplitRibbonButtonRightDisabled";
		}
	}
}

SmallSplitRibbonButton.prototype.refreshSharedState = function()
{
	this.sharedState = evalSharedState(this.sharedStateID);
}

SmallSplitRibbonButton.prototype.updateSharedState = function()
{
	value = "[" + this.sharedState[0] + "," + this.sharedState[1] + ",'" + escapeString(this.sharedState[2]) + "','" + escapeString(this.sharedState[3]) + "','" + escapeString(this.sharedState[4]) + "','" + escapeString(this.sharedState[5]) + "']";
	document.getElementById(this.sharedStateID).value = value;
}


// Ribbon Separator

function RibbonSeparator(clientID)
{
	this.clientID = clientID;
}

