/*
Customized javascript for glossary tabs.
*/
jQuery.fn.glossaryTabs = function (selected){
	var sel = selected || 1;
	return this.each(function () {
		var tab = jQuery('.tab');
		var ipl	= 'a[href^=#]';
		var conteiner = jQuery(this);

		// Go through all the in-page links in this
		// and hide all but the selected's contents
		
		conteiner.find(ipl).each(function (i) {
			var link = jQuery(this);
			if ((i + 1) === sel) {
				link.addClass('active_tab');
				jQuery(link.attr('href')).show();
			}
			else {
				jQuery(link.attr('href')).hide();
			}
		});
		
		// When clicking the UL (or anything within)
		tab.click(function (e) {
			var clicked	= jQuery(e.target);
			var link	= false;

			if (clicked.is(ipl)) {
				link = clicked;
			}
			else {
				var parent = clicked.parents(ipl);

				if (parent.length) {
					link = parent;
				}
			}

			if (link) {
				var selected = 	conteiner.find('a.active_tab');

				if (selected.length) {
					jQuery(selected.removeClass('active_tab').attr('href')).hide();
				}

				jQuery(link.addClass('active_tab').attr('href')).show();

				return false;
			}
		});
	});
};