var div_menu = function(id) {

var debug=true,id=id,
	m_ol=Array(), m_fl=Array(), m_bl=Array(), m_il=Array(), ci=Array();

	var lt = 400; //Время до закрытия выпадающего меню...

	this.dpt;
	this.dpq=Array();
	this.m_dl=Array();

	var d=document, obj=d.getElementById(id), isDOM=obj?1:0, isIE=(d.all&&!self.opera)?1:0,
			isOPERA=(self.opera)?1:0, isFF=(d.getElementById&&!d.all)?1:0, isNS4=0;

	if (isIE) var browserVer='ie';
	else if (isOPERA) var browserVer='opera';
	else if (isFF) var browserVer='ff';

	if (!obj || typeof(obj) != 'object') { if (debug) alert('Не найден основной блок...'); return false; }

	if (!d.myMenuEx) d.myMenuEx=Array();
	d.myMenuEx[id] = this;

	/*
		mode=1 Все папки закрываються только по желанию пользователя.
		mode=2 Если пользователь открывает папку, то все остальные закрываються, включая потомков...
		mode=3 Если пользователь открывает папку, то остальные папки текущего уровня закрываются..
	*/
	var mode=obj.getAttribute('mode');if(!mode) mode='1';
	var reg=RegExp(id+'="(.*?)"','g'),lee=reg.exec(d.cookie);
	if(lee) {
		lee=lee[1].split(',');
		for (i in lee) {
			if(lee[i]!='')ci[lee[i]]=true;
	  }
	}

	this.debugcon=function(s) {
		var str,i,win; if (typeof(s)=='object') for (i in s) { str+=i+': '+s[i]+'<br>'; }
		if (typeof(s)=='string') { str = s; } var win=window.open(); win.document.write(str);
		return false;
	}


	this.elmPos = function(e,p) {
		var x=0,y=0,w=p?p:this.win;
		e=e?(e.substr?(isNS4?w.d.anchors[e]:getRef(e,w)):e):p;
		if(isNS4){if(e&&(e!=p)){x=e.x;y=e.y};if(p){x+=p.pageX;y+=p.pageY}}
		if (e && this.MS && navigator.platform.indexOf('Mac')>-1 && e.tagName=='A') {
			e.onfocus = Function('with(event){self.tmpX=clientX-offsetX;'+'self.tmpY=clientY-offsetY}');
			e.focus();x=tmpX;y=tmpY;e.blur();
		} else while(e) { if (e.style.position == 'absolute') return{x:x,y:y}; x+=e.offsetLeft;y+=e.offsetTop;e=e.offsetParent; }
		return{x:x,y:y};
	};


	this.set = function(o,e) {
		var x; if (!o) return false;
		if (x=o.getAttribute('class'+e)) o.className=x;
		if (x=o.getAttribute('style'+e)) {
			if (!isOPERA) o.style.cssText=x; else o.style=x;
		}
	}

	this.prepare = function(e) {
		var x=e.getAttribute('mtype'), l=Number(e.level), i, f=Array();
		if (x=='items') f=['Closed', 'Opened'];
		else if (x=='item') f=['Actived', 'Over', 'Out', 'Click'];
		else if (x=='body') f=['OpenedOut', 'OpenedOver', 'Actived', 'Over', 'Out', 'Click'];

		for (i in f) { if (!m_ol[l]) continue;
			if (!e.getAttribute('class'+f[i]) && m_ol[l]['class'+f[i]]) e.setAttribute('class'+f[i], m_ol[l]['class'+f[i]]);
			if (!e.getAttribute('style'+f[i]) && m_ol[l]['style'+f[i]]) e.setAttribute('style'+f[i], m_ol[l]['style'+f[i]]);
		}
		if (x=='item'||x=='body') this.set(e,'Out');
	}

	this.getParent = function(e) {
		var t;
		while(e.parentNode) {
			e=e.parentNode;
			if (e.id==id) return e;
			t=e.getAttribute('mtype');
			if (t=='folder'||t=='drop') return e;
		} if (debug) alert('Не правильно структурированно меню, не возможно найти родительскую папку...');
	}

	this.prepareOptions = function(f) {
		var o=['Actived', 'Opened', 'Closed', 'Over', 'Out', 'Click', 'OpenedOut', 'OpenedOver'], m=false, i, l=f.level;

		if (!m_ol[l]) m_ol[l]=Array();
		for (i in o) {
			if (m_ol[l]) {
				if (m=f.getAttribute('class'+o[i])) m_ol[l]['class'+o[i]] = m;
				if (m=f.getAttribute('style'+o[i])) m_ol[l]['style'+o[i]] = m;
			}
		}
	}

	obj.level = 0;
	this.prepareOptions(obj);
	var w = obj.getElementsByTagName('div'), i;
	for (i=0;i<w.length;i++) {

			var t=w[i].getAttribute('mtype'),p=false;

			if (/^(folder|drop|item|items|items_drop|body)$/.test(t)) {
				w[i].setAttribute('mindex',id);
				p=this.getParent(w[i]);
				var zz = "document.myMenuEx[this.getAttribute('mindex')].event_master(this, '";

				if (t=='body'||t=='item') {
					w[i].onmouseover = function() {eval(zz+'over\');');}
					w[i].onclick = function() {eval(zz+'click\');');}
					w[i].onmouseout	= function() {eval(zz+'out\');');}
				}
			}

			if (t=='folder'||t=='drop') {
				w[i].level=p.level+1; var ll=m_fl.length;
				this.prepareOptions(w[i]); m_fl[ll]=w[i];
				w[i].index=ll; ll=false;
			}
			if (t=='items') {
				w[i].level=p.level; this.prepare(w[i]);
				m_il[p.index]=w[i];
			}
			if (t=='items_drop') {
				w[i].level=p.level; this.m_dl[p.index]=w[i];
				w[i].style.display='none';
			}
			if (t=='body') {
				w[i].level=p.level-1; this.prepare(w[i]);
				m_bl[p.index]=w[i]; w[i].index=p.index;
			}
			if (t=='item') {
				w[i].level=p.level; this.prepare(w[i]);
				w[i].index=p.index;
			}
	}






	this.event_master = function(o,e) {

		var x,t=o.getAttribute('mtype'),pt,i=Number(o.index),fl=m_fl[i];
		pt = (fl) ? fl.getAttribute('mtype') : false ;

		if (e=='over') {
			if (ci[Number(o.index)] && t=='body') this.set(o,'OpenedOver'); else this.set(o,'Over');
			if (t=='body' || t=='item') { this.fdrop(o,pt); }
		} else if (e=='out') {
		  if (ci[Number(o.index)] && t=='body') this.set(o,'OpenedOut'); else this.set(o,'Out');
			document.fundrop(id);
		} else if (e=='click') {
			this.set(o,'Click');
			if (t=='body' && pt == 'folder') { this.foc(o); }
		}
	}





	this.foc = function(o) {
		var x,z,m,w,d;
		if (o) {
			var d=Number(o.index), f=m_fl[d], i=m_il[d], isopen=(ci[d])?1:0;
			if (!i||!f) { if (debug) alert('Не верный формат меню, в папке должен быть: DIV mtype="body", и DIV mtype="items"'); return false; }

			switch (mode) {
				case '1':
					ci[d] = (!ci[d])?i:false;
				break;
				case '2':
					ci=Array();
					w=this.getParent(i);
					while (w) {
						if (w.getAttribute('mtype')) {
							ci[w.index]=w;
						} else break;
						w=this.getParent(w);
					}
					if (isopen) { ci[d]=false; }
				break;
				case '3':
				break;
			}
		}

		x = '';	var bs='';
		for (x in m_fl) {
			m=m_il[x]; if (!m) continue;
			if (ci[x]) {
				bs+=x+',';
				this.set(m_bl[x], 'OpenedOver');
				this.set(m,'Opened');
			}	else {
				this.set(m_bl[x], 'Over');
				this.set(m,'Closed');
			}
		}
		document.cookie=id+'="'+bs.replace(/,$/, '')+'"';
	}
	this.foc();



	this.fdrop = function(oE) {
		var p=this.getParent(oE),i,v,x=oE;
		this.dpq=Array();

		while (x.getAttribute('mtype')) {
				if (x && x.getAttribute('mtype')=='drop') { this.dpq[Number(x.index)] = x; if(!v)v=x; }
				x = this.getParent(x);
		}

		if (!v) return false;

		if (this.dpt)	clearTimeout(this.dpt);
		for(b in this.m_dl) { if (!this.dpq[b]) this.m_dl[b].style.display='none'; }

		if (p.getAttribute('mtype')=='drop'&&oE.getAttribute('mtype')=='body') {
			i=m_dl[Number(oE.index)];
			if (!i&&debug){alert('Не верно структурированно меню... в <DIV type="drop"> должен быть контейнер <DIV type="items_drop">');return false;}

			pos=this.elmPos(oE);
			var gu=p.getAttribute('dropto'),gl=p.getAttribute('left'),gt=p.getAttribute('top');
			gu=(gu)?gu:'right'; gl=(gl)?gl:0; gt=(gt)?gt:0;

			with(i.style){
				position='absolute';
				if(gu=='right'){left=pos.x+oE.offsetWidth+gl+'px';top=pos.y+gt+'px';}
				if(gu=='left'){left=pos.x-i.offsetWidth-gl+'px';top=pos.y+gt+'px';}
				if(gu=='bottom'){left=pos.x+gl+'px';top=pos.y+oE.offsetHeight+gt+'px';}
				this.set(i,'Opened');
				display='block';
			}
		}
	}


	document.fundrop = function(n,k) {

		var t = document.myMenuEx[n], i;
		if (!k) {
			if (t.dpt) clearTimeout(t.dpt);
			t.dpt = setTimeout ('document.fundrop("'+n+'","1");', lt);
		} else {
			for (i in t.m_dl) { t.m_dl[i].style.display='none'; }
		}
	}
}
