/**
 *	Structure describing a chat tab
 */
var Tab = function(id) {
	this.id = id; 				// the identifier, or channel name respectively
	this.unseen = false;	// true if new contents are present
	this.messageCount = 0;
	this.toString = function() { 
		return this.id;
	}
}

var tabs = new Array();						// storage of all open tabs
tabs.push(new Tab(TAB_DEFAULT));	// add default tab as first element

var currentTab = TAB_DEFAULT;			// the id of the currently shown tab

/**
 *	Switches to the chat tab identified by a given HTML component
 */
function openTab(tab) {
	var id = tab.id.substring(tab.id.lastIndexOf(".") + 1);
	showTab(id);
}

/**
 *	Closes a chat tab identified by a given HTML component
 */
function closeTab(tab) {
	var id = tab.id.substring(tab.id.lastIndexOf(".") + 1);
	
	// update tab array
	var old = tabs;
	tabs = new Array();
	for(var i = 0; i < old.length; i++) {
		if (old[i] != id) 
			tabs.push(old[i]);
	}
	
	// show default tab
	showTab(TAB_DEFAULT);
	
	// remove associated chat box from dom tree
	var chat = document.getElementById("chat.channel." + id);
	chat.parentNode.removeChild(chat);
	
	// update tab display
	updateTabList();
}

/**
 *	Shows the chat tab with a given ID
 */
function showTab(id) {
    if (document.getElementById("chat.channel." + id)) {
    	// update style of last activated tab
        document.getElementById("chat.channel." + currentTab).style.visibility = "hidden";
        document.getElementById("tabcontrol.channel." + currentTab).className = "tabcontrol-channel-tab-inactive";
	
        // update style of new activated tab
        document.getElementById("chat.channel." + id).style.visibility = "visible";
        document.getElementById("tabcontrol.channel." + id).className = "tabcontrol-channel-tab-active";
	
        // update unseen state/alert color
        if(id != "") {
            tabs[tabs.indexOf(id)].unseen = false;
        }
        document.getElementById("tabcontrol.channel." + id).style.color = "";
	
        // set variables
        currentTab = id;
        // focus
        if (!document.getElementById("txt").disabled)
            document.getElementById("txt").focus();
    }
}

/**
 *	Updates the graphical display of the tabs of tabbed chat
 */
function updateTabList() {
	var showTabControl = tabs.length > 1 ? true : false;
	
	// determine/update top position of chat panel
	var top = showTabControl == true ? 17 : 0;
	document.getElementById("chat.panel").style.top = top + "px";
	
	// initialize variables
	var chat = document.getElementById("chat");
	var tabDefault = document.getElementById("tabcontrol.channel." + TAB_DEFAULT);
	
	// clear tab list
	for(var i = chat.childNodes.length - 1; i > 0; i--) {
		if(chat.childNodes[i].id && chat.childNodes[i].id.indexOf("tabcontrol.") == 0 && chat.childNodes[i].id != tabDefault.id)
			chat.removeChild(chat.childNodes[i]);
	}
	
	// if tab control shall be shown
	if(showTabControl) {
		// show default tab
		tabDefault.style.visibility = "visible";
		
		// iterate over tabs and display them: do NOT draw tab 0 which is DEFAULT_TAB
		for(var i=1; i<tabs.length; i++) {
			// prepare id string
			var id = "tabcontrol.channel." + tabs[i];
			
			// prepare closer icon
			var closer = document.createElement("img");
			closer.src = "img/close.png";
			closer.id = id;
			closer.onclick = function() { 
				closeTab(this);
				return false;
			};
			
			// create tab 
			var e = document.createElement("div");
			e.id = id;
			e.onclick = function() { 
				openTab(this);
			};
			
			e.appendChild(document.createTextNode(tabs[i]));
			e.appendChild(new Spacer(10));
			e.appendChild(closer);
			
			// decide on styling
			if(tabs[i] != currentTab) {
				e.className="tabcontrol-channel-tab-inactive";
			} else {
				e.className="tabcontrol-channel-tab-active";
			}
			
			if (tabs[i].unseen == true) {
				e.style.color = TAB_ALERT_COLOR;
			}
			
			// append tab to dom tree
			chat.appendChild(e);
		}
	} else {
		// hide default tab			
		tabDefault.style.visibility = "hidden";
	}
}

