
function load_ajax_lib() {
    if (!document.head) {
	document.write('<script src="/ajax.js"></script>');
    } else {
	var head = document.head;
	var script = document.createElement('script');
	script.src = '/ajax.js';
	head.appendChild(script);
    }
}
load_ajax_lib();

function TAB(title, url) {
  this.title = title;
  this.url = url;
  this.title_obj;
  this.body_obj;
  this.parent;
}

/**
 * Creates a tab collection
 * @param {String} obj_id The id of the object to dump all the tab data in
 * @param {String} title The title of the tab
 * @param {String} url The url to pull the tab data from
 * @param {String} default_value The value to show for a tab while the data is being loaded.
 *   only used when default_value != ""
 * @param {Bool} save_history Should tab clicks be counted as part of the browser history
 * @param {String} cache if the cache is "on" "off" or "pre"
 *    "off" the tabs will refresh the tab every time a user clicks on it
 *    "pre" will pre-cache all the tabs
 *    "on" (default) the tab will load the first time a user clicks on it
 * @param {Bool} set_title set the title of the page to the title of the tab
 * @param {String} pre_title only used when set_title is true.  Set the title of the page to pre_title + the name of the tab
 */ 
function TABS(obj_id, title, url, default_value, save_history, cache, set_title, pre_title) {
  this.tabs = {};
  this.tab_obj = document.getElementById(obj_id);
  this.tab_obj.innerHTML = "";
  this.update(title, url);
  if (cache) {
      this.cache = cache;
  } else {
      this.cache = "on";
  }
  this.save_history = save_history;
  this.default_value = default_value;
  this.set_title = set_title;
  if (pre_title) {
      this.pre_title = pre_title;
  } else {
      this.pre_title = "";
  }
}

TABS.prototype.update = function(title, url) {
  this.tabs[title] = new TAB(title, url);
};

TABS.prototype.select = function(tab) {
    var set_history = false;
    if (this.tabs[tab].title_obj.className != "TAB_SELECTED") {
	set_history = true;
    }
    for (name in this.tabs) {
	if (this.tabs[name].title_obj != undefined) {
	    this.tabs[name].title_obj.className = "TAB";
	}
	if (this.tabs[name].body_obj != undefined) {
	    this.tabs[name].body_obj.className = "TAB_BODY";
	}
    }
    this.tabs[tab].title_obj.className = "TAB_SELECTED";
    this.tabs[tab].body_obj.className = "TAB_BODY_SELECTED";
    clean_ajax();
    var is_loading = false;
    var has_killed = false;
    if (this.set_title == true) {
	document.title = this.pre_title +tab;
    }
    if (this.cache == "off" || this.tabs[tab].body_obj.innerHTML == this.default_value) {
	for (var i = 0; i < ajax_array.length; i++) {
	    if (ajax_array[i].req.readyState < 4 && !ajax_array[i].killed) {
		if (ajax_array[i].url == this.tabs[tab].url + "&from_ajax=true" ||
		    ajax_array[i].url == this.tabs[tab].url + "?from_ajax=true") {
		    is_loading = true;
		    break;
		} else {
		    // If this if is removed than when cache == off all ajax commands before the
		    // call to load the current tab are killed.
		    // This may help improve the speed at which the current tab is rendered.
		    if (this.cache == "off") {
			ajax_array[i].kill();
			has_killed = true;
		    }
		}
	    }
	}
	if (!is_loading) {
	    this.load(tab, set_history && this.save_history);
	}
	// Right now this if can never be hit since has_kill can only be set if this.cache == off;
	if (this.cache != "off" && has_killed) {
	    for (name in this.tabs) {
		if (name != tab && this.tabs[name].body_obj.innerHTML == this.default_value) {
		    var found = false;
		    for (var i = 0; i < ajax_array.length; i++) {
			if (ajax_array[i].req.readyState < 4 && !ajax_array[i].killed) {
			    if (ajax_array[i].url == this.tabs[name].url + "&from_ajax=true" ||
				ajax_array[i].url == this.tabs[name].url + "?from_ajax=true") {
				found = true;
			    }
			}
		    }
		    if (!found) {
			this.load(name, false);
		    }
		}
	    }
	}
    } else {
	if (set_history) { 
	    var tabs_obj = this;
	    function hist() {
		tabs_obj.resetTabs(tab);
	    }
	    setTabHistory(hist, tab);
	} 
    }
};

TABS.prototype.load = function(tab, set_history) {
    var body_obj = this.tabs[tab].body_obj;
    if (this.cache == "off" || body_obj.innerHTML == this.default_value) {
	if (this.default_value != "") {
	    body_obj.innerHTML = this.default_value;
	}
	var url = this.tabs[tab].url;
	var ajax = new AJAX(url, body_obj.id);
	ajax.save_history = false;
	ajax.resultObject = body_obj;
	ajax.main();
    } 
    if (set_history) { 
	var tabs_obj = this;
	function hist() {
	    tabs_obj.resetTabs(tab);
	}
	setTabHistory(hist, tab);
    }  
};

TABS.prototype.resetTabs = function(tab_id) {
    if (this.set_title == true) {
	document.title = this.pre_title +tab_id;
    }
    var tab_obj = this;
    function onload(y) {
	return function() {
	    tab_obj.select(y);
	}
    }
    for (name in this.tabs) {
	this.tabs[name].title_obj = document.getElementById(this.tabs[name].title_obj.id);
	this.tabs[name].body_obj = document.getElementById(this.tabs[name].body_obj.id);
	this.tabs[name].title_obj.onclick = onload(name);
	if (name == tab_id && this.tabs[name].body_obj.innerHTML == this.default_value) {
	    this.load(tab_id, false);
	}
    }
    // Send the request for these after the current tab has started to be loaded.
    for (name in this.tabs) {
	if (this.cache == "pre" && name != tab_id && this.tabs[name].body_obj.innerHTML == this.default_value) {
	    this.load(name, false);
	}
    }
}

TABS.prototype.rendertabs = function() {
  var is_default_value = true;
  var tab_obj = this;
  function onload(y) {
    return function() {
      tab_obj.select(y);
    }
  }

  for (name in this.tabs) {
    var title_obj = document.createElement('div');
    var body_obj = document.createElement('div');
    this.tabs[name].title_obj = title_obj;
    this.tabs[name].body_obj = body_obj;
    this.tabs[name].parent = this;
    title_obj.tab = this.tabs[name];

    title_obj.className = "TAB";
    body_obj.className = "TAB_BODY";
    title_obj.id = "TITLE_TAB." + name;
    body_obj.id = "TAB." + name;
    title_obj.innerHTML = name;
    body_obj.innerHTML = this.default_value;
  }
  
  var title_wrapper = document.createElement('div');
  title_wrapper.id = "TAB.TITLE_WRAPPER";
  title_wrapper.className = "TAB_TITLE_WRAPPER";
  this.tab_obj.appendChild(title_wrapper);
  for (name in this.tabs) {
    title_wrapper.appendChild(this.tabs[name].title_obj);
    this.tabs[name].title_obj.onclick = onload(name);
  }
  for (name in this.tabs) {
      this.tab_obj.appendChild(this.tabs[name].body_obj);
  }
  for (name in this.tabs) {
      if (is_default_value) {
	  this.select(name);
	  is_default_value = false;
      } else if (this.cache == "pre") {
	  this.load(name, false);
      }
  }


};


// Handle the browser history 

var tab_history_array = new Array();
var tab_history_current = tab_history_array.length - 1;
var tab_history_div = "main"; // the id of the object whose html we should remember

function TABHISTORY(refresh_command) {
    if (refresh_command) {
	this.refresh_command = refresh_command;
    } else {
	this.refresh_command = "";
    }
    if (document.getElementById(tab_history_div)) {
	this.html = document.getElementById(tab_history_div).innerHTML;
    }
}

function loadTabHistory(history_id, tab_id) {
    if (document.getElementById(tab_history_div)) {
	if (tab_history_current != history_id) {
	    tab_history_current = history_id;
	    var hist = tab_history_array[history_id];
	    if (hist) {
		var history_div = document.getElementById(tab_history_div);
		history_div.innerHTML = hist.html;
		if (hist.refresh_command != "") {
		    hist.refresh_command();
		}
	    } else {
		var obj = document.getElementById("TITLE_TAB." + tab_id);
		var tab_obj = obj.tab.parent;
		tab_obj.resetTabs(tab_id);
		obj.tab.parent.select(tab_id);
	    }
	}
    }
}

function setTabHistory(command, tab_id) {
    if (document.getElementById(tab_history_div)) {
	while (tab_history_array.length > tab_history_current + 1) {
	    tab_history_array.pop();
	}
	if (!document.getElementById('tabhistory')) {
	    var iframe = document.createElement('iframe');
	    iframe.id = "tabhistory";
	    iframe.style.display = 'none';
	    document.body.appendChild(iframe);
	}
	var history_id = tab_history_array.length;
	tab_history_current = history_id;
	iframeDoc(document.getElementById('tabhistory')).location = "/tab_history.php?history_id=" + history_id + "&tab_id=" + escape(tab_id);
	tab_history_array[history_id] = new TABHISTORY(command);
    }
}
