(function($){
	$.fn.Mtabs = function()
	{
		function setActiveTab(tab) 
		{
			var $tabs = $(tab).parents('.tabs').find('a');
			//remove old class "active"
			$tabs.removeClass('active');
			//set active tab class "active"
			$(tab).addClass('active');
			//set tabs content invisible
			$tabs.each(function(){
				$($(this).attr('href')).css('display', 'none');
			});
			//display active tab content
			$($(tab).attr('href')).css('display', '');
		};
		function toggleId(hash)
		{
			if ($(hash).length)
				$(hash).attr('tempid', hash).removeAttr('id');
			else
				$('[tempid="'+hash+'"]').attr('id', hash.substr(1)).removeAttr('tempid');
		}
		function setHash(hash) {
			toggleId(hash);
			location.hash = hash;
			toggleId(hash)
		}
		return this.each(function()
		{
			var $this = $(this);
			//initialize active tab
			var hashObject = $('a[href='+location.hash+']', $this);
			if (hashObject.length) {
				setActiveTab(hashObject);
				var hash = location.hash;
				location.hash = '';
				setHash(hash)
			}
			//or select first tab as active
			else {
				setActiveTab($('a:first', $this));
			}
			//click event for tab changing
			$('a', $this).live('click', function(event) {
				event.preventDefault();
				setActiveTab($(this));
				//set anchor
				var hash = $(this).attr('href');
				setHash(hash)
			});
		});
	};	
})(jQuery);
