// Generic Tab Helpers

function switchTab(tabsID, tabName) {
	//tabName will always be the correct, up to date id of the tab we wish to switch to
	if (currentTab(tabsID) != tabName) {
		$(tabsID + '_tab_names').childElements().each(function(element) {
			replaceClassName(element, 'tab_true','tab_false');
		});
		$(tabsID + '_tab_contents').childElements().each(function(element) {
			replaceClassName(element, 'tab_true','tab_false');
		});
		var tab = $(tabsID + '_' + tabName + '_name');
		if (tab.className.match('more_tab')) {
			var switchTabElement = $(tabsID + "_more_tabs").previous();
			var switchTabHtml = switchTabElement.innerHTML;
			var switchTabId = switchTabElement.getAttribute('tabId');
			var switchTabDomId = switchTabElement.id;
			var switchTabOnClick = switchTabElement.onclick;
			switchTabElement.setAttribute('tabId', tab.getAttribute('tabId'));
			switchTabElement.update(tab.innerHTML);
			switchTabElement.onclick = tab.onclick;
			switchTabElement.id = tab.id;
			tab.setAttribute('tabId', switchTabId);
			tab.update(switchTabHtml);
			tab.onclick = switchTabOnClick;
			tab.id = switchTabDomId;
			tab = switchTabElement;
			hideMoreTab(tabsID);
		}	
		replaceClassName($(tabsID + "_" + tabName + "_contents"), 'tab_false', 'tab_true');
		replaceClassName(tab, 'tab_false', 'tab_true');
		/* sneaky little TM insert here..., but only because prototyype cant tell me the offset/height of an element unless it is visible..*/
		//if (TM) TM.resizeSidebarScrolls();
	}
}

/*automatically checks for a hash to switch to a specific div :-) */
function autoSwitchTab(tabsID) {
	var hash = location.hash.replace(/#/,""); //.replace(/_/g, " ");
	if (hash != "" && $(tabsID + '_tab_names') && $(tabsID + '_' + hash + '_name')) {switchTab(tabsID, hash);}
}

function currentTab(tabsID) {
	var tab =  $(tabsID + '_tab_names').select('.tab_true')[0].getAttribute('tabId');
	return tab;
}

function showMoreTab(tabsID) {
	$(tabsID + '_more_tab_content').show();
}

function hideMoreTab(tabsID) {
	$(tabsID + '_more_tab_content').hide();
}

function toggleMoreTab(tabsID) {
	if ($(tabsID + '_more_tab_content').visible()) {
		hideMoreTab(tabsID);
	} else {
		showMoreTab(tabsID);
	}
}

/* sideBar psuedo tabs, 
	we are assuming tabsID has same id as sidebar
	this will activate the sidebar of the same name first */

function sidebarTab(tabsID, tabName) {
	switchSidebar(tabsID);
	var element = $(tabsID + '_tab_names').select("a[tabId='" + tabName + "']")[0];
	var elementClick = element.onclick;
	elementClick();
}

function switchSidebar(sidebarId) {
	var oldSidebar = currentSidebar();
	var newSidebar = $('sidebar_' + sidebarId);
	var oldMenu = $(oldSidebar.id.split("_")[1] +'_menu');
	var newMenu = $(sidebarId +'_menu');

	if (oldSidebar) {oldSidebar.className = 'sidebar_false';}
	if (newSidebar) {newSidebar.className = 'sidebar_true';}
	if (oldMenu) {oldMenu.removeClassName('active_sidebar_menu')}
	if (newMenu) {newMenu.addClassName('active_sidebar_menu')}	
	
	if (TM) {TM.showSidebar();}
}

function currentSidebar() {
	//warning - returns element, NOT id of sidebar
	return $('sidebar').select('.sidebar_true')[0];
}
