firebug_on = false;

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

// Initialize the tab object
DB_Tab.prototype.init = 
function (tab_header_id, image_path)
{
	// Get the tab_header's DOM element
	var temp_menu = document.getElementById(tab_header_id);
	if (!temp_menu)
	{ // If DOM element does not exist, write to firebug console
		if (firebug_on)
		console.warn(tab_header_id.toString() + " does not exists");
		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));
	var on_image = new Image();
	var off_image = new Image();
	on_image.src = temp_path+"_on.gif";
	off_image.src = temp_path+"_off.gif";
	// Might put in checks to see if images worked, for now just put the images in
	this.images["on"] = on_image;
	this.images["off"] = off_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.innerHTML;
			if (firebug_on)
			console.log("%s's content id: %s (rel)",tab_header_id,content_id);
		}
		else
		{
			if (firebug_on)
			console.warn("rel set for %s does not exist", tab_header_id);
		}
	}
	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.innerHTML;
			if (firebug_on)
			console.log("%s's content id: %s (default)",tab_header_id,content_id);
		}
		else 
		{ // No Content
			this.content = "No content Available";
			if (firebug_on)
			console.warn("No content for %s", tab_header_id);
		}
	}
	// Debug
	if (firebug_on)
	console.log("****DEBUG*****\ntab_id: %s content_id: %s\nimage_on: %s \nimage_off: %s\nContent:%s",tab_header_id, content_id, this.images["on"].src, this.images["off"].src, this.content);
}
// Change image
DB_Tab.prototype.changeImage =
function (mode)
{
	// Check if image exists
	if (this.images[mode])
	{
		var bg_image = "url("+this.images[mode].src+")";
		this.menu.style.backgroundImage = bg_image;
	}
	else
	{
		if (firebug_on)
		console.warn("Image change failed! mode: %s", mode);
	}
}

Tab_Controller = 
function (tab_container_id, image_path)
{
	// Member Variables
	this.container;
	this.active;
	this.viewer;
	this.tabs = {};
	this.image_path;

	this.init(tab_container_id, image_path);
}

//-------------------------------------------------------------------
Tab_Controller.prototype.init =
function (tab_container_id,image_path)
{
	this.image_path = image_path;

	// Find Tab Container
	this.container = document.getElementById(tab_container_id);
	if (!this.container)
	{
		if (firebug_on)
		console.error("********* ERROR ***********\ntab_container_id: %s\nError: Tab Container does not exist", tab_container_id);
		return false;
	}

	// Get the tab_viewer, may add ability to handle multiple ones
	this.viewer = getElementsByClassName(this.container, "div", "tab_viewer")[0];
	if (!this.viewer)
	{
		if (firebug_on)
		console.warn("tab_container_id: %s\nNo viewer found", tab_container_id);
	}

	// Get the tab menu items. class: tab_header
	var temp_menus = [];
	temp_menus = getElementsByClassName(this.container, "h2", "tab_header");
	if (firebug_on)
	console.log("number of tab_header items: %s", temp_menus.length);
	// Use them to build tabs
	for (var i=0; i < temp_menus.length; i++)
	{
		var tab_name = temp_menus[i].id;
		if (firebug_on)
		console.log("tab_name #%s: %s", i, tab_name);
		this.tabs[tab_name] = new DB_Tab(tab_name, this.image_path);
		// Default Tab
		if (i == 0)
		{
			this.activate(tab_name);
		}
	}
}

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];
	if (oTab)
	{
		oTab.changeImage("on");
		this.viewer.innerHTML = oTab.content;
		this.active = oTab;
	}
}
Tab_Controller.prototype.clickHandler =
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.activate(tab_id);
		}
	}
	return ret_func;
}

// ------------------------------------------------------------------
function load_tab()
{
	//dale = new db_tab("latest_tab", "/images/10300/custom/tabs/");
	word = new Tab_Controller("bulldog_media","images/10300/custom/tabs/");
	word.attachMenuHandler("click", word.clickHandler());
}
