﻿/*
Minitab jQuery plugin.

This creates a pseudo tab plugin to use with UL lists. Each
LI should contain a link. If the link starts with # then it will
be used to locate the id of the item to show/hide.

You show show and hide the items because the minitab item do not
hide on initialization.
*/	 
	 (function($)
	 {
	  /* Function linkRadioButton
		 * Enables the radio button when clicking the link near it,
		 * And also triggers the event bound to the link when the 
		 * radio button is clicked
		 */
		function linkRadioButton(listItem) {
			var link = listItem.children("a"),
				input = listItem.children("input[type=radio]");
			
			link.bind("click", function() {
				input.attr("checked","checked");
			});
			
			/* When some one clicks on the radio button the link is also executed */
			input.bind("click", function () {
				if (link[0].hash.length >1) link.trigger('click');
				else window.location.href= link[0].href;
				return true;
			});
		}
		$.fn.minitab = function() {
			this.each(function() {
				/* Lets keep in our scope the set of list items we're
				working with */
				var tabs = $(this);
				
				/* Input manipularion
				 * 1. Select always the first
				*/
				tabs.find("li:first input").attr("checked","checked");
				
				/* Lets process all the list items we find */
				tabs.find('li').each(function () {
					var listitem = $(this);
					
					linkRadioButton(listitem);
					
					/* We collect only the first link for each list item */
					var links = listitem.children('a');
					if (links.length == 0) return;
					
					/* We need a link with a hash value, this will
   					be the id of the element to hide/show */
					var link = links[0];
					if (!link.hash || link.hash.length < 2) return;
					
					/* We get the destination */
					var dest = $(link.hash);
					if (dest.length == 0) return;
					
					/* We hide it */
					//dest.addClass("hidden");
					
					$(link).bind("click", function () {
						if (listitem.hasClass("selected")) return false;
						
						/* Hide the previously selected item */
						tabs.find('li.selected').each(function() {
							var selectedListItem =  $(this);
							selectedListItem.removeClass("selected");
							selectedListItem.children('a').each(function() {
								$(this.hash).addClass("hidden");
							});
						});
						listitem.addClass("selected");
						dest.removeClass("hidden");
						
						/* Stop the event */
						return false;
					});
				});
			});
		};
	 })(jQuery);