//Revisions:
//2009-04-22 by Martin Johansson/CIKADO

function menuGet(objA) {
	return GetAncestor(objA,2).getElementsByTagName('UL')[0];
}
function activate(objA) {

	var objUL=menuGet(objA);
	if (!objUL || objUL.style.visibility!='visible') objA.className='menu-highlight';
}
function deactivate(objA) {

	var objUL=menuGet(objA);
	if (!objUL || objUL.style.visibility!='visible') objA.className='';
}

function IFrameFix(boolHide,intTop) {

//	if (strClient!='OPERA' && strClient.indexOf('IE')!=0) return;



	//Added 2006-11-29 by Martin Johansson/CIKADO
	var strUA=navigator.userAgent;
	if (strClient!='OPERA' && strUA && (strUA.indexOf('MSIE 7')>0 || strUA.indexOf('MSIE 8')>0 || strUA.indexOf(' Firefox/')>0)) return;



	var objArrIFrames=document.getElementsByTagName('IFRAME');
	for (var i=0; i<objArrIFrames.length; i++) {

		objArrIFrames[i].style.visibility=(boolHide ? 'hidden' : '');
/*
		if (boolHide && intTop>GetOffsetTop(objArrIFrames[i])	) {
			objArrIFrames[i].style.visibility='hidden';
		}
		else {
			objArrIFrames[i].style.visibility='';
		}
*/

	}
}
function GetOffsetTop(o) {
	var i=0;
	while (o) {
		i+=o.offsetTop;
		o=o.offsetParent;
	}
	return i;
}

var objCurrent=null;
function menuOpen(objA) {

	menuClose(objA);

	var objUL=menuGet(objA);
	if (!objUL) {
		if (objCurrent && objCurrent.style.visibility!='visible') {
			IFrameFix(false);
			objCurrent=null;
		}
		return false;
	}
	

	var objLIs=objUL.childNodes;
	var intMax=0;
	for (var i=0; i<objLIs.length; i++) if (objLIs[i].nodeName!='#text') {
		var objSpan=objLIs[i].getElementsByTagName('span')[0];
		if (objSpan) {
			var intWidth=objSpan.offsetWidth;
			if (intWidth>intMax) intMax=intWidth;
//			if (strClient=='IE5') objSpan.runtimestyle.width='100%';
		}
	}

	var intImg=2+10+2;
	var intBorder=(document.compatMode=='CSS1Compat' ? 0 : 1+1);
	var intPadding=6+6;
	objUL.style.width=(intBorder+intPadding+intMax+intImg)+'px';

	if (objCurrent==null) IFrameFix(true);
// IFrameFix(true,GetOffsetTop(objUL)+objUL.offsetHeight);


	objA.className='menu-selected';
	objUL.style.visibility='visible';
//	objUL.style.display='inline';
	objCurrent=objUL;

	return false;
}

function menuClose(objA) {

	if (objCurrent) {

		while (GetAncestor(objCurrent).className!='menu' && !menuContains(objCurrent,objA)) {
			objCurrent.style.visibility='hidden';
//			objCurrent.style.display='none';
			GetAncestor(objCurrent,2).getElementsByTagName('A')[0].className='';
			objCurrent=GetAncestor(objCurrent,3); //objCurrent.parentNode.parentNode.parentNode;
		}
	}
//	return objCurrent;
}
function menuCloseAll() {
	clearTimeout(intTimeoutId);
	menuClose(null);
	objCurrent=null;

	IFrameFix(false);
}
function menuKeyDown(e) {
	if (!e) e=document.parentWindow.event;
	if (e.keyCode==27) menuCloseAll();
}
function menuContains(objAncestor,obj) {
	while (obj) {
		obj=GetAncestor(obj);
		if (obj==objAncestor) return true;
	}
	return false;
}

var intMainTimeout=10;
var intSubTimeout=100;
var intTimeoutId=null;
function menuOver(objA) {

//	if (strClient!='OPERA') objA.style.cursor=(objA.getAttribute('href') ? 'hand' : 'default');
	activate(objA); 

	var o=GetAncestor(menuGet(objA),4);
	var intTimeout=(o && o.className!='menu' ? intSubTimeout : intMainTimeout) ;

	if (intTimeout>0) {
		if (strClient=='IE5') intTimeoutId=setTimeout("menuOpen(document.all['"+objA.uniqueID+"']);",intTimeout); 
		else if (strClient=='IE5.2') intTimeoutId=setTimeout("menuOpen(document.all['"+objA.sourceIndex+"']);",intTimeout); 
		else intTimeoutId=setTimeout(function() {menuOpen(objA);},intTimeout);
	}
	else menuOpen(objA);
}

function menuOut(objA) {
	clearTimeout(intTimeoutId);
	deactivate(objA)
}

function GetAncestor(obj,intCount) {

	if (!intCount) intCount=1;
	for (var i=0; i<intCount; i++) if (obj) obj=(obj.parentNode ? obj.parentNode : obj.parentElement);
	return obj;
}

try {
	var strClient=GetClient();
	if (strClient!='NS4') {
		if (!menuInit()) {
			if (window.addEventListener) window.addEventListener('load',menuInit,false);
			else if (window.attachEvent) window.attachEvent('onload',menuInit);
			else window.onload=menuInit;
		}
	}
}
catch (e) {
	if (window.intDebug>0) alert('Error: '+e.message);
}

function menuInit() {
	var boolMenusFound=false;
	try {
		var objDivs=document.getElementsByTagName('div');
		for (var i=0; i<objDivs.length; i++) if (objDivs[i].className=='menu') {
			boolMenusFound=true;
			menuSet(objDivs[i]);
		}
	}
	catch (e) {
		if (window.intDebug>0) alert('Error: '+e.message);
	}
	finally {
		return boolMenusFound;
	}
}

function GetClient() {

	if (window.opera) return 'OPERA';
	if (document.all) {
		if (document.compatMode) return 'IE6'; 
		if (document.namespaces) return 'IE5.5'; //document.namespaces will throw an error before window.onload if this script is loaded with defer...
		if (document.documentElement.runtimeStyle) return 'IE5';
		if (document.getElementById) return 'IE5.2'; //Mac		check for getElementsByTagName instead?
		return 'IE4';
	}
	if (document.layers && !document.getElementsByTagName) return 'NS4';

	return ''; //Mozilla or unknown...
}

function menuSet(objMenu) {

	var as=objMenu.getElementsByTagName('A');
	for (var i=0; i<as.length; i++)  new menu(as[i]);

	if (document.addEventListener) document.addEventListener('keydown',menuKeyDown,false);
	else if (document.attachEvent) document.attachEvent('onkeydown',menuKeyDown);
	else document.onkeydown=menuKeyDown;
/*
	if (objMenu.addEventListener) objMenu.addEventListener('blur',menuCloseAll,false);
	else if (objMenu.attachEvent) objMenu.attachEvent('onblur',menuCloseAll);
	else objMenu.onblur=menuCloseAll;
*/
	if (document.addEventListener) document.addEventListener('mousedown',menuCloseAll,false);
	else if (document.attachEvent) document.attachEvent('onmousedown',menuCloseAll);
	else document.onmousedown=menuCloseAll;

	objMenu.onmousedown=function(e) {
		if (!e) e=objMenu.document.parentWindow.event;
		e.cancelBubble=true;
	}
}

function menu(objA) {

	if (strClient=='OPERA') objA.tabIndex=0;
	objA.hideFocus=true;
	objA.onmouseover=function() {if (window.menuOver) menuOver(this);}
	objA.onmouseout=function() {if (window.menuOut) menuOut(this);}
	objA.onfocus=function() {activate(this);}
	objA.onblur=function() {deactivate(this);}
	if (!objA.onclick) objA.onclick=function() {menuOpen(this);}; else objA.onmouseup=function() {menuCloseAll();} 

	var objLI=GetAncestor(objA,2); //objA.parentNode.parentNode;

//Removed 2009-04-22 by Martin Johansson/CIKADO - MSIE 8 have problems if we use this...
//	objLI.onresize=menuCloseAll; //when resizing browser text size...

	if (strClient=='IE5' || strClient=='IE5.5') {

		if (strClient=='IE5') GetAncestor(objA).noWrap=true;
		
		var objUL=GetAncestor(objLI);	
		if (GetAncestor(objUL).className!='menu') objLI.runtimeStyle.marginRight='-16px'; //???
	}
}
