var menuStatus=false,menuLoaded=false;
var menuName='pangoNav';
var menuId=1;
var menuAction=2; //1=smooth, 2=accel., 3=fade
var thisItemH=0,prevItemH=0;
var thisItemId='',prevItemId='';

function setMenuStyles() {
	if (!document.getElementById) {
		return;
	}
	var h,tA=navigator.userAgent.toLowerCase();
	if (window.opera) {
		if (tA.indexOf("opera 5")>-1 || tA.indexOf("opera 6")>-1) {
			return;
		}
	}
	h='<style type="text/css">.menuContent{display:none;}</style>';
	h+='\n<!--[if lte IE 7]><style>.navMenu,.navMenu div{zoom:100%;}</style><![endif]-->';
	h+='\n<!--[if lte IE 6]><style>.navMenu,.menuContent,.menuSlider a,.menulist a{height:1%;}</style><![endif]-->';
	document.write(h);
}

setMenuStyles();

function initMenu() {
	if (document.getElementById) {
		var menuEl = document.getElementById('menu');
		if (menuEl) {
			menuH = xHeight(menuEl);
			menuOtherH = navmenuH - menuH;
		}
		else {
			return;
		}
		var j,parentId,parentEl,tD,aAnchors,sliderId,anchorId;
		parentId=menuName+menuId;
		parentEl=document.getElementById(parentId);
		if (parentEl) {
			aAnchors=parentEl.getElementsByTagName("A");
			anchorId=menuName+'T'+menuId;
			for (j=0;j<aAnchors.length;j++) {
				if (aAnchors[j].id  &&  aAnchors[j].id.indexOf(anchorId) == 0) {
					aAnchors[j].onclick=function() {
						return setSlide(this);
					};
					if (aAnchors[j].className == 'slideDown') {
						aAnchors[j].menuState=1;
						var wrapperId = aAnchors[j].id.replace('T','W');
						var wrapperEl = document.getElementById(wrapperId);
						if (wrapperEl) {
							thisItemH = xHeight(wrapperEl);
							thisItemId = aAnchors[j].id;
						}
					}
					else {
						aAnchors[j].menuState=0;
					}
					aAnchors[j].menuParams=parentId;
				}
			}
		}
		menuStatus=true;
		menuLoaded=true;
	}
}

function setSlide(anchorEl) {
	anchorEl.blur();
	prevItemId = thisItemId;
	thisItemId = anchorEl.id;
	prevItemH = thisItemH;
	thisItemH = 0;
	var i,j=null,itemId,itemEl,returnVal=true;
	var contentPrefix=menuName+'C'+menuId;
	var contentId=thisItemId.replace('T','C');
	var contentEl=document.getElementById(contentId);
	if (contentEl) {
		returnVal=false;
		var menuEl=document.getElementById(anchorEl.menuParams);
		var menuDiv=menuEl.getElementsByTagName("DIV");
		for (i=0;i<menuDiv.length;i++) {
			if (menuDiv[i].id && menuDiv[i].id.indexOf(contentPrefix)>-1) {
				if (menuDiv[i].id == contentId) {
					j=i;
					if ( anchorEl.className == "slideDown") {
						anchorEl.menuState=0;
						anchorEl.className='';
						hideMenuItem(menuDiv[j]);
					}
					else {
						anchorEl.menuState=1;
						anchorEl.className="slideDown";
						showMenuItem(menuDiv[j]);
					}
				}
				else {
					itemId=menuDiv[i].id.replace('C','T');
					itemEl=document.getElementById(itemId);
					if (itemEl.menuState==1) {
						itemEl.className='';
						itemEl.menuState=0;
						hideMenuItem(menuDiv[i]);
					}
				}
			}
		}
	}
	return returnVal;
}

function showMenuItem(submenuEl) {
	var wrapperId,wrapperEl,isIE5=(navigator.appVersion.indexOf("MSIE 5")>-1);
	if (menuStatus && menuAction == 3) {
		submenuEl.style.display='block';
		menuFade(submenuEl.id,0);
	}
	else if ((menuStatus && menuAction == 1 || menuStatus && menuAction == 2) && !isIE5) {
		wrapperId=submenuEl.id.replace("C","W");
		wrapperEl=document.getElementById(wrapperId);
		if (hasOverflow(submenuEl) || hasOverflow(wrapperEl)) {
			submenuEl.style.display='block';
			return;
		}
		wrapperEl.style.overflow="hidden";
		wrapperEl.style.height="1px";
		submenuEl.style.display='block';
		thisItemH=xHeight(submenuEl);
		var glideWait = checkOpen();
		if (glideWait > 0) {
			if (wrapperEl.glideTimer) {
				clearTimeout(wrapperEl.glideTimer);
			}
			wrapperEl.glideTimer=setTimeout("menuGlide('"+wrapperId+"',1,"+thisItemH+")",glideWait);
		}
		else {
			menuGlide(wrapperId,1,thisItemH);
		}
	}
	else {
		submenuEl.style.display='block';
	}
}

function hideMenuItem(submenuEl) {
	var wrapperH,wrapperId,wrapperEl,isIE5=(navigator.appVersion.indexOf("MSIE 5")>-1);
	if ((menuStatus && menuAction == 1 || menuStatus && menuAction == 2) && !isIE5) {
		wrapperId=submenuEl.id.replace("C","W");
		wrapperEl=document.getElementById(wrapperId);
		if (submenuEl.style.display!="none") {
			if (hasOverflow(submenuEl) || hasOverflow(wrapperEl)) {
				submenuEl.style.display='none';
				return;
			}
			wrapperH=wrapperEl.offsetHeight;
			wrapperEl.style.overflow="hidden";
			menuGlide(wrapperId,wrapperH,0);
		}
	}
	else {
		submenuEl.style.display='none';
	}
}

function hasOverflow(el) {
	var s,m;
	s=el.style.overflow;
	if (!s) {
		if (el.currentStyle) {
			s=el.currentStyle.overflow;
		}
		else if (document.defaultView.getComputedStyle(el,"")) {
			s=document.defaultView.getComputedStyle(el,"").getPropertyValue("overflow");
		}
	}
	m=(s && s == 'auto')?true:false;
	return m;
}

function menuFade(id,op) {
	var d=document.getElementById(id);
	op+=.05;
	op=(op >= 1)?1:op;
	if ((navigator.appVersion.indexOf("MSIE")>-1)) {
		d.style.filter='alpha(opacity='+op*100+')';
 
	}
	else {
		d.style.opacity=op;
	}
	if (op<1) {
		if (d.navMenuF) {
			clearTimeout(d.navMenuF);
		}
		d.navMenuF=setTimeout("menuFade('"+id+"',"+op+")",40);
	}
}

function menuGlide(wrapperId,glideH,targetH) {
	var m,tt,pc=0.5,glideWait=10,glideIncr=20;
	var wrapperEl,submenuId,submenuEl;
	wrapperEl=document.getElementById(wrapperId);
	m=(glideH<=targetH)?0:1;
	if (menuAction == 1) {
		tt=Math.abs(parseInt(Math.abs(targetH)-Math.abs(glideH)));
		glideIncr=(tt*pc<1)?1:tt*pc;
	}
	glideIncr=(m == 1)?glideIncr*-1:glideIncr;
	wrapperEl.style.height=glideH+"px";
	if (glideH >= targetH) {
		if (targetH <= 0) {
			submenuId=wrapperEl.id.replace("W","C");
			submenuEl=document.getElementById(submenuId);
			submenuEl.style.display="none";
			wrapperEl.style.height="auto";
		}
		else {
			wrapperEl.style.height="auto";
		}
		checkClosed();
	}
	else {
		glideH+=glideIncr;
		if (m == 0) {
			glideH=(glideH >= targetH)?targetH:glideH;
		}
		else {
			glideH=(glideH<=targetH)?targetH:glideH;
		}
		if (wrapperEl.glideTimer) {
			clearTimeout(wrapperEl.glideTimer);
		}
		wrapperEl.glideTimer=setTimeout("menuGlide('"+wrapperId+"',"+glideH+","+targetH+")",glideWait);
	}
}

function checkClosed() {
	if ((thisItemId == prevItemId && thisItemH == 0) || (thisItemH == 0 && prevItemH == 0)) {
		menuH = menuH - prevItemH;
		if (typeof(resetStretch) == 'function') {
			resetStretch();
			scroll(0,0);
		}
	}
}

function checkOpen() {
	var glideWait = 0;
	if (thisItemH == prevItemH) {
		return glideWait;
	}
	menuH = menuH + (thisItemH - prevItemH);
	//alert('menuH='+menuH+'\nthisItemH='+thisItemH+'\nprevItemH='+prevItemH);
	if (typeof(fitStretch) == 'function') {
		glideWait = fitStretch();
	}
	return glideWait;
}

addLoadEvent(initMenu);

