/* ------------------------------------------------------------
 * PROJECT        : FHSC Interface Standard
 * FILENAME       : jqtabcontrol.js
 * ------------------------------------------------------------
 * DATE CREATED   : 02 Apr 2007
 * LAST UPDATED   : 03 Apr 2007
 * ------------------------------------------------------------
 * AUTHOR(S)      : Kevin Scholl (http://www.ksscholl.com/)
 * ------------------------------------------------------------ */

$(document).ready(function() {
	$("ul.tabBar li.disabled a").attr("title","This tab is disabled...");
	});

/* ------------------------------------------------------------
 * MAXIMUM NUMBER OF TABS TO DISPLAY IF...
 * ------------------------------------------------------------ */
 
var VISIBLE_TABS =
	($(window).width() <= 800)                               ? 4 : // ...screen real estate smaller than 800px wide
	($(window).width() >  800  && $(window).width() <= 1024) ? 6 : // ...screen real estate between 800px and 1024px wide
	($(window).width() >  1024 && $(window).width() <= 1280) ? 8 : // ...screen real estate between 1024px and 1280px wide
	                                                           10;  // ...screen real estate greater than 1280px wide

/* ------------------------------------------------------------
 * OBJECT PROTOTYPE
 * ------------------------------------------------------------ */

function tabGroup(groupIndex,showTabs) {
	
	// variables
	this.GROUP_INDEX      = groupIndex;
	this.NUM_TABS         = 1; // DO NOT CHANGE (set dynamically at initialization)
	if(showTabs)
		this.SHOW_TABS      = showTabs; // specified number of tabs to show in the interface
	else
		this.SHOW_TABS      = 5; // default number of tabs to show in the interface, if not specified
	this.ACTIVE_TAB       = "";
	this.ACTIVE_SECT      = "";
	this.CURR_TAB         = "";
	this.CURR_SECT        = "";
	this.FOCUS_TAB        = "";
	
	// arrays
	this.TAB_ID_ARRAY     = new Array();
	this.TAB_UC_ARRAY     = new Array();
	
	// functions
	this.getTabs          = getTabs;
	this.initTabs         = initTabs;
	this.setActiveTab     = setActiveTab;
	this.setControls      = setControls;
	this.toggleVisibility = toggleVisibility;
	this.initTabControl   = initTabControl;
	this.runTabControl    = runTabControl;
	this.showSection      = showSection;	
	this.goTab            = goTab;
	
	}

/* ------------------------------------------------------------
 * GET TABS
 * ------------------------------------------------------------ */

function getTabs() {

	// determine number of tabs
	var g = this.NUM_TABS;
	while (document.getElementById(this.GROUP_INDEX + "tab" + g)) {
		this.NUM_TABS = g;
		g++;
		}
		
	// if the number of tabs is less than the max number of tabs allowed to be shown,
	// adjust the max number to be shown down to the number of tabs
	if (this.NUM_TABS < this.SHOW_TABS)
		this.SHOW_TABS = this.NUM_TABS;
		
	//  load the tab IDs into the ID array
	for (i = 0; i < this.NUM_TABS; i++) {
		this.TAB_ID_ARRAY[i] = this.GROUP_INDEX + "tab" + (i + 1);
		}
	
	}

/* ------------------------------------------------------------
 * INITIALIZE TABS
 * ------------------------------------------------------------ */

function initTabs() {
	
	// set all tabs to not display
	for (d = 0; d <= this.NUM_TABS; d++) {
	  $("#" + this.GROUP_INDEX + "tab" + d).hide();
		}
		
	// populate the visible tab array
	if (this.FOCUS_TAB + this.SHOW_TABS > this.NUM_TABS) {
		var m = (this.NUM_TABS + 1) - this.SHOW_TABS;
		for (n = 0; n < this.SHOW_TABS; n++) {
			this.TAB_UC_ARRAY[n] = this.GROUP_INDEX + "tab" + m;
			m++;
			}
		}
	else {
		if (this.FOCUS_TAB == 0) var m = 1;
		else var m = this.FOCUS_TAB;
		for (n = 0; n < this.SHOW_TABS; n++) {
			this.TAB_UC_ARRAY[n] = this.GROUP_INDEX + "tab" + m;
			m++;
			}
		}

	// adjust focus tab setting for active tabs at the end of string
	if (this.FOCUS_TAB > ((this.NUM_TABS + 1) - this.SHOW_TABS))
		this.FOCUS_TAB = ((this.NUM_TABS + 1) - this.SHOW_TABS);
	
	// set the visible tabs to display
	for (p = 0; p < this.SHOW_TABS; p++) {
	  $("#" + this.TAB_UC_ARRAY[p]).show();
		}
		
	}

/* ------------------------------------------------------------
 * SET THE ACTIVE TAB
 * ------------------------------------------------------------ */

function setActiveTab() {
	
	if (this.ACTIVE_TAB != 0) {
		if (this.CURR_TAB != "")
		  $("#" + this.GROUP_INDEX + "tab" + this.CURR_TAB).removeClass("active");
		$("#" + this.GROUP_INDEX + "tab" + this.ACTIVE_TAB).addClass("active");
		this.CURR_TAB = this.ACTIVE_TAB;
		}
  else
	  return;
		
	}

/* ------------------------------------------------------------
 * SET DISPLAY OF THE TAB CONTROLS
 * ------------------------------------------------------------ */

function setControls() {
	
	if ($("#" + this.TAB_ID_ARRAY[0]).css("display") != "none" && $("#" + this.TAB_ID_ARRAY[this.TAB_ID_ARRAY.length - 1]).css("display") == "none") {
		$("#" + this.GROUP_INDEX + "First").attr("src","images/btn_action_rd_first_dim.gif");
		$("#" + this.GROUP_INDEX + "Previous").attr("src","images/btn_action_rd_prev_dim.gif");
		$("#" + this.GROUP_INDEX + "Next").attr("src","images/btn_action_rd_next.gif");
		$("#" + this.GROUP_INDEX + "Last").attr("src","images/btn_action_rd_last.gif");
		}
	else if ($("#" + this.TAB_ID_ARRAY[0]).css("display") == "none" && $("#" + this.TAB_ID_ARRAY[this.TAB_ID_ARRAY.length - 1]).css("display") != "none") {
		$("#" + this.GROUP_INDEX + "First").attr("src","images/btn_action_rd_first.gif");
		$("#" + this.GROUP_INDEX + "Previous").attr("src","images/btn_action_rd_prev.gif");
		$("#" + this.GROUP_INDEX + "Next").attr("src","images/btn_action_rd_next_dim.gif");
		$("#" + this.GROUP_INDEX + "Last").attr("src","images/btn_action_rd_last_dim.gif");
		}
	else {
		$("#" + this.GROUP_INDEX + "First").attr("src","images/btn_action_rd_first.gif");
		$("#" + this.GROUP_INDEX + "Previous").attr("src","images/btn_action_rd_prev.gif");
		$("#" + this.GROUP_INDEX + "Next").attr("src","images/btn_action_rd_next.gif");
		$("#" + this.GROUP_INDEX + "Last").attr("src","images/btn_action_rd_last.gif");
		}
		
	}

/* ------------------------------------------------------------
 * TOGGLE VISIBILITY OF TABS AND CONTROLS
 * ------------------------------------------------------------ */

function toggleVisibility() {
	
	$("#tabGroup" + this.GROUP_INDEX).show();
	$("#tabControl" + this.GROUP_INDEX).css("visibility",(this.NUM_TABS > this.SHOW_TABS) ? "visible" : "hidden");
	
	}

/* ------------------------------------------------------------
 * INITIALIZE TAB CONTROL - THE FIRST FUNCTION
 * ------------------------------------------------------------ */

function initTabControl(activeTab,activeSect) {
	
	this.ACTIVE_TAB  = activeTab;
	this.FOCUS_TAB   = activeTab;
	this.ACTIVE_SECT = activeSect;

	this.getTabs();
	this.initTabs();
  this.setActiveTab();	
  this.setControls();	
	this.toggleVisibility();
	this.showSection();
	
	}

/* ------------------------------------------------------------
 * RUN TAB CONTROL
 * ------------------------------------------------------------ */

function runTabControl(activeTab,activeSect) {
	
	this.ACTIVE_TAB  = activeTab;
	this.ACTIVE_SECT = activeSect;

  this.setActiveTab();
	this.showSection();
	
	}
	
/* ------------------------------------------------------------
 * CONTROL
 * ------------------------------------------------------------ */

function goTab(dir) {
	
	switch (dir) {
		case "first":
			if (this.FOCUS_TAB == 1)
				return;
			else {
				this.FOCUS_TAB = 1;
				this.initTabs();
				}
			break;
		case "prev":
			if (this.FOCUS_TAB == 1)
				return;
			else {
				this.FOCUS_TAB--;
				this.initTabs();
				}
			break;
		case "next":
			if (this.FOCUS_TAB == ((this.NUM_TABS + 1) - this.SHOW_TABS))
				return;
			else {
				this.FOCUS_TAB++;
				this.initTabs();
				}
			break;
		case "last":
			if (this.FOCUS_TAB == this.NUM_TABS)
			  return;
			else {
				this.FOCUS_TAB = ((this.NUM_TABS + 1) - this.SHOW_TABS);
				this.initTabs();
				}
			break;
		}	
		
	this.setActiveTab();
	this.setControls();
	
	}

/* ------------------------------------------------------------
   DISPLAY SELECTED SECTION
 * ------------------------------------------------------------ */

function showSection() {
	
	if (this.CURR_SECT != "")
	  $("#" + this.CURR_SECT).hide();
  $("#" + this.ACTIVE_SECT).show();
	this.CURR_SECT = this.ACTIVE_SECT;
	
	}