﻿Accordion = function()
{
	return {
		Initialise: function(target, tabs) {
			// Find the tabs in the Accordion
			tabItems = [];
			for (t=0;t<tabs.length;t++) {
				thisTab = tabs[t];
				tabContainer = document.getElementById(thisTab.tabHtmlId);
				if (tabContainer) {
					tabClass = '';
					if (t==0)
						tabClass = 'first';
					else if (t==tabs.length - 1)
						tabClass = 'last';
					titleDiv = null;
					bodyDiv = null;
					divs = tabContainer.getElementsByTagName('div');
					for (d=0;d<divs.length;d++) {
						// Locate the title and body divs in this tab
						thisDiv=divs[d];
						if (thisDiv.className == thisTab.titleClass)
							titleDiv = thisDiv;
						else if (thisDiv.className == thisTab.bodyClass)
							bodyDiv = thisDiv;
					}
					if (bodyDiv && titleDiv) {
						// If we found both title and body, add to accordion
						accordionTab = {};
						accordionTab.title = titleDiv.innerHTML;
						accordionTab.html = bodyDiv.innerHTML;
						accordionTab.cls = tabClass;
						tabItems.push(accordionTab);
					}
				}
			}
			
			if (tabItems.length > 0) {
				// Clear original html out ready for Ext control
				body = document.getElementById(target);
				if (body) {
					body.innerHTML = '';
					var panel = new Ext.Panel({
						applyTo: target,
						layout:'accordion',
						defaults: {
							bodyStyle: 'padding: 5px'
						},
						layoutConfig: {
							titleCollapse: true,
							animate: true,
							activeOnTop: false
						},
						items: tabItems
					});
					panel.doLayout();
				}
			}
		}
	}
}();

iCM.Accordion = Accordion
