(function($) {
		
	$.fn.extend({
		makeTabs: function(options) {
			return this.each(function() {
				$.wvTabs.init($(this), $.extend({}, $.fn.makeTabs.defaults, options));
			});
		}
	});
			
	$.fn.makeTabs.defaults = {
		resize: true,
		maxHeight: 0
	}

	$.extend({
		
		wvTabs: {
			
			_uID: 0,
			settings: [],
			
			init: function(jQobj, settings)	{
				// Save settings
				this.settings[this._uID] = settings;
				// Create the new tab container and stick it above the existing items
				this.createTabContainer(jQobj);
				// Add the relevant classes needed
				this.setTabClasses(jQobj);
				// Set size options (before they're hidden otherwise the height is 0px!)
				this.setSize(jQobj, this._uID);
				// turn off all blocks apart from first one
				this.setUp(jQobj);
				// bind click event to each a to display relavant more block
				this.setTabEvents(jQobj);
				this._uID++;
			},
			
			createTabContainer: function(jQobj)	{
				var tabContainer = '';
				$(jQobj).find('h2').each(function(i)	{
					tabContainer += '<li><h2><a href="#">' + $(this).text() + '</a></h2></li>' + '\n'; 
					$(this).remove();
				}); 
				$(jQobj).prepend('<div class="tab_block"> <ul> ' + tabContainer + ' </ul> </div>');
			},
			
			setTabClasses: function(jQobj)	{
				$('div.tab_block li:first', $(jQobj)).addClass('current first');
				$('div.tab_block li:last', $(jQobj)).addClass('last');
			},
			
			setSize: function(jQobj, uID)	{
				// Pointer to this plugin
				var pointer = this;
				// Only do this if it's set to resize to the 'biggest'
				if(pointer.settings[uID].resize == 'biggest')	{
					// Find the biggest height
					$('div.active_block ul', $(jQobj)).each(function()	{ 
						if($(this).height() > pointer.settings[uID].maxHeight)	pointer.settings[uID].maxHeight = $(this).height(); 
					})
					// Set them all to this height
					$('div.active_block ul', $(jQobj)).css('height', pointer.settings[uID].maxHeight);
				}
			},
			
			setUp: function(jQobj)	{
				$('div.active_block', $(jQobj)).hide().eq(0).show();
			},
			
			setTabEvents: function(jQobj)	{
				// Pointer to this plugin
				var pointer = this;
				// Loop through each tab and set the event
				$('div.tab_block a', $(jQobj)).each(function() {
					$(this).bind('click', function() {
						// Hide current, show new
						$('div.active_block', $(jQobj)).hide().eq($('div.tab_block a', $(jQobj)).index(this)).show();
						// Sort the 'current' class out
						$('div.tab_block li', $(jQobj)).removeClass('current').eq($('div.tab_block a', $(jQobj)).index(this)).addClass('current');
						// If IE6, fix any PNGs (only once)
						if ($.browser.msie && $.browser.version<7 && !$(this).hasClass('fixed')) 	{
							$(this).addClass('fixed');
							$('div.active_block', $(jQobj)).eq($('div.tab_block a', $(jQobj)).index(this)).find('img[@src$=.png],input[@src$=.png]').ifixpng();
							
						}
						return false;
					});
				});										   
			}
			
		}
		
	});
	
})(jQuery);
