﻿Accordion = function () {
	var layout;
	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 = "<a href=\"#\" onclick=\"return false\">" + titleDiv.innerHTML + "</a>";

						if (accordionTab.title == "Your Guildford") {
							accordionTab.title = "<a href=\"index.aspx?articleid=362&amp;menuitem=2\">Your Guildford</a>";
						}

						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,
						fill: true,
						layout: 'accordion',
						defaults: {
							bodyStyle: 'padding: 5px; height: auto'
						},
						layoutConfig: {
							titleCollapse: true,
							collapsed: true,
							animate: true,
							activeOnTop: false
						},
						items: tabItems
					});
					// 1. open the services tab by default
					// 2. Get querystring for menuitem if match set the active item else just set zero
					function gup( name ) {
						var regexS = "[\\?&]"+name+"=([^&#]*)";
						var regex = new RegExp( regexS );
						var tmpURL = window.location.href;
						var results = regex.exec( tmpURL );
						if( results == null )
							return 0;
						else
							return results[1];
					}

					var selecteditem = gup("menuitem");

					selecteditem = parseInt(selecteditem);

					panel.getLayout().setActiveItem(parseInt(selecteditem));

					panel.doLayout();
				}
			}
		}
	}
}();

iCM.Accordion = Accordion;
