//	$Id$

var ibmHomePageTabs = {};
var IBMHomePageTabs = ibmHomePageTabs;

ibmHomePageTabs.init = function () {
	var con = document.getElementById('ibm-home-tasks'), dl = [];
	var nl = [
		document.getElementById('ibm-tab-content-0'),
		document.getElementById('ibm-tab-content-1'),
		document.getElementById('ibm-tab-content-2'),
		document.getElementById('ibm-tab-content-3'),
		document.getElementById('ibm-tab-content-4'),
		document.getElementById('ibm-tab-content-5')
	];
	for (var i = 0; i < nl.length; i++)
		if (nl[i].nodeName.toLowerCase() == 'div')
			dl[dl.length] = nl[i];
	for (var i = 1; i < dl.length; i++)
		dl[i].style.display = 'none';
	nl = ibmCommon.getElementsInClass(con, 'ibm-tab-content-submenu');
	for (var i = 0; i < nl.length; i++) ibmHomePageTabs.setMenuStyles(nl[i]);
};

ibmHomePageTabs.setMenuStyles = function () {
	if (arguments.length < 1) return;
	var el = arguments[0];
	el.style.backgroundColor = '#FFFFFF';
	el.style.border = '#CCC solid 1px';
	el.style.display = 'none';
	el.style.position = 'absolute';
	el.style.width = '208px';
	el.style.zIndex = '9';
}

ibmHomePageTabs.deactivateAll = function () {
	var el, con = document.getElementById('ibm-home-tasks');
	if (con == null) return;
	var nl = IBMCommon.getElementsInClass(con, 'ibm-active');
	for (var i = 0; i < nl.length; i++) {
 		IBMCommon.deleteClassName(nl[i], 'ibm-active');
		if (nl[i].nodeName.toLowerCase() == 'div')
			nl[i].style.display = 'none';
	}
};

ibmHomePageTabs.focusContent = function (id) {
	var el = document.getElementById(id);
	nl = IBMCommon.getElementsInClass(el, 'ibm-access');
	for (var i = 0; i < nl.length; i++)
		if (nl[i].nodeName.toLowerCase() == 'a') {
			nl[i].focus();
			return;
		}
};

ibmHomePageTabs.setActive = function (ref, id) {
	var nl, pn = ref.parentNode;
	while (pn.nodeName.toLowerCase() != 'li' &&
		pn.nodeName.toLowerCase() != 'body' &&
		pn.nodeName.toLowerCase() != 'html') pn = pn.parentNode;
	if (!IBMCommon.inClass(pn, 'ibm-active')) {
		ibmHomePageTabs.deactivateAll();
		pn.className += ' ibm-active';
		document.getElementById(id).className += ' ibm-active';
		document.getElementById(id).style.display = 'block';
		ibmHomePageTabs.focusContent(id);
	}
};

ibmHomePageTabs.delay = 50;

ibmHomePageTabs.delayedSetActive = function (ref, id) {
	ref._timerId = window.setTimeout(function () {
			ibmHomePageTabs.setActive(ref, id);
		}, ibmHomePageTabs.delay);
};

ibmHomePageTabs.cancelSetActive = function (ref) {
	if (ref._timerId) window.clearTimeout(ref._timerId);
	ref._timerId = null;
};

ibmHomePageTabs.delayedHideHPT = function (menuId) {
	if (!IBMCommon.isDOM) return;
	var menu = document.getElementById(menuId);
	if (menu == null) return;
	menu._timerId = window.setTimeout(function () {
		ibmHomePageTabs.hideHPT(menuId);
	}, ibmHomePageTabs.delay);
};

ibmHomePageTabs.getX = function (el) {
	var x = 0;
	if (el.offsetParent)
		while (el.offsetParent) {
			x += el.offsetLeft;
			el = el.offsetParent;
		}
	else if (el.x) x += el.x;
	return x;
};

ibmHomePageTabs.getY = function (el) {
	var y = 0;
	if (el.offsetParent)
		while (el.offsetParent) {
			y += el.offsetTop;
			el = el.offsetParent;
		}
	else if (el.y) y += el.y;
	return y;
};

ibmHomePageTabs.hideAllHPT = function () {
	if (ibmHomePageTabs.hptCache == null)
		ibmHomePageTabs.hptCache = 
			ibmCommon.getElementsInClass(document.documentElement,
			'ibm-tab-content-submenu');
	for (var i = 0; i < ibmHomePageTabs.hptCache.length; i++)
		ibmHomePageTabs.delayedHideHPT(ibmHomePageTabs.hptCache[i].id);
};

ibmHomePageTabs.hideHPT = function (menuId) {
	if (!IBMCommon.isDOM) return;
	var menu = document.getElementById(menuId);
	if (menu == null) return;
	menu.style.display = 'none';
	if (menu._timerId) menu._timerId = null;
};

ibmHomePageTabs.setPosition = function (menu) {
	if (!menu._triggerId) return;
	var trig = document.getElementById(menu._triggerId);
	var x = ibmHomePageTabs.getX(trig);
	var y = ibmHomePageTabs.getY(trig);
	menu.style.top = (y - menu.offsetHeight + 18) + 'px';
	menu.style.left = (x + trig.offsetWidth) + 'px';
};

ibmHomePageTabs.showHPT = function (ref, menuId) {
	if (!IBMCommon.isDOM) return;
	var menu = document.getElementById(menuId);
	if (menu == null) return;
	var al = menu.getElementsByTagName('a');
	if (al.length < 1) {
		window.location = ref.getAttribute('href');
		return;
	}
	if (menu._timerId) {
		window.clearTimeout(menu._timerId);
		menu._timerId = null;
	}
	if (ref != null) {
		if (!ref.id || ref.id == '' || ref.id == null)
			ref.id = IBMCommon.generateId();
		menu._triggerId = ref.id;
	}
	menu.style.display = 'block';
	ibmHomePageTabs.setPosition(menu);
	//if (al.length > 0) al.item(0).focus();
};

ibmHomePageTabs.hptCache = null;

ibmHomePageTabs.hideSubmenuMarker = function () { 
	var sm = document.getElementById('ibm-interest-dd1'); 
	if (sm == null) return; 
	var al = sm.getElementsByTagName('a'); 
		if (al.length > 0) return; 
		var a = document.getElementById('ibm-interest-dd1-trigger'); 
		a.className = a.className.replace(/ibm-tab-content-submenu-trigger/, '');
};

ibmHomePageTabs.cancelDelayedHideHPT = function (menuId) {
	if (!IBMCommon.isDOM) return;
	var menu = document.getElementById(menuId);
	if (menu && menu._timerId) window.clearTimeout(menu._timerId);
};

