/* Copyright it-motive AG, Duisburg */

function InitXMedia(title, tabID)
{
	setPageTitle(title);

	try { InitTabs(tabID);          } catch(e){ alert(e); }
	try { InitXMediaApplications(); } catch(e){}

	// Eventuell vorhandene Aktivierungsfunktionen der xMediaApplications aufrufen - nur für Applikationen, die nicht in Tabs sind!
	try
	{
		var xMediaTabs         = document.getElementById("xMediaTabs");
		var xMediaApplications = document.getElementsByTagName("xMediaApplicationAnchor");
		var xMediaApplicationsInTabs, i, j, inTab;

		if(xMediaTabs) xMediaApplicationsInTabs = xMediaTabs.getElementsByTagName("xMediaApplicationAnchor");
		else           xMediaApplicationsInTabs = new Array();

		for(i=0; i<xMediaApplications.length; i++)
		{
			inTab = false;
			for(j=0; j<xMediaApplicationsInTabs.length; j++) if(xMediaApplicationsInTabs[j] == xMediaApplications[i]){ inTab = true; break; }
			if(!inTab && xMediaApplications[i].id) ActivateXMediaApplication(xMediaApplications[i].id);
		}
	}
	catch(e){}
}
function ExitXMedia(title)
{
	setPageTitle("");
}


function setPageTitle(title)
{
	var w = window;

	while(w.parent != w)
	{
		w = w.parent;

		try
		{
			f = eval("w.setPageTitle");

			if(f)
			{
				f(title);
				return;
			}
		}
		catch(e){ }
	}
}

function InitTabs(id)
{
	var tabHeaders = document.getElementById("xMediaTabHeaders");
	var tabID      = "";

	// Wenn es tabs gibt, gibt es tabHeader
	if(tabHeaders && tabHeaders.childNodes && tabHeaders.childNodes.length)
	{
		for(var i=0; i<tabHeaders.childNodes.length; i++)
		{
			var idAttr = tabHeaders.childNodes[i].getAttribute("id");
			if(idAttr && idAttr.search(/^xMediaTabHeader_/) == 0)
			{
				tabID = idAttr.replace(/^xMediaTabHeader_/, "");

				if(id) { if(id == tabID) break; }
				else     break;
			}
		}
	}

	// Einen Tab zeigen
	if(tabID) ShowTab(tabID);
}
function ShowTab(id)
{
	var tab       = document.getElementById("xMediaTab_"+id);
	var tabHeader = document.getElementById("xMediaTabHeader_"+id);
	var node, i, xMediaApplications;

	if(tab && tabHeader)
	{
		// nix tun, wenn der Tab nicht sichtbar ist
		if(tab.style.display != "none") return;

		// erstmal alle Tabs unsichtbar machen
		for(i=0; i<tab.parentNode.childNodes.length; i++)
		{
			node = tab.parentNode.childNodes[i];
			if(node != tab) node.style.display = "none";
		}
		// erstmal CSS class für alle Tab-Header auf passiv setzen
		for(i=0; i<tabHeader.parentNode.childNodes.length; i++)
		{
			node = tabHeader.parentNode.childNodes[i];
			if(node != tabHeader && node.id.search(/^xMediaTabHeader_/) == 0)
			{
				node.className = node.className.replace(/active\b/i, "passive");
			}
		}
		// Aktiven Tab sichtbar machen
		tab.style.display = "block";

		// CSS class für aktiven Tab-Header setzen
		tabHeader.className = tabHeader.className.replace(/passive\b/i, "active");

		// Eventuell vorhandene Aktivierungsfunktionen der xMediaApplications aufrufen
		xMediaApplications = tab.getElementsByTagName("xMediaApplicationAnchor");
		for(i=0; i<xMediaApplications.length; i++) if(xMediaApplications[i].id) ActivateXMediaApplication(xMediaApplications[i].id);
	}
}

function ActivateXMediaApplication(id)
{
	// Wenn es eine Funktion "XMediaApplication_#id#_Activated()" gibt, soll sie ausgeführt werden (a la OnShow Event-Handler)
	try		{ eval("XMediaApplication_"+id+"_Activated")(); }
	catch(e){ }
}
