
if (!window['$hnj'] || !$hnj.registry.included('/scripts/components/widgets.js')) { 
$hnj.namespace('$hni.widgets');

(function () {
	var tabIdCounter = 0;

	var removeEmptyTabs = function(id)
	{
		var tabs = $hnj('div#' + id + ' div.hni_WidgetTabFrame_HeadBar li');
		for (var t = 0; t < tabs.size(); t++) {
			contentTabs = $hnj('div#WidgetTab_' + tabIdCounter + " div.hni_WidgetTabFrame_ContentWrap div.hni_WidgetTabFrame_Content");
			var contentTab = contentTabs[t];
			var emptyTab = true;
			$hnj('li', contentTab).each(function () { 
				if (this.innerHTML.length > 0)	
					emptyTab = false; 
			});
			if (emptyTab)
			{
				$hnj(tabs[t]).remove();
				$hnj(contentTab).remove();
			}
		}
	}

	var fixHeights = function (tabContents) {
		var maxHeight = 0;
		if (tabContents.size() > 0) {
			// find max height of all divs
			for (var t = 0; t < tabContents.size(); t++) {
				$hnj(tabContents[t]).show();
				if (tabContents[t].offsetHeight > maxHeight) {
					maxHeight = tabContents[t].offsetHeight;
				}
				if (t > 0) {
					$hnj(tabContents[t]).hide();
				}
			}
			//reset height on all divs that aren't as tall as max height
			for (var t = 0; t < tabContents.size(); t++) {
				var tab = $hnj(tabContents[t]);
				tab.attr("originalHeight", parseInt(tab.height()));
				// height must be more than 0. Prevents any problems if the entire widget is hidden by default.
				if (maxHeight > 0) {
					tab.css("height", maxHeight + "px");		
				}
			}
			$hnj(tabContents[0]).show();
		}
		return maxHeight;
	};

	var getFixedHeight = function (contentWrap) {
		var fixedHeight = contentWrap.css('height');
		fixedHeight = /^(\d+)px$/.exec(fixedHeight || '');
		if (fixedHeight && fixedHeight.length == 2) {
			fixedHeight = parseFloat(fixedHeight[1]);
			if (fixedHeight < 0 || isNaN(fixedHeight)) {
				return false;
			}
			return fixedHeight;
		}
		return false;
	};

	var onLessClick = function (hoverTab, event) {
		if (event) {
			event.stopImmediatePropagation();
			event.stopPropagation();
			event.preventDefault();
		}
		hoverTab.empty().hide();
	};

	var allHoverTabs = [];

	var expandTab = function(tabContents, hoverTab, offset) {
		$hnj.each(allHoverTabs, function (index, tab) {
			tab.empty().hide();
		});
		if (!tabContents || !hoverTab) return;
		var clone = $hnj(tabContents).clone(true);
		hoverTab.empty().append(clone).show();

		var lessLink =
			$hnj('<a href="#" class="hni_WidgetTabMore">Collapse</a>');
		lessLink.appendTo(hoverTab).click(function (event) {
			onLessClick(hoverTab, event);
		});

		hoverTab.css('left', offset.left);
		hoverTab.css('top', offset.top);
	};

	$hni.widgets.discoverTabs = function() {
		
		$hnj('div.hni_WidgetTabFrame').each(function () {
			tabIdCounter++;
			var id = this.id = 'WidgetTab_' + tabIdCounter;
			var activeTabIndex = 0;
			var cursor = 0;
			var frame = $hnj(this);

			removeEmptyTabs(id);
			var tabs = $hnj('div#' + id + ' div.hni_WidgetTabFrame_HeadBar li');
			if (tabs.size() < 1) return;

			var contentWrap = $hnj('div#' + id + ' div.hni_WidgetTabFrame_ContentWrap');
			if (!contentWrap || contentWrap.length < 1) {
				throw "cannot find proper tab widget markup for ID " + id;
			}
			contentWrap.css('position', 'relative');

			$hnj(tabs[activeTabIndex]).addClass('hni_WidgetTabFrame_Tab_active');

			var tabContents = $hnj('div#' + id + ' div.hni_WidgetTabFrame_Content');
			var maxHeight = fixHeights(tabContents);
			var fixedHeight = getFixedHeight(contentWrap);

			var hoverTab = $hnj(
				'<div class="hni_WidgetTabMoreHover" style="z-index: 101010;"> \
				 </div>');
			hoverTab.empty().hide();

			var moreLink =
				$hnj('<a href="#" class="hni_WidgetTabMore">Expand</a>');

			if (fixedHeight && fixedHeight < maxHeight) {
				moreLink.appendTo(contentWrap);
				moreLink.click(function (event) {
					event.stopImmediatePropagation();
					event.stopPropagation();
					event.preventDefault();
					expandTab(
						tabContents[activeTabIndex], hoverTab, contentWrap.position());
				});
				hoverTab.appendTo(this);
				hoverTab.width(contentWrap.width());
				allHoverTabs.push(hoverTab);
				
				if ($hnj(tabContents[0]).attr("originalHeight") <= fixedHeight) {
					moreLink.hide();
				} else {
					moreLink.show();
				} 
			}

			$hnj(tabs).each(function () {
				var tabIndex = cursor++;
				$hnj(this).click(function (event) {
					onLessClick(hoverTab, event);
					if (tabIndex != activeTabIndex) {
						if (tabs.size() > tabIndex) {
							$hnj(tabs[activeTabIndex]).removeClass('hni_WidgetTabFrame_Tab_active');
							$hnj(tabs[tabIndex]).addClass('hni_WidgetTabFrame_Tab_active');
						}
						if (tabContents.size() > tabIndex) {
							$hnj(tabContents[activeTabIndex]).hide();
							var newTab = $hnj(tabContents[tabIndex]);
							newTab.show();
							// recalculate height after showing new tab because the height may not be fixed
							fixedHeight = getFixedHeight(contentWrap);
							if (fixedHeight &&
							    newTab.attr("originalHeight") <= fixedHeight) {
								moreLink.hide();
							} else {
								moreLink.show();
							}
						}
						activeTabIndex = tabIndex;
					}
				});
			});
		});
	};
})();

$hnj(document).ready(function () {
	setTimeout($hni.widgets.discoverTabs, 150);
});

}
if (window['$hnj']) { $hnj.registry.register('/scripts/components/widgets.js', false); };


