var oMenus = new Array();
var smActive = null;
var smTID = null;
var smOverTID = null;
var scrollTID = null;
var isOver = false;
var noSbActive  = -1;

SM_MIN_WIDTH = 180;
SM_PADDING = 3;
SM_PADDING_RIGHT = 15;

SM_TIME_OUT = 400;
SM_TIME_MENU_HIDE = 800;

SM_SCROLL_VALUE = 2;
SM_SCROLL_TIME = 10;

MM_A_ID = "jmContainer";
MM_TEXT_ID = "jmBody";

px = "px";

function getBodyClientHeight() {
	var h = 0;
	if ( isOpera ) h = document.getElementsByTagName("body")[0].clientHeight;
	else h = document.documentElement.clientHeight;
	return h;
}

function getBodyScrollTop() {
	var t = 0;
	if ( document.documentElement && document.documentElement.scrollTop ) t = document.documentElement.scrollTop;
	else if ( self.pageYOffset ) t = self.pageYOffset;
	else if ( document.body ) t = document.body.scrollTop;
  return t;
}

function getRelX( strObj ){
	var strRefObj = ( getRelX.arguments[1] ) ? getRelX.arguments[1] : "";
	var oObj = getObj( strObj );
	var iX = 0;
	
	while ( (oObj.offsetParent != null ) && ( oObj.tagName != "BODY" ) ){ 
		iX += oObj.offsetLeft;
		oObj = oObj.offsetParent;
		if ( ( strRefObj != "" ) && ( oObj.id == strRefObj ) ) {
			 break;
		}
	}
	return iX;
}

function getRelY( strObj ){
	var strRefObj = ( getRelY.arguments[1] ) ? getRelY.arguments[1] : "";
	var oObj = getObj( strObj );
	var iY = 0;
	
	while ( ( oObj.offsetParent != null ) && ( oObj.tagName != "BODY" ) ){ 
		iY += oObj.offsetTop;
		oObj = oObj.offsetParent;
		if ( ( strRefObj != "" ) && ( oObj.id == strRefObj ) ) {
			 break;
		}
	}
	return iY;
}

function setSmScrollTop( menuObj, value ) {
	if ( isOpera ) {
		o = getObj("op" + menuObj.num);
		if ( o ) {
			o.style.top = value + px;
			o.posTop = value;
		}
	}
	else menuObj.menuBody.scrollTop = value;
}

function smReset( obj ) {
	setSmScrollTop( obj, 0 );
	if ( obj.arrUp && obj.arrDown ) {
		obj.arrUp.className = "arrup disable";
		obj.arrDown.className = "arrdn";
	}
}

function smScroll(tObj) {
	if ( isOpera ) obj = getObj("op" + tObj.parentNode.num);
	else obj = tObj.parentNode.menuBody;
	if ( tObj.className.indexOf( "arrup" ) > -1 ) {
		if ( ( isOpera && obj.posTop > -1 ) || ( !isOpera && obj.scrollTop < 1 ) ) {
			tObj.className = "arrup disable";
			setSmScrollTop( tObj.parentNode, 0 );
			smStopScroll();
			return;
		}
		d = ( isOpera ) ? 1 : -1;
	} else {
		if ( ( isOpera && ( obj.posTop < tObj.parentNode.menuBody.offsetHeight - obj.offsetHeight + 1 ) ) || ( !isOpera && obj.scrollTop+obj.offsetHeight > obj.scrollHeight - 1 ) ) {
			to = (isOpera) ? tObj.parentNode.menuBody.offsetHeight - obj.offsetHeight : obj.scrollHeight;
			tObj.className = "arrdn disable";
			setSmScrollTop( tObj.parentNode, to );
			smStopScroll();
			return;
		}
		d = ( isOpera ) ? -1 : 1;
	}
	scrollVal = SM_SCROLL_VALUE * d;
	
	if ( isOpera ) {
		obj.posTop += scrollVal;
		obj.style.top = obj.posTop + px;
	}
	else obj.scrollTop += scrollVal;
	if ( tObj.parentNode.arrUp.className == "arrup disable" ) tObj.parentNode.arrUp.className = "arrup";
	if ( tObj.parentNode.arrDown.className == "arrdn disable" ) tObj.parentNode.arrDown.className = "arrdn";
}

function smStartScroll( event ) {
	tObj = ( isIE ) ? window.event.srcElement : event.currentTarget;
	clearTimeout(smTID);
	oContain = (isOpera) ? tObj.parentNode.parentNode :  tObj.parentNode;
	if ( oContain.className.indexOf( "sMain" ) > -1 ) {
		if  ( smActive != null ) smHideActive();
		if ( noSbActive > -1 ) {
			oMenus[0].menuItems[noSbActive - 1].className = "noSubmenu";
			noSbActive = -1;
		}
	}
	scrollTID = setInterval(function() { smScroll( tObj ); }, SM_SCROLL_TIME );
}

function smStopScroll() {
	if ( scrollTID ) clearInterval( scrollTID );
}

function setSmHeight( menuObj, height ) {
	if (height < 1 || height >= menuObj.height ) {
		menuObj.style.height = menuObj.height + px;
		menuObj.menuBody.style.height = menuObj.height + px;
		if ( menuObj.arrUp && menuObj.arrDown ) {
			menuObj.removeChild(menuObj.arrUp);
			menuObj.removeChild(menuObj.arrDown);
		}
		menuObj.arrUp = null;
		menuObj.arrDown = null;
	} else {
		menuObj.style.height = height + px;
		if ( !menuObj.arrUp && !menuObj.arrDown ) {
			var oAUp = document.createElement("A");
			var oADown = document.createElement("A");
			oAUp.href = oADown.href = "javascript:void(0)";
			menuObj.insertBefore(oAUp, menuObj.menuBody);
			menuObj.appendChild(oADown);
			menuObj.arrUp = oAUp;
			menuObj.arrDown = oADown;
			menuObj.arrUp.className = "arrup disable";
			menuObj.arrDown.className = "arrdn";
			menuObj.arrUp.onmouseover = menuObj.arrDown.onmouseover = smStartScroll;
			menuObj.arrUp.onmouseout = menuObj.arrDown.onmouseout = smStopScroll;
		}
		menuObj.menuBody.style.height = ( height - ( menuObj.arrUp.offsetHeight + menuObj.arrDown.offsetHeight ) ) + px;
	}
}

function vSwitch( obj, flag ){
	if ( flag ){
		obj.style.visibility = "visible";
	} else {
		obj.style.visibility = "hidden";
	}
}

function smHideActive(){
	oMenus[0].menuItems[smActive.num - 1].className = "";
	if ( smActive.style.visibility == "visible" ) {
		vSwitch( smActive, 0 );
		smReset( smActive );
	}
}

function smShow( tObj, show ) {
	if  ( smActive != null ) smHideActive();
	if (  tObj.className.indexOf("noSubmenu") > -1 ) { 
		tObj.className = "noSubmenu siActive";
		noSbActive = tObj.num;
		return;
	}
	if ( show ) {
		var mtop = 0;
		if ( oMenus[tObj.num].style.visibility != "visible" ) {
			num = tObj.num;
			oContain = (isOpera) ? tObj.parentNode.parentNode :  tObj.parentNode;
			if ( oContain.className.indexOf( "smContainer" ) > -1 ) {
				obj = oMenus[num];
				obj.top = getRelY( tObj );
				obj.left = getRelX( oMenus[0] ) + oMenus[0].width;
				setSmHeight( obj, getBodyClientHeight() - 4 );
				 if ( obj.top + obj.clientHeight - oContain.scrollTop >= getBodyScrollTop() + getBodyClientHeight() ) {
					mtop = getBodyScrollTop() + getBodyClientHeight() - obj.clientHeight + oContain.scrollTop - 3;
					if ( mtop <= 0 ) {
						mtop = 1; 
					} 
					if ( mtop < getBodyScrollTop() )  {
						mtop = getBodyScrollTop() + 1;
					}
					obj.top = mtop;
				}
				if (oContain.scrollTop > 0) obj.top -= oContain.scrollTop;
			}
			obj.style.top = obj.top + px;
			obj.style.left = obj.left + px;
			smActive = obj;
			if ( noSbActive > -1 ) { 
				oMenus[0].menuItems[noSbActive - 1].className = "noSubmenu";
				noSbActive = -1;
			}
			oMenus[0].menuItems[smActive.num - 1].className = "siActive";
			vSwitch( obj, 1 );
		}
	} // if ( show )
}

function smHideAll() {
		if  ( smActive != null ) {
			smHideActive();
			smReset( smActive );
			smActive = null;
		}
		if ( oMenus[0].style.visibility == "visible" ) {
			oMenus[0].style.visibility = "hidden";
			oMenus[0].menuBody.scrollTop = 0;
		}
		clearTimeout(smTID);
		menuOver = false;
}

function mItemMouseOver (event) {
	var tObj = ( isIE ) ? window.event.srcElement : event.currentTarget;
	clearTimeout(smTID);
	smTID = setTimeout( function() { smShow( tObj, 1); }, SM_TIME_OUT );
}

function mMouseOut() {
	clearTimeout(smTID);
	clearTimeout(smOverTID);
	smOverTID = setTimeout( "smHideAll()", SM_TIME_MENU_HIDE );
}

function mMouseOver(event) {
	var tObj = ( isIE ) ? window.event.srcElement : event.currentTarget;
	if ( tObj.id == MM_A_ID || tObj.id == MM_TEXT_ID || tObj.id == "ellipsis" || tObj.id == "txtForOpera" ) {
		clearTimeout(smTID);
		if  ( smActive != null ) smHideActive();
		if ( noSbActive > -1 ) {
			oMenus[0].menuItems[noSbActive - 1].className = "noSubmenu";
			noSbActive = -1;
		}
	}
	clearTimeout(smOverTID);
	menuOver = true;
}

function initJMenu() {
	jm_w = getObj( MM_TEXT_ID );
	if ( isOpera ) {
		s = "<span id='txtForOpera'>" + jm_w.innerHTML + "</span>";
		jm_w.innerHTML = s;
		jmtxtO = getObj( "txtForOpera" );
	}
	if ( ( isOpera && jmtxtO && ( jmtxtO.offsetWidth > jm_w.clientWidth ) ) || ( jm_w && ( jm_w.scrollWidth > jm_w.clientWidth ) ) ) {
		getObj( "ellipsis" ).style.visibility = "visible";
	}
	
	oDivs = document.getElementsByTagName( "div" );
	j = 0;
	for ( i = 0; i < oDivs.length; i++ ) {
		if  ( oDivs[i].className == "submenu" ) {
			
			mdiv = oDivs[i].getElementsByTagName( "div" );
			
			o = oDivs[i];
			oCntnr = document.createElement("div");
			ohtml = o.innerHTML;
			o.innerHTML = "";
			o.menuBody = o.appendChild(oCntnr);
			o.menuBody.className = ( j == 0 ) ? "smContainer sMain" : "smContainer";
			if ( isOpera ) {
				oCntnr2 = document.createElement("div");
				o2 = o.menuBody.appendChild(oCntnr2);
				o2.id = "op" + j;
				o2.style.position = "relative";
				o2.innerHTML = ohtml;
			} else o.menuBody.innerHTML = ohtml;
			o.arrUp = null;
			o.arrDown = null;
			o.menuItems = o.menuBody.getElementsByTagName( "a" );
			if ( isIE && !isOpera ) o.height = o.clientHeight - 16;
			else o.height = o.clientHeight;
			o.width = ( o.clientWidth < SM_MIN_WIDTH ) ? SM_MIN_WIDTH : o.clientWidth;
			o.style.width = o.width + px;
			o.top = 0;
			o.left = 0;
			
			iPaddings = ( j == 0 ) ? SM_PADDING_RIGHT + SM_PADDING : SM_PADDING * 2;
			if ( isIE && !isOpera ) {
				smMaxWidth = 1;
				for ( k = 0; k < o.menuItems.length; k++ ){
					o.menuItems[k].style.width = 1;
					if ( smMaxWidth < o.menuItems[k].scrollWidth ) smMaxWidth = o.menuItems[k].scrollWidth;
				}
				o.style.width = o.width = ( smMaxWidth < SM_MIN_WIDTH ) ? SM_MIN_WIDTH : smMaxWidth;
			} // if (isIE)
			
			for ( k = 0; k < o.menuItems.length; k++ ){
				o.menuItems[k].style.width = o.width - iPaddings + px;
				o.menuItems[k].num = k+1;
				if ( j != 0 )	{
					o.menuItems[k].onmouseover = function() {
						clearTimeout(smTID);
					}
				}
			}
			if ( o.arrUp && o.arrDown ) o.menuBody.style.height = ( o.height - ( o.arrUp.offsetHeight + o.arrDown.offsetHeight ) ) + px;
			else  o.menuBody.style.height = o.height + px;
			if ( j > 0 ) {
				while ( oMenus[0].menuItems[j-1].className == "noSubmenu" ) {
					oMenus[j] = null;
					j++;
				}
			}
			
			o.num = j;
			oMenus[j] = o;
			oMenus[j].onmouseout = mMouseOut;
			oMenus[j].onmouseover = mMouseOver;

			if ( j == 0 ) {
				getObj( MM_A_ID ).onclick =  function () {
					obj = oMenus[0];
					if ( obj.style.visibility == "hidden" || obj.style.visibility == "" ) {
						obj.top = getRelY( MM_A_ID )+getObj( MM_A_ID ).offsetHeight;
						obj.left = getRelX( MM_TEXT_ID );
						obj.style.top = obj.top + px;
						obj.style.left = obj.left + px;
						
						smReset(obj)
						setSmHeight( obj, getBodyClientHeight() - obj.top + getBodyScrollTop() - 2 );
						oMenus[0].menuBody.scrollTop = 0;
						vSwitch( obj, 1 );
					} else {
						smHideAll();
					}
				}
				getObj( MM_A_ID ).onmouseout = mMouseOut;
				getObj( MM_A_ID ).onmouseover = mMouseOver;
			}
			
			smReset(oMenus[j]);
			j++;
			
		} // if  ( oDivs[i].className == "submenu" )
	} // for ( i = 0; i < oDivs.length; i++ )
	if ( oMenus[0].menuItems[oMenus[0].menuItems.length-1].className == "noSubmenu" ) {
		oMenus[oMenus[0].menuItems.length] = null;
	}
	 
	for ( i = 0; i < oMenus[0].menuItems.length; i++ ) {
		oMenus[0].menuItems[i].onmouseover = mItemMouseOver;
	} 
	
} // function init()

