(function($) {
	
	$.fn.tabs = function()
	{
		// Number tabs
		this.each(function(){
			$(this)
				.children('ul')
				.children('li:not(.clear)')
				.each(function(i) {
					$(this).attr('index',i);
				});
				
			$(this)
				.children('.tabbed')
				.children('div')
				.each(function(i) {
					
					$(this).attr('index',i);
					
					// Hide all but first
					if (i != 0) {
						$(this).hide();
					}
					
				});
		});
		
		// Set events
		function tab_over()
		{
			$(this).children('div').fadeTo('fast', 0.8);
			return false;
		}
		
		function tab_out()
		{
			$(this).children('div').fadeTo('fast', 0.5);
			return false;
		}
		
		function tab_click()
		{
			
			// Items we need
			var $this = $(this);
			var index = $this.attr('index');
			var $parent = $this.closest('.tabs');
			var $content = $parent.find('div[index='+index+']');
			var $content_siblings = $parent.find('.tabbed > div:visible');
			var $siblings = $parent.find('li:not(.clear)');
			
			// If it's already selected, return
			if ($this.hasClass('sel') && $content.is(':visible')) {
				return false;
			}
					
			// Unselect all the other tabs and then select this	
			$siblings.removeClass('sel');
			$this.addClass('sel');
			
			// Hide visible siblings and show the selected one
			$content_siblings.stop().fadeOut(200, function(){
                $content.fadeIn('fast', function(){
                    if (jQuery.browser.msie) {
                        $this.get(0).style.removeAttribute('filter');
                    }
                });
            });
			return false;
		}
		
		this.each(function() {	
			// Set Hover Event for tabs
			$(this)
				.find('.tab')
				.parent()
				.hover(tab_over,tab_out)
				.click(tab_click);
		});
		
		// Click the first one
		$('.tabs ul li:first-child').click();
	};
	       
})(jQuery);