function PopupMenu (itemSelector, listSelector){
	this.itemSelector=itemSelector;
	this.listSelector=listSelector;

	this.list = {};

	this.getAbsolutePos = function (el,x,y){
		var r = { x: el.offsetLeft, y: el.offsetTop };
		if(el.offsetParent && el.offsetParent.nodeName!='body' && el.offsetParent.nodeName!='BODY'){
			var tmp = this.getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	}

	this.createItem = function(el){
		el.TID = null;
		el.level=null;
		el.parent  = (el.parentNode && el.parentNode.hasAttribute('pid')) ? $(el.parentNode.getAttribute('pid')):null;
		el.getLevel=function(){
			if (el.level!=null){
				return el.level;
			} else if(el.parentNode && el.parentNode.hasAttribute('pid')){
				el.level=el.parentNode.getLevel()+1;
				return el.level;
			} else {
				el.level=0;
				return 0;
			}
		}.bind(this);
		el.getLevel();

		el.child=null;
		el.opened=false;
		el.positioned=false;

		el.open=function(){
			if(el.opened==false){
				el.opened=true;
				el.toggleClassName('active');
				el.child.show();
				if (el){
					var r=this.getAbsolutePos(el);

					if (el.level==0){
						el.child.style.top=(r.y+el.offsetHeight)+'px';
						el.child.style.left=r.x+'px';
					} else {
						var r=this.getAbsolutePos(el);
						width=el.child.getWidth();

						if (el.offsetWidth + r.x + width > document.body.clientWidth) {
							el.child.style.marginLeft=-(el.child.offsetWidth)+'px';
						} else {
							el.child.style.marginLeft=(el.offsetWidth)+'px';
						}
						el.child.style.marginTop='-'+(el.offsetHeight+3)+'px';
					}
				}
			}
		}.bind(this);

		el.close=function(){
			if(el.opened==true){
				el.opened=false;
				el.toggleClassName('active');
				el.child.hide();
			}
		}
		el.startClose=function(){
			if (el.TID == null) {
				el.TID = setTimeout(el.close, 60);
			}
		}
		el.stopClose=function(){
			clearTimeout(el.TID);
			el.TID=null;
			if (el.parent) {
				el.parent.stopClose();
			}
		}

		el.observe('mouseover', function(e){
			el.stopClose();
			el.open();
		});
		el.observe('mouseout', function(e){
			el.startClose()
		});

		return el;
	}

	this.createList = function(el){
		el.parent=null;
		el.level=null;
		el.getLevel=function(){
			if (el.level!=null){
				return el.level;
			} else if(el.parent!=undefined){
				el.level=el.parent.getLevel();
				return el.level;
			} else {
				el.level=0;
				return 0;
			}
		}.bind(this);

		el.observe('mouseover', function(e){
			if(el.parent!=null){
				el.parent.stopClose(el);
			}
		});
		el.observe('mouseout', function(e){
			if(el.parent!=null){
				el.parent.startClose();
			}
		});

		return el;
	}

	this.init = function(){
		$$(this.listSelector).each(function(el) {
			var pid = el.readAttribute('pid');
			if (pid){
				el.pid=pid;
				this.list[pid]=this.createList(el);
			}
		}.bind(this));

		this.list = new Hash(this.list);

		$$(this.itemSelector).each(function(el) {
			var item = this.createItem(el);
			var child = this.list.get(el.id);
			if (child!=undefined){
				item.child = child;
				child.parent=item;
			}
		}.bind(this));
	}

	this.cloneWidth = function(hard){
		this.list.each(function(item) {
			var el = item[1];
			if (el.level == 0 && el.parent){
				if (hard == true || el.getWidth() < el.parent.offsetWidth)
					el.style.width = el.parent.offsetWidth+'px';
			}

		});
	}

	this.init();
}
