var xm, ym;			
var ob;
var over = false;

var isIE = document.all ? 1 : 0;
var isNav = (document.layers ? 1 : (document.all ? 0 : 1));
var isOpera = isIE && ((window.navigator.userAgent.indexOf("opera") != -1) || (window.navigator.userAgent.indexOf("Opera") != -1));
var isIEMac = isIE && ((window.navigator.userAgent.indexOf("mac") != -1) || (window.navigator.userAgent.indexOf("Mac") != -1));
var isW3C = document.getElementById ? 1 : 0;

function IsIE()		{ return isIE;} 
function IsNav()	{ return isNav;  }
function IsOpera()  { return isOpera;}
function IsIEMac()	{ return isIEMac;}
function IsW3C()	{ return isW3C; }

function getNumberElementStyle(obj){
	var oyo = new String(obj);
	oyo = oyo.substring(0, oyo.length - 2);
	return new Number(oyo);
}

function CSFindElementStyle(n,ly) {
	if (IsW3C()) {
		return CSFindElement(n,ly).style;
	} else if (IsIE()) {
		return CSFindElement(n,ly).style;
	} else {
		return CSFindElement(n,ly);
	}
}

function CSFindElement(n,ly) {
	
	if (IsW3C()) {
		return document.getElementById(n);
	} else if (IsIE()) {
		return document.all(n);
	} else if (IsNav()) {
		var curDoc = ly ? ly.document : document;
		var elem = curDoc[n];

		if (elem) return elem;
	
		for (var i=0;i<curDoc.layers.length;i++) {
			elem = CSFindElement(n,curDoc.layers[i]);
			if (elem) return elem;
		}
		return elem;
	} else {
		return document[n];
	}
}

function CSSetStyleVis(s,v) {
	if (IsW3C()) {
		CSFindElementStyle(s, 0).visibility = (v == 0) ? 'hidden' : 'visible';
	} else if (IsIE()) {
		CSFindElementStyle(s, 0).visibility = (v == 0) ? 'hidden' : 'visible'; 
	} else {
		CSFindElementStyle(s, 0).visibility = (v == 0) ? 'hidden' : 'visible'; 
	}
}
		
function MD(e) {
	if (over){	
		if (IsIE()){
			ob = CSFindElement(idMenu,0);		
			ob = ob.style;		
			xm=event.x;		
			ym=event.y;	
		}else if (IsNav()) {	
			ob = CSFindElement(idMenu,0);	
			xm=e.layerX;		
			ym=e.layerY;		
			return false;		
		}
	}
}

function MM(e) {

			//alert('MUOVI');
			
	if (ob) {	
		if (IsIE()){	
			var oxn = getNumberElementStyle(ob.left);
			var oyn = getNumberElementStyle(ob.top);

			alert(oxn + " e " + oyn);

			ob.top = oyn + (event.y - ym);
			ob.left = oxn + (event.x - xm);
			xm = event.x;
			ym = event.y;		
			return false;
		}else if (IsNav()) {		
			ob.style.top = e.pageY-ym;
			ob.style.left = e.pageX-xm;
		}
	}
}

function MU() {
	if(ob) 	{		
		var nextCL = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).left);
		var nextCT = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).top);
		
		var nextOL= getNumberElementStyle(CSFindElementStyle(idMenu2, 0).left);
		var nextOT= getNumberElementStyle(CSFindElementStyle(idMenu2, 0).top);	
		
		if(nextCL > maxX || nextOL > maxX) {
			CSFindElementStyle(idMenu2, 0).left = maxX;
			CSFindElementStyle(idMenu1, 0).left = maxX;
		}else if (nextCL < minX || nextOL < minX) {
			CSFindElementStyle(idMenu2, 0).left = minX;
			CSFindElementStyle(idMenu1, 0).left = minX;
		}
	
		if(nextCT > maxY || nextOT > maxY2 ){
			CSFindElementStyle(idMenu2, 0).top = maxY2;
			CSFindElementStyle(idMenu1, 0).top = maxY;
		}else if (nextCT < minY || nextOT < minY) {
			CSFindElementStyle(idMenu2, 0).top = minY;
			CSFindElementStyle(idMenu1, 0).top = minY;
		}
	}
	ob = null;
	over = false;
}

function MO() {
	if(ob && (!IsIE() || IsOpera())) 	{	
		var nextCL = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).left);
		var nextCT = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).top);
		
		var nextOL= getNumberElementStyle(CSFindElementStyle(idMenu2, 0).left);
		var nextOT= getNumberElementStyle(CSFindElementStyle(idMenu2, 0).top);

		var uscito = false;
		if(nextCL > maxX || nextOL > maxX) {
			CSFindElementStyle(idMenu2, 0).left = maxX;
			CSFindElementStyle(idMenu1, 0).left = maxX;
			uscito = true;
		}else if (nextCL < minX || nextOL < minX) {
			CSFindElementStyle(idMenu2, 0).left = minX;
			CSFindElementStyle(idMenu1, 0).left = minX;
			uscito = true;
		}
	
		if(nextCT > maxY || nextOT > maxY2 ){
			CSFindElementStyle(idMenu2, 0).top = maxY2;
			CSFindElementStyle(idMenu1, 0).top = maxY;
			uscito = true;
		}else if (nextCT < minY || nextOT < minY) {
			CSFindElementStyle(idMenu2, 0).top = minY;
			CSFindElementStyle(idMenu1, 0).top = minY;
			uscito = true;
		}
		if (uscito) {
			ob = null;
			over = false;
		}
	}
}

function ApriMenu() { 
//alert("ApriMenu");
	var oyon = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).top);
	var oxon = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).left);

	CSFindElementStyle(idMenu2, 0).top = oyon;
	CSFindElementStyle(idMenu2, 0).left = oxon;
	idMenu = idMenu2;
	if (oyon > maxY - diffMenu2VsMenuH){
		var temp = maxY - diffMenu2VsMenuH;
		CSFindElementStyle(idMenu2, 0).top = temp;
		CSFindElementStyle(idMenu1, 0).top = temp;
	}
	
	CSSetStyleVis(idMenu1, 0);
	CSSetStyleVis(idMenu2, 1);
	
	
	if(parent.MapFrame.useExtServices){
		//alert("visibile");
		top.MapFrame.document.all.useExtServices.style.visibility = "visible";
	}else{
//	alert("non visibile");
		top.MapFrame.document.all.useExtServices.style.visibility = "hidden";
	}
}

function ChiudiMenu() {
	var oyon = getNumberElementStyle(CSFindElementStyle(idMenu2, 0).top);
	var oxon = getNumberElementStyle(CSFindElementStyle(idMenu2, 0).left);
				
	idMenu = idMenu1;
	CSFindElementStyle(idMenu1, 0).top = oyon;
	CSFindElementStyle(idMenu1, 0).left = oxon;

	CSSetStyleVis(idMenu1, 1);
	CSSetStyleVis(idMenu2, 0);
	
	
	top.MapFrame.document.all.useExtServices.style.visibility = "hidden";
	
}