
jQuery.fn.MV_tabs = function(options)
{
	
	/**
	 * Options
	 * 
	 * @param nav The selector for which ul
	 * to use as the tab navigation.
	 * 
	 * @param navSelectedClass Classname to
	 * apply to the selected tab link
	 * 
	 * @param fx Effect type
	 * 
	 * @param fxSpeed Speed of the transition
	 * 
	 */
	var opt = jQuery.extend({

		nav : ".tabs",
		navSelectedClass : "selected",
		fx : "fade",
		fxSpeed : 150,
		current : $(this).find("> div:first"),
		width : $(this).width(),
		onSwitch : function(){},
		beforeSwitch : function(){},
		afterSwitch : function(){},
		onClick : function(){},
		onLoad : function(){}
		
	}, options);

	var tabs = $(opt.nav);
	var contentWrappers = $(this).find("> div");
	var first = $(this).find("> div:first-child");

	preventSwitch = false;
	
	opt.onLoad.call(this);
	
	/**
	  * Hide all but the first content
	  * elements, and mark the first 
	  * link.
	  */
	$(this).find("> div:not(:first)").hide();

	/**
	  * Set the marker class for the first
	  * link. If an anchor is not set in the
	  * url. Otherwise go to this.
	  */ 
	lurl = document.location.toString();
	lurlmatch = 0;
	if ( lurl.match('#') )
	{
		lurlmatch = 1;
		var lurls = lurl.split("#");
		var firstLinkId = lurls[1];
		//$("a[href='#"+lurls[1]+"']").trigger("click");
		runFx(lurls[1]);
		opt.current = $("#" + lurls[1]);
		//alert("l: " + lurls[1]);
	}
	else
	{
		var firstLinkId = first.attr("id");	
	}
	$("a[href='#" + firstLinkId + "']").toggleClass(opt.navSelectedClass);

	/**
	  * Set some basic styles for the content
	  * wrappers.
	  */
	contentWrappers.css({

		"width" : opt.width
		//"position" : "absolute"
		
	});

	/**
	  * Bind a click event to all tab
	  * links.
	  */ 
	tabs.find("a").click( function(event) {

		preventSwitch = false;

		/**
		  * Get the ID of the content wrapper
		  * that should be activated.
		  */
		var href = $(this).attr("href").replace("#", "");
		
		/**
		  * Prevent from jumping out of the
		  * current page when link is clicked.
		  */
		event.preventDefault();

		opt.onClick.call(this, opt.current.attr("id") );
		
		if ( !preventSwitch )
		{
			
			/**
			  * Set the selected marker class for
			  * the selected link.
			  */
			tabs.find("a").removeClass(opt.navSelectedClass);
			$(this).toggleClass(opt.navSelectedClass);
			
			/**
			  * This checks if you try to activate
			  * an already visible content wrapper.
			  * Do nothing if current link is clicked. 
			  */
			if ( $("#" + href).is(":hidden") )
			{
				
				/**
				  * Run the effects for switching the
				  * content wrappers.
				  */
				runFx(href);
	
			}
	
			/**
			  * Set current chosen tab
			  */
			opt.current = $("#" + href);
			  
		}
		
	});

	/**
	  * Controller function for firing
	  * correct effect for the content
	  * switch. 
	  */
	function runFx(href)
	{

		opt.beforeSwitch.call(this, href);
		
		/**
		  * opt.fx is the specified effect
		  * option for the plugin.
		  */
		switch (opt.fx)
		{

			case "fade":

				fade(href);
				
			break;

			default:

				fade(href);
				
			break;
			
		}
		  
		/**
		  * Run callback function after
		  * the transition (or during?),
		  * if this is specified in the
		  * plugin options.
		  */
		opt.onSwitch.call(this, href);

	}

	/**
	  * Effect function for fading
	  * transition.
	  */
	function fade(href)
	{
		
		opt.current.hide();
		$("#" + href).fadeIn(opt.fxSpeed, function() {

			opt.afterSwitch.call(this, href);
			
		});
		
	}
	  
	  this.initialize = function() {
		  //do something ...
		  return this;
	  };

	  this.preventSwitch = function() {
		  
		  preventSwitch = true;
		  
	  };
	  return this.initialize();

}

