window.onload = mladdevents;

function mladdevents(){
	if(window.mlrunShim == true){
		var Iframe = document.createElement("iframe");
		Iframe.setAttribute("src","about:blank");
		Iframe.setAttribute("scrolling","no");
		Iframe.setAttribute("frameBorder","0");
		Iframe.style.zIndex = "2";
		Iframe.style.filter = 'alpha(opacity=0)';
	}
	var effects_a = new Array();
	var navigation = document.getElementById('ulNavigation');
	var lis = navigation.childNodes;
	for (var i = 0; i < lis.length; i++) {
	    if (lis[i].className.indexOf('clsSelected') == -1) {
	        lis[i].onmouseover = mlover;
	        lis[i].onmouseout = mloutSetTimeout;
	        if (window.mlrunShim == true) {
	            lis[i].appendChild(Iframe.cloneNode(false));
	        }
	        var uls = lis[i].getElementsByTagName('ul');
	        for (var k = 0; k < uls.length; k++) {
	            var found = 'no';
	            for (var z = 0; z < effects_a.length; z++) {
	                if (effects_a[z] == uls[k]) {
	                    found = 'yes';
	                }
	            }
	            if (found == 'no') {
	                effects_a[effects_a.length] = uls[k];
	                uls[k].style.zIndex = '100';
	                mlEffectLoad(uls[k]);
	            }
	        }
	    }
	}
}

function mloutSetTimeout(e){
	if (!e){
		var the_e = window.event;
	}
	else{
		var the_e = e;
	}
	var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.toElement;
	if (reltg) {
		var under = ancestor(reltg, this);
		if (under === false && reltg != this) {
			window.mlLast = this;
			var parent = this.parentNode;
			while (parent.parentNode && parent.className.indexOf('clsNavigation') == -1) {
				parent = parent.parentNode;
			}
			mlout();
		}
	}
}

function mlout(){
    if (window.mlLast == null) return false;
	var uls = window.mlLast.getElementsByTagName('ul');
	var sib;
	for (var i = 0; i <uls.length; i++) {
		mlEffectOut(uls[i]);
		if (window.mlrunShim == true) {
			sib = uls[i];							
			while (sib.nextSibling && sib.nodeName != 'IFRAME') {
			    sib = sib.nextSibling;
			}
			sib.style.display = 'none';
		}
	}
	window.lastover = null;
}

function mlover(e){
	if (!e){
		var the_e = window.event;
	}
	else{
		var the_e = e;
	}
	the_e.cancelBubble = true;
	if (the_e.stopPropagation) {
		the_e.stopPropagation();
	}
	clearTimeout(window.mlTimeout);
	if (window.mlLast && window.mlLast != this && ancestor(this ,window.mlLast) == false) {
		mlout();
	}
	else{
		window.mlLast = null;
	}
	var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.fromElement;
	var ob = this.getElementsByTagName('ul');
	var under = ancestor(reltg, this);
	if (ob[0] && under == false) {
		if (window.lastover != ob[0]) {
			if (window.mlrunShim == true) {
				var sib = ob[0];
				while (sib.nextSibling && sib.nodeName != 'IFRAME') {
					sib = sib.nextSibling
				}
				ob[0].style.display = 'block';
				sib.style.top = ob[0].offsetTop + 'px';
				sib.style.left = ob[0].offsetLeft - 2 + 'px';
				sib.style.width = ob[0].offsetWidth + 'px';
				sib.style.height = ob[0].offsetHeight -2 + 'px';
				sib.style.border = '1px solid red';
				sib.style.display = 'block';
			}
			mlEffectOver(ob[0], this);
			window.lastover = ob[0];
		}
	}
}

function mlIncreaseHeight(ob){
	var current = parseInt(ob.style.height);
	var newh = current + 1;
	ob.style.height = newh + 'px';
}

function mlEffectOver(ob, parent){
	ob.style.display = 'block';
	if (ob.offsetHeight) {
	    var height = ob.offsetHeight
	    ob.style.height = '0px';
	    ob.style.width = '150px';
		ob.style.overflow = 'hidden';
		for (var i = 0; i < height; i++){
			setTimeout(function(){ mlIncreaseHeight(ob) }, i * 3);
		}
		setTimeout(function(){ ob.style.overflow='visible'; }, height * 3)
	}
}

function mlEffectOut(ob){
	ob.style.display = 'none';
}

function mlEffectLoad(ob){
	var parent = ob.parentNode;
	while (parent.parentNode && parent.className.indexOf('clsNavigation') == -1) {
		parent = parent.parentNode;
	}
}

function ancestor(child, parent){
	if (child == null) return false;
	if (navigator.userAgent.indexOf('Gecko') != -1 && navigator.userAgent.indexOf('Opera') == -1) {
		var allc = parent.getElementsByTagName('*');
		for(var i = 0; i < allc.length; i++){
			if (allc[i] == child) {
				return true;
			}
		}
	}
	else{
		for (; child.parentNode; child = child.parentNode) {
			if (child.parentNode === parent) return true;
		}
	}
	return false;
}