(function() {
var L = YAHOO.lang, U = ZC.Util, Dom = YAHOO.util.Dom, Evt = YAHOO.util.Event;

var oBlock = ZC.Core.Block.Create('Menu_Menu');
oBlock.prototype.CustomSetupEnd = function()
{
	var oMenu = new YAHOO.widget.MenuBar(this.aDef.ID), aMenuLinks; 
	oMenu.render(); 
	oMenu.show(); 

	this.oYUIMenuBar = oMenu;

	aMenuLinks = Dom.getElementsByClassName('menulink', 'a');
	if (aMenuLinks.length)
	{
		Evt.on(aMenuLinks, 'click', function(oEvent)
		{
			var elTarget = Evt.getTarget(oEvent),
				aSubmenus = oMenu.getSubmenus(),
				sGoToMenuID = elTarget.href.replace(/.*#/, ''), 
				i, iMax;

			for (i = 0, iMax = aSubmenus.length; i < iMax; i++)
			{
				if (aSubmenus[i].id == sGoToMenuID)
				{
					aSubmenus[i].show();
					aSubmenus[i].parent.cfg.setProperty('selected', true);
					aSubmenus[i].focus();
					Evt.stopEvent(oEvent);
					return;
				}
			}
		}, this, true);
	}
}

oBlock = ZC.Core.Block.Create('TabView');
oBlock.prototype.CustomSetupEnd = function()
{
	var aTabs, aTabLinks, sOrientation = this.GetAttribDefault('Orientation', 'top'), bTopAndBottom = false,
		elBottomTabsDiv, elTabParent, elContentParent, fnSwitchTab, Cookie, sCookieID, iStoredIndex;

	// special case: we add our own clone of the tabs at the bottom and handle clicks / class changes on them separate from the YUI tabview code
	// TODO: get similar support integrated into YUI so we don't have to do this.
	if (sOrientation == 'top+bottom')
	{
		bTopAndBottom = true;
		sOrientation = 'top';
	}

	this.oYUITabView = new YAHOO.widget.TabView(this.aDef.ID, { orientation: sOrientation });

	aTabs = this.oYUITabView.get('tabs');
	if (!aTabs)
		return;

	this.oTabs = {};
	U.ForEach(aTabs, function(oTab) 
	{
		// create a mapping from tab id => YUI Tab object
		this.oTabs[oTab.get('href').substr(1)] = oTab;
	}, this);

	if (bTopAndBottom)
	{
		elTabParent = this.oYUITabView.getElementsByClassName('yui-nav', 'ul' )[0];
		elContentParent = this.oYUITabView.getElementsByClassName('yui-content')[0];
		if (elTabParent && elContentParent)
		{
			// we can make use of the YUI tab-styles by creating a container div for the bottom tabs with the yui-navset-bottom class
			elBottomTabsDiv = document.createElement('div');
			elBottomTabsDiv.className = 'yui-navset-bottom';
			elBottomTabsDiv.appendChild(elTabParent.cloneNode(true));
			Dom.insertAfter(elBottomTabsDiv, elContentParent);

			fnSwitchTab = function(oEvent, iIndex)
			{
				Evt.stopEvent(oEvent);
				this.oYUITabView.set('activeIndex', iIndex);
			}
			U.ForEach(Dom.getChildren(elBottomTabsDiv.firstChild), function(elTab, iIndex)
			{
				var oTab, fnPrevAddClass, fnPrevRemoveClass;

				oTab = this.oYUITabView.getTab(iIndex);

				Evt.on(elTab, 'click', fnSwitchTab, iIndex, this);

				fnPrevAddClass = oTab.addClass;
				oTab.addClass = function (sClass)
				{
					Dom.addClass(elTab, sClass);
					fnPrevAddClass.apply(this, arguments);
				}
				fnPrevRemoveClass = oTab.removeClass;
				oTab.removeClass = function (sClass)
				{
					Dom.removeClass(elTab, sClass);
					fnPrevRemoveClass.apply(this, arguments);
				}
			}, this);
		}
	}

	aTabLinks = Dom.getElementsByClassName('tabviewlink', 'a');
	if (aTabLinks.length)
	{
		Evt.on(aTabLinks, 'click', function(oEvent)
		{
			var elTarget = Evt.getTarget(oEvent),
				sTabID = elTarget.href.replace(/.*#/, '');

			if (!L.isUndefined(this.oTabs[sTabID]))
			{
				this.oYUITabView.set('activeTab', this.oTabs[sTabID]);
				Evt.stopEvent(oEvent);
			}
		}, this, true);
	}

	sCookieID = this.GetAttribDefault('StoreSelectedTab');
	if (sCookieID)
	{
		Cookie = YAHOO.util.Cookie;
		iStoredIndex = Cookie.getSub('TabViewSelectedTab', sCookieID, Number);
		if (iStoredIndex)
		{
			this.oYUITabView.set('activeIndex', iStoredIndex);
		}

		this.oYUITabView.on('activeIndexChange', function(oEvent)
		{
			var oExpiry = new Date();
			oExpiry.setUTCFullYear(oExpiry.getUTCFullYear() + 1);

			Cookie.setSub('TabViewSelectedTab', sCookieID, this.oYUITabView.get('activeIndex'), { expires: oExpiry });
		}, this, true);
	}
}
oBlock.prototype.EnableTab = function(sTabID, bEnable)
{
	if (L.isUndefined(bEnable))
		bEnable = true;

	if (!L.isUndefined(this.oTabs[sTabID]))
		this.oTabs[sTabID].set('disabled', !bEnable);
}
oBlock.prototype.DisableTab = function(sTabID)
{
	this.EnableTab(sTabID, false);
}

oBlock.prototype.AddTabClass = function(sTabID, sClass)
{
	if (!L.isUndefined(this.oTabs[sTabID]))
		this.oTabs[sTabID].addClass(sClass);
}

oBlock.prototype.RemoveTabClass = function(sTabID, sClass)
{
	if (!L.isUndefined(this.oTabs[sTabID]))
		this.oTabs[sTabID].removeClass(sClass);
}



})();
