/** 
 * API to use for all the tabbing. 
 * Requires: JQuery 1.2+
 *
 * The default active tab class is 'current'.
 * 
 * Usage:
 *
 *		- Create the api:
 *		<script type="text/javascript">
 *			var instance = tabs_api();
 *			instance.config.prefix = "myprefix";
 *			instance.config.active_class = "myactiveclass";
 *			instance.config.content_frame = "mycontent";
 *			instance.config.map = {
 *				'1' : '/home.html',
 *				'2' : '/new.html'
 *			}
 *
 *          // Set initial selected tab when the page is loaded.
 *			$(function() {
 *				instance.set('1'); 
 *			});
 *		</script>
 *
 *		- Swap to a new tab:
 *		<a id="myprefix_1" href="#" onclick="return(instance.set('1'));">Home</a>	
 *		<a id="myprefix_2" href="#" onclick="return(instance.set('2'));">New</a>	
 *		<div id="mycontent"></div>
 *			
 */
var tabs_api = function(prefix) {

	var obj = {

		/** All the configurable bits of the tabbing. */
		'config' : {

			/** Prefix for page elements for this set of tabs. */
			'prefix' : '',

			/** This class is added to the active tab and removed from the last tab. */
			'active_class' : 'current',

			/** This is the id of the element to put the content into. */
			'content_frame' : null,

			/** 
			 * A series of mappings between id and url to load for the tab content.
			 * Expects data in the form: { id : url, id : url, ... }.
			 */
			'map' : {}
		},

		/** The last tab that was selected (or null) */
		'last' : null,

		/** The last id we saw. */
		'last_id' : null,

		/** Loads a specific tab */
		'set' : function(id) {
			try {
				var last_id = id;
				var url = obj.config.map[id];
				if (url) {
					var id = obj.config.prefix + id;
					if (obj.last != null) 
						obj.last.removeClass(obj.config.active_class);
					var e = jQuery(id);
					e.addClass(obj.config.active_class);
					obj.last = e;
					obj.last_id = last_id;

					// load the content
					if (obj.config.content_frame != null) {
						e = jQuery(obj.config.content_frame);
						e.load(url);
					}
				}
			}
			catch (e) {
			}

			return(false); // For event handlers
		},

		/** Reloads the current url; eg. for updating on a timer. */
		'refresh' : function() {
			if (obj.last_id != null) 
				obj.set(obj.last_id);	
		}
	};

	return(obj);
};
