var tabs = Array();
var a = document.URL.split("//"); // split at protocol
a = (a[1] ? a[1] : a[0]).split("/");
// select the correct tab, show the div and hide the other divs
function showTab(tabIndex) {
    var idx = 0;
    var tabsList = $("faq_TabsList");
    if(tabsList) {
		if (a[1] == 'Corporate' || a[1] == 'Tests') {
			tabsList.childElements().each(function(e) {
            	var link = e.childElements()[0];
				var eLen = tabsList.childElements().length - 1;
    	        if (idx == tabIndex) {
					if (idx == eLen) {
						e.className = "faq_tab faq_tab_last on";
					} else {
						e.className = "faq_tab on";
					}
    	        }
        	    else {
					if (idx == eLen) {
						e.className = "faq_tab faq_tab_last";
					} else {
						e.className = "faq_tab";
					}
            	}
	            idx++;
    	    });
        	for(idx=0; idx<tabs.length; idx++) {
            	if(tabIndex==idx) {
                	tabs[idx].style.display = "block";
	            }
    	        else {
        	        tabs[idx].style.display = "none";
            	}
	        }
		} else {
			tabsList.childElements().each(function(e) {
				//var link = e.childElements()[0];
				var link = e.getElementsByTagName("A")[0];
				if (idx == tabIndex) {
					link.className = "on";
				}
				else {
					link.className = "";
				}
				idx++;
			});
			for(idx=0; idx<tabs.length; idx++) {
				if(tabIndex==idx) {
					tabs[idx].style.display = "block";
				}
				else {
					tabs[idx].style.display = "none";
				}
			}
		}
    }
    return false;
}


Event.onDOMReady(function() {
    if(!$("faq_openTabs")) 
    {
        return;
    }
    var faqId;
    // first check if we need to show an FAQ direct or not
    if(window.location.search.indexOf("faqid=")>0) {
        var faqQuery = window.location.search.substr(window.location.search.indexOf("faqid=") + 6);
        if(faqQuery.indexOf("&")>0) {
            // trim it
            faqId = faqQuery.substr(0, faqQuery.indexOf("&"));
        }
        else {
            faqId = faqQuery;
        }
    }
    // find the tabs and stick them in an array
    var tabsTotal = 0;
    $("faq_openTabs").childElements().each(function(e) {
        if (e.id.startsWith("faq_tab")) {
            tabs[tabsTotal] = e;
            tabsTotal++;
        }
    });
    var tabToShow;
    // create the accordions
    for (i = 0; i < tabsTotal; i++) {
        var thisTab = new accordion('accordion' + i, {
            classNames: {
                toggle: 'toggler',
                toggleActive: 'toggler_active',
                content: 'element'
            },
            direction: 'vertical'
        });
        // if we need to show an faq direct then show it
        if(faqId) {
            var faqIdx = 0;
            var faqs = $$('#accordion' + i + ' .toggler');
            faqs.each(function(faq) {
                if(faq.id == 'faq' + faqId) {
                    tabToShow = i;
                    thisTab.activate($$('#accordion' + i + ' .toggler')[faqIdx]);
                }
                faqIdx++;
            });
        }
    }
    // now work out which tab to show by default
    if(!tabToShow) {
        tabToShow = 0;
        for (i = 0; i < tabsTotal; i++) {
            var tab = tabs[i];
            if(tab.hasClassName("on")) {
                tabToShow = i;
                break;
            }
        }
    }
    // show the relevant tab
    showTab(tabToShow);

	// and add the print link
	/*if (!window.print) {return;} // Check that the browser supports window.print
	var oTarget = document.getElementById("print_tabs");
	var oList = document.createElement('li');
	var oLink = document.createElement('a');
	var oSpan = document.createElement('span');
	oList.className = 'print'; //added to allow new print icon to be displayed (JH 18/04/08)
	oLink.href = 'http://www.morrisons.co.uk/Food/Healthy-Eating/7-day-meal-planner/7-Day-Meal-Planner-Printer-Version/'; // Make the link focusable for keyboard users
	oSpan.appendChild(document.createTextNode("Print friendly version"));
	/* oLink.onclick = function() {
		// open all tabs
		var tabIndex = 0;
		for(var i=0; i<tabs.length; i++) {
			if(tabs[i].style.display == "block") {
				tabIndex = i;
			}
			tabs[i].style.display = "block";
		}
		// print...
		window.print(); 
		// close them again!
		showTab(tabIndex);
		return false;
	} */ // Return false prevents the browser from following the link and jumping to the top of the page after printing
	/*
	oLink.appendChild(oSpan);
	oList.appendChild(oLink);
	var oPosition = oTarget.childNodes[2]; // Determine where to insert the print link list item
	if(!oPosition) {
		var oPosition = oTarget.lastChild; // This is alternative positioning calculation for IE because it's rubbish
	}
	oTarget.insertBefore(oList,oPosition); // Insert the print link list item in between the other two list items
    */
});
