// THESE FUNCTIONS IMPLEMENT DYNAMIC MENU VISUALIZATION FUNCTIONALITY
// Modified by Artem Novikov, 2002

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function P7_setLayerZIndex(l, i) {
  if((g=MM_findObj(l))!=null) {
   b=(document.layers)?g:g.style;b.zIndex=i;
  }
};

function AN_show(o) {
	var g;
	if ((g=MM_findObj(o))!=null) {
		b=(document.layers)?g:g.style;
		b.visibility="visible";
		b.zIndex=(o.toString()=='MenuCloser')?65534:65535;
		
	}
};

function AN_updateActivity() {
	// remember when last shown
	document.AN_lastShown=new Date();
}

function AN_hide(o) {
	var g;
	if ((g=MM_findObj(o))!=null) {
		b=(document.layers)?g:g.style;
		b.visibility="hidden";
		b.zIndex=-65535;
  }
};

function P7_autoLayers() {
	// create variables
	var g,b,k,u,f,args=P7_autoLayers.arguments;

	// support for multiple menus
	var a = parseInt(args[0]);if(isNaN(a))a=0;

	// initialize the array of shown documents
	if (!document.p7setc) {
		p7c=new Array();
		document.p7setc=true;
  		for (u=0;u<10;u++) {
			p7c[u] = new Array();
		}
	}

	// hide all shown objects for current menu
	for(k=0; k<p7c[a].length; k++) {
		f=false;
		for (u=1; u<args.length; u++) {
			if (p7c[a][k]==MM_findObj(args[u])) f=true;
		};
		if (f==false) AN_hide(p7c[a][k]);
	}

	// 
	for(k=1; k<args.length; k++) {
		if((g=MM_findObj(args[k])) != null) {
			// show all non-overlapping button, hide overlapping ones
			AN_showOverlappingControls(g, args[k].toString());
			
			AN_show(args[k]);

			// is the shown object already in the array?			
			f=false;
			for(j=0;j<p7c[a].length;j++) {
				if(args[k]==p7c[a][j]) {
					f=true;
				}
			}
			
			// if not, insert it there
			if(!f) {
				p7c[a][p7c[a].length++]=args[k];
			}
		}
	}
}

function pviiW3Cbg(obj, pviiColor) {
	if (obj) {
		if (obj.style) {
			obj.style.backgroundColor=pviiColor;
		}
	}
}


// returns absolute positions of an objects
function AN_totalPos(g) {
	var U=0, L=0, B, R;
	for(;g;) {	
		if (g.offsetLeft) L+=g.offsetLeft;
		if (g.offsetTop) U+=g.offsetTop;
		if (!B) B=g.offsetHeight;
		if (!R) R=g.offsetWidth;
		g=g.offsetParent;
	};
	if (R&&L) R+=L;
	if (B&&U) B+=U;
	if (!B) B=0;
	if (!R) R=0;
	if (!U) U=0;
	if (!L) L=0;
	var r=new Array(L, U, R, B);
	return r;
};

// hide comboboxes if they overlap the given object
function AN_showOverlappingControls(c, f) {
	// not implemented for NC
	if (!(document.body)) return;
	
	var RC; if (c) RC=AN_totalPos(c); var RS, g, b;
	var a=(f.toString()=='MenuCloser');
	var sls=document.body.getElementsByTagName('SELECT');
	if (sls) for(var i=0;i<sls.length;i++) {
		g=sls[i];
		if (c) RS=AN_totalPos(g);
		if ((a==true)||(!c)||((AN_overlaps(RS, RC)==true)&&(a==false))) {
			b=(document.layers)?g:g.style;
			b.visibility=((a==true)?"visible":"hidden");
		};
	};
};

// true if o1 partially overlaps o2
function AN_overlaps(o1, o2) {
 if ((!o1)||(!o2)) return false;
 if (o1[0]>o2[2]) return false;
 if (o1[2]<o2[0]) return false;
 if (o1[1]>o2[3]) return false;
 if (o1[3]<o2[1]) return false;
 return true;
};

function HideAllMenus() {
	P7_autoLayers(0, 'GlobalMenu');
	AN_showOverlappingControls(null, 'MenuCloser');
};

function CheckMenuHide() {
	if (!document.AN_lastShown) {
		document.AN_lastShown=new Date();
		return;
	};
	var c=new Date();
	var n1=c.getTime(); var n2=document.AN_lastShown.getTime();
	if (n1-n2>1500) HideAllMenus();
};

function WriteCloser()  {
	var w=0; var h=0;
	if (document.body) {
		h=document.body.clientHeight;
		w=document.body.clientWidth;
	} else {
		window.setInterval("CheckMenuHide()", 500);
		return;	
	};

	document.open();
	document.writeln("<DIV id='MenuCloser' style='POSITION: absolute; "+
		"VISIBILITY: hidden; Z-INDEX: 0; TOP: 0px; LEFT: 0px; "+
		"WIDTH: "+w+"px; HEIGHT: "+h+"px;'>");
	document.writeln('<A href="javascript:;" onmouseover="javascript: P7_autoLayers(0, \'GlobalMenu\'); '+
		'AN_showOverlappingControls(null, \'MenuCloser\');">');
	document.writeln("<IMG id='ImgMenuCloser' border=0 height="+h+
		" width="+w+" src='dt.gif'></A><DIV>");
	document.close();
};
