DB_Tab = 
function (tab_header_id, image_path,image_type)
{
	// Member Variables
	this.id
	this.menu;
	this.images = {};
	this.content = "";
	this.scrollTop = 0;
	this.imageCount = 0;
	this.imagesProcessed = 0;
	this.init(tab_header_id, image_path,image_type);
}

// Initialize the tab object
DB_Tab.prototype.init = 
function (tab_header_id, image_path,image_type)
{
	// Get the tab_header's DOM element
	var temp_menu = document.getElementById(tab_header_id);
	if (!temp_menu)
	{ // If DOM element does not exist
		return false;
	}
	else 
	{ // Set the menu if it exists
		this.menu = temp_menu;
		this.id = tab_header_id;
	}

	// Get the images for the tab_menu
	// Right now there is only support for on/off. Default is off
	var temp_path = trim(String(image_path)) + trim(String(tab_header_id));

	/* Backwards Compatibility */
	if (!image_type)
	{
		image_type = '';
	}

	if (image_type != 'none')
	{
		if (image_type == 'off' || image_type == 'both' || image_type == '')
		{
			var off_image = this.loadImage(temp_path+"_off.gif");
			this.images["off"] = off_image;
		}

		if (image_type == 'both' || image_type == '')
		{
			var on_image = this.loadImage(temp_path+"_on.gif");
			this.images["on"] = on_image;
		}
		if (image_type == 'active' || image_type == '')
		{
			var active_image = this.loadImage(temp_path+"_active.gif");
			this.images["active"] = active_image;
		}
	}

	// Set Default Image
	this.changeImage("off");

	// tab_header elements can have their rel set to the id of the content
	// we check that first, then the default case.
	var content_id = this.menu.rel;
	if (content_id)
	{
		var temp_content = document.getElementById(content_id);
		if (temp_content) 
		{
			this.content = temp_content;
		}
		else
		{
		}
	}
	else 
	{ // Try default content_id which is _tab replaced with _content
		if (/_tab$/.test(tab_header_id))
		{
			content_id = tab_header_id.replace(/_tab$/,"_content");
			temp_content = document.getElementById(content_id);
		}
		// Check if content exists
		if (temp_content)
		{
			this.content = temp_content;
		}
		else 
		{ // No Content
			this.content = "No content Available";
		}
	}
}
// Preload an image
DB_Tab.prototype.loadImage = 
function(image_href)
{
	var obj = this;
	obj.imageCount++;
	var temp_image = new Image();
	temp_image.valid = true;
	temp_image.onerror = function () {
		this.valid = false;
		obj.imagesProcessed++;
	}
	temp_image.onload = function () {
		obj.imagesProcessed++;
	}
	temp_image.src = image_href;
	return temp_image;
}
	
// Change Image. Only runs after all images have been processed
DB_Tab.prototype.changeImage =
function (mode)
{
	var self = this;
	var image_func = 
	function () {
		if (self.images[mode])
		{
			if ((self.images[mode].complete && self.images[mode].valid) )
			{
				var bg_image = "url("+self.images[mode].src+")";
				self.menu.style.backgroundImage = bg_image;
			}
		}
	}
	self.image_load_event(image_func);
}

// Takes a function and only runs it once all images have been loaded
DB_Tab.prototype.image_load_event = 
function (func)
{
	var self = this;
	if (self.imageCount <= self.imagesProcessed)
	{
		func();
	}
	else 
	{
		setTimeout(function(){self.image_load_event(func);}, 100);
	}
}

DB_Tab.prototype.addClass = 
function (className)
{
	var obj = this.menu;
	var cls = obj.className;
	if (!cls)
	{
		obj.className = className;
		return true;
	}
	if (cls == className)
	{
		return false;
	}
	var classes = obj.className.split(" ");
	for (var i = 0; i < classes.length; i++)
	{
		if (classes[i] == className)
		{
			return false;
		}
	}
	obj.className = (cls + " " + className);
	return true;
}

DB_Tab.prototype.removeClass =
function (className)
{
	var obj = this.menu;
	var cls = obj.className;
	if (cls.length == 0)
	{
		return false;
	}
	if (cls == className)
	{
		obj.className == "";
		return true;
	}
	var new_class = [];
	var classes = obj.className.split(" ");
	var count = 0;
	for (var i=0; i < classes.length; i++)
	{
		if (classes[i] != className)
		{
			new_class[count] = classes[i];
			count++;
		}
	}
	obj.className = new_class.join(" ");
	return true;
}

Tab_Controller = 
function (tab_container, image_path,image_type,defaultTab)
{
	// Member Variables
	this.container;
	this.active;
	this.viewer;
	this.tabs = {};
	this.image_path;
	this.tab_menu;

	this.init(tab_container, image_path,image_type,defaultTab);
}

//-------------------------------------------------------------------
Tab_Controller.prototype.init =
function (tab_container,image_path,image_type, defaultTab)
{
	this.image_path = image_path;

	// Find Tab Container
	if (typeof(tab_container) == 'string')
		this.container = document.getElementById(tab_container);
	else if (typeof(tab_container) == 'object') 
	{
		this.container = tab_container;
	}
	if (!this.container)
	{
		log("Tab container not found",tab_container)
		return false;
	}

	// Get the tab_viewer, may add ability to handle multiple ones
	this.viewer = getElementsByTagAndClass(this.container, "div", "tab_viewer")[0];
	if (!this.viewer)
	{
	}

	// Get the tab_menu if it exists
	var temp_tab_menu = getElementsByTagAndClass(this.container, "*", "tab_menu")[0];
	if (temp_tab_menu) 
	{
		this.tab_menu = temp_tab_menu;
	}

	// Get the tab menu items. class: tab_header
	var temp_menus = [];
	temp_menus = getElementsByTagAndClass(this.container, "*", "tab_header");
	// Use them to build tabs

	var default_tab;
	for (var i=0; i < temp_menus.length; i++)
	{
		var tab_name = temp_menus[i].id;
		this.tabs[tab_name] = new DB_Tab(tab_name, this.image_path,image_type);
		var oTabContent = this.tabs[tab_name].content;
		oTabContent.parentNode.removeChild(oTabContent);
		this.viewer.insertBefore(oTabContent, this.viewer.firstChild);
		// Default Tab
		if (tab_name == defaultTab || i == 0)
		{
			default_tab = tab_name;
		}
	}
	this.activate(default_tab);
}

Tab_Controller.prototype.attachMenuHandler =
function (evt, func)
{
	for (var key in this.tabs)
	{
		EventUtil.addEventHandler(this.tabs[key].menu, evt, func)
	}
}

Tab_Controller.prototype.activate =
function (tab_id)
{
	var oTab = this.tabs[tab_id];
	var self = this;
	if (oTab)
	{
		if (this.tab_menu)
		{
			var image_func = 
			function () {
				if (oTab.images["active"])
				{
					if (oTab.images["active"].complete && oTab.images["active"].valid)
					{
						var bg_image = "url("+oTab.images["active"].src+")";
						self.tab_menu.style.backgroundImage = bg_image;
					}
				}
			}
			oTab.image_load_event(image_func);
		}

		oTab.changeImage("on");
		oTab.addClass("active");
		for (var tempTab in this.tabs)
		{
			this.tabs[tempTab].content.style.display = "none";
		}
		oTab.content.style.display = "block";
		this.active = oTab;
	}
}

Tab_Controller.prototype.eventHandler =
function ()
{
	var self = this;
	var ret_func = function ()
	{
		var evt = EventUtil.getEvent();
		var oTarget = evt.target;
		var tab_id = oTarget.id;
		if (tab_id != self.active.id)
		{
			self.active.changeImage("off");
			self.active.removeClass("active");
			self.activate(tab_id);
		}
	}
	return ret_func;
}

init_all_tabs =
function (image_path) {
	if (typeof(DB_Flash) == 'undefined' && !image_path)
		{ return false; }

	var oTabs = getElementsByTagAndClass(document ,"*", "tab_container"); 
	if (oTabs && oTabs.length > 0)
	{
		for (var i = 0; i < oTabs.length; i++)
		{
			var oTab = oTabs[i];
			var cls = oTab.className.split(' ');
			if (cls.length == 1)
			{ // fast path. tab_container must have more than one class
				continue;
			}
			var image_check_list = ["none","active","off","both"];
			var event_check_list = ["mouseover", "mousedown", "click"];
			if (!image_path)
			{
				var image_path = "/fls/"+DB_Flash.DB_OEM_ID+"/custom/tabs/";
			}
			/*
				Should have parsed this out into a function 
				set these vars to null so i don't get last
				tabs data
			*/
			var image_mode = null; 
			var event_mode = null;
			var default_tab = null;
			// Find what image_mode the tab uses
			// Could be optimized but oh well
			for (var j = 0; j < cls.length; j++)
			{
				var cl = cls[j];
				if (!image_mode)
				{
					for (var k = 0; k < image_check_list.length; k++)
					{
						if (cl == image_check_list[k])
						{ 
							image_mode = cl;	
						}
					}
				}
				if (!event_mode)
				{
					for (var k = 0; k < event_check_list.length; k++)
					{
						if (cl == event_check_list[k])
						{ 
							event_mode = cl;	
						}
					}
				}
				if (!default_tab)
				{
					if (/^default_\w*_tab$/.test(cl))
					{
						default_tab = cl.substring(8);
						log('default_tab', default_tab);
					}
				}
			}
			if (image_mode && event_mode)
			{// If we have both an image_mode and event_mode: tab is valid
				if ( typeof(window.Tab_Overlord) == 'undefined')
				{
					window.Tab_Overlord = {};
				}
				// Get a name for the new Tab Controller
				var tab_name = "tab_" + i;
				if ( oTab.id )
				{
					tab_name = oTab.id;	
				}
				else if (oTab.parentNode && oTab.parentNode.id)
				{
					tab_name = oTab.parentNode.id;
				}
				var oTabC = new Tab_Controller(oTab,image_path,image_mode, default_tab);
				window.Tab_Overlord[tab_name] = oTabC;
				oTabC.attachMenuHandler(event_mode, oTabC.eventHandler());
				log(tab_name, "Tab Controller instantiated by init_all_tabs()");
				log("image path:",image_path,"image_mode:",image_mode,"event_mode:",event_mode);
			}
		}
	}

}

/*
	oTabs = new Tab_Controller("schedule_results","/images/14800/custom/tabs/","active");
	oTabs.attachMenuHandler("mouseover", oTabs.eventHandler());
*/
