var mpnAccordion = {};

mpnAccordion.create = function(config) {
	//Initialize tab indexes.
	$(config.accordionSelector).find(config.itemSelector).attr("tabIndex", -1);
	$(config.accordionSelector).find(".in").find(config.itemSelector).attr("tabIndex", 0);
	
	//Binding aria-expanded state on divs that are hidden or shown for screen readers using bootstrap events for accordion
	//Also setting tabIdex of 0 or -1 to allow navigation via tabs or suppress depending on state of the accordion
	$(config.accordionSelector).on('shown.bs.dropdown', function(event) {
		var dropdown = $(event.target);
		// Set aria-expanded to true
		dropdown.attr('aria-expanded', true);
	
		$originDivElem = $(dropdown).prev(".accordion-heading").find(".accordion-toggle");
		$originDivElem.find("i").removeClass().addClass('accordionToggleIcon icon-chevron-up');
		$originDivElem.attr('aria-label','Click or Use the Enter/Space bar keys to Collapse the Section');
	
		//Remove the  negative tabIndex if set  to allow tab based nav
		dropdown.find(config.itemSelector).each(function() {
			$(this).attr('tabIndex', 0);
		});
	});


	$(config.accordionSelector).on('hidden.bs.dropdown', function(event) {
		var dropdown = $(event.target);
		// Set aria-expanded to false
		dropdown.attr('aria-expanded', false);

		$originDivElem = $(dropdown).prev(".accordion-heading").find(".accordion-toggle");
		$originDivElem.find("i").removeClass().addClass('accordionToggleIcon icon-chevron-down');
		$originDivElem.attr('aria-label','Click or Use the Enter/Space bar keys to Expand the Section');

		//Set a negative tabIndex to exclude from tabbing altogether
		dropdown.find(config.itemSelector).each(function(){
			$(this).attr('tabIndex', -1);
		});
	});		
};
