/* ------------------------------------------------------------
 * PROJECT        : 
 * FILENAME       : jq.buildTabbedNav.js
 * ------------------------------------------------------------
 * LAST UPDATED   : 21 Mar 2010
 * ------------------------------------------------------------
 * AUTHOR(S)      : Kevin Scholl (http://www.ksscholl.com/)
 * ------------------------------------------------------------ */

jQuery.fn.buildTabbedNav = function(settings) {
	settings = jQuery.extend({
		tabClass     : "tabBar",
		current      :  null,
		disabled     :  [],
		cornerBG     : "#FFF",
		cornerRadius :  6,
		linkAction   : "page" // "module" or "page"
		}, settings);
	this.each(function() {
		
		// declare class to apply proper styling
		if ($(this + ":not(." + settings.tabClass + ")"))
  		$(this).addClass("" + settings.tabClass + "");
		
		// set specified tab to current state
		if (settings.current != null)
		  $(this).find(" > li:eq(" + settings.current + ") > a").addClass("current");

		// set specified tab(s) to disabled state
		for (i = 0; i < settings.disabled.length; i++) {
			$(this).find(" > li:eq(" + settings.disabled[i] + ") > a").addClass("disabled");
			}

		// apply rounded corners
  	$(this).find("li a").not("ul." + settings.tabClass + " ul li a").cornerz({
		  background : "" + settings.cornerBG + "", 
			radius     :  settings.cornerRadius, 
			corners    : "tl tr"});

		// update tab state and module visibility
		if (settings.linkAction == "module") {
			$(this).siblings("fieldset").css("margin","0").not(":eq(" + settings.current + ")").hide().end();
			$(this).find("li a").click(function() {
				if(!$(this).is(".current, .disabled")) {
					$(this).addClass("current").parent().siblings().find("a").removeClass("current");
					$(this).parent().parent().siblings("fieldset").hide();
					idx = $(this).parent().index();
					$(this).parent().parent().siblings("fieldset:eq(" + idx + ")").fadeIn(1000,function(){
						$(this).removeAttr("filter");
						});
					}
 				return false;
				});
			}
		// do not follow links of current or disabled tabs
		else {
			$(this).find("li a").click(function() {
				if($(this).is(".disabled")) return false;
				else location.href = $(this).attr("href");
				});
			}

    });
	};

