var _timeoutID = null;
var _timeoutLI = null;
var _timeoutMS = 500;

function menu_show_pane() {
    if(_timeoutID) {
        if(_timeoutLI) {
            if(_timeoutLI == this) {
                clearTimeout(_timeoutID);
            } else {
                _timeoutLI.sub.style.visibility="hidden";
                _removeClass(_timeoutLI,"active");
            }
        } else {
            clearTimeout(_timeoutID);
        }
    }
    
	var li = this;
	_addClass(li,"active");
	
	var li_xy     = _getXYWH(li);
	var scroll_xy = _getWindowScrollOffset();
	
	var is_ie = (navigator.userAgent.match(/Explorer/));
	var is_sa = (navigator.userAgent.match(/Safari/));
	var is_ff = (navigator.userAgent.match(/Mozilla/));
	var is_ns = (navigator.userAgent.match(/Netscape/));
	if(li.isTop) {
		li.sub.style.left = ((li_xy.x+(!li.isHorizontal?li_xy.w:0))+scroll_xy.x)+"px";
		
		if(is_sa) { // scroll offset needed to position menu layer
			li.sub.style.top = (li_xy.y+((li.isHorizontal)?li_xy.h:0)+scroll_xy.y)+"px";
		} else if(is_ff) {
            /*
            if((navigator.userAgent.match(/Firefox\/[0-9\.]{1,}/))) {
                var vers = (String)(navigator.userAgent.match(/Firefox\/[0-9\.]{1,}/));
                // alert("ff vers: "+vers);
                var ver  = ((vers && vers.length && vers.length > 0 && vers.match)?parseFloat(vers.match(/[0-9]{1,}\.[0-9]{1,}/)):null);
            } else {
                var ver  = null;
            }
            // alert("ff ver: "+ver);
            if(ver >= 3) {
                li.sub.style.top = (li_xy.y+(li.isHorizontal?li_xy.h:0))+"px";
            } else {
                // alert("li_xy: "+li_xy.y+","+li_xy.x+","+li.isHorizontal);
                li.sub.style.top = (li_xy.y+(li.isHorizontal?li_xy.h:0))+"px";
            }
            */
            
            li.sub.style.top = (li_xy.y+(li.isHorizontal?li_xy.h:0))+"px";
		} else if(is_ie) {
			li.sub.style.top = (li_xy.y+((li.isHorizontal)?li_xy.h:0))+"px";
		} else {
			li.sub.style.top = (li_xy.y+((li.isHorizontal)?li_xy.h:0)+scroll_xy.y)+"px";
		}
	} else {
		li.sub.style.left = (li.offsetWidth+scroll_xy.x)+"px";
		if(is_ff) {
			li.sub.style.top = (li.offsetTop)+"px";
		} else {
			li.sub.style.top = (li.offsetTop+scroll_xy.y)+"px";
		}
	}
	
	li.sub.style.visibility="visible";
}

function menu_hide_pane() {
    var is_ff = (navigator.userAgent.match(/Firefox/));
    var is_ie = (navigator.userAgent.match(/MSIE/));
    
    if(is_ff) { // timed menu pane hides help ff, and others, but seem to cause issues with sub panes in sf
        _timeoutLI = this;
        _timeoutID = setTimeout(_menu_hide_pane,_timeoutMS,this);
    } else {
        var _li = this;
        _li.sub.style.visibility="hidden";
        _removeClass(_li,"active");
    }
}

function _menu_hide_pane(_li) {
    if(_li) {
        _li.sub.style.visibility="hidden";
        _removeClass(_li,"active");
    }
}

function menu_steup() {
	var menu = document.getElementById("menu");
	if(!menu) {
		return;
	}
	var menuIsHorizontal=_hasClass(menu,"horizontal");
	var lis = menu.getElementsByTagName("li");
	for(var i=0,len=lis.length;i<len;i++) {
		var li=lis[i];
		var uls = li.getElementsByTagName("ul");
		if(!uls || uls.length==0) {
			continue;
		}
		var ul=uls[0];
		li.sub=ul;
		li.onmouseover = menu_show_pane;
		li.onmouseout  = menu_hide_pane;
		
		li.isTop = li.parentNode==menu;
		li.isHorizontal = (menuIsHorizontal && li.isTop);
		if(li.addedArrow || li.isTop) {
			continue;
		}
		
		var arrow=document.createElement("span");
		arrow.innerHTML="&nbsp;&rArr;";
		var a = li.getElementsByTagName("a");
		if(a && a.length>0 && a[0].parentNode==li) {
			a[0].innerHTML+="&nbsp;&raquo;";
		} else {
			li.insertBefore(arrow,li.childNodes[1]);
		}
		li.addedArrow=true;
	}
}

function menu_tab_show(tab) {
	if(tab) {
		var menu_element = document.getElementById("m_" + tab + "_tab");
		if(menu_element) {
			var existing_class_attributes = ((menu_element.getAttribute("class"))?menu_element.getAttribute("class"):menu_element.getAttribute("className"));
			// console.log(existing_class_attributes);
			menu_element.setAttribute("class",((existing_class_attributes && existing_class_attributes.length > 0)?existing_class_attributes+" selected":"selected"));
			menu_element.setAttribute("className",((existing_class_attributes && existing_class_attributes.length > 0)?existing_class_attributes+" selected":"selected")); // for IE
			// console.log(menu_element.getAttribute("class"));
		}
	}
}

_attachEvent(window,"load",menu_steup,true);

function _getXY(obj) {
	var x=0,y=0;
	while(obj) {
		x+=obj.offsetLeft - (obj.scrollLeft || 0);
		y+=obj.offsetTop - (obj.scrollTop || 0);
		obj=obj.offsetParent;
	}
	return {x:x,y:y};
}

function _getXYWH(obj) {
	if(!obj) {
		return { x:0, y:0, w:0, h:0 };
	}
	var objXY = _getXY(obj);
	return { x:objXY.x, y:objXY.y, w:obj.offsetWidth||0, h:obj.offsetHeight||0 };
}

function _getWindowScrollOffset() {
	var iebody  = ((document.compatMode && document.compatMode != "BackCompat")?document.documentElement:document.body);
	var offsetX = ((window && window.pageXOffset)?window.pageXOffset:((iebody.scrollLeft)?iebody.scrollLeft:0));
	var offsetY = ((window && window.pageYOffset)?window.pageYOffset:((iebody.scrollTop)?iebody.scrollTop:0));
	
	return {x:offsetX,y:offsetY};
}

function _attachEvent(obj,evt,fnc,useCapture) {
	if(obj.addEventListener) {
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) {
		return obj.attachEvent("on"+evt,fnc);
	} else {
		obj["on"+evt]=fnc;
	}
	return true;
}

function _hasClass(obj,cName) { return (!obj || !obj.className)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.className) }
function _addClass(obj,cName) { if (!obj) return; if (obj.className==null) obj.className=''; return obj.className+=(obj.className.length>0?' ':'')+cName; }
function _removeClass(obj,cName) { if (!obj) return; return obj.className=obj.className.replace(RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),''); }