function initializeTabbedWidgets(tabbedWidgetElements)
{
	tabbedWidgetElements.each(function(e) {
		var tabbedItemElements = $(this).children('[id^="tabbedElement"]');
		tabbedItemElements.each(function(index, elem) {
			var tabTitleChildren = $(elem).children('.title');
			if(index > 0) {
				var tabContentChildren = $(elem).children('.content');
				tabContentChildren.hide();
			}
			tabTitleChildren.each(function(index, elem) {
				$(elem).click(function(event) {
					handleTabClick(this);
				});
			});
		})
	});
}

function handleTabClick(titleElem) {
	var titleElem = $(titleElem);
	var showContentElem = titleElem.parent().children('.content');
	titleElem.parent().parent().find('.content:visible').not(showContentElem).slideUp();
	showContentElem.filter(':hidden').slideDown();
}

$(document).ready(function() {
	var tabbedWidgetElements = $('[id^="tabbedElements"]');
	initializeTabbedWidgets(tabbedWidgetElements);
});



mmwWidgetTabbedConstructor = function() {
	this.switchTab = function(tabWrapElem, contentWrapElem, index) {
		deactivateAll(tabWrapElem);
		$($(tabWrapElem).children('.td').get(index)).addClass('active');
		hideAll(contentWrapElem);
		$($(contentWrapElem).children().get(index)).addClass('show');
	}
	var deactivateAll = function(tabWrapElem) {
		$(tabWrapElem).children().removeClass('active');
	}
	var hideAll = function(contentWrapElem) {
		$(contentWrapElem).children().removeClass('show');
	}
}
mmw.widget.tabbed = new mmwWidgetTabbedConstructor();

