//		custom Plugin for dynamically loading tabbed content
//		02/18/09 by Neil
//		
//		*needs to be attached to an image (for now)

(function($){
	$.fn.tab = function(contentUrl,options) { // begin extension
		if (contentUrl == null) //make sure the user passes the contentUrl
			return this; // if they don't, return the wrapper unmolested
		var settings = $.extend({
			containerId: "tabContent",
			tabWrapperId: "tabWrapper",
			imageTabs: true,
			onState: "_on.",
			offState: "_off.",
			textOnClass: "selected",
			callback: "true",
			h1: 1,
			h2: 2
		},options || {});
		
		return this.each(function(){ // start the wrapper manipulation
			$(this).css("cursor","pointer")
			.click(function(){ // attach an event to the click
				if (settings.imageTabs) { // if the tabs are images
					var f=settings.offState;
					var n=settings.onState;
					var tabState = $(this).attr("src");
					if (tabState.indexOf(f) > 0) { // the image clicked is in the off state
						$("#" + settings.tabWrapperId + " img").each(function(){ // flip each img to the off state
							$(this).attr("src",$(this).attr("src").indexOf(n) > 0 ? $(this).attr("src").replace(n,f) : $(this).attr("src"));
						});
						$(this).attr("src",$(this).attr("src").replace(f,n)); // flip the clicked img to the on state	
						$("#" + settings.containerId).html("").load(contentUrl,(settings.callback ? $.nav(settings.h1,settings.h2) : function(){})); // load the content in the container
					}
				}
				else { // if the tabs are text
					var n=settings.textOnClass;
					var isActive = $(this).hasClass(n);
					if(!isActive) { // the text tab is in the off state
						$("#" + settings.tabWrapperId + " a").each(function(){ // remove all active classes from the tabs
							$(this).removeClass(n);
						});
						$(this).addClass(n); // add the active class to the selected tab
						$("#" + settings.containerId).html("").load(contentUrl, (settings.callback ? $.nav(settings.h1, settings.h2) : function(){})); // load the content in the container
					}
				}
			});
		});
	}
})(jQuery); 