/* ------------------------------------------------------------
 * PROJECT        : 
 * FILENAME       : jq.buildTabbedNav.js
 * ------------------------------------------------------------
 * LAST UPDATED   : 18 Feb 2010
 * ------------------------------------------------------------
 * AUTHOR(S)      : Kevin Scholl (ksscholl@magellanhealth.com/)
 * ------------------------------------------------------------ */

jQuery.fn.buildTabbedNav = function(settings) {
	settings = jQuery.extend({
		tabClass     : "tabBar",
		current      :  null,
		disabled     :  [],
		cornerBG     : "#FFF",
		cornerRadius :  6,
		delay        :  400,
		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.tabPane").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.tabPane").hide();
					idx = $(this).parent().index();
					$(this).parent().parent().siblings("fieldset.tabPane:eq(" + idx + ")").fadeIn(1000);
					}
 				return false;
				});
			}
		// do not follow links of current or disabled tabs
		else {
			$(this).find("li a").click(function() {
				if($(this).is(".current, .disabled")) {
					return false;
					}
				});
			}

    });

	$(document).ready(function(){
	
		// dropdown navigation
		$("ul.tabBar li").hover(
			function () {
				if (!$(this).find("a").hasClass("disabled"))
					$(this).find(" > ul").delay(settings.delay).slideDown("fast");
				}, 
			function () {
				$(this).find(" > ul").clearQueue().fadeOut("fast");
				}
			);
		// add subnavigation indicator
		$("ul.tabBar li:has(ul)").find("> a")
			.css("padding-right","16px")
			.click(function () {
				return false;
				})
			.append("<span class=\"hasChildren\">&nbsp;</span>");
		// account for current tab status
		$("ul.tabBar li a.current").parent().find("ul").css("background","#EED").find("a").css("border-bottom","1px solid #DDB");
		$("ul.tabBar li ul li:last-child a").css({
			"border-bottom"  : "0",
			"padding-bottom" : "6px"
			});
		// do nothing when clicking disabled tab
		$("ul.tabBar li a.disabled").click(function () {
			return false;
			}).parent("[class]").append("<span class=\"disabled\">&nbsp;</span>");
	
		});

};	